Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 33

Chapter 2 : Introduction to Assembly

Programming The Intel 8086 ISA


Instruction Set Architecture
Outline
 Types of instructions
 Arithmetic instructions
 Shift and Rotation instructions
 Boolean Instructions
 Branching Instructions
 Unconditional Jump Instructions
 Conditional Jump Instructions
 Translating Conditional Structures

2
Next ...
 Types of instructions
 Arithmetic instructions
 Shift and Rotation instructions
 Boolean Instructions
 Branching Instructions
 Unconditional Jump Instructions
 Conditional Jump Instructions
 Translating Conditional Structures

3
Branching Instructions (1)
 Branching : transfers the further execution of the program to a new position of the
code

 3 types of branching are possible:


• unconditional branching
• conditional branching
• subroutine call interrupt

 Unconditional branching
JMP xyz, LOOP xyz Causes an unconditional jump to the line labeled xyz. The jump is
always performed

 Conditional branching
JZ , JNZ JE , JNE, JC, JO, JNO, …Conditional branching are determined by indicators
(flags) which are themselves positioned by the previous instructions

4
Branching Instructions (2)
 Subroutine call
• CALL xyz (procedure call) that begins at line xyz
• The position of the instruction following the
CALL is stacked to ensure correct continuation
after the subroutine’s execution
• RET Return from subroutine, execution continues at the position collected in the
stack

 Interruption INT calls software interrupt


 No high-level control structures in assembly language, like if…else, do…while, …
 Comparisons and conditional jumps are used to:
• Implement conditional structures such as IF statements
• Implement conditional loops

5
Next ...
 Types of instructions
 Arithmetic instructions
 Shift and Rotation instructions
 Boolean Instructions
 Branching Instructions
 Unconditional Jump Instructions
 Conditional Jump Instructions
 Translating Conditional Structures

6
Unconditional Jump Instructions (1)
JMP destination

 JMP is an unconditional jump to a destination instruction


 A label is used to identify the destination address

 Example:
label :
INC AX
DEC BX
JMP label ; infinite loop

 JMP provides an easy way to create a loop


 Loop will continue endlessly unless we find a way to terminate it

7
Unconditional Jump Instructions (2)
 JMP causes the modification of the IP register: IP  IP + displacement
 JMP adds to IP register, the number of bytes (distance) between the jump
instruction and its destination. For a back jump, the distance is negative (2‘s
complement code)
 The used displacement value to reach a certain instruction is:
displacement = @ of referred instruction - @ of following instruction
 Example: The following program writes indefinitely the value 0 at 0140H. The first
instruction is located at 100H

0100 B8 00 00 MOV AX, 0 ; clear AX


0103 A3 01 40 MOV [140] ; AX wrote to address 140
0106 EB FC JMP 0103 ; branching to 103
0107 xxx ; never executed instruction
The displacement is in this case equal to FCh : -4 (= 103h-107h)
8
Unconditional Jump Instructions (3)
LOOP destination
 The LOOP instruction creates a counting loop
 The loop statement works automatically with the CX register (counts the iterations)

 Logic: CX  CX – 1
if CX != 0 ; jump to destination label

 Example: calculate the sum of integers from 1 to 10


MOV AX, 0 ; sum = AX
MOV CX, 10 ; count = CX
L1:
ADD AX, CX ; accumulate sum in AX
LOOP L1 ; decrement CX until 0
Note: 1) The ':' character at the end of the label is required only if the label is alone on the line
2) LOOPNE ; continue if not zero
9
Unconditional Jump Instructions (4)
Your Turn
What will be the final value of AX MOV AX,6 Solution: 10
MOV CX,4
L1:
INC AX
LOOP L1

How many times will the loop MOV AX,1 Solution: 216 = 65536
execute? MOV CX,0
L2:
DEC AX
LOOP L2

What will be the final value of AX? Solution: same value 1

10
Unconditional Jump Instructions (5)
Nested LOOP
 If you need to code a loop within a loop, you must save the outer loop counter
(CX value)
 Example
data segment
count DD ?
Ends
code segment
MOV CX, 100 ; set outer loop count to 100
L1:
MOV count, CX ; save outer loop count
MOV CX, 20 ; set inner loop count to 20
L2:
XXX xx, yy ; any instruction
LOOP L2 ; repeat the inner loop
MOV CX, count ; restore outer loop count
LOOP L1 ; repeat the outer loop
11
Unconditional Jump Instructions (8)
Example1 : Summing an Integer Array
data segment
intArray DW 100h,200h,300h,400h,500h,600h
Ends
code segment
MOV SI, OFFSET intArray ; address of intArray
MOV CX, LENGTHOF intArray ; loop counter
MOV AX, 0 ; clear the accumulator
L1:
ADD AX,[SI] ; accumulate sum in AX
; SI contains the @ of an array element (pointer)
ADD SI, 2 ; point to next integer
LOOP L1 ; repeat until CX = 0

12
Unconditional Jump Instructions (9)
Example2 : Summing an Integer Array
data segment
intArray DD 10000h,20000h,30000h,40000h,50000h,60000h
Ends
code segment
MOV SI, 0 ; index of intArray
MOV CX, LENGTHOF intArray ; loop counter
MOV AX, 0 ; clear the accumulator
L1:
ADD AX, intArray[SI*4] ; accumulate sum in AX
; SI is used as a scaled index
INC SI ; increment index
LOOP L1 ; repeat until CX = 0

13
Unconditional Jump Instructions (10)
Example : Copying a String from source to target
data segment
source DB "This is the source string", 0
target DB SIZEOF source DUP(0) ; Good use of SIZEOF
Ends
code segment
MOV SI,0 ; index register
MOV CX, SIZEOF source ; loop counter
L1:
MOV AL,source[SI] ; get char from source
MOV target[SI],AL ; store it in the target
; ESI is used to index source & target strings
INC SI ; increment index
LOOP L1 ; loop for entire string

14
Next ...
 Types of instructions
 Arithmetic instructions
 Shift and Rotation instructions
 Boolean Instructions
 Branching Instructions
 Unconditional Jump Instructions
 Conditional Jump Instructions
 Translating Conditional Structures

15
Conditional Jump Instructions (1)
Jcondition destination ; condition is the jump condition
 A conditional jump is executed only if some condition is satisfied, otherwise
execution continues sequentially to the next instruction
 The condition of the jump carries on the state of one (or more) status indicators
(flags) of the microprocessor
 Example :

 Types of Conditional Jump Instructions


• Jumps based on specific flags
• Jumps based on equality
• Jumps based on the value of CX
• Jumps based on unsigned comparisons
• Jumps based on signed comparisons
16
Conditional Jump Instructions (2)
 Jumps based on specific flags

Note: the indicators are positioned for the result of the last operation (by the ALU)

17
Conditional Jump Instructions (3)
Jumps Based on Signed Comparisons Jumps Based on Unsigned Comparison

Jumps Based on Equality

JE is equivalent to JZ
JNE is equivalent to JNZ

 Conditional jump usually follow the CMP (comparison instruction)


Example : if(A != B) ≡ CMP A,B
JNZ executedCodeIfTrue
18
Conditional Jump Instructions (4)
 Example 1  Example 2
Jump to L1 if unsigned AX is greater than Var1 Jump to L1 if signed AX is greater than Var1
CMP AX, Var1 CMP AX, Var1
JA L1 ; JA condition JG L1 ; JG condition
; CF = 0, ZF = 0 ; OF = SF, ZF = 0

 Example 3
Jump to L1 if signed AX is greater than or equal to Var1
CMP AX, Var1
JGE L1 ; JGE condition
; OF = SF

 Example 4
Jump to label L1 if bits 0, 1, and 3 in AL are all set
AND AL,00001011b ; clear bits except 0,1,3
CMP AL,00001011b ; check bits 0,1,3
JE L1 ; all set? jump to L1

19
Conditional Jump Instructions (5)
 Example : Add two signed numbers N1 and N2 located respectively to offsets 1100H and
1101H. The result will be stored at offset 1102H if positive, at offset 1103H if it’s negative and at
offset 1104H if it’s zero
N1 + N2 MOV AL,[1100H]
ADD AL, [1101H]
JS negative
Result YES
>0 JZ zero
NO MOV [1102H], AL
Store at
JMP end
Result 1102H
YES Negative:
=0
MOV [1103H], AL
NO
Store at JMP end
1104H
Store at Zero:
1103H MOV [1104H], AL
End: ………

20
Conditional Jump Instructions (6)
Application1: Sequential Search
; Receives: SI = array address
; CX = array size
; AX = search value
; Returns: SI = address of found element

search PROC USES CX


JCXZ notfound ; jump if CX=0
L1: CMP [SI], AX ; array element = search value?
JE found ; yes? found element
ADD SI, 4 ; no? point to next array element
LOOP L1
notfound: MOV SI, 0 ; if not found then SI = 0
found : RET ; if found, SI = element address
search ENDP

21
Conditional Jump Instructions (7)
Application2: Locate the first zero value in an array
 If none is found, let SI point to last array element

Data SEGMENT
array DW -3,7,20,-50,10,0,40,4
Code SEGMENT
MOV SI, OFFSET array – 2 ; start before first
MOV CX, LENGTHOF array ; loop counter
L1:
ADD SI, 2 ; point to next element
CMP WORD PTR [SI], 0 ; check for zero
LOOPNE L1 ; continue if not zero
JZ found ; found zero
notfound:
... ; SI points to last array value
found:
... ; SI points to first zero value

22
Next ...
 Types of instructions
 Arithmetic instructions
 Shift and Rotation instructions
 Boolean Instructions
 Branching Instructions
 Unconditional Jump Instructions
 Conditional Jump Instructions
 Translating Conditional Structures

23
Translating Conditional Structures(1)
 Block-Structured IF Statements
 IF statement in high-level languages (such as C or Java)
• Boolean expression (evaluates to true or false)
• List of statements performed when the expression is true
• Optional list of statements performed when expression is false
 Example : Translate IF statements into assembly language
C Language Assembly

MOV AX,var1
if( var1 == var2 ) CMP AX,var2
X = 1; JNE elsepart
MOV X, 1
else
JMP next
X = 2;
elsepart:
mov X, 2
next:
24
Translating Conditional Structures(2)
 Example : Implement the following IF in assembly language All variables are
signed integers

C Language Assembly

MOV AX,var1
if (var1 <= var2)
{ CMP AX,var2
var3 = 10; JLE ifpart
} MOV var3,6
Else MOV var4,7
{ JMP next
var3 = 6; ifpart:
var4 = 7; MOV var3, 10
} next:
25
Translating Conditional Structures(3)
 Compound Expression with AND
if ((al > bl) && (bl > cl)) {X =
1;}
One Possible Implementation

NO
AL > BL
CMP AL,BL ; first expression al > bl
YES JA L1 ; unsigned comparison : jump if above
JMP next
NO
BL > CL L1:
CMP BL,cl ; second expression bl > cl
YES
JA L2 ; unsigned comparison
JMP next ; both are true
X=1 L2: MOV X,1
next:

26
Translating Conditional Structures(4)
Better Implementation for AND
YES
AL ≤ BL
CMP AL,BL ; first expression al > bl
NO JBE next ; quit if false
CMP BL,CL ; second expression bl > cl
YES
BL ≤ CL JBE next ; quit if false
MOV X,1 ; both are true
NO
next:

X=1

 Reversing the relational operator, We allow the program to test to the second
expression
 Number of instructions is reduced from 7 to 5
27
Translating Conditional Structures(5)
 Application: IsDigit Procedure
 Sets the Zero flag if the character is a decimal digit

If (al >= '0' && al <= '9') {ZF = 1;}

CMP AL,’0’ ; AL < '0' ?


JB next ; yes? ZF=0, return
CMP AL,’9’ ; AL > '9' ?
JA next ; yes? ZF=0, return
TEST AL,0 ; ZF = 1
next:

28
Translating Conditional Structures(6)
Compound Expression with OR
 HLLs use short-cut evaluation for logical OR
 If first expression is true, second expression is skipped
if ( (al > bl) || (bl > cl) ) {X = 1;}

YES
AL > BL
CMP AL,BL ; is AL > BL?
NO JA L1 ; yes, execute if part
CMP BL,CL ; no: is BL > CL?
YES
BL ≤ CL JBE next ;no: skip if part
L1:
NO
MOV X,1 ; set X to 1
next:
X=1

29
Translating Conditional Structures(7)
 WHILE Loops
while( AX < BX) { AX = AX + 1; }
One Possible Implementation

top:
CMP AX,BX ; AX < BX ?
YES
AX ≥ BX JAE next ; false? then exit loop
INC AX ; body of loop
NO
JMP top ; repeat the loop
next:
AX = AX+1

NEXT
30
Translating Conditional Structures(8)
Your Turn . . .
 Implement the following loop, assuming unsigned integers

top:
while ( BX <= var1) { CMP BX,var1
BX = BX + 5; JA next
var1 = var1 - 1 ADD BX,5
} DEC VAR1
JMP top
next:

31
Translating Conditional Structures(9)
Yet Another Solution for While
CMP BX,var1
while ( BX <= var1) { JA next
BX = BX + 5; top:
var1 = var1 - 1 ADD BX,5
} DEC VAR1
CMP BX,var1
JBE top
next:

 Check the loop condition at the end of the loop


 No need for JMP, loop body is reduced by 1 instruction

32
Summary
Arithmetic
ADD, SUB, INC, DEC, NEG, …
Shift and Rotation instructions
SHL, SHR, SAL, SAR,…
Bitwise instructions (manipulate individual bits in operands)
AND, OR, XOR, NOT, …
CMP: compares operands, sets condition flags for later conditional jumps and loops
Conditional Jumps & Loops
Flag values: JZ, JNZ, JC, JNC, JO, JNO, JS, JNS,…
Equality: JE, JZ, JNE, JNZ,…
Signed: JG, JGE,…
Unsigned: JA, JAE, JB, JBE,…
LOOP, LOOPNZ
JMP and LOOP Applications
Traversing and summing arrays, Computing the Max, Sequential Search
Translating Conditional Structures
if…else, while, isDigit,…
33

You might also like