Sample Program II

Here is a slightly larger sample program that provides a better sense of the typical ASB program structure and illustrates a few features of the language, including:

•   PROGRAM statement assigns a version to the RUN module (visible with DIR/V)

•   ! (exclamanation point) starts a comment up thru the end of the line

•   ? (question mark) is an alias for PRINT, outputting text to the screen or a stream file

•   ; (semi-colon) at end of print statement stifles CRLF output.

•   ++INCLUDE reads in source code from external file, like #include in C

•   MAP Statements define variables in a fixed layout

•   Dynamic Arrays (DIMX) allocate a dynamic array

•   Dynamic Structure Functions

•   DEFINE statements (symbol definitions)

•   Error trapping (ON ERROR GOTO)

•   IF … ENDIF conditional statements

•   DO / WHILE / UNTIL and FOR … NEXT loops

 

program factor,1.0(100)  ! factorization example

!-------------------------------------------------------

!Edit History

![100] April 05, 2014 12:51 PM          Edited by jack

!   created as example

!-------------------------------------------------------

 

on error goto TRAP

 

++include ashinc:types.def          ! extended data type defs (e.g. BOOLEAN)

 

define TRUE = -1

define FALSE = 0

 

map1 params

    map2 nf,i,4                     ! the number to factor

    map2 nx,i,4                     ! working value

    map2 i,i,4                      ! loop counter

    map2 count,i,2                  ! # of factors found

 

dimx factors(1),i,4, auto_extend    ! array of factors

 

    input "Enter number to factor: ",nf

 

    nx = nf

    i = 2                           ! first factor to consider

 

    do while i <= nx

        if fn'is'factor(i,nx) then

            nx /= i

            count += 1

            factors(count) = i

            ? "found ";i;" target now ";nx

            repeat

        endif

        i += 1

    loop

   

    ? "Factors of ";nf;": ";

 

    if count then  

        for i = 1 to count

            if i > 1 ? ",";

            ? factors(i);

        next i

    elseif nf = 1 then

        ? 1                         ! only factor of 1 is 1

    else

        ? "none found"

    endif

    ?

    end

 

TRAP:

    ? "Error #";err(0)

    end

 

!---------------------------------------------------------

! return TRUE if candidate is a factor of target

!---------------------------------------------------------

function fn'is'factor(candidate as i4, target as i4) as BOOLEAN

    if (target mod candidate) = 0 then

        fn'is'factor = TRUE

    endif

endfunction