Assembly-Language Additional Resources
Assembly-Language Additional Resources
Assembly-Language Additional Resources
We have learned from the previous chapters that the CPU contains registers as
the highest hierarchy in the CPU structure. These registers have its own function in
Assembly language. Figure 2.17 presents the different registers in the CPU.
8086 CPU has 8 general purpose registers, each register has its own name:
The main purpose of registers is to keep a number (variable). The size of the above
registers is 16 bit, like: 0110010110100101b (in binary form), or 12345 in decimal
(human) form.
Four general purpose registers (AX, BX, CX, DX) are made of two separate 8-bit
registers, for example if AX = 0110101011101110b, then AH = 01101010b and AL =
11101110b. AH refers to higher order accumulator and AL refers to lower order
accumulator. Figure 2.18 illustrates the bit placement.
Therefore, when you modify any of the 8 bit registers, 16 bit register is also
updated, and vice-versa. The same is for other 3 registers.
Because registers are located inside the CPU, they are much faster than the
memory.
Segment Registers
Every programming language needs an IDE in order to write the source code,
compile, debug and execute the program. In this course, we shall be learning about
the emu8086 IDE.
There are other IDE’s where you can write the source code for assembly language
but as to my experience, the emu8086 provides a simpler, user-friendly environment.
It has a much easier syntax than any of the major assemblers, but will still generate a
program that can be executed on any computer that runs 8086 machine code.
3. If not yet open, you can double click this icon to launched the file. You should
see something like this in your screen:
4. Click the “New” button to start writing your code. Click OK.
5. Saving, Opening, closing a file (program) are the same as with any Word or text
editor.
6. The emulate button is the most important part in the IDE because this is where
you compile and execute the program:
This is it for now. We shall continue using the IDE later as we proceed with sample
programs.
MOV Instruction
One of the most common commands used in Assembly program is the MOV
command. It has the following features:
• Copies the second operand (source) to the first operand (destination). For
example:
mov AX, BX
REG: AX,BX,CX,DX,AH,AL,BL,BH,CH,CL,DH,DL,DI,SI,BP,SP.
Other examples:
Variables
name DB value
name DW value
name can be any letter or digit combination, though it should start with a letter.
.model small header file that tells the assembler that you intend to use the small
memory model - one code segment, one data segment and one stack segment.
.data this section defines your data, values and initialization. (variable
declaration section)
.code signifying the code segment. The source code follows. Let’s examine
carefully the code segment:
• Divided into four columns: labels, mnemonics, operands, and comments,
• Labels refer to the positions of variables and instructions, represented by the
mnemonics.
• Operands are required by most assembly language instructions
• Comments aid in remembering the purpose of various instructions
Example:
.data
var1 db 7 ;var1 with a value of 7
var2 dw 1234h ;var2 with a value of 1234h
.code
ret
• Labels can be from 1 to 31 characters long and may consist of letters, digits, and
the special characters? @ _ $ %
ret allows your program to return to its main function after executing.
Now that we have defined the structure of the Assembler, we will create our first
program. Typically, the Hello World program is the simplest code for all
programming examples.
As you probably know from the previous lesson about MOV instruction, we will also
use it in this example.
we declared a variable named msg with its value in byte (DB) followed by a string
“Hello World $”. The infusion of the dollar sign $ signals the assembler to stop
displaying other characters. Removing the dollar sign will cause the assembler to
display other characters not defined in the variable msg.
this code is what we call a directive – a part of assemblers command, that is written
after the .code segment. Their function is to tell the assembler to start looking for
the address of the variables declared in the .data segment. In this case, the
variable msg.
this line of code is where the string “Hello World” is displayed. The mov ah, 9h and
int 21h are always paired to function as a output directive in assembly much like a
printf(); function in C.
mov dx, offset msg allows the assembler to point the address of the variable msg
and transfer its contents to the dx register. Once it is transferred, the mov ah,09h
and int 21h will do the printing in the screen.
this code simply tells the assembler to end the process. Again it is a directive which are
paired together.
ret
.MODEL SMALL
.DATA
char msg[20]=”Hello World”; msg db "Hello World $"
return 0; ret
}
Now that you have an idea of how Assembly program is written, we will continue with
the variable declaration.
this next example illustrates the use of variable declaration in numerical form:
As you can see, we declared two (2) variables in this program: var1 and var2.
var1 has a value of 7(decimal) in DB (define byte) while var2 has a value of
1234h(hexadecimal)in DW (define word) because it require a bigger byte.
this line of code allows the variable values to be transferred in the registers AL and
BX. In this case AL will have a value of 7 and BX will have a value of 1234.
Note the AL is used instead of AX because var1 has a value of 7 which can be
represented in an 8-bit. While var2 with a value of 1234h cannot be represented in
an 8-bit. So the whole length of the BX register will be used.
Note too that we did not display the contents of AL and BX. Though we can do that but
it suffice as this time.
We can also reserve a space for variables, in our previous example var1 db 7, we
have actually allocated a size/space/value in our variable. But sometimes, we can also
assigned values which are undefined like var3 db ?. The question mark (?) will
command the assembler to allocate uninitialized byte.
Let’s have an example of multi-line text. Let’s create an program that will display a
name and an address.
Note that all the programs that we are creating at this time requires displaying only,
we have not yet started data entry (inputting text).
LEARNING ACTIVITY
org 100h
.model small
.data
.code
mov ax, @data
mov ds, ax
int 21h
ret
make sure that you have copied exactly the code. You may wish to copy and paste it in
your IDE if you wish.
If your program does not encounter an error, your will see the emulator window and
the source code window displayed on top of your source code screen:
Figure 2-21. Emulator and original source code
The emulator box contains the registers with their initialized values when the program
was not yet executed. Clicking the single step icon is very helpful to track down values
of each register as the program is executed line-by-line. Clicking the run icon will
execute the whole program.
Since we are interested in determining the values of each register using the MOV
instruction, we shall be using the single step icon at this time.
Continue to click the single step icon until the code mov ax, 1234h is highlighted.
Then click the single step icon once more passing that line of code.
Click again the single step icon. What is the value of BX? Right, it is not 5678.
Click agai the single step icon. What is now the value of AX? You should get a value of
5678.
To make a software interrupt there is an INT instruction, it has very simple syntax: INT
value where value can be a number between 0 to 255 (or 0 to 0FFh), generally we
will usehexadecimal numbers.
LEARNING ACTIVITY