16-bit register Segment register memory Save all 16-bit registers Save flags
PUSHA instruction pushes all the internal registers onto the stack in the following order: AX, CX, DX, BX, SP, BP, SI, DI. The value of the SP is that before the PUSHA instruction. The PUSHF (push flag) instruction copies the contents of the flag register to the stack.
Dr. Ridha Jemal EE353: Introduction to Microprocessor 1431-1432 2
Ex1: POP BX
BL [ss:SP] BH [ss:SP+1] SP SP+2 Ex2:MOV AX, 7C3DH PUSH AX POP BX Int 3
; AX BX ; AL AH
10
11
12
13
STD: set direction flag so that the pointers are auto decremented.
CLD: clear direction flag so that the pointers are auto incremented.
15
16
17
18
19
An XLAT instruction first adds the contents of AL to BX to form a memory address within the data segment.
o Copies the contents of this address into AL. o Only instruction adding an 8-bit to a 16-bit number.
21
PA
Thus AL [0310DH]. Assuming this memory locations contains 52H (EBCDIC code for CR) this value is placed in AL. Thus = 52H Performs the direct table lookup technique often used to convert one code to another .
Dr. Ridha Jemal
22
The operation of the XLAT instruction at the point just before 6DH is loaded into AL
23
; Load the offset address ; AL=05 will retrieve 6th element ; Pull out of table the element ; Put in AL
24
25
26
Dest S o u r c e + D e s t; F l a g b i t s a r e a f f ected
INC Dest
Increment instruction (INC) adds 1 to a register or a memory location. The INC can add 1 to any register or memory location, except a segment register. With indirect memory increments, the size of the data must be described by using the BYTE PTR or WORD PTR directives. The reason is that the assembler program cannot determine if, for example, the INC [DI] instruction is a byte-, or word -sized increment.
27
An addition-with-carry instruction (ADC) adds the bit in the carry flag (C) to the operand data. This instruction mainly appears in software that adds numbers that are wider than 16 bits in the 8086.
Dr. Ridha Jemal EE353: Introduction to Microprocessor 1431-1432 28
Many forms of subtraction (SUB) appear in the instruction set. The only types of subtraction not allowed are memory-to-memory and segment register subtractions. Like other arithmetic instructions, the subtract instruction affects the flag bits.
29
DEC Dest
DEC instruction subtracts a 1 from a register or the con-tents of a memory location. The decrement indirect memory data instructions require BYTE PTR, or WORD PTR.
30
Arithmetic Instructions
SBB Instruction (Subtract with borrow):
o SBB Dest, Source
o Dest Source - Dest; o Flag bits is affected Ex: MOV AL,A7h MOV BL,3Fh SUB AL,BL SUB AL,5 A subtraction-with-borrow (SBB) instruction functions as a regular sbtraction, except that the carry flag (C), which holds the borrow, also subtracts from the difference. The most common use for this instruction is for subtractions that are wider than 16 bits in the 8086. Wide subtractions require that borrows propagate through the subtraction, just as wide additions propagate the carry. Like the SUB instruction, SBB affects the flags. Notice that the immediate subtract from memory instruction in this table requires a BYTE PTR , or WORD PTR directive.
Dr. Ridha Jemal EE353: Introduction to Microprocessor 1431-1432 31
Arithmetic Instructions
MUL Instruction (Multiplication of unsigned number ):
o MUL Data AX AL*Data or DXAX AX*Data
o The source operand can be a memory location or a register. o When we multiply an n-bit number with an m-bit number, the product will be at most (n+m) bits. Ex: MOV AL,05h MOV BL,04h MUL BL ; ALAL*BL = 14h
32
Arithmetic Instructions
IMUL Instruction (Multiplication of signed number ):
o IMUL Data AX AL*Data or
DXAX AX*Data
o When we multiply an n-bit number with an m-bit number, the product will be at most (n+m) bits. Ex: MOV AL,-1 ; MOV AL,FFh MOV BL,3 IMUL BL ; AX = FFFDh
33
34
35
Arithmetic Instructions
DIV Instruction (Multiplication of unsigned number ):
o DIV Data Quotient in AL ; Reminder in AH
Ex: MOV MOV IDIV Example: Write an MOV MOV CBW IDIV
Dr. Ridha Jemal
AX,16 ; AL = 10h BL,-3 ; BL = FDh (2S comp. of 3) BL Suppose an 8-bit number X is present in AL. assembly code to calculate AL,X BL,4 BL ; Convert Byte to word BH is filled with zeros BL
EE353: Introduction to Microprocessor 1431-1432 36
Logic Instructions
Logical Operation : AND, OR, NOT, XOR
AND Instruction : Affected Flag: OF, SF, ZF, PF, CF
AND D e s t, S o u r c e ; D e s t ( D e s t Source) Some flag bits change (S, Z and P. C = 0) Uses any addressing mode except memory to memory and segm e n t r e g i s t e rs a d d r e s s i n g. Ex: MOV MOV AND AND AX,3FCAh CX,CC09h AL,CL AL,F1h
;AL = 08 ;AL = 0
The AND operation is used for masking. We can set a particular bit in a register to 0 without changing other bits.
x.0 = 0 x.1 = x x x x x 1 1 1 1 x x x x x x x x 1 1 0 0 x x 0 0
37
Logic Instructions
OR Instruction : O R D e s t , S o u r c e;
Affected Flag: OF, SF, ZF, PF, CF Dest ( D e s t S o u r c e ) Some f l a g b i t s c h a n g e ( S , Z a n d P . C = 0 ) Uses any addressing mode except memory to memory and segm e n t r e g i s t e rs a d d r e s s i n g. Ex: MOV AL,13h MOV BL,30h OR AL,BL
;AL = 33h
x x x x 0 0 0 0 x x x x
AL,BH SI,DI BX,[SI] Data[SI+20h],AL
0 x x 0 1 0 0 1 1 x x 1
38
Logic Instructions
XOR Instruction : XOR Dest, Source;
Dest ( D e s t S o u r c e ) XOR used Complementing). 0 0 = 0 0 1 = 1 1 1 = 0 x 1 = x x 0 = x Ex: XOR OR AND XOR
Dr. Ridha Jemal
is
to
complement
particular
bits
(Bit
;
39
Logic Instructions
NOT Instruction : N O T D e s t;
Affected Flag: None NOT i s a l o g i c a l i n v e r s i o n Ex: NOT CL ;CH is ones complement NOT TEMP NOT BYTE PTR [1000h] ;1s complement of the byte
in 1000h
40
Logic Instructions
Shift Instruction
PF, SF and ZF flags are affected by shift instructions but left unchanged by the rotate instructions.
Target, Count;
Logic Instructions
Shift Instruction
42
Logic Instructions
Shift Instruction SHR Shift Logical Right Instruction : SHR Target, Count ;
o o SHR Target, 1; SHR Target, CL;
;
EE353: Introduction to Microprocessor 1431-1432 43
Logic Instructions
Shift Instruction SAL Shift Arithmetic left Instruction :
o o SAL Target, 1 ; SAL Target, CL ;
SAL divides a signed number by 2 on each shift The target can be a register or a memory
Carry SAL 0
Target
44
Logic Instructions
Shift Instruction SAR Shift Arithmetic Right Instruction :
o o SAR Target, 1; SAR Target, CL;
Target
;
45
Logic Instructions
Example
Shifting Example
An 16-bit unsigned number X is present in AX register. Write a program to compute 10.X, assuming that final result can be filled in 16 bits.
Solution #1 using MUL (Delay consuming solution) Ex: MOV BX,10 ; 2 clock cycles MUL BX ; DXAX=10X -> 120 clock cycles Solution #2 using SHIFT an ADD Ex: MOV AX,1 ; AX=2X -> 2 clock cycles MOV BX,AX ; BX=2X -> 2 clock cycles SHL AX,1 ; AX=4X -> 2 clock cycles SHL AX,1 ; AX=8X -> 2 clock cycles ADD AX,BX ; AX=10X -> 4 clock cycles -> 12 clock cycles
Dr. Ridha Jemal EE353: Introduction to Microprocessor 1431-1432 46
Logic Instructions
Rotate Instruction ROL/ROR Rotate Left (Right) Instruction :
o o ROL/ROR Target, 1; ROL/ROR Target, CL;
ROL
Target
ROR
Target
Ex:
47
Logic Instructions
Rotate Instruction RCL/RCR Rotate Through Carry Left (Right) Instruction :
o o RCL/RCR Target, 1; RCL/RCR Target, CL;
Carry
RCL
Target
RCR
Target
Ex: Consider a 64-bit number X=X63X62X0 located in DXCXBXAX registers. Write an assembly code to shift X one position left.
Sol1:
Sol2:
48
Logic Instructions
Rotate Instruction RCL Shift Arithmetic Right Instruction :
o o SAR Target, 1; SAR Target, CL;
All except immediate and segment registers. Count must be 1 or CL Ex: MOV SAR MOV MOV SAR AL,6 AL,1 BL,-16 CL,3 BL,CL
49
Comparison Instruction
CMP (Compare)Instruction :
CMP So u r c e 1 S o ur c e2 ; U p d a t e f l a g s The comparison instruction (CMP) is a subtraction that changes only the flag bits; the destination operand never changes. A comparison is useful for checking the entire contents of a register or a memory location against another value. A CMP is normally followed by a conditional jump instruction, which tests the condition of the flag bits. The only disallowed forms of compare are memory-tomemory and segment register compares. Ex1: CMP CL,BL CMP AX,SP CMP AX,SP CMP AX,2000h CMP [DI],CH CMP CL,[BP] Ex2: CMP AL,10h ;compare AL with 10h JAE label ;if 10h or obove Dr.Ridha Jemal EE353: Introduction to Microprocessor 1431-1432 50
Jump Instructions
Short Jump (relative jump): is a two-byte instruction where the target address within -128 to +127 of the IP value
JMP Instruction : JMP Disp.
o Unconditional jump (without testing any condition) o Conditional jump where condition is tested through one of the following flag bits : Z, C, P, O, S Ex: XOR BX,BX ; BX= 0 Start: MOV AX,1 ; AX= 1 ADD AX,BX ; BX= 0 JMP SHORT Next ; or JMP Next (Opcode=EB) Next: MOV BX,AX JMP Start
51
Jump Instructions
Near Jump : Direct: this is a 3-bytes instruction. the first byte is the code and the next two are the signed number displacement value. Register indirect jump JMP DI ; IP DI o Any non-segment register can be used. Memory indirect jump: JMP WORD PTR [BX]
Far Jump: Target address is outside the present code segment. A far jump instruction replaces the contents of both CS and IP with the four bytes following the opcode Direct far JMP Memory indirect far jump JMP DWORD PTR [BX]
Dr. Ridha Jemal EE353: Introduction to Microprocessor 1431-1432 52
Jump Instructions
Opcode Short Jump EB Disp.
Near Jump
E9
Disp. Low
Disp. High
Far Jump
EA
IP Low
IP High
CS Low
CS High
53
Jump Instructions
Unconditional Jump
Example: Add two numbers in ax and bx
Comments
Labels can be defined in separated lines. Example: calc andstop Labels can be defined in the same line Example: back
54
Jump Instructions
Conditional Jump Checking only one flag Instruction JZ , JE Description
Condition inverse
Instruction
Jump if Zero (Equal). Jump if Carry (Below, Not Above JC , JB, JNAE Equal) JS Jump if Sign. JO Jump if Overflow. JPE, JP Jump if Parity Even. JNZ , JNE Jump if Not Zero (Not Equal). JNC , JNB, Jump if Not Carry (Not Below, Above JAE Equal) JNS Jump if Not Sign. JNO Jump if Not Overflow. JPO, JNP Jump if Parity Odd (No Parity).
Dr. Ridha Jemal
ZF = 1 CF = 1
SF = 1 OF = 1 PF = 1 ZF = 0 CF = 0
SF = 0 OF = 0 PF = 0
Jump Instructions
Conditional Jump Checking only one flag
Instruction JZ label JNZ label JE label JNE label JC label JNC label JS label Name Jump if Zero Jump if Not Zero Jump if Equal Jump if Not Equal Jump if Carry Jump if Not Carry Jump if Sign condition Jump if ZF=1 Jump if ZF=0 Jump if ZF=1 Jump if ZF=0 Jump if CF=1 Jump if CF=0 Jump if SF=1
Jump if Not Sign Jump if Overflow Jump if Not Overflow Jump if Parity
Jump if Not Parity
Jump Instructions
Conditional Jump Checking on Signed numbers
Instruction JE , JZ JNE , JNZ JG , JNLE JL , JNGE JGE , JNL JLE , JNG Description Jump if Equal (=). Jump if Zero. Jump if Not Equal (<>). Jump if Not Zero. Condition ZF = 1 ZF = 0 ZF = 0 et SF = OF SF <> OF SF = OF Instruction inverse JNE, JNZ JE, JZ JNG, JLE JNL, JGE JNGE, JL
ZF = 1 ou SF <> OF JNLE, JG
57
Jump Instructions
Conditional Jump Checking on Unsigned numbers
Instruction JE , JZ JNE , JNZ JA , JNBE Description Jump if Equal (=). Jump if Zero. Jump if Not Equal (<>). Jump if Not Zero. Condition ZF = 1 ZF = 0 Inst.inverse JNE, JNZ JE, JZ
Jump if Above (>). CF = 0 and ZF = 0 JNA, JBE Jump if Not Below or Equal (not <=).
JB , JNAE, JC
Jump if Below (<). Jump if Not Above or Equal (not >=). Jump if Carry.
Jump if Above or Equal (>=). Jump if Not Below (not <). Jump if Not Carry. Jump if Below or Equal (<=). Jump if Not Above (not >).
CF = 1
CF = 0
JNAE, JB
JBE , JNA
Dr. Ridha Jemal
CF = 1 ou ZF = 1 JNBE, JA
58
Example
59
Example
1.
60
Example
Start
Ex:
N1+N2
Yes
MOV AL,[1100h] ; Al=N1 ADD AL,[1101h] ; Al=N1+N2 JS Negative JZ AL,Null MOV[1102h],AL ;Positive result JMP End Negative: MOV[1103h],AL ;Negative result JMP End Null: MOV[1104h],AL ;Null result End: HLT
Yes
61
62
Suppose two arrays X and Y containing 10 elements located in the data segment X is defined within 8 bits (byte) where Y is defined using 16 bits (word)
mov ax, 4c00h ; exit to operating system. int 21h Ends end start ; set entry point and stop the assembler.
63
Suppose two arrays X and Y containing 10 elements located in the data segment X is defined within 8 bits (byte) where Y is defined using 16 bits (word)
64
65
; multi-segment executable file template. data segment MSG1 DB 30h,31h,32h,0Dh,0Ah,'aaa',0dh,0Ah,0 MSG2 DB 'This is a sample line.',0 ends
code segment
string proc near lodsb cmp al,0 je stop mov dl,al mov ah,2 int 21h jmp string stop: ret
start: ; set segment registers: mov ax, data mov ds, ax ; add your code here cld mov si, offset MSG1 call string mov si, offset MSG2 call string mov ax, 4c00h ; exit to operating system. int 21h ends end start ; set entry point and stop the assembler.
66