Desktop Programming > HLA

Six Fast Steps to Programming in High Level Assembly (HLA) ...

(1/4) > >>

David:
  ~~16~Six Fast Steps to Programming in High Level Assembly (HLA)

This series of programs and comments is a continuation of the series Six Fast Steps to Programming in C ... and ... Six Fast Steps to Programming in C++

Update!  Update: please see this next link:

http://developers-heaven.net/forum/index.php/topic,2636.0.html

FREE homework help NOW available via e-mail ... (or in person if in Toronto Ontario Canada region.)
You can contact me via:
http://sites.google.com/site/andeveryeyeshallseehim/home/he-comes
http://developers-heaven.net/forum/index.php/board,9.0.html
The following fast paced tutorial may be of interest to new students of High Level Assembly (HLA) ...


Also now includes examples of using HLA strings with readLine, (available to you now by including my file "readLine.hhf" ... please see link to file "readLine.hhf" below), to read in dynamic HLA strings of ANY length, (length limited only by available memory).  This will add 2 more example programs, example 7. and example 8. to the 1. to 6. example HLA programs that follow.

Link for 7.
A simple example of my HLA type Vec container ... using an HLA Vec of HLA strings ...
http://developers-heaven.net/forum/index.php?topic=2599.msg2961#msg2961

Link for 8.
Using an HLA SLList type container of HLA strings ... (also demo's using 'split' line of text into a SLList of 'words')
http://developers-heaven.net/forum/index.php?topic=2599.msg2962#msg2962


Note: when you have exhausted these following pages, you may like to see vector2.hhf and list2.hhf (and func's .hhf files also) at the following link:

vector 2 link ...
http://developers-heaven.net/forum/index.php?topic=2600.0

list 2 link ...
http://developers-heaven.net/forum/index.php?topic=2600.0


The first part of this tutorial consists of a series of 6 example programs that students may like to have for reference and to use as a working 'shell' from which to start a project.

1. The first program is just a most basic 'shell' that assembles, and when run, asks the user to press 'Enter' to continue/exit ...

2. The next program illustrates how to get valid numeric input in a loop and how to ask for more at the end of a repeat ... until type HLA loop that calls a function 'more()' at the end of the loop

3. The next program demo's a way to get and validate user input using a FUNCTION  ... (a type of HLA procedure - one that returns a value in a register - commonly the 32 bit EAX register) ... to get valid input.

4. The next three programs progress from inputting numbers into an ARRAY that has the size fixed at compile time ... to using a vector ... and then a list container ... to hold as many numbers as the user wishes (limited only by available memory). They also illustrate how to SORT, FIND, or ERASE elements from the respective containers.

Note: you will need to have the appropriate pair of the following custom helper files:  vector.hhf, vector_func's.hhf ... or ... sllist.hhf and sllist_func's.hhf in the same folder as the programs '5.' and '6.' that use them, to assemble the programs '5'. and '6.' that use vector_func's.hhf and sllist_func's.hhf ... Please note that a link to these 4 helper files is provided below.

Note also, that all the numeric input example programs here, find the (integer) sum and the (floating point) average value ... of the numbers input.  Please note also the use of the FPU to handle floating point numbers.  Following are links to help with using the FPU and floating point numbers (and all things HLA) ...

http://216.92.238.133/Webster/www.artofasm.com/Windows/HTML/AoATOC.html

http://216.92.238.133/Webster/www.artofasm.com/Windows/HTML/RealArithmetic.html#998833

Please use this next link to access all things 'HLA" ...

http://216.92.238.133/Webster/

You may also like to see the SLOWER PACED beginning steps at this next link to  ...

BEGINNING COMPUTER PROGRAMMING USING HLA

https://docs.google.com/document/pub?id=1axLRopTi6Qb7ky_Er-IokvtkuneJMYqZAow8Ldas2t0


Or look here for some other programming links, etc ...

http://secureservices.ca

A student goal (and reward) here may be simply expressed like this ...

After the student has understood these examples, that student may then readily handle ... IN HLA ... many common beginner student type data processing problems ... with much of the same ease of using the C++ STL vector and list containers and the C++ STL support functions.

David:
Ok ... here is that promised 1st step HLA program .. just a 'shell' program that you may find helpful, to save some time typing in a basic HLA shell, from which you may add your code for your particular program ...

(You can now always begin with a working HLA shell program that you can assemble and run ... and so can then start with some working executable code that gives the output expected ... so that you can verify that, so far, everything works as expected.)

Note: to assemble these example programs in HLA, you must first have the HLA assembler installed on your computer.  Here is a link to the recently updated HLA home site, where you can find out about all things HLA, including downloading the latest version of HLA.

http://216.92.238.133/Webster/

Or go directly to the download page ...

http://216.92.238.133/Webster/HighLevelAsm/index.html

Once you have downloaded and installed HLA on your PC, then you can simply copy/paste these next example HLA programs into some 'working directory/folder', that you keep for HLA code on your PC.  (Note that your HLA program file names must end with .hla to assemble with the HLA assembler.)

Then go to a 'command line' inside that directory/folder  .. and then issue the command:

HLA yourProgramName.hla

to invoke the HLA assembler to run ... and to produce your executable program code file ... for that program  ... provided that your program had no HLA syntax errors ...

(Or you could use this following batch file ...  copy paste this file into a file with name ending with .bat in your working folder)

rem ~~~ batch file begins here ~~~
@echo off
SET fname=
SET /P fname=Enter the name of the file to be assembled/run:
hla %fname%
dir %fname%.exe
pause
del %fname%.obj
del %fname%.link
%fname%
pause
rem ~~~ batch file ends here ~~~


1.


--- Code: ---// shell.hla // // 2012-07-31 //

// http://developers-heaven.net/forum/index.php/topic,46.0.html

program shell;

#include( "stdlib.hhf"  )

begin shell;

    stdout.put( nl "Press 'Enter' to continue/exit ... " );
    stdin.readLn(); // keep 'Window' open until 'Enter' key is pressed

end shell;

--- End code ---

David:
Now ... here is the 2nd in this 6 fast step series ... a loop example that uses an HLA function I named 'more' ... (Note: an HLA 'function' is just a kind of HLA procedure that returns a value).  Please see the code and comments below ... and don't forget to assemble and run this working example.

Again, you may find this 'more' example quite useful, as a working 'shell', from which to start your beginner code projects, that loop and ask if you wish to loop again at the end of the loop.


2.


--- Code: ---// more.hla // // 2012-09-03 //

// http://developers-heaven.net/forum/index.php/topic,46.0.html

program more_demo;

#include( "stdlib.hhf" )


// defaults to 'true' ... unless 'n' or 'N' entered
procedure more; @nodisplay; @returns( "al" );
begin more;
    stdout.puts( "More (y/n) ? " );
    stdin.getc();
    stdin.flushInput();
   
    chars.toLower( al );
    if( al == 'n' ) then mov( false, al );
    else mov( true, al );
    endif;
end more;


static
    count:  int32   := 0;
    sum:    int32   := 0;
    avg:    real32;
   
    tmpStr: str.strvar( 32 );



begin more_demo;

    repeat

        try
            stdout.puts( "Enter next integer to sum: " );
            stdin.geti32();
            stdin.flushInput();
           
            add( eax, sum );
            inc( count );

        exception( ex.ConversionError )
            stdout.puts( "Invalid input! Integers only please ..." nl );

        exception( ex.ValueOutOfRange )
            stdout.puts( "Invalid input! Value out of range ... " nl );
           
        endtry;

    until( !more() );
   
    // The FINIT instruction initializes the FPU for proper operation.
    // Your applications should execute this instruction before
    // executing any other FPU instructions.

    finit();            // initialize floating point (math) unit

    // The FILD (integer load) instruction converts a 16, 32, or 64 bit two's
    // complement integer to the 80 bit extended precision format and pushes the
    // result onto the stack. This instruction always expects a single operand. This
    // operand must be the address of a word, double word, or quad word integer
    // variable.

    fild( sum );        // float integer load
    fild( count );

    // With no operands, the FDIVP instruction pops ST0 and ST1,
    // computes ST1/ST0, and pushes the result back onto the stack.

    fdivp(); // find totScores / totOutOf and leave result on top of FPU stack (in st0)

    // The FSTP instruction POPS and copies the value on the top of the floating
    // point register stack to another floating point register or to a 32, 64, or 80 bit
    // memory variable. When copying data to a 32 or 64 bit memory variable, the
    // 80 bit extended precision value on the top of stack is rounded to the smaller
    // format as specified by the rounding control bits in the FPU control register.

    fstp( avg );    // POP and store sto, the top of the FPU stack, into average

    stdout.put
    (
        "The average of ", count, " numbers with sum ", sum, " is "
    );
    str.put( tmpStr, avg:20:2 );
    str.delLeadingSpaces( tmpStr ); // to trim off any leading spaces ...
    stdout.puts( tmpStr );
   
   
    stdout.put( nl "Press 'Enter' to continue/exit ... " );
    stdin.readLn(); // keep 'Window' open until 'Enter' key is pressed

end more_demo;

--- End code ---

David:
This 3rd example illustrates a way to get valid input in a function that prompts for the specific input, using a passed in string parameter, and then returns the validated input.  (It also demos the use of HLA's powerful and very user friendly exception handling.)

 
3.


--- Code: --- // getValidInt.hla // // 2012-09-03 //
 
// http://developers-heaven.net/forum/index.php/topic,46.0.html

program getValidInt_demo;

#include( "stdlib.hhf" )


procedure getValidInt( prompt: string ); @nodisplay; @returns( "eax" );
begin getValidInt;
    forever
        try
            stdout.puts( prompt );
            stdin.geti32(); //  returns 32 bit integer in eax
            stdin.flushInput();
        unprotected
            break;
        exception( ex.ConversionError )
            stdout.puts( "Invalid input! Integers only please ..." nl );
            stdin.flushInput();
        exception( ex.ValueOutOfRange )
            stdout.puts( "Invalid input! Value out of range ... " nl );
            stdin.flushInput();
        endtry;
    endfor;
end getValidInt;


// defaults to 'true' ... unless 'n' or 'N' entered
procedure more; @nodisplay; @returns( "al" );
begin more;
    stdout.puts( "More (y/n) ? " );
    stdin.getc();
    stdin.flushInput();
   
    chars.toLower( al );
    if( al == 'n' ) then mov( false, al );
    else mov( true, al );
    endif;
end more;



static
    count:  int32   := 0;
    sum:    int32   := 0;
    avg:    real32;
    tmpStr: str.strvar( 32 );



begin getValidInt_demo;

    repeat

        // note: need to preserve/restore any reg's used when using
        // try..endtry as in getValidInt below ...
        getValidInt( "Enter next value to sum: " ); // returns int32 in eax

        add( eax, sum );
        inc( count );

    until( !more() );

    finit();        // initialize floating point (math) unit

    fild( sum );    // float integer load
    fild( count );

    // With no operands, the FDIVP instruction pops ST0 and ST1,
    // computes ST1/ST0, and pushes the result back onto the stack.

    fdivp();        // find sum / count and ...
                    // leave result on top of FPU stack (in st0)

    fstp( avg );    // POP and store sto, the top of the FPU stack, into avg

    stdout.put
    (
        "The average of ", count, " numbers with sum ", sum, " is "
    );
    conv.r32ToStr( avg, 20, 2, ' ', tmpStr );
    str.delLeadingSpaces( tmpStr );
    stdout.puts( tmpStr );
   

    stdout.put( nl "Press 'Enter' to continue/exit ... " );
    stdin.readLn(); // keep 'Window' open until 'Enter' key is pressed

end getValidInt_demo;

--- End code ---

David:
Now ... an array, with the size prefixed at assemble time ... followed by a dynamic array example, that uses dynamic memory, to allocate sufficient memory to hold an array of integers, with the max input size, specified by the user ... WHILE the program is running...


Note, the first program below, uses a constant to specify, at assemble time, the max array capacity.  This constant value can be then easily changed to a larger value, (then reassemble your program with this new value), to accommodate a larger anticipated number of integers.


4.


--- Code: ---// arrayInt.hla // // 2011-09-03 //

// enters integer data, finds sum, average, sorts, finds value, erase, shows

// http://developers-heaven.net/forum/index.php/topic,46.0.html

program aryInt_demo;

#include( "stdlib.hhf" )

const
    MAX_SIZE: uns32 := 4; // use small size here for simple testing purposes
   
type
    //Aryi32: int32[MAX_SIZE];
    pInt32: pointer to int32;

static
    last:   int32;
    myAry:  int32[MAX_SIZE]; // Aryi32;
    pMyAry: pInt32 := &myAry;
   
    count:  uns32;
    sum:    int32   := 0;
    avg:    real32;
    tmpStr: str.strvar( 32 );
   


procedure getValidInt( prompt: string ); @nodisplay; @returns( "eax" );
begin getValidInt;
    forever
        try
            stdout.puts( prompt );
            stdin.geti32(); //  returns 32 bit integer in eax
            stdin.flushInput();
        unprotected
            break;
        exception( ex.ConversionError )
            stdout.puts( "Invalid input! Integers only please ..." nl );
            stdin.flushInput();
        exception( ex.ValueOutOfRange )
            stdout.puts( "Invalid input! Value out of range ... " nl );
            stdin.flushInput();
        endtry;
    endfor;
end getValidInt;


// defaults to 'true' ... unless 'n' or 'N' entered
procedure more; @nodisplay; @returns( "al" );
begin more;
    stdout.puts( "More (y/n) ? " );
    stdin.getc();
    stdin.flushInput();
   
    chars.toLower( al );
    if( al == 'n' ) then mov( false, al );
    else mov( true, al );
    endif;
end more;


procedure sumAry( pAry: pInt32; size:uns32 ); @nodisplay; @returns( "eax" );
begin sumAry;
    push( ebx ); push( ecx );
    mov( 0, eax );
    mov( pMyAry, ebx );
    mov( size, ecx );
    for(  dec(ecx) ; (type int32 ecx) >= 0; dec( ecx ) )  do
        add( (type int32 [ebx + ecx*4]), eax );
    endfor;
    pop( ecx ); pop( ebx );
end sumAry;


procedure showAry( pAry: pInt32; size:uns32 ); @nodisplay;
begin showAry;
    push( ebx ); push( ecx );
    mov( pAry, ebx );
    for( mov( 0, ecx ); ecx < size; inc( ecx ) ) do   
        stdout.put( (type int32 [ebx + ecx*4]), " " );
    endfor;
    pop( ecx ); pop( ebx );
end showAry;


procedure isortAry( pAry: pInt32; size:uns32 ); @nodisplay;
begin isortAry;
    push( eax ); push( ebx ); push( ecx ); push( edx ); push( esi );
    // int i, j, cmp;
    mov( pAry, ebx );
    // start with an array of just the first 2 elements (if exists)
    for( mov( 1, ecx ); ecx < size; inc( ecx ) )  do
   
        // get copy of this new cmp element on each outer loop
        mov( [ebx+ecx*4], edx );
        // get index of element just to the left of the above 'cmp'
        // to start comparisons
        mov( ecx, esi );
        dec( esi );
        while( (type int32 esi) >= 0 && (type int32 edx) < [ebx + esi*4] ) do
       
            //ary[j+1] = ary[j]; // copy element 'up'
            //--j; // decrement j in preparation for next inner loop
            mov( [ebx + esi*4], eax );
            inc( esi );
            mov( eax, [ebx + esi*4] );
            sub( 2, esi );
           
        endwhile;
       
        //insert element at index j+1 (since j was decremented above)
        //ary[j+1] = cmp;
        inc( esi );
        mov( edx, [ebx+esi*4] );
       
    endfor;
    pop( esi ); pop( edx ); pop( ecx ); pop( ebx ); pop( eax );
end isortAry;


procedure findAry( pAry: pInt32; size:uns32; value: int32 );
                                @nodisplay; @returns( "eax" );
begin findAry;
    push( ebx ); push( edx );
    mov( value, edx );
    mov( pAry, ebx );
    for( mov( 0, eax ); eax < size; inc( eax ) )  do
        if( (type int32 [ebx+eax*4]) == edx ) then break; endif;
    endfor;
    // check end condition ...
    if( eax == size ) then mov( -1, eax ); endif;
    pop( edx ); pop( ebx );
end findAry;

// if valid index erase element, return new size in eax ...
procedure eraseAry( pAry: pInt32; size:uns32; index: uns32 );
                                @nodisplay; @returns( "eax" );
begin eraseAry;
    push( ebx ); push( edx );
   
    mov( pAry, ebx );
    mov( index, edx );
    if( (type int32 edx) >= 0 && edx < size ) then
        dec( size );
        for( mov( index, eax ); eax < size; inc( eax ) ) do
            // copy each element above, down one index, so erased
            //ary[i] = ary[i+1];
            inc( eax );
            mov( (type int32 [ebx+eax*4]), edx );
            dec( eax );
            mov( edx, (type int32 [ebx+eax*4]) );
        endfor;
    else
        dec( size );
        stdout.put( nl "ERROR! Index ", index, " out of range 0..",
                    size, nl );
        inc( size );
    endif;
   
    mov( size, eax ); // now update size and return (new - if updated) size...
   
    pop( edx ); pop( ebx );
end eraseAry;

   


begin aryInt_demo;

    mov( 0, ecx ); // using ecx as counter ...
    repeat

        // note: need to preserve/restore any reg's used when using
        // try..endtry as in getValidInt below ...
        push( ecx ); // preserve ecx
        getValidInt( "Enter next value to sum: " ); // returns int32 in eax
        pop( ecx );  // restore ecx
        mov( eax, myAry[ecx*4] );
        inc( ecx );

        if( ecx == MAX_SIZE ) then
            stdout.put( "You have reached ", MAX_SIZE, ", the max size.", nl );
            break;
        endif;

    until( !more() );

    mov( ecx, count );  // count now holds size of entered array ...
    sumAry( pMyAry, count );
    mov( eax, sum );
   
    finit();            // initialize floating point (math) unit
   
    fild( sum );        // float integer load
    fild( count );

    // With no operands, the FDIVP instruction pops ST0 and ST1,
    // computes ST1/ST0, and pushes the result back onto the stack.

    fdivp();        // find totScores / totOutOf and ...
                    // leave result on top of FPU stack (in st0)

    fstp( avg );    // POP and store sto, the top of the FPU stack, into avg

    stdout.put
    (
        "The average of ", count, " numbers with sum ", sum, " is "
    );
    conv.r32ToStr( avg, 20, 2, ' ', tmpStr );
    str.delLeadingSpaces( tmpStr ); // to trim off any leading spaces ...
    stdout.puts( tmpStr );
   
   
    stdout.puts( nl "showAry: " );
    showAry( pMyAry, count );
   
    //last = my_ary[ i-1 ];
    dec( ecx );
    mov( mov( myAry[ecx*4], eax ), last );
   
    stdout.puts( nl "After isort..." nl );
    isortAry( pMyAry, count );
    stdout.puts( "showAry: " );
    showAry( pMyAry, count );
   
   
    findAry( pMyAry, count, last );     // index returned in eax ...
    if( (type int32 eax) != -1 ) then   // i.e. if found ...
   
        eraseAry( pMyAry, count, eax ); // count holds size, eax holds index
        mov( eax, count );              // update count ...

        stdout.put( nl "After erasing ", last, "..." nl );
        stdout.put( "showAry: " );
        showAry( pMyAry, count );
       
    else stdout.put( nl, last, " NOT found in myAry." nl );
    endif;


    stdout.put( nl "Press 'Enter' to continue/exit ... " );
    stdin.readLn(); // keep 'Window' open until 'Enter' key is pressed

end aryInt_demo;

--- End code ---



This next example program, uses a dynamic array, with max capacity specified by the user of the running program, when it first begins to run.


--- Code: ---// dynamicAryInt.hla // // 2011-08-11 //

// enters integer data, finds sum, average, sorts, finds value, erase, shows

// http://developers-heaven.net/forum/index.php/topic,46.0.html

program dynamicAryInt;

#include( "stdlib.hhf" )

   
type
    pInt32: pointer to int32;

static
    last:   int32;
    pMyAry: pInt32;
   
    maxSize:uns32;
    count:  uns32;
    sum:    int32   := 0;
    avg:    real32;
    tmpStr: str.strvar( 32 );
   


procedure getValidInt( prompt: string ); @nodisplay; @returns( "eax" );
begin getValidInt;
    forever
        try
            stdout.puts( prompt );
            stdin.geti32(); //  returns 32 bit integer in eax
            stdin.flushInput();
        unprotected
            break;
        exception( ex.ConversionError )
            stdout.puts( "Invalid input! Integers only please ..." nl );
            stdin.flushInput();
        exception( ex.ValueOutOfRange )
            stdout.puts( "Invalid input! Value out of range ... " nl );
            stdin.flushInput();
        endtry;
    endfor;
end getValidInt;


// defaults to 'true' ... unless 'n' or 'N' entered
procedure more; @nodisplay; @returns( "al" );
begin more;
    stdout.puts( "More (y/n) ? " );
    stdin.getc();
    stdin.flushInput();
   
    chars.toLower( al );
    if( al == 'n' ) then mov( false, al );
    else mov( true, al );
    endif;
end more;


procedure sumAry( pAry: pInt32; size:uns32 ); @nodisplay; @returns( "eax" );
begin sumAry;
    push( ebx ); push( ecx );
    mov( 0, eax );
    mov( pMyAry, ebx );
    mov( size, ecx );
    for(  dec(ecx) ; (type int32 ecx) >= 0; dec( ecx ) )  do
        add( (type int32 [ebx + ecx*4]), eax );
    endfor;
    pop( ecx ); pop( ebx );
end sumAry;


procedure showAry( pAry: pInt32; size:uns32 ); @nodisplay;
begin showAry;
    push( ebx ); push( ecx );
    mov( pAry, ebx );
    for( mov( 0, ecx ); ecx < size; inc( ecx ) ) do   
        stdout.put( (type int32 [ebx + ecx*4]), " " );
    endfor;
    pop( ecx ); pop( ebx );
end showAry;


procedure isortAry( pAry: pInt32; size:uns32 ); @nodisplay;
begin isortAry;
    push( eax ); push( ebx ); push( ecx ); push( edx ); push( esi );
    // int i, j, cmp;
    mov( pAry, ebx );
    // start with an array of just the first 2 elements (if exists)
    for( mov( 1, ecx ); ecx < size; inc( ecx ) )  do
   
        // get copy of this new cmp element on each outer loop
        mov( [ebx+ecx*4], edx );
        // get index of element just to the left of the above 'cmp'
        // to start comparisons
        mov( ecx, esi );
        dec( esi );
        while( (type int32 esi) >= 0 && (type int32 edx) < [ebx + esi*4] ) do
       
            //ary[j+1] = ary[j]; // copy element 'up'
            //--j; // decrement j in preparation for next inner loop
            mov( [ebx + esi*4], eax );
            inc( esi );
            mov( eax, [ebx + esi*4] );
            sub( 2, esi );
           
        endwhile;
       
        //insert element at index j+1 (since j was decremented above)
        //ary[j+1] = cmp;
        inc( esi );
        mov( edx, [ebx+esi*4] );
       
    endfor;
    pop( esi ); pop( edx ); pop( ecx ); pop( ebx ); pop( eax );
end isortAry;


procedure findAry( pAry: pInt32; size:uns32; value: int32 );
                                @nodisplay; @returns( "eax" );
begin findAry;
    push( ebx ); push( edx );
    mov( value, edx );
    mov( pAry, ebx );
    for( mov( 0, eax ); eax < size; inc( eax ) )  do
        if( (type int32 [ebx+eax*4]) == edx ) then break; endif;
    endfor;
    // check end condition ...
    if( eax == size ) then mov( -1, eax ); endif;
    pop( edx ); pop( ebx );
end findAry;

// if valid index erase element, return new size in eax ...
procedure eraseAry( pAry: pInt32; size:uns32; index: uns32 );
                                @nodisplay; @returns( "eax" );
begin eraseAry;
    push( ebx ); push( edx );
   
    mov( pAry, ebx );
    mov( index, edx );
    if( (type int32 edx) >= 0 && edx < size ) then
        dec( size );
        for( mov( index, eax ); eax < size; inc( eax ) ) do
            // copy each element above, down one index, so erased
            //ary[i] = ary[i+1];
            inc( eax );
            mov( (type int32 [ebx+eax*4]), edx );
            dec( eax );
            mov( edx, (type int32 [ebx+eax*4]) );
        endfor;
    else
        dec( size );
        stdout.put( nl "ERROR! Index ", index, " out of range 0..",
                    size, nl );
        inc( size );
    endif;
   
    mov( size, eax ); // now update size and return (new - if updated) size...
   
    pop( edx ); pop( ebx );
end eraseAry;

   


begin dynamicAryInt;

    getValidInt( "Max number of int's to input: " );
    mov( eax, maxSize );
   
    intmul( @size(int32), eax );
    mem.alloc( eax );
    mov( eax, pMyAry );

    mov( pMyAry, ebx );
    mov( 0, ecx ); // using ecx as counter ...
    repeat

        // note: need to preserve/restore any reg's used when using
        // try..endtry as in getValidInt below ...
        push( ecx );
        getValidInt( "Enter next value to sum: " ); // returns int32 in eax
        pop( ecx );
        mov( pMyAry, ebx ); // restores ebx
       
        mov( eax, [ebx+ecx*4] );
        inc( ecx );

        if( ecx == maxSize ) then
            stdout.put( "You have reached ", maxSize, ", the max size.", nl );
            break;
        endif;

    until( !more() );

    mov( ecx, count );  // count now holds size of entered array ...
    sumAry( pMyAry, count );
    mov( eax, sum );
       
    finit();            // initialize floating point (math) unit
   
    fild( sum );        // float integer load
    fild( count );

    // With no operands, the FDIVP instruction pops ST0 and ST1,
    // computes ST1/ST0, and pushes the result back onto the stack.

    fdivp();        // find totScores / totOutOf and ...
                    // leave result on top of FPU stack (in st0)

    fstp( avg );    // POP and store sto, the top of the FPU stack, into avg

    stdout.put
    (
        "The average of ", count, " numbers with sum ", sum, " is "
    );
    conv.r32ToStr( avg, 20, 2, ' ', tmpStr );
    str.delLeadingSpaces( tmpStr ); // to trim off any leading spaces ...
    stdout.puts( tmpStr );
   
   
    stdout.puts( nl "showAry: " );
    showAry( pMyAry, count );
   
    //last = my_ary[ i-1 ];
    dec( ecx );
    mov( pMyAry, ebx );
    mov( mov( (type int32 [ebx+ecx*4]), eax ), last );
   
    stdout.puts( nl "After isort..." nl );
    isortAry( pMyAry, count );
    stdout.puts( "showAry: " );
    showAry( pMyAry, count );
   
   
    findAry( pMyAry, count, last );     // index returned in eax ...
    if( (type int32 eax) >= 0 ) then    // i.e. if found ...
   
        eraseAry( pMyAry, count, eax ); // count holds size, eax holds index
        mov( eax, count );              // update count ...

        stdout.put( nl "After erasing ", last, "..." nl );
        stdout.put( "showAry: " );
        showAry( pMyAry, count );
       
    else stdout.put( nl, last, " NOT found in myAry." nl );
    endif;
   
    free( pMyAry );


    stdout.put( nl "Press 'Enter' to continue/exit ... " );
    stdin.readLn(); // keep 'Window' open until 'Enter' key is pressed

end dynamicAryInt;

--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version