2.12 Sample Programs
This section contains several little HLA programs that demonstrate some of HLA's features appearing in this chapter. These short examples also demonstrate that it is possible to write meaningful (if simple) programs in HLA using nothing more than the information appearing in this chapter. You may find all of the sample programs appearing in this section in the subdirectory of the "volume1" directory in the software that accompanies this text.
2.12.1 Powers of Two Table Generation
The following sample program generates a table listing all the powers of two between 2**0 and 2**30.
// PowersOfTwo- // // This program generates a nicely-formatted // "Powers of Two" table. It computes the // various powers of two by successively // doubling the value in the pwrOf2 variable. program PowersOfTwo; #include( "stdlib.hhf" ); static pwrOf2: int32; LoopCntr: int32; begin PowersOfTwo; // Print a start up banner. stdout.put( "Powers of two: ", nl, nl ); // Initialize "pwrOf2" with 2**0 (two raised to the zero power). mov( 1, pwrOf2 ); // Because of the limitations of 32-bit signed integers, // we can only display 2**0..2**30. mov( 0, LoopCntr ); while( LoopCntr < 31 ) do stdout.put( "2**(", LoopCntr:2, ") = ", pwrOf2:10, nl ); // Double the value in pwrOf2 to compute the // next power of two. mov( pwrOf2, eax ); add( eax, eax ); mov( eax, pwrOf2 ); // Move on to the next loop iteration. inc( LoopCntr ); endwhile; stdout.newln(); end PowersOfTwo; Program 2.8 Powers of Two Table Generator Program2.12.2 Checkerboard Program
This short little program demonstrates how to generate a checkerboard pattern with HLA.
// CheckerBoard- // // This program demonstrates how to draw a // checkerboard using a set of nested while // loops. program CheckerBoard; #include( "stdlib.hhf" ); static xCoord: int8; // Counts off eight squares in each row. yCoord: int8; // Counts off four pairs of squares in each column. ColCntr: int8; // Counts off four rows in each square. begin CheckerBoard; mov( 0, yCoord ); while( yCoord < 4 ) do // Display a row that begins with black. mov( 4, ColCntr ); repeat // Each square is a 4x4 group of // spaces (white) or asterisks (black). // Print out one row of asterisks/spaces // for the current row of squares: mov( 0, xCoord ); while( xCoord < 4 ) do stdout.put( "**** " ); add( 1, xCoord ); endwhile; stdout.newln(); sub( 1, ColCntr ); until( ColCntr = 0 ); // Display a row that begins with white. mov( 4, ColCntr ); repeat // Print out a single row of // spaces/asterisks for this // row of squares: mov( 0, xCoord ); while( xCoord < 4 ) do stdout.put( " ****" ); add( 1, xCoord ); endwhile; stdout.newln(); sub( 1, ColCntr ); until( ColCntr = 0 ); add( 1, yCoord ); endwhile; end CheckerBoard; Program 2.9 Checkerboard Generation Program2.12.3 Fibonacci Number Generation
The Fibonacci sequence is very important to certain algorithms in Computer Science and other fields. The following sample program generates a sequence of Fibonacci numbers for n=1..40.
// This program generates the fibonocci // sequence for n=1..40. // // The fibonocci sequence is defined recursively // for positive integers as follows: // // fib(1) = 1; // fib(2) = 1; // fib( n ) = fib( n-1 ) + fib( n-2 ). // // This program provides an iterative solution. program fib; #include( "stdlib.hhf" ); static FibCntr: int32; CurFib: int32; LastFib: int32; TwoFibsAgo: int32; begin fib; // Some simple initialization: mov( 1, LastFib ); mov( 1, TwoFibsAgo ); // Print fib(1) and fib(2) as a special case: stdout.put ( "fib( 1) = 1", nl "fib( 2) = 1", nl ); // Use a loop to compute the remaining fib values: mov( 3, FibCntr ); while( FibCntr <= 40 ) do // Get the last two computed fibonocci values // and add them together: mov( LastFib, ebx ); mov( TwoFibsAgo, eax ); add( ebx, eax ); // Save the result and print it: mov( eax, CurFib ); stdout.put( "fib(",FibCntr:2, ") =", CurFib:10, nl ); // Recycle current LastFib (in ebx) as TwoFibsAgo, // and recycle CurFib as LastFib. mov( eax, LastFib ); mov( ebx, TwoFibsAgo ); // Bump up our loop counter: add( 1, FibCntr ); endwhile; end fib; Program 2.10 Fibonacci Sequence Generator
|