Discovery Zone: Keeping Knowledge Free!
Search:
Keywords:
In Association with Amazon.com
Another great reference
cover
Assembly Language Programs and Organizations of the IBM PC

ASSEMBLY PROGRAMMING AND OS DESIGN


2. Intro to Assembly
2.1 Data Types
2.2 Registers
2.3 DOS Int Calls
2.4 Basic Commands
2.4.1 DOS Hello World
2.5 BIOS Int Calls
2.5.1 BIOS Hello World
2.6 String Routines

<HOME>

 

 

 

 

 

 

 

 

Another great reference
cover 80X86 IBM PC and Compatible Computers: Assembly Language, Design and Interfacing Vol. I and II

 

 

 

 

 

 

 

 

 

 

 

Need a good reference?
cover
Assembly Language Step-by-Step: Programming with DOS and Linux, 2nd Edition
CH2.4 Basic Commands

<HOME><TOC><PREV><UP><NEXT>

The next thing we need to know is variables and labels. Variables can be declared in your code using two methods. The first method is to produce constants that only exist for compiling purposes. This is done with the EQU statement.

Ex.6

Two           equ    2
Exit          equ    4Ch

              org    100h
              mov    AX,Two
              add    AX,Two

              mov    AH,Exit
              int    21h


As you can see, Two and Exit are just substitutes for actual values of 2 and 4ch respectively. These allow us to make code more readable as well as provide a way to define a value in one place that may be used thousands of times in our code.

The second method is to actually produce variables using the basic assembly data types.

db - byte
dw - word
dd - double word

This actually sets aside memory to store these values. For example:

Ex.7

              org    100h
              mov    AX,2
              add    AX,2
              mov    [Result],AX  ;save results in memory

              mov    AH,4Ch
              int    21h

Result        db     0            ;one byte of memory for result


Example 7 sets aside a byte of memory at the end of our code to store the result of the add operation.  You may be wondering why we have brackets around Result.  The easiest way to think of it is that any time you are saving to or reading from memory, the variable pointing to that memory must be in brackets.  This will be explained in more detail later.

Labels are used to identify to the assembler where a location in the code is. A very common label that we will use in every program from here out is Main. Main will always be the main part of our code which will be executed as soon as we run it. Labels are declared by adding a colon onto the end of them.

Ex.8

              org    100h
              jmp    Main

Result        db     0

Main:         mov    AX,2
              add    AX,2
              mov    [Result],AX

              mov    AH,4Ch
              int    21h

In example 8, we introduce the JMP command. This command simply jumps to a point in the code that you specify using a label. We move the variable Result up to the top and simply jump over it to the beginning of our code. This will be the standard structure of code from here on out in this text.

... Constants declared here

              org    Beginning Address
              jmp    Main

... Variables declared here

Main:         ... Main program begins here

...

              mov    AH,4Ch
              int    21h    ;code complete

We already know everything we need to revisit Hello World, but first lets put everything together to finally output the result to 2+2.

Ex.9

Two           equ    2
Exit          equ    4Ch

              org    100h
              jmp    Main

Result        db     0

Main:         mov    AL,Two
              add    AL,Two
              mov    [Result],AL

              mov    DL,'0'        ;ASCII values for numbers start with '0'
              add    DL,[Result]   ;DL = '0'(48) + Result (4)
              mov    AH,02h
              int    21h

              mov    AH,Exit
              int    21h

Compile the above with NASMW -o ex9.exe ex9.asm.  You can experiment with the values you add.  Note that any result above 9 is going to output something other than a number.

<HOME><TOC><PREV><UP><NEXT>
 

www.cynergysoft.com

Email any questions to the author, Nathan Daniels.

Copyright © 2001