It took me a while to get my first Assembly program running, as well as numerous search on Google. books and help file. And in the end, all I did was just outputting some text to the screen.
However, I would like to sum up all the steps here, for all those who haven't tried to get a better start.
Not to delay any more, we are going to build our first assembly program, which is a "simple" Hello World program.
The assembler
First, you will need to get some tools. In CG2007, we are assigned to use the TurboASM to assemble the code. However, as far as I had look through the sample code, it's quite complicated (especially, if you 've just started to learn like me). Hence, found another assembler that might do the trick: the A86 macro assembler.
To start, we will download the assembler here and put on to one of your directories on your HDD.
Next, go to the DOS command prompt (Start menu - Programs - Accessories - Command Prompt) and move to the directory you have extracted the A86 compiler (H:\A86 for me).
When you open the command prompt, it will look something like this:
C:\>You will move to another directory with the "cd" command. Type:
cd H:\A86and the result will look like
H:\A86>
So you are now ready to compile your program.
Coding assembly
Next, you need to code. At this point, coding with assembly might seem challenging, because the syntax is quite different from any other high-level language like C, C++, C#, Java, etc.
Typically, a command in Assembly will have 3 component (not always, as you will soon see)
[command] [operand1] [operand2]
The command will determine the type of action, operand1 and 2 are registers. Since we are coding for the Intel 8086 chip, which offers 4 general 16-bit register (AX, BX, CX, DX) or 8 8-bit registers split from those (AH, AL, BH, BL, CH, CL, DH, DL), we will only be able to use these names (For more information about register, you will need to read through my lecturer's note or an external guide that I will provide at the end of the post).
Next, you will need a text editor. Notepad of Notepad++ will do.
Now, enter the following line code. I hope it's self-explained:
;An assemble comment is denoted with a semi-colon at the beginning
;it extends till the end of the line
MOV AH,02
;notice that the command doesn't end with anything, and there are 3 components
; with a comma between the second and the third
;MOV is the command to move data. AH and 02(number 02)
;The commands move the DATA from the SECOND operand to the FIRST.
;In this case, since 02 is data already, it will move the VALUE 02 to the register AH
;You can have ~MOV AH, BL~ (w/o the ~), which moves data from register BL to AH
;You can also have ~MOV CH, "A"~, which moves the LETTER A (ASCII value 65 or 41H) to CH
;Doing ~MOV 12, CL is not acceptable, as the first operand is the destination register
; which can't be a value
;By moving the value 02 to AH, we call a subfunction to handle outputing a character
; this character is stored in DL
MOV DL,"H"
;here, we store the "H" into DL. The program hasn't output anything at this point
INT 21h
;Here, 21h means number 21 in hexadecimal
;INT is the command to call the interrupt. the number after it defines the iterrupt to call
; in this case, it's a dos interrupt, which calls the subfunctions about
; to display the character
MOV AH,04Ch
;Again, we call a sub-function to exit the program
MOV AL,00
;AL will store the exit code
INT 21h
;call the DOS interrupt, executing the sub-function above and exit
As you can see, to call a functions, we don't use function names but rather very obscure number. Hence, it would help to have a list of functions and interrupt available for reference. You can look at some commonly used functions here.
Compile and assemble
Then, we need to compile our program. As your DOS command prompt is in the directory of the A86 already, save the code file there also (example: H:\A86\test1.asm).
Then, from the DOS command prompt, type
A86 test1.asm(or replace the filename you have chosen)
The assembly source code will get assembled into a .COM file.
Then, in type this to execute it in the command prompt
test1
You should see a single character H on screen.
Things to note
In conclusion, you will need these three command to write a single ASCII letter:
MOV AH,02
;This is to select the display character sub-function. It must be loaded to AH
MOV DL,"H"
;copy the data to be used. refer to the documents to know which register to store it
INT 21h
;call the interrupts that handles the sub-function.
For your pratice, try writing out the rest of the "ello World!" text.
I hope this guide helps as the first and easy step to assembly. Be wary that if you are coding with TurboASM, it's really different. I am working hard to understand it, so perhaps we will have an another guide soon.
Sincerely yours,
This guide was based on reference from:
- http://www.skynet.ie/~darkstar/assembler
No comments:
Post a Comment