Basic Assembler Instructions
The 80x86 Registers
In the processor of the computer, there are several built-in storage
locations, called the registers. They are of great importance, as every
computation the computer does must involve at least one of them. The
registers names consist of two letters. The complete list of registers
common to all 80x86 processors is AX, BX, CX, DX, SI, DI, CS, DS, ES.
The first four are known as the general purpose registers, SI and DI are
called the index registers, and CS, DS, and ES are called the segment
registers. Only the first four will be important for us now. All of
these registers are 16-bit, i.e. they can store a number from 0 to 65535,
or, in hex, from 0 to FFFFxH (Recall the discussion of binary numbers in
the previous chapter). The first four registers can also be subdivided
into eight 8-bit registers. These are divided by taking the first letter
of the original register name, and adding either a 'H' or an 'L'. For
example, the AX register is divided like this:
AX
/----------------\
1 0 0 1 1 1 0 1
\-------/\------/
AH AL
Load up D86 and look at the list of registers in the bottom left of the
screen. Notice that the values are given in hexadecimal.
Using memory
Sometimes we require more storage space than can be got from just the
registers. In this case we need to allocate memory to store some temporary
data in. In an assembler, we instruct the assembler to allocate space by
using the 'DB', 'DW', or 'DD' commands.
The 'DB' command is used to allocate a byte sized variable. It is used as
follows:
The name statement is used to specify the name by which the variable
will be referred to later in the program. The initial_value is
used to specify an optional value with which the variable will be
initialised. If it doesn't matter, use a '?' instead. The 'DW' command is used to
allocate a word sized variable, and it is used the same as the 'DB' command.
Similarly, the 'DD' command allocates two words of space.
The First Instruction: MOV
The most basic of all the instructions is the MOV instruction. As its
name suggests, it stands for "move". The format of the MOV instruction
is:The instruction MOV AX,BX then,
would copy the value in the BX register and place it in the AX register.
The source and destination parts are called the operands.
Note:It should be noted that the order of the source and
destination is the opposite to what would be expected in ordinary english.
MOV AX,BX does NOT mean "move AX to BX", but "move AX
with/from BX".
The operands to the MOV command do not have to be
registers. The destination and source can be any combination of
registers, memory variables, and constant numbers(immediate values),
except for two memory variables or two constants. They do have to be the
same size (i.e. the instruction MOV AX,BL would not be allowed). Here are
some examples:
- MOV BX,AX
- This stores the value in AX into BX.
- MOV CX,42
- This stores the number 42 into CX.
- MOV CL,AL
- This stores the number in CL, the lower half
of CX, into AL, the lower half of AX.
- MOV CX,25DCxH
- This stores the hexadecimal number 25DC into CX.
- MOV DX,variable
- This stores the value which is in
the memory location referenced by variable into DX.
- MOV variable,AX
- This stores the value in AX to the
memory location referenced by variable.
Load up D86 and try typing in some of these instructions. You should see
the registers changing with your commands. Remember the registers are shown
in hex.
The ADD instruction
The add instruction is the most basic arithmetic instruction on the 80x86.
The instruction takes the form:
The source will be added to the destination and the result will be stored in
the destination. Some examples are:
- ADD DX,42
- This adds 42 to the value stored in DX and stores
the result in DX.
- ADD DL,AH
- This adds the value in AH to the value in DL
and stores the result in DL.
- ADD variable1,AX
- This adds the value in AX to the memory
location referenced by variable1
The SUB Instruction
The SUB instruction is similar to the ADD instruction, except it subtracts
the numbers instead of adding then. The format of the instruction is:
The source is subtracted
from the destination and the result is stored in the destination. Examples
are the same as the previous instructions and the operand choice is
identical. Go into D86 and try it!
The Flags
Suppose we have a program which contains the instruction ADD AL,BL, and
when this instruction is executed, AL contains 204 and BL contains 72.
We'll convert these numbers to binary and carry out the addition.
204: 1 1 0 0 1 1 0 0
+ 72: 0 1 0 0 1 0 0 0
-----------------
=276: 1 0 0 0 1 0 1 0 0
When we try to store this in AL we have a problem. AL is an 8-bit register,
but our result is 9-bits in size! The processor will handle this as follows:
the lower 8 bits will be stored in AL, and the 9th bit will be stored in
the 'carry flag'. The carry flag is a 1-bit location in the processor
which is set with the 9th bit after every addition (or the 17th bit after
an addition involving two 16-bit numbers).There are other flags, the most
important being the 'zero flag'. After any operation, if the result was
zero, the 'zero flag' will be set. If the result was non-zero, the flag
will be cleared.
In D86, the flags appear as a row of letters above the
registers. The carry flag is a 'c' and the zero flag is a 'z'. If the
letter is visible the flag is set.
Instructions using the Carry Flag: ADC,SBC
The ADC instruction stands for 'add with carry' and takes the form:
It is the same as the ADD
instruction except the value of the carry flag is also added to give the
result. It can be used to add numbers larger than 16 bits. For example,
to add two 32-bit numbers, one stored in the AX and BX registers, the
other stored in the CX and DX registers, we would use the following
instructions:
The SBC instruction stands for subtracting with carry. The instructions
we would use to subtract two 32-bit numbers stored in AX and BX, and the
other in CX and DX.
Example Program 1:
As practice for using your assembler, open your text editor and enter the
following program. It won't do a lot, but this is only the second chapter
after all.
mov ax,174 ; stores 174 into the ax register
add ax,52 ; now adds 52 to this, so we have 226 in ax
mov tempvar,ax ; store this in the tempvar memory variable
mov bh,10xH ; these two instructions together
mov bl,f3xH ; store 10f3xH into the bx register
add tempvar,bx ; adds this value to the memory variable
mov ax,b4d2xH ; now a 32-bit addition using the carry flag
mov bx,75c2xH
mov cx,9e83xH
mov dx,432cxH
add bx,dx
adc ax,cx
int 20h ; this line is necessary to let the computer
; know the program has finished
tempvar DW ?
After the program has been entered, save it as 'example1.asm', and if you
are using A86 assemble it using the command line
'a86 example1.asm'. If you run the program now, hopefully it will do nothing!
At this stage, you will need to use
Back to main index