6 - Introd. To Assem. Part 2
6 - Introd. To Assem. Part 2
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Branching Instructions
Unconditional Jump Instructions
Conditional Jump Instructions
Translating Conditional Structures
3Bh 00111011
0 0 0 𝑶𝑹
0Fh 00001111
1 1 0 𝒚 3Fh 001 11 11 1
1 0 1 Set to 1
Unchanged
1 1 1 𝟏
Forcing certain bits to be “1” without changing the other bits
XXXXXXXX
𝑶𝑹
00010010
XXX1XX1X
3Bh 00111011
0 0 0 𝑋𝑶𝑹
0Fh 00001111
1 1 0 𝒚 34h 001 10 10 0
1 0 1 invrted
Unchanged
0 1 1 𝒚
Inverting certain bits without changing the other bits
XXXXXXXX
𝑿𝑶𝑹
00010010
XXXXXX
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Branching Instructions
Unconditional Jump Instructions
Conditional Jump Instructions
Translating Conditional Structures
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Branching Instructions
Unconditional Jump Instructions
Conditional Jump Instructions
Translating Conditional Structures
Example
Label:
INC AX
DEC BX
JMP Label ; infinite loop
𝑇 h𝑒𝑑𝑖𝑠𝑝𝑙𝑎𝑐𝑒𝑚𝑒𝑛𝑡 𝑖𝑠𝑒𝑞𝑢𝑎𝑙𝑡𝑜:=𝟏𝟎𝟑h−𝟏𝟎𝟕h¿ − 𝟒¿ 𝑭𝑪 h
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 15
Next
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Branching Instructions
Unconditional Jump Instructions
Conditional Jump Instructions
Translating Conditional Structures
Example
------- Instructions
------- before Conditional Jumps are based on
JNZ L1 Specific flags
------- Executed if The value of CX
ZF =1 Unsigned comparisons
-------
L1: Signed comparisons
------- Destination if
------- ZF = 0
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 17
Conditional Jump Instructions
Jumps based on specific flags
Flags Description Mnemonic
ZF = 1 Jump if zero JZ
ZF = 0 Jump if not zero JNZ
CF = 1 Jump if carry JC
CF = 0 Jump if no carry JNC
OF = 1 Jump if overflow JO
OF = 0 Jump if no overflow JNO
SF = 1 Jump if sign JS
SF = 0 Jump if no sign JNS
PF = 1 Jump if parity JP
PF = 0 Jump if no parity JNP
Note: The flags are updated according to the result of the last ALU operation
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
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 20
Conditional Jump Instructions
Example: Add two signed numbers N1 and N2. The result will be stored in
A1 if positive, or in A2 if negative, or in A3 if zero
N1 + N2 MOV AL,N1
ADD AL,N2
JS negative
Result YES
>0 JZ zero
NO MOV A1,AL
Store in A1 JMP next1
Result YES Negative:
=0
MOV A2,AL
NO
Store in A3 JMP next1
Zero:
Store in A2
MOV A3,AL
Next1: ………
DEC CX
JNZ destination ; jump to destination label
Your Turn
MOV AX,6
MOV CX,4
What will be the final value L1:
of AX INC AX
LOOP L1 Solution: 10
MOV AX,1
MOV CX,0
How many times will the loop L2:
execute? DEC AX
LOOP L2 Solution: 216 = 65536
What will be the final value
of AX?
𝑳𝑶𝑶𝑷𝑵𝑬 destination
The loop continues while the unsigned value of &
So it will end the loop if
or
data segment
intArray DW 100h,200h,300h,400h,500h,600h
Ends
code segment
MOV AX,data
MOV DS,AX
MOV SI,OFFSET intArray ; address of intArray
MOV CX,6 ; loop counter
MOV AX,0 ; clear the accumulator
L1:
ADD AX,[SI] ; accumulate sum in AX
code segment
MOV AX,data
MOV DS,AX
MOV SI,0 ; index register
MOV CX,26 ; loop counter
L1:
MOV AL,source[SI] ; get char from source
MOV target[SI],AL ; store it in the target
; SI points to source & target
INC SI ; increment index
LOOP L1 ; loop for entire string
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 29
Example 2: Copying a String
data segment
source DB "This is the source string", 0
target DB 26 DUP(0)
s_size EQU (target – source)
Ends
code segment
MOV AX,data
MOV DS,AX
MOV SI,0 ; index register
MOV CX,s_size ; loop counter
L1:
MOV AL,source[SI] ; get char from source
MOV target[SI],AL ; store it in the target
; SI points to source & target
INC SI ; increment index
LOOP L1 ; loop for entire string
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 30
Example 3: Sequential Search
MOV SI,OFFSET array main
MOV CX,10
MOV AX,30
CALL search
; Receives: SI = array address
; CX = array size
; AX = search value
; Returns: SI = address of found element
search PROC
JCXZ notfound ; jump if CX=0
L1:
CMP [SI],AX ; array element = search value?
JE found ; yes? found element
ADD SI,2 ; 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
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 31
Next
Types of instructions
Arithmetic instructions
Shift and Rotation instructions
Boolean Instructions
Branching Instructions
Unconditional Jump Instructions
Conditional Jump Instructions
Translating Conditional Structures
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:
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 33
Translating Conditional Structures
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:
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 34
Translating Conditional Structures
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:
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?
NO
BL > CL JNA next ;no: skip if part
L1:
YES
MOV X,1 ; set X to 1
next:
X=1
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
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 39
Translating Conditional Structures
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:
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: