Chapter 7 P3
Chapter 7 P3
Computer Organization
&
Assembly Language Programming
Dr Adnan Gutub
aagutub ‘at’ uqu.edu.sa
[Adapted from slides of Dr. Kip Irvine: Assembly Language for Intel-Based Computers]
Most Slides contents have been arranged by Dr Muhamed Mudawar & Dr Aiman El-Maleh from Computer Engineering Dept. at KFUPM
Presentation Outline
Conditional Processing Computer Organization & Assembly Language Programming slide 2/55
1
AND Instruction
Bitwise AND between each pair of matching bits
AND destination, source
Following operand combinations are allowed AND
AND reg, reg
Operands can be
AND reg, mem
8, 16, or 32 bits
AND reg, imm and they must be
AND mem, reg of the same size
AND mem, imm
AND instruction is 00111011
often used to AND 00001111
cleared unchanged
clear selected bits 00001011
Conditional Processing Computer Organization & Assembly Language Programming slide 3/55
Conditional Processing Computer Organization & Assembly Language Programming slide 4/55
2
OR Instruction
Bitwise OR operation between each pair of matching bits
OR destination, source
Following operand combinations are allowed OR
OR reg, reg
Operands can be
OR reg, mem
8, 16, or 32 bits
OR reg, imm and they must be
OR mem, reg of the same size
OR mem, imm
OR instruction is 00111011
often used to OR 11110000
set unchanged
set selected bits 11111011
Conditional Processing Computer Organization & Assembly Language Programming slide 5/55
Conditional Processing Computer Organization & Assembly Language Programming slide 6/55
3
Converting Binary Digits to ASCII
OR instruction can convert a binary digit to ASCII
0 =00000000 1 =00000001
'0' = 0 0 1 1 0 0 0 0 '1' = 0 0 1 1 0 0 0 1
XOR Instruction
Bitwise XOR between each pair of matching bits
XOR destination, source
Following operand combinations are allowed XOR
XOR reg, reg
Operands can be
XOR reg, mem
8, 16, or 32 bits
XOR reg, imm and they must be
XOR mem, reg of the same size
XOR mem, imm
XOR instruction is 00111011
often used to XOR 11110000
inverted unchanged
invert selected bits 11001011
Conditional Processing Computer Organization & Assembly Language Programming slide 8/55
4
Affected Status Flags
Sample Output
Conditional Processing Computer Organization & Assembly Language Programming slide 10/55
5
Encrypting a String
KEY = 239 ; Can be any byte value
BUFMAX = 128
.data
buffer BYTE BUFMAX+1 DUP(0)
bufSize DWORD BUFMAX
Conditional Processing Computer Organization & Assembly Language Programming slide 11/55
TEST Instruction
Bitwise AND operation between each pair of bits
TEST destination, source
The flags are affected similar to the AND Instruction
However, TEST does NOT modify the destination operand
TEST instruction can check several bits at once
Example: Test whether bit 0 or bit 3 is set in AL
Solution: test al, 00001001b ; test bits 0 & 3
We only need to check the zero flag
; If zero flag => both bits 0 and 3 are clear
; If Not zero => either bit 0 or 3 is set
Conditional Processing Computer Organization & Assembly Language Programming slide 12/55
6
NOT Instruction
Inverts all the bits in a destination operand
NOT destination
Result is called the 1's complement
Destination can be a register or memory NOT
Conditional Processing Computer Organization & Assembly Language Programming slide 13/55
CMP Instruction
CMP (Compare) instruction performs a subtraction
Syntax: CMP destination, source
Computes: destination – source
Destination operand is NOT modified
All six flags: OF, CF, SF, ZF, AF, and PF are affected
CMP uses the same operand combinations as SUB
Operands can be 8, 16, or 32 bits and must be of the same size
Examples: assume EAX = 5, EBX = 10, and ECX = 5
cmp eax, ebx ; OF=0, CF=1, SF=1, ZF=0
cmp eax, ecx ; OF=0, CF=0, SF=0, ZF=1
Conditional Processing Computer Organization & Assembly Language Programming slide 14/55
7
Unsigned Comparison
CMP can perform unsigned and signed comparisons
The destination and source operands can be unsigned or signed
Signed Comparison
For signed comparison, we examine SF, OF, and ZF
Signed Comparison Flags
signed destination < signed source SF ≠ OF
signed destination > signed source SF = OF, ZF = 0
destination = source ZF = 1
Conditional Processing Computer Organization & Assembly Language Programming slide 16/55
8
Next . . .
Conditional Processing Computer Organization & Assembly Language Programming slide 17/55
Conditional Structures
No high-level control structures in assembly language
Comparisons and conditional jumps are used to …
Implement conditional structures such as IF statements
Implement conditional loops
9
Jumps Based on Specific Flags
Conditional Jump Instruction has the following syntax:
Jcond destination ; cond is the jump condition
Destination
Destination Label
Prior to 386
Jump must be within
–128 to +127 bytes
from current location
IA-32
32-bit offset permits
jump anywhere in
memory
Conditional Processing Computer Organization & Assembly Language Programming slide 19/55
Conditional Processing Computer Organization & Assembly Language Programming slide 20/55
10
Examples of Jump on Zero
Task: Check whether integer value in EAX is even
Solution: TEST whether the least significant bit is 0
If zero, then EAX is even, otherwise it is odd
Conditional Processing Computer Organization & Assembly Language Programming slide 21/55
11
Jumps Based on Signed Comparisons
12
Computing the Max and Min
Compute the Max of unsigned EAX and EBX
mov Max, eax ; assume Max = eax
cmp Max, ebx
Solution: jae done
mov Max, ebx ; Max = ebx
done:
13
BT Instruction
BT = Bit Test Instruction
Syntax:
BT r/m16, r16
BT r/m32, r32
BT r/m16, imm8
BT r/m32, imm8
bt AX, 9 ; CF = bit 9
jc L1 ; jump if Carry to L1
Conditional Processing Computer Organization & Assembly Language Programming slide 27/55
Next . . .
Conditional Processing Computer Organization & Assembly Language Programming slide 28/55
14
LOOPZ and LOOPE
Syntax:
LOOPE destination
LOOPZ destination
Logic:
ECX = ECX – 1
if ECX > 0 and ZF=1, jump to destination
Conditional Processing Computer Organization & Assembly Language Programming slide 29/55
Conditional Processing Computer Organization & Assembly Language Programming slide 30/55
15
LOOPZ Example
The following code finds the first negative value in an array
.data
array SWORD 17,10,30,40,4,-5,8
.code
mov esi, OFFSET array – 2 ; start before first
mov ecx, LENGTHOF array ; loop counter
L1:
add esi, 2 ; point to next element
test WORD PTR [esi], 8000h ; test sign bit
loopz L1 ; ZF = 1 if value >= 0
jnz found ; found negative value
notfound:
. . . ; ESI points to last array element
found:
. . . ; ESI points to first negative value
Conditional Processing Computer Organization & Assembly Language Programming slide 31/55
Your Turn . . .
Locate the first zero value in an array
If none is found, let ESI be initialized to 0
.data
array SWORD -3,7,20,-50,10,0,40,4
.code
mov esi, OFFSET array – 2 ; start before first
mov ecx, LENGTHOF array ; loop counter
L1:
add esi, 2 ; point to next element
cmp WORD PTR [esi], 0 ; check for zero
loopne L1 ; continue if not zero
JE Found
XOR ESI, ESI
Found:
Conditional Processing Computer Organization & Assembly Language Programming slide 32/55
16
Next . . .
Conditional Processing Computer Organization & Assembly Language Programming slide 33/55
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
Task: Translate IF statements into assembly language
Example:
mov eax,var1
cmp eax,var2
if( var1 == var2 ) jne elsepart
X = 1; mov X,1
else jmp next
X = 2; elsepart:
mov X,2
next:
Conditional Processing Computer Organization & Assembly Language Programming slide 34/55
17
Your Turn . . .
Translate the IF statement to assembly language
All values are unsigned
cmp ebx,ecx
if( ebx <= ecx )
ja next
{
mov eax,5
eax = 5;
mov edx,6
edx = 6;
next:
}
Conditional Processing Computer Organization & Assembly Language Programming slide 35/55
Your Turn . . .
Implement the following IF in assembly language
All variables are 32-bit signed integers
mov eax,var1
if (var1 <= var2) { cmp eax,var2
var3 = 10; jle ifpart
} mov var3,6
else { mov var4,7
var3 = 6; jmp next
var4 = 7; ifpart:
}
mov var3,10
next:
18
Compound Expression with AND
HLLs use short-circuit evaluation for logical AND
If first expression is false, second expression is skipped
if ((al > bl) && (bl > cl)) {X = 1;}
19
Your Turn . . .
Implement the following IF in assembly language
All values are unsigned
Conditional Processing Computer Organization & Assembly Language Programming slide 39/55
IsDigit PROC
cmp al,'0' ; AL < '0' ?
jb L1 ; yes? ZF=0, return
cmp al,'9' ; AL > '9' ?
ja L1 ; yes? ZF=0, return
test al, 0 ; ZF = 1
L1: ret
IsDigit ENDP
Conditional Processing Computer Organization & Assembly Language Programming slide 40/55
20
Compound Expression with OR
HLLs use short-circuit evaluation for logical OR
If first expression is true, second expression is skipped
if ((al > bl) || (bl > cl)) {X = 1;}
Conditional Processing Computer Organization & Assembly Language Programming slide 41/55
WHILE Loops
A WHILE loop can be viewed as
IF statement followed by
The body of the loop, followed by
Unconditional jump to the top of the loop
21
Your Turn . . .
Implement the following loop, assuming unsigned integers
Conditional Processing Computer Organization & Assembly Language Programming slide 43/55
22
Next . . .
Conditional Processing Computer Organization & Assembly Language Programming slide 45/55
Indirect Jump
Direct Jump: Jump to a Labeled Destination
Destination address is a constant
Address is encoded in the jump instruction
Address is an offset relative to EIP (Instruction Pointer)
Indirect jump
Destination address is a variable or register
Address is stored in memory/register
Address is absolute
23
Switch Statement
Consider the following switch statement:
Switch (ch) {
case '0': exit();
case '1': count++; break;
case '2': count--; break;
case '3': count += 5; break;
case '4': count -= 5; break;
default : count = 0;
}
24
Jump Table and Indirect Jump
Jump Table is an array of double words
Contains the case labels of the switch statement
Can be defined inside the same procedure of switch statement
jumptable DWORD case0,
case1, Assembler converts
case2,
case3, labels to addresses
case4
Next . . .
Conditional Processing Computer Organization & Assembly Language Programming slide 50/55
25
Bubble Sort
Consider sorting an array of 5 elements: 5 1 3 2 4
First Pass (4 comparisons) 5 1 3 2 4
Compare 5 with 1 and swap: 1 5 3 2 4 (swap)
Compare 5 with 3 and swap: 1 3 5 2 4 (swap)
Compare 5 with 2 and swap: 1 3 2 5 4 (swap)
Compare 5 with 4 and swap: 1 3 2 4 5 (swap)
Second Pass (3 comparisons) largest
Compare 1 with 3 (No swap): 1 3 2 4 5 (no swap)
Compare 3 with 2 and swap: 1 2 3 4 5 (swap)
Compare 3 with 4 (No swap): 1 2 3 4 5 (no swap)
Third Pass (2 comparisons)
Compare 1 with 2 (No swap): 1 2 3 4 5 (no swap)
Compare 2 with 3 (No swap): 1 2 3 4 5 (no swap)
No swapping during 3rd pass array is now sorted
Conditional Processing Computer Organization & Assembly Language Programming slide 51/55
26
Bubble Sort Procedure – Slide 1 of 2
;---------------------------------------------------
; bubbleSort: Sorts a DWORD array in ascending order
; Uses the bubble sort algorithm
; Receives: ESI = Array Address
; ECX = Array Length
; Returns: Array is sorted in place
;---------------------------------------------------
bubbleSort PROC USES eax ecx edx
outerloop:
dec ECX ; ECX = comparisons
jz sortdone ; if ECX == 0 then we are done
mov EDX, 1 ; EDX = sorted = 1 (true)
push ECX ; save ECX = comparisons
push ESI ; save ESI = array address
Conditional Processing Computer Organization & Assembly Language Programming slide 53/55
27
Summary
Bitwise instructions (AND, OR, XOR, NOT, TEST)
Manipulate individual bits in operands
CMP: compares operands using implied subtraction
Sets condition flags for later conditional jumps and loops
Conditional Jumps & Loops
Flag values: JZ, JNZ, JC, JNC, JO, JNO, JS, JNS, JP, JNP
Equality: JE(JZ), JNE (JNZ), JCXZ, JECXZ
Signed: JG (JNLE), JGE (JNL), JL (JNGE), JLE (JNG)
Unsigned: JA (JNBE), JAE (JNB), JB (JNAE), JBE (JNA)
LOOPZ (LOOPE), LOOPNZ (LOOPNE)
Indirect Jump and Jump Table
Conditional Processing Computer Organization & Assembly Language Programming slide 55/55
28