Desktop Programming > HLA

BEGINNING COMPUTER PROGRAMMING WITH HLA (High Level Assembly)

(1/6) > >>

David:
BEGINNING COMPUTER PROGRAMMING

 
(Using a Try it and See it approach)

                                                                                                                         
(AND ... a Computer Student's/Teacher's DREAM Introductory Computer Language - HLA)

Update: Update: please see this next link:

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

FREE homework help NOW available ...

You can contact me via:
http://sites.google.com/site/andeveryeyeshallseehim/home/he-comes
http://developers-heaven.net/forum/index.php/topic,2587.0.html


You may also want to see ...

http://developers-heaven.net/forum/index.php?topic=46.0

BEGINNING COMPUTER PROGRAMMING (using HLA and Python 3.1 and C++) 


http://developers-heaven.net/forum/index.php?topic=2599.0

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

 
 
Introduction:

 

To facilitate the solid insights provided in learning about computing by some exposure to Assembly Language, but also to provide a friendly and facile start for new programmers, HLA provides all the facility and power of Assembly Language, but with a user friendly syntax, yet offers many of the elegant structures of an HLL (High Level Language) like Pascal or C or even the OOP of a language like C++.  And so, going on to C, or C++, or any other modern HLL, will be more readily enabled, and with some real appreciation of what’s going on under the hood and inside the machine.

 

The author has successfully taught Computer Programming at a Canadian grade 7/8 Junior High and 9 to 12 High School level when PCs first became available using varied approaches, including using a personally authored simulated SIMPLE SIMON compiler running on a PC.   But his final approach, after which this course is being patterned, was the most fun, and also the most successful, for both students ... and teacher.

 

Please enjoy and profit from your work. Don't be afraid to make changes and see what happens.  Now dig in ... You may be surprised how much you will learn.

 

Shalom shalom,

David W. Zavitz

Toronto, Ontario, CANADA

dwzavitz@gmail.com

 
For arrangements to use this text beyond individual personal use, please contact the author at the above e-mail.

© (C) 2007-08-17

 

Acknowledgements:

This course and text would not be possible without the very gifted insights, teaching experiences, and programming skills of Randall Hyde,

http://en.wikipedia.org/wiki/Randall_Hyde

the Author, Creator, and so far, the Sustainer of HLA. I also would like to acknowledge the tremendous encouragement given by my youngest son Matthew, who's recent Engineering need to know a little C++, and then Assembly ... provided the incentive for a little more then that, from dad, (which led to HLA and now this attempt to benefit beginning computer students in general.)  Also, I would like to thank my students at Scarborough Christian High School, who besides being very promising young ladies and gentlemen, provided exemplary feedback as we went along into some not yet fully charted territory.

 

 
Table of Contents:

Chapter 01: The Computer prints a message
Chapter 02: The Computer gets some input
Chapter 03: The Computer adds some numbers
Chapter 04: The Computer repeats a procedure
Chapter 05: The Basic Elements of computer programming … and low level example(s)
Chapter 06: The Computer rolls the dice … a first simulation
Chapter 07: First Memory Flow Lab
Chapter 08: Memory Flow Lab2
Chapter 09: Memory Flow Lab3
Chapter 10: HLA Data Types
Chapter 11: The Computer does some Algebra with real numbers ... and Input Data Validation
Chapter 12: Pointers, Strings, your own Types, Records, Arrays, and dynamic things on the fly!
Chapter 13: The Computer files its Records
Chapter 14: The Computer reads its Files
Chapter 15: The Computer updates (edits, deletes, sorts, chops-duplicates-in) its Files
Chapter 16: The Computer … vast OS’s … and the garbage collects?  (Operating Systems ... what is DOS?)
Chapter 17: The Computer delivers daily Manna ... (gui Canadien ... eh?)
Chapter 18: OOP ... goes the computer?
Chapter 19: OOP 2
Chapter 20: Shaping Moving OOP with HLA   ... a CLASS ACT ! (revised 2013-05-24)

David:
Chapter 01: The Computer prints a message



Teacher ... set's up each computer, (the presumption here is that you are using a Windows OS, but only minor changes may be needed to adapt this text to Linux, Mac OSX, FreeBSD, or in the future, when HLA is available for them, other OS's also), and ...

 
installs and configures HLA files in the directory c:\hla\

 
For finding everything regarding HLA:

http://216.92.238.133/Webster/

 
If you are a first-time HLA user, the best thing to do is download and run the HLA setup program hlasetup.exe. This version replaces both the HLA and freeHLA (fhla) versions from previous releases.

http://216.92.238.133/Webster/HighLevelAsm/HLADoc/HLARef/HLARef_pdf/03_InstallingHLA.pdf

 
Teacher … also, as needed, to show how to use Windows, some simple OS commands, from the command line like cd to a directory, or md or dir i.e. showing the contents of the current directory.  And also, as needed, to discuss folders, and folders inside folders, and the path to a file in some folder (i.e. the tree structure in organizing files on the disk.)

 
Student ... Create and go to the working directory c:\hla\projects  Note the use of rem indicates a remark or comment. No need for student to enter the rem or what comes after this here:

 
click start  (click on the Circle Shaped Icon in the Lower Left Corner of a Windows 7 OS Screen)
click run  (but skip to next command below here ... if you are using Windows 7 OS ... or later)

enter cmd ( i.e. type in cmd and then press the Enter key)
enter (the following lines on the command line, but not rem or what comes after):

md projects  rem md projects means make a directory called projects
cd projects  rem cd projects means change to the directory projects

md chapt01_print_message
cd chapt01_print_message


Using a text editor program (already installed on your computer) like notepad.exe enter this next line (on the command line):

notepad.exe  print_message.hla

and then select yes when asked Do you want to create a new file?


Now enter these next five lines into the text editor workspace, (i.e. copy and paste the lines into the text editor workspace):


--- Code: ---program print_message;

#include( "stdlib.hhf" )

begin print_message;

stdout.put( "Hello World!" );

end print_message;

--- End code ---

 
Now save this as a file with the name print_message.hla         

(Make sure notepad doesn't save it as print_message.hla.txt)

(Enter dir to see the files in that DIRectory.  If it does have a .txt on the end of it, then rename it by entering the command below … (note: ren means rename)

ren  print_message.hla.txt  print_message.hla

or               

ren  print_message.txt  *.hla

Note: the file needs to end in .hla to let it be compiled by HLA

Now enter this command on the command line to compile/assemble, (the program text file just saved):

hla print_message.hla

Now run the new executable file just produced and see the output by entering this next command:

print_message.exe

See also: http://en.wikipedia.org/wiki/High_Level_Assembly

 
There is absolutely nothing stopping a programmer from writing the "Hello World" program in low-level assembly language, should they really want to do this. However, for the beginner who is experiencing their first hour with assembly language, the former code is far more approachable than this latter code (i.e., explaining the stack and how parameters are passed to a procedure via the stack is a relatively advanced subject).


--- Code: ---program HelloWorld;

#include( "stdlib.hhf" )

static
// nl is concated to end of string at compile time
hwString : string := "Hello World ... low_level" nl;
prsEnter : string := "Press 'Enter' to continue/exit ... ";

begin HelloWorld;

// Push the address of the "Hello World" string
push( hwString );

// Call an HLA Standard Library function that
// will print the string whose address has
// been pushed on the stack.
call stdout.puts;

push( prsEnter );
call stdout.puts;
call stdin.readLn; // keep window open until the enter key is pressed

end HelloWorld;
--- End code ---

 
Addendum:

A user, "Kocmotex" ...

http://developers-heaven.net/forum/index.php/topic,50.msg100.html#new

pointed out a typo error in the "Hello Word", that is now fixed :  "(Was) ... the use of a colon to terminate the third line of chapter 1's program ... employed as a device to demonstrate to the reader the operation of HLA's error reporting system" ?

It may be a good, even at this point, to try out making that "typo error" in your program. See what error message the HLA compiler/assembler presents to you, to help you find errors in your HLA program, so that you might fix them ... and then successfully make ... in this case ... the "print_message.exe" file.

David:
Chapter 02: The Computer gets some input

 

Student ... (Go to the working directory)

click start
click run

enter cmd

enter (the following on the command line):

cd c:\hla\projects

md chapt02_get_input

cd chapt02_get_input

 
Using a text editor like notepad.exe enter this next line (on the command line):

notepad.exe  get_input.hla (or just enter ... notepad  get_input.hla)

and then select yes when asked Do you want to create a new file?

 
Now enter these lines into the text editor workspace, (i.e. copy and paste the lines into the text editor workspace):


--- Code: ---program get_input;

#include( "stdlib.hhf" )

// after two front slash characters, 'comments' may be inserted
// into HLA programs

// declare first_name as a string variable in static memory and
// also reserve (i.e. allocate) space for 64 (byte-size) characters there

static
// declare first_name, last_name as strings in static memory ...
// and reserve (i.e. allocate) space for 64 byte-size characters
first_name: str.strvar(64);
last_name: str.strvar(64);

begin get_input;

stdout.put( "Please enter your first name: " );
stdin.get( first_name );

stdout.put( "Now enter your last name: " );
stdin.get( last_name );

stdout.put( "Are you really ", last_name, ", ", first_name, "?" );

end get_input;
--- End code ---


Now save this file with the name get_input.hla

Now enter this following command, on the command line, to compile/assemble  (the program text file just saved) to produce a new executable file with the default name  get_input.exe

hla get_input.hla (or just enter the command line command:  hla get_input)

Now run the file get_input.exe by entering this next command, (on the command line):

get_input.exe (or just enter:  get_input)


An example of using stdin.a_gets() to dynamically allocate space for the input string as needed.


--- Code: ---program get_input2;

#include( "stdlib.hhf" )

// after two front slash characters, 'comments' may be inserted into HLA programs
// declare firstname; lastname as string variables in var (dynamic) memory ...

var
firstname: string;
lastname: string;

begin get_input2;

stdout.put( "Please enter your first name: " );
stdin.a_gets();
mov( eax, firstname );

stdout.put( "Now enter your last name: " );
stdin.a_gets();
mov( eax, lastname );

stdout.put( "Are you really ", lastname, ", ", firstname, "?" nl );
str.free( lastname );
str.free( firstname );

end get_input2;
--- End code ---

David:
Chapter 03: The Computer adds some numbers

 

Student ... (Go to the working directory)

click start
click run

enter cmd

then enter (on the command line):

cd c:\hla\projects

md chapt03_add_nums

cd chapt03_add_nums

 
Using a text editor like notepad.exe enter this next line (on the command line):

notepad  add_nums.hla

and then select yes when asked Do you want to create a new file?


Now enter these lines into the text editor workspace, (i.e. copy and paste the lines into the text editor workspace):


--- Code: ---program add_nums;

#include( "stdlib.hhf" )

// declare three 32 bit integer variables in the HLA static memory space

static
num1: int32:= 23; // initialize num1 to 23
num2: int32:= 77; // initialize num2 to 77
sum: int32; // sum is NOT initialized

begin add_nums;

stdout.put( "The sum of ", num1, " and ", num2, " is " );

// eax is a 32 bit register in the microprocessor
mov( num1, eax );// move the (value of the) integer num1 into eax

add( num2, eax );// add the (value of the) integer num2 to (what's in) eax

mov( eax, sum ); // move the 32 bit value now in the eax register into the
// 32 bits of static memory that we reserved and called 'sum'                       

stdout.put( sum )
stdout.newln();  // output an appropriate carriage return/newline character(s)

stdout.put( nl, "Enter an integer " ); // nl will output a newline character(s) as above
stdin.get( num1 );

// Note: ok to leave off comma here, as adjacent strings are all concatenated together
stdout.put( nl "Enter a second integer " );
stdin.get( num2 );

stdout.put( nl "The sum of ", num1, " and ", num2, " is " );
mov( num1, eax );
add( num2, eax );
mov( eax, sum );
stdout.put( sum );
stdout.newln();  // output an appropriate carriage return/newline character(s)

end add_nums;
--- End code ---

 
Now save this file with the name add_nums.hla

Now enter this command on the command line to compile/assemble, (the program text file just saved), producing the executable file with the name add_nums.exe

hla add_nums.hla  (or just enter:  hla add_nums)

Now run the file add_nums.exe by entering this next command (on the command line), and observe:

add_nums.exe (or just enter the now executable command:  add_nums)

David:
Chapter 04: The Computer repeats a procedure

 

Student ... (Go to the working directory)

click start
click run

enter cmd

then enter (on the command line):

cd c:\hla\projects

md chapt04_repeat_proc

cd chapt04_repeat_proc


Using a text editor like notepad.exe enter this next line (on the command line):

notepad  repeat_proc.hla

and then select yes when asked Do you want to create a new file?


Now enter these lines into the text editor workspace, (i.e. copy and paste the lines into the text editor workspace):
 

--- Code: ---program repeat_proc;

#include( "stdlib.hhf" )

static
done: boolean:= false; 

// The above reserves a byte of static memory and those 8 bits of
// memory space are now referred to (by the name) 'done' ...
// The value is initialized to 'false' i.e. 0000_0000

// Note: HLA can presently address 2^32 bytes of memory, so it takes a 32 bit
// variable to be able to address each byte of memory available. The highest
// address addressable, 1111_1111_1111_1111_1111_1111_1111_1111, has all 32
// binary bits set.

// Note: the address to get to the memory location 'done' is a 32 bit address, but
// the memory space reserved for the boolean variable 'done' is just one byte


// The following procedure asks for two integers to be entered and then finds
// their sum. It then prints out the two integers just input, and also their sum.

procedure get_nums_find_sum;

// Now declare three 'automatic' variables to hold 32 bit integers
// This is done in this area AFTER the top 'procedure ...' line
// but BEFORE the 'begin ...' line

var
num1: int32; // These 'automatic' variables, inside the procedure's
num2: int32; // declaration section, are created EACH time 'called'.
sum: int32; // And when the procedure is EXITed, they are 'lost'

begin get_nums_find_sum;

stdout.put( "Enter an integer: " );
stdin.flushInput(); // clears the 'stdin' input buffer
stdin.get( num1 );

stdout.put( "Enter a second integer: " );
stdin.get( num2 );

stdout.put( "The sum of ", num1, " and ", num2, " is " );

mov( num1, eax );
add( num2, eax );
mov( eax, sum ); // the int32 (in 'var' memory) with label 'sum'
                    // now holds the 32 bit value of (num1 + num2)
    stdout.put( sum );

end get_nums_find_sum;

 

begin repeat_proc; // Ok now ... start the 'main' section of this program.

// This section is where the 'repeat_proc.exe' program file begins to
// execute, when it first starts to run, after it is loaded into memory.

repeat // This marks the top of the repeat loop structure.

get_nums_find_sum(); // now execute this procedure we defined above

// We could call the 'macro' stdout.put which passes two nl
// OS appropriate codes and the string "Again (y/n) ? " ... each to
// appropriate procedure calls of the stdout library 'put' function ...

// but here, ok to use "stdout.puts( someString );" since the ...

stdout.puts( nl nl "More (y/n) ? " ); // strings here all get concatenated together by the compiler

// flushInput() is a library procedure and is in
// the 'stdin' namespace in HLA
stdin.flushInput(); // this flushes any characters like 'nl' that were
// left in the stdin buffered input stream

stdin.getc(); // puts the first character entered into 'al' (al is an 8 bit register,
// the lowest 8 bit byte of 'eax', eax is a 32 bit register)

if( al = 'n' ) then // if the 8 bits in 'al' match the eight bits in
// the ASCII representation of the character 'n' ... then

mov( true, done );

// move the 8 bit value for 'true' into the 8 bits of memory the computer
// refers to by (the name of) 'done'

elseif( al = 'N' ) then // now check, if need to,
// if a capital 'N' character was entered
mov( true, done );

else // if, and only if, neither of above two checks were true,
// then do this section ...
// output a string (that gets all concatenated together
// before assembled by HLA)
stdout.puts( nl "Ok ... Here we go again." nl );

endif;  // This marks the 'end' of the if..elseif...elseif..else..endif structure.

until( done ); // This marks the bottom of the 'repeat/until' structure.

// This 'repeat..until' structure is always traversed at least once,
// since the 'condition' to exit is NOT tested UNTIL the end.

// If the value of the boolean variable is 'true'
// the loop is exited. Otherwise, it begins again
// right after the top 'repeat' (address in the memory).

end repeat_proc;
--- End code ---


Now save this text file with the name repeat_proc.hla

Now enter the following command on the command line to compile/assemble, (the program text file just saved), producing the executable file with the name repeat_proc.exe

hla repeat_proc.hla  (or just enter this command:  hla repeat_proc)

Now run the file repeat_proc.exe by entering this next command (on the command line).  Then supply suitable input and observe:

repeat_proc.exe  (or just enter:  repeat_proc)



Notice the 'done' procedure call used below, an example of an HLA function, (a procedure that returns a value.)


--- Code: ---program repeat_proc2;

#include( "stdlib.hhf" )

procedure get_nums_find_sum;
begin get_nums_find_sum;
stdout.puts( "Enter an integer: " );
stdin.flushInput();
stdin.geti32();
mov( eax, ebx );

stdout.puts( "Enter a second integer: " );
stdin.flushInput();
stdin.geti32();

stdout.put( "The sum of ", (type int32 ebx), " and ", (type int32 eax), " is " );
add( ebx, eax );
stdout.puti32( eax );
end get_nums_find_sum;

procedure done; @returns( "al" );
begin done;
stdout.puts( nl "Done (y/n) ? " );
stdin.flushInput();
stdin.getc();
if( al == 'y' || al == 'Y' ) then
mov( 1, eax);
else
stdout.puts( "Ok ... Here we go again." nl );
mov( 0, eax );
endif;
end done;


begin repeat_proc2;

repeat
get_nums_find_sum();
until( done() );

end repeat_proc2;
--- End code ---



And note the input validation procedure (function) added below ... Now the program won't crash on bad numeric input,


--- Code: ---program repeat_proc3;

#include( "stdlib.hhf" )

procedure getValidInt( msg:string ); @returns( "eax" );
begin getValidInt;
forever
try
stdout.put( msg );
stdin.flushInput();
stdin.geti32(); // May raise an exception.
unprotected
break;
exception( ex.ValueOutOfRange )
stdout.put( "Value was out of range, re-enter please ..." nl );
exception( ex.ConversionError )
stdout.put( "Value contained illegal char's, re-enter please ..." nl );
endtry;
endfor;
end getValidInt;

procedure get_nums_find_sum;
begin get_nums_find_sum;

mov( getValidInt( "Enter a first integer: " ), ebx );
getValidInt( "Enter a second integer: " );

stdout.put( "The sum of ", (type int32 ebx), " and ", (type int32 eax), " is " );
add( ebx, eax );
stdout.puti32( eax );
end get_nums_find_sum;

procedure done; @returns( "al" );
begin done;
stdout.puts( nl "Done (y/n) ? " );
stdin.flushInput();
stdin.getc();
if( al == 'y' || al == 'Y' ) then
mov( 1, eax);
else
stdout.puts( "Ok ... Here we go again." nl );
mov( 0, eax );
endif;
end done;


begin repeat_proc3;

repeat
get_nums_find_sum();
until( done() );

end repeat_proc3;
--- End code ---

Navigation

[0] Message Index

[#] Next page

Go to full version