0% found this document useful (0 votes)
18 views42 pages

6 - Introd. To Assem. Part 2

Uploaded by

hafsatamer03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views42 pages

6 - Introd. To Assem. Part 2

Uploaded by

hafsatamer03
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Next

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

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE


1
YANBU
Boolean Instructions (AND)
𝐀𝐍𝐃 destination, source ;Bitwise AND between each pair of matching bits
Operands can be 8 or 16 bits and they must be of the same size
AND instruction is often used to clear selected bits
3Bh 00111011

0 0 0 0Fh 00001111
0 1 0
0 0Bh 000 01 01 1
0 0 1 Unchanged
𝒚 Cleared
1 1 1
XXXXXXXX
 Forcing certain bits to be “0” without changing 𝑨𝑵𝑫
the other bits 11101101
XXX0XX0X

𝐓𝐄𝐒𝐓 destination,source ; Bitwise AND between each pair of matching bits


without updating the destination

It is like CMP instruction which is usually followed by a conditional jump

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 2


Boolean Instructions (OR)
𝐎𝐑 destination ,source ;Bitwise OR between each pair of matching bits

Operands can be 8 or 16 bits and they must be of the same size


OR instruction is often used to set selected bits

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

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 3


To Uppercase Example
The AND function can be used to convert characters to uppercase
Note the difference between uppercase and lowercase ASCII codes

‘a’ = 61h = 0 1 1 0 0 0 0 1 ‘b’ = 62h = 0 1 1 0 0 0 1 0


‘A’ = 41h = 0 1 0 0 0 0 0 1 ‘B’ = 42h = 0 1 0 0 0 0 1 0
There is
Forcing bit5 to 0 (cleared), produce upper case character an error in this
So we can use the AND function to reset bit5 toexample;
0 where the space
character
The value used to reset bit5 to 0 is called the mask with ASCII code
¿ 11011111
20H will also be converted
Example to 00H!
Data Segment
mystring DB “Convert Me” ; convert this to “CONVERT ME”
ENDS
MOV CX,10 ;LENGTH OF mystring
MOV SI,OFFSET mystring
L1: AND BYTE PTR [SI],11011111b ;clear bit 5
INC SI
LOOP L1
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 4
To Lowercase Example
The OR function can be used to convert characters to lowercase

‘A’ = 41h = 0 1 0 0 0 0 0 1 ‘B’ = 42h = 0 1 0 0 0 0 1 0


‘a’ = 61h = 0 1 1 0 0 0 0 1 ‘b’ = 62h = 0 1 1 0 0 0 1 0
Forcing bit5 to 1 (Set), produce lower case character
So we can use the OR function to set bit5 to 1
The mask used to set bit5 to 1 is ¿ 00100000
Example
Data Segment
mystring DB “Convert Me” ; convert this to “convert me”
ENDS
MOV CX,10 ;LENGTH OF mystring
MOV SI,OFFSET mystring
L1: OR BYTE PTR [SI],11011111b ;set bit 5
INC SI
LOOP L1
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 5
Boolean Instructions (XOR)
𝐗𝐎𝐑 destination ,source ;Bitwise XOR between each pair of matching bits

Operands can be 8 or 16 bits and they must be of the same size


XOR instruction is often used to invert selected bits

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

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 6


Boolean Instructions (NOT)
𝐍𝐎𝐓 destination ; Inverts all the bits in a destination operand

Result is called the 1's complement 3Bh 00111011


𝑵𝑶𝑻
Destination can be a register or memory
C4h 110 00 10 0

Affected Status Flags

1) Carry Flag: cleared by AND, OR, and XOR


2) Overflow Flag: cleared by AND, OR, and XOR
3) Sign Flag: Copy of the sign bit in result
4) Zero Flag: Set when result is zero
5) Parity Flag: Set when parity in least-significant byte (LSB) is even

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 7


Next

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

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE


8
YANBU
Branching Instructions
 Branching
 Transfers the execution of the program to a new position of the code
 Update the Instruction Pointer (IP) to a new value
 There are 3 types of branching
 Unconditional branching
 Conditional branching
 Subroutine call & Interrupt
 Unconditional branching “JMP xyz”
 Causes an unconditional jump to the line labeled “xyz”.
 The jump is always performed
 Conditional branching JZ/JE, JNZ/JNE, JC, JO, JNO, …
 Branching is performed or not according to the value of certain flags
 Usually these flags are updated by the previous instructions

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 9


Branching Instructions
 SOUBROUTINE “Call”
SUB2 935 ---------
main
939 ---------
100 ---------- 941 ---------
104 ---------- SUB1 510 --------- 944 ---------
107 ---------- 515 --------- 950 RET
108 CALL SUB1 517 CALL SUB2 951 ---------
115 ---------- 522 ---------
117 ---------- 526 RET
IP 100
125
120
117
527
526
951
950
944
941
939
935
522
517
515
510
115
108
107
104
120 CALL SUB1 527 ---------
125 ----------
STACK
522
 To support the mechanism of subroutine calls,
processors reserve part of the memory to store the 115
return address
 This memory area must have the property of “Last
In First Out” (LIFO) to support subroutine nesting
 This LIFO memory area is called the STACK
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 10
Branching Instructions
 SOUBROUTINE “Call”
 CALL xyz: (procedure call) that begins at line xyz
 The address of the instruction following the CALL is the return
address
 The return address is saved (in the stack) to ensure correct
return after subroutine execution
 RET: Return from subroutine, execution is resumed at the
position saved in the stack
 INTERRUPTION “INT ” calls software interrupt
 It is almost like subroutine calls that transfer control to a
special subroutine “Interrupt Service Routine”
 Software interrupts is the entry point to the Operating System

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 11


Why we need Branching Instructions

 Need for branch instructions


 There are no high-level control structures in assembly
language, like (if…else, do…while, …) so;
 Comparisons and conditional jumps are used to:
 Implement conditional structures such as IF statements
 Implement conditional loops (For loops)

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 12


Next

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

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE


13
YANBU
Unconditional Jump Instructions
𝐉𝐌𝐏 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

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 14


Unconditional Jump Instructions
 JMP updates the value of the IP register: IP  IP + displacement
 It adds to the IP register, the number of bytes (distance) between the
jump instruction and its destination.
 For a backward jump, the distance is negative (2‘s complement)
𝑑𝑖𝑠𝑝𝑙𝑎𝑐𝑒𝑚𝑒𝑛𝑡=𝑎𝑑𝑑𝑟𝑒𝑠𝑠𝑜𝑓 𝑑𝑒𝑠𝑡𝑖𝑛𝑎𝑡𝑖𝑜𝑛𝑖𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑖𝑜𝑛−𝑎𝑑𝑑𝑟𝑒𝑠𝑠𝑜𝑓 𝑛𝑒𝑥𝑡𝑖𝑛𝑠𝑡𝑟𝑢𝑐𝑡𝑖𝑜𝑛
Example  The following program writes continuously the value 0 at 140H.

0100 B8 00 00 MOV AX, 0 ; clear AX


0103 A3 01 40 MOV [140],AX ; AX wrote to address 140
0106 EB FC JMP 0103 ; branching to 103
0107 xxx ; never executed instruction

𝑇 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

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE


16
YANBU
Conditional Jump Instructions
𝐉 (𝒄𝒐𝒏𝒅𝒊𝒕𝒊𝒐𝒏)destination
 A conditional jump is executed only if some condition is satisfied,
otherwise execution continues sequentially to the next instruction
 The condition of the jump is based on the state of one (or more) flags

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

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 18


Conditional Jump Instructions
 Jumps based on Comparison
 A compare (CMP OP1,OP2) is used before jump instruction
Signed Unsigned

Description Mnemonic Description Mnemonic


JG JA
JL JB
JGE/JNL JAE/JNB
JLE/JNG JBE/JNA

 Jumps based on Equality


Description Mnemonic
JE JE = JZ
JNE JNE = JNZ
JCXZ
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 19
Conditional Jump Instructions
Example 1 (AL = 0C3h) Example 2 (AL = 0C3h)
Jump to L1 if unsigned AL is greater than 15 Jump to L1 if signed AL is greater than 15
𝐶 3h𝑢𝑛𝑠𝑖𝑔𝑛𝑒𝑑 𝑑𝑒𝑐𝑖𝑚𝑎𝑙 195 𝐶 3h𝑆𝑖𝑔𝑛𝑒𝑑 𝑑𝑒𝑐𝑖𝑚𝑎𝑙 −61
→ →

CMP AL,15 CMP AL,15


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
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: ………

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 21


LOOP Instruction
𝑳𝑶𝑶𝑷 destination
 The LOOP instruction creates a counting loop
 It works automatically with the CX register as the counter
 It is equivalent to:

DEC CX
JNZ destination ; 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

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 22


LOOP Instruction

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?

Solution: same value 1

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 23


LOOPZ/LOOPNZ Instruction
 LOOPZ/LOOPNZ instruction works just like the LOOP instruction except
that it has one additional condition:
 In LOOPZ; the Zero flag must be “1” in order for control to transfer to
the destination label.
 In LOOPNZ; the Zero flag must be “0” in order for control to transfer to
the destination label.
 LOOPZ = LOOPE
 LOOPNZ = LOOPNE

𝑳𝑶𝑶𝑷𝑵𝑬 destination
 The loop continues while the unsigned value of &
 So it will end the loop if
 or

 Usually a CMP/TEST instruction is used just before the LOOPNE to affect


the ZF and hence the loop may not be continued (break condition)

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 24


LOOPNE Example
 Locate the first zero value in an array
 If found let SI points to it; else let
Data SEGMENT
array DW -3,7,20,-50,10,0,40,4
ENDs
Code SEGMENT
MOV AX,Data
MOV DS,AX
MOV CX, 8 ; loop counter
MOV SI, OFFSET array – 2 ; start before first
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:
MOV SI,-1 ; SI points to last array value
found:
. . . ; SI points to first zero value
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 25
Nested LOOPs
𝑵𝒆𝒔𝒕𝒆𝒅 𝑳𝒐𝒐𝒑𝒔
 If you need to code a loop within a loop, you must save the outer
loop counter (CX value)
𝑬𝒙𝒂𝒎𝒑𝒍𝒆
data segment
count DW ? Can be
Ends replaced by
PUSH & POP
code segment
MOV CX, 70 ; set outer loop count to 70
L1:
MOV count, CX ; save outer loop count
MOV CX, 20 ; set inner loop count to 20
L2:
×70×20 *********** ; any instruction
LOOP L2 ; repeat the inner loop
MOV CX, count ; restore outer loop count
LOOP L1 ; repeat the outer loop
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 26
Nested LOOPs
𝑵𝒆𝒔𝒕𝒆𝒅 𝑳𝒐𝒐𝒑𝒔
 If you need to code a loop within a loop, you must save the outer
loop counter (CX value)
𝑬𝒙𝒂𝒎𝒑𝒍𝒆
data segment
count DW ?
Ends
code segment
MOV CX, 70 ; set outer loop count to 70
L1:
PUSH CX ; save outer loop count
MOV CX, 20 ; set inner loop count to 20
L2:
×70×20 *********** ; any instruction
LOOP L2 ; repeat the inner loop
POP CX ; restore outer loop count
LOOP L1 ; repeat the outer loop
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 27
Example 1 Summing an Integer Array

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

; SI points to array elements


ADD SI,2 ; point to next integer
LOOP L1 ; repeat until CX = 0
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 28
Example 2: Copying a String
data segment
source DB "This is the source string", 0 string size
We can get
target DB 26 DUP(0) by making the
assembler calculates
Ends (target-source)

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

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE


32
YANBU
Translating Conditional Structures
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

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:

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 35


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

X=1

 Applying the jump instruction with the “NO” branch in flowchart,


We allow the program to test the second expression
 Number of instructions is reduced from 7 to 5
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 36
Translating Conditional Structures
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:

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 37


Translating Conditional Structures
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?
NO
BL > CL JNA next ;no: skip if part
L1:
YES
MOV X,1 ; set X to 1
next:
X=1

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 38


Translating Conditional Structures
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
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:

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 40


Translating Conditional Structures
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

Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 41


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
 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, Search
Translating Conditional Structures
if…else, while, isDigit,…
Chapter 2 : Introduction to Assembly Programming, Computer Architecture CCSE YANBU 42

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy