LEC 5 Assembly
LEC 5 Assembly
LEC 5 Assembly
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
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
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
Example:
label :
INC AX
DEC BX
JMP label ; infinite loop
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
Logic: CX CX – 1
if CX != 0 ; jump to destination label
How many times will the loop MOV AX,1 Solution: 216 = 65536
execute? MOV CX,0
L2:
DEC AX
LOOP L2
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 :
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
JE is equivalent to JZ
JNE is equivalent to JNZ
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
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
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:
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