Professional Documents
Culture Documents
Instruction set
The 8086 instruction set is relatively simple and it has a total of 117 basic instructions. If
you understand all basic instructions then you can fully control the 8086. Moreover, since
Intel processors are upward compatible so your assembly language program can run on
all Intel processor including the P5 or Core2 Duo!!!!
We will go through the different groups (not different instructions) one by one, however,
for details descriptions, you should refer to the document provided by the emu8086
software (www.emu8086.com) or a proper reference.
The main purpose of the data transfer instructions is to move data either between its
internal registers or between an internal register and a storage location in memory. The
most frequently used data transfer instruction is the move operation, mnemonic MOV.
Mem16 Seg-reg
The above table shows the syntax for the MOV instruction and the possible combinations
for the source and destination.
Example:
MOV AL, BL ; register to register
MOV AL, 16 ; immediate (a value 16) to register
Load effective address (LEA) is a frequently used instruction for saving the offset
address of a variable. This is similar to x = &y in C++ programming!
As its name implies, LDS will load a specified register and the DS register in a single
operation!
Syntax: LDS SI, [200] ; 4 bytes of data will be fetched, the first 2 bytes (location
200 and 201) stored in SI and the following 2 bytes (locations 202 and 203) stored in
DS.
There is also the load register and ES (LES). Similar to LDS, LES will load a specific
register and the ES register.
Arithmetic instructions
Packed decimal two BCD digits are packed into a byte register or memory location.
Each BCD is 4 bits representing digit 0 to 9.
Unpacked decimal numbers are stored one BCD digit per byte
After an arithmetic operation, the flags (such as Carry, Sign, Overflow, Zero) are updated
accordingly. Based on the flag status, different operations can be performed, similar to an
IF ELSE syntax.
ADC Add with carry ADC D, S D = S+D+CF OF, SF, ZF, AF, PF, CF
CF = carry
DAA Decimal adjust for DAA SF, ZF, AF, PF, CF, OF
addition undefined
Destination Source
Register Register
Register Memory
Memory Register
Register Immediate
Memory Immediate
AX Immediate
Example
AL = 12H, BL = 70H
Carry = 1
ADC AL, BL
Then result is in AL = 83H
The carry is only 1-bit and it is being added to the LSB (least significant bit).
Example
BX = 1234 CX= 0123
Carry is 0
SBB BX, CX ; subtract with borrow
Give 1111
Subtraction
Destination Source
Register Register
Register Memory
Memory Register
AX Immediate
Register Immediate
Memory immediate
If BX = 3AH
NEG BX
Give FFC6H
Multiplication
When multiplying 8-bit data, result is 16-bit and stored in AX (a 16-bit register).
When multiplying two 16-bit data, result is 32-bit and result stored in AX and DX.
The AX holds the LSW (Least significant word) and DX stores the MSW (Most
significant word). Only the source operand is specified in the multiply operation and
the other operand must be stored in AL, or AX. This also applies in the division
operation.
In 8-bit division, the result is stored in AH and AL. Where AH is the remainder and AL is
the quotient.
For 16-bit division, AX contains the quotient and DX contains the remainder
MOV AL, 39
MOV BL, 4
DIV BL ; so AL/BL and results stored in AH = 3 (remainder) and AL =9
Special functions
CBW convert byte (8-bit) to word (16-bit). So what is the additional 8 bits?
CBW fill AH with 0 if AL is positive; fill AH with 1s if AL is negative then AH
becomes FF. In doing so, the sign of the original data will be retained.
CWD convert word (16-bit) to double word (32-bit) and the DX register is used to store
the high order word.
CWD fill DX with 0 if AL is positive; fill DX with 1s if AX is negative then DX
becomes FF
Both CBW and CWD do not require operand, the data must be stored in AL or AX.
Ans.
AX = FFA1 after CBW ; AL 8-bit converts to 16-bit AX
DX = FFFF after CWD ; AX is 16-bit converts to 32-bit (DX + AX)
S, Z, A, P undefined
DIV Division DIV S AL = Q (AX/S8)
(unsigned) AH = R(AX/S8) OF, CF, SF, ZF, AF,
PF all undefined
IMUL Integer IMUL S AX = AL*S8 OF, CF depends on
multiply DX,AX=AX*S16 result
(signed) S, Z, A, P undefined
IDIV Integer divide IDIV S AX = Q(AX/S8) OF, CF, SF, ZF, AF,
(signed) AH=R(AX/S8) PF all undefined
AX = Q(DX,AX)/S16
DX=R(DX,AX)/S16
If Q is positive and
exceeds 7FFF or if Q
is negative and
becomes less than
8001 then type 0
interrupt occurs
Multiplication related functions
Try to write a simple assembly language program to convert from Centigrade (Celsius) to
Fahrenheit assuming result is an integer.
The equation for the conversion is
f = c * 9 / 5 + 32
What operations are required?
Do you need to define variables?
Sample is available in the ftp site
Logic instructions
Eg AND AX, BX
The above is applying logical AND to value in AX and value in BX. Result is stored in
AX. The bits in AX and BX are logical ANDed bit-by-bit.
Example:
MOV AL, 10101100B
AND AL, 11110000B ; bit by bit operation
AL 1 0 1 0 1 1 0 0
1 1 1 1 0 0 0 0
AND 1 0 1 0 0 0 0 0
Describe the result of executing the following:
MOV AL, 01010101B
AND AL, 00011111B (AL = 00010101B)
OR AL, 11000000B (AL = 11010101B)
XOR AL, 00001111B (AL = 11011010B)
NOT AL (AL = 00100101B)
Ans. AL = 00100101
Shift instructions
As its name implies, shift instructions are for shifting the bit(s) out of the operand. So
what will happen to the bit shifted out? What will happen to the vacated bit position?
There are 4 shift operations
Can perform: logical shift and arithmetic shift
Can shift left as well as right
Logical shift the vacated bit will be filled with 0 and the bit shifted out will be stored
in the Carry flag.
Arithmetic shift the vacated bit will be filled with the original most significant bit (this
only applies in right shift, in left shift vacated bit is filled with 0 as well.)
Original 0 0 0 0 0 0 0 1
Shifted 0 0 0 0 0 0 1 0
1 bit
left
On the other hand, shift to the right is the same as divide the value by 2. For example,
if the original value is 2 then shift to the right by 1 bit, the value becomes 1.
Shift operations can be used in multiply and divide operations.
SAR Shift arithmetic right SAR D, Count Shift the (D) right by C, O depends on
the number of bit result
positions equal to
count and fill the OF = 0 if first
vacated bits positions operand keeps
on the left with the original sign
original most
significant bit
Shift left
Shift Right
Example
Mov AL, 10001100b
MOV CL, #1
SHR AL, CL ; shift right 1 time
Rotate instructions
Rotate is similar to shift but the vacate bit is filled with the bit rotated out from the other
side.
Rotate left through carry (RCL) bit rotated out is stored in the carry flag, the vacated bit
is filled by the carry flag
Rotate left
Carry
Rotate operations
Exercise
1. How would the decimal number 1234 be coded in ASCII and stored in memory
starting at address 0C000H? Assume that least significant digit is stored at the lower
addressed memory location
Ans. 1 = (31H in 0C003H) 2 = (32H in 0C002H) 3 = (33H in 0C001H) 4 = (34H in
0C000H)
2. If register BX contains the value 0100H, register DI contains 0010 and register
DS contains 1075, what physical memory location is swapped when the following
instruction is executed
XCHG [BX+DI], AX
PA = 10750+0100+0010 = 10860H
3. Compute the physical address for the specified operand in each of the following
instructions:
MOV [DI], AX (destination operand) (0B000+0200 = 0B200)
MOV DI, [SI] (source operand) (0B000+0100 = 0B100)
MOV DI+XYZ, AH (destination operand) (0B000+0200+0400 = 0B600)
Given CS=0A00, DS=0B00, SI=0100, DI=0200, BX=0300, XYZ=0400
Assembly language programming III
Once you have gained the basic concept of assembly language programming, we can now
discuss more advanced topics including:
Flag control instructions
Compare and jump
Concept of subroutines
Loop and string instructions
Flag instructions
The flags are stored in a single register and a flag has only 2 status (set or clear).
When a flag is set, ie it is equal to 1; on the other hand, the flag is clear when it is 0.
The flag instructions allow programmers to manipulate the flag status directly. You can
set or clear a particular flag, or perform a complement (0->1, 1->0).
The instructions that can control the flags are given below:
The CLC, STC, and CMC are the most commonly used.
Compare instructions
Compare instructions are used when you need to make a branch or jump based on the
results after the comparison. In high-level programming, you have the IF, WHILE
statements.
In assembly language programming, when you perform a compare the overflow, sign,
zero, auxiliary carry, parity and the carry flag will be affected.
You can compare two 8-bit or two 16-bit values, and the operands can be stored in a
Register, memory, AX register or an immediate.
The compare instruction mnemonic is CMP and after the compare a branch or jump
instruction is usually followed according to the flags status.
When the compare operation is executed, the source operand is subtracted from the
destination operand. But the result of the subtraction is not saved (i.e. values in the source
and destination will not change)
Flags are changed according to the subtraction result and decision making is based on the
flags status.
Example
You can compare value of x and 100, after the compare operation, test the Carry
flag. If Carry flag is clear then x > 100 is true!!
Exercise
Determine the status for flags (C, S, Z, O) as the following instructions are being
executed
MOV AX, 1234
MOV BX, 0ABCDH ; this is a 16-bit value
CMP AX, BX
Assume that initially all flags are clear
Branch and jump instructions can alter the flow of the program, as shown below.
Normal program flow is top-down. However, the program flow can also be adjusted by
programming.
Program flow changed by a
Jmp A jump
A:
There are three kinds of jump and branch jmp, looping, and subroutine call.
Jump (jmp) after the jump, the program will execute instructions at the jump label
and will not return to the point where the jump is invoked.
Syntax: jmp label
Subroutine call (call) program will go to the location of the subroutine, execute the
subroutine and when it finishes return back to the original program
Syntax: call subroutine (or acall subroutine)
Jump instruction
The Jump instruction alters the execution path of instructions in the program by changing
the values of the code segment register (CS) and the instruction pointer (IP). In most
cases, only the IP value is changed.
Program execution is not intended to return to the next sequential instruction after the
jump, so no return information is saved.
Two kinds of jump operation: unconditional and conditional
Unconditional jump
As its name implies, there are no status requirements imposed for the jump to occur
When the instruction is executed, the jump always takes place to change the execution
sequence.
Conditional Jump
The jump operation depends on some status conditions (for example jump if Carry flag is
set!)
If condition(s) is/are met, then the jump takes place; otherwise execution continues
with the next sequential instruction of the program.
There are 2 kinds of unconditional jump: intrasegment jump, and intersegment jump
Intrasegment jump is limited to addresses within the current code segment (only the IP
value is changed)
Intersegment jump permits jumps from one code segment to another (both CS and IP
values are changed).
JMP $ -14
Short-label an 8-bit number is coded as an immediate operand to specify a signed
displacement (+127 to 128)
JMP START
Near-label operand specifies a new value of IP with a 16-bit displacement
START is a label
Example
Conditional JUMP
Conditional jump is based on some conditions. If the condition is satisfied then the jump
takes place otherwise, the instruction following the jump will be executed.
Example
Exercise
A student has written a for loop using C++ program, how the loop can be implemented
with 8086 assembly language programming?
Stack operations
The stack (or the stack segment) is used as a temporary storage. But how can we put
data into the stack?
Syntax:
PUSH AX ; store data in the AX register onto to the stack
POP BX ; get data stored in the top of the stack and put it to the BX register
Data must be 16-bit so you can do PUSH AX but not PUSH AL!
The Stack is also a segment so it is also access via the segment (SS) and the offset SP
When data is getting in and out of the stack, the SP (Stack Pointer) is updated
accordingly. SP is the offset relative to the SS (Stack segment register). The initial value
of SP is FFFEH and when data is stored into the stack then the stack grows
downward, ie the SP value decreases.
When data is being extracted, or POP, from the stack, the SP value increases, or the
stack grows upward.
The SP always points to the Top of the Stack (TOS). The TOS refers to the location
holding the last data PUSHed into the stack.
The stack is a Last-in-first-out device. The last data pushed to the stack will be extracted
in the next POP operation.
We can also store contents of flag register into the stack by PUSHF and restore by POPF
Example
PUSH AX
PUSH BX
POP AX
POP BX
There are intrasegment call (subroutine locates in the same code segment) and
intersegment call (subroutine locates in a different segment).
During a CALL operation, address for the instruction following the call instruction
(or the IP value) is stored in the STACK.
The IP value saved is the instruction following the CALL operation and this is very
important because when returning from a subroutine, we need to know what instruction
to be executed and this is by extracting the saved value from the stack!
CALL FARPROC
Farproc (this is a intersegment call) represents a 32-bit immediate operand that is stored
in the 4 bytes that follow the opcode of the call instruction in program memory. These
two words are loaded directly from code segment memory into IP and CS
Example
Write a procedure, or subroutine named SUM that adds 2 numbers 5 and 31 together and
places their sum in DX. Assume that this procedure is to be called from another
procedure in the same code segment and that at the time it is to be called, DX contains
the value of ABCD16 and this value must be saved at entry of the procedure and restored
at
its completion.
LOOP instructions
Check CX
If CX != 0 then loop
Loop while equal (loope) checks the contents of both CX and the ZF flag. Each time the
loop instruction is executed, CX decrements by 1 without affecting the flags, its contents
are checked for 0, and the state of ZF that results from execution of the previous
instruction is also tested for.
Loopne (loop not equal) meaning is looping will continue when not equal!
Loopne (loop if CX != 0 and ZF = 0)
Exercise
String instructions
String refers to a series of data words (characters) (or bytes) that reside in consecutive
memory locations.
You can move a string from a block of memory to a block elsewhere.
Can scan a string of data elements stored in memory looking for a specific value
Can also compare two strings
The instructions MOVS, MOVSB, MOVSW all perform the same basic operation.
An element of the string specified by the source index (SI) register with respect to the
current data segment (DS) register is moved to the location specified by the destination
index (DI) register with respect to the current extra segment (ES) register.
Example, for a string abcdefgh is stored in the memory then the character a will be
stored at the lowest address and h will be stored at the highest address. So for auto
increment of addresses, we will process from a to h. While for auto-decrement, we
will process from h to a.
Example
Compare strings
Compare operations perform: subtracts the destination operand from the source operand
and adjusts flags CF, PF, AF, ZF, SF, and OF accordingly.
The result of subtraction is not saved
Operation does not affect the operands
Scan string
Example
mov AX, 0
mov DS, AX
mov ES, AX
mov AL, 05
mov DI, A000
mov CX, 0F
CLD
Again: stosb
loopne again
Next:
Exercises
1. Refer to the following 8086 assembly language program, determine the contents of msg3 at the end of
the program, as well as the value of BL.
name "mycode" ; output file name (max 8 chars for DOS compatibility)
org 100h ; set location counter to 100h
lea si, msg
lea di, msg2
mov cx, 10
CLD
push si
next_char:
movsb
loop next_char
cmp_char:
inc bl
cmpsb
loopnz cmp_char
2. Refer to the following assembly program, and answer the following questions. Give proper
explanation to your answer.
#make_BIN#
next:
ADD AL, V1[BX] ; add two values
MOV V1[BX], BL ; move
INC BX ; BX=BX+1
LOOP next ; loop back to next
; variables:
V1 DB 24, 3, 2, 4, 1 ; just like an array
V2 DB 0
3. A student has written an assembly program to add data stored in an array called store. Where is
the sum stored in the current program? Now values stored in the array store become -12, 23, 18, 12, 58,
225. Modify the program so that it can now evaluate the average value of the 6 numbers.You must give
proper explanation for the modifications made. (Hints: You should consider the data size.)
#make_COM#
; com file is loaded at CS:0100h
ORG 100h
Jmp start
store db 23, 18, 12, 58
Start: ; program begins
LEA BX, store
mov CX, 4
Repeat: mov al, [bx]
add dl, al
add bx, 1
loop repeat
Finish: ; program terminates
RET
End