Professional Documents
Culture Documents
Conditional Structure: Computer Organization and Assembly Language Lec #18
Conditional Structure: Computer Organization and Assembly Language Lec #18
Conditional Structure: Computer Organization and Assembly Language Lec #18
Islamabad
CONDITIONAL STRUCTURE
• JMP
• LOOP
CONDITIONAL STRUCTURES
There are no high-level logic structures such as if-then-
else, in the IA-32 instruction set. But, you can use
combinations of comparisons and jumps to implement
any logic structure.
First, an operation such as CMP, AND or SUB is
executed to modified the CPU flags. Second, a
conditional jump instruction tests the flags and change
the execution flow accordingly.
CMP AL, 0
JZ L1
:
L1:
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 3
JCOND INSTRUCTION
Examples:
JB, JC jump to a label if the Carry flag is set
JE, JZ jump to a label if the Zero flag is set
JS jumps to a label if the Sign flag is set
JNE, JNZ jump to a label if the Zero flag is clear
JECXZ jumps to a label if ECX equals 0
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 4
CONDITIONAL JUMPS
Jumps Based On . . .
Specific flags
Equality
Unsigned comparisons
Signed Comparisons
Applications
Encrypting a String
Bit Test (BT) Instruction
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 5
JCOND RANGES
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 6
JUMPS BASED ON SPECIFIC FLAGS
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 7
JUMPS BASED ON EQUALITY
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 8
JUMPS BASED ON UNSIGNED
COMPARISONS
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 9
JUMPS BASED ON SIGNED
COMPARISONS
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 10
EXAMPLE: SCANNING A ARRAY
• Find the first even number in an array of unsigned integers
.date
intArray DWORD 7,9,3,4,6,1
.code
...
mov ebx, OFFSET intArray
mov ecx, LENGTHOF intArray
L1: test DWORD PTR [ebx], 1
jz found
add ebx, 4
loop L1
...
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 11
TEST
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 12
AND INSTRUCTION
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 13
OR INSTRUCTION
OR destination, source
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 14
XOR INSTRUCTION
00111011
XOR 0 0 0 0 1 1 1 1
NOT 00111011
11000100 inverted
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 16
APPLICATIONS (1 OF 4)
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 17
APPLICATIONS (2 OF 4)
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 18
APPLICATIONS (3 OF 4 )
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 19
APPLICATIONS (4 OF 4)
or al,al
jnz IsNotZero ; jump if not zero
• Block-Structured IF Statements
• Compound Expressions with AND
• Compound Expressions with OR
• WHILE Loops
• Table-Driven Selection
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 21
BLOCK-STRUCTURED IF STATEMENTS
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 22
YOUR TURN . . .
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 25
COMPOUND EXPRESSION WITH AND (2 OF
3)
if (al > bl) AND (bl > cl)
X = 1;
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 26
COMPOUND EXPRESSION WITH AND (3 OF
3)
if (al > bl) AND (bl > cl)
X = 1;
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 27
YOUR TURN . . .
Implement the following pseudocode in assembly language. All
values are unsigned:
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 28
COMPOUND EXPRESSION WITH OR (1
OF 2)
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 29
COMPOUND EXPRESSION WITH OR (1
OF 2)
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 30
WHILE LOOPS
A WHILE loop is really an IF statement followed by the body of the loop,
followed by an unconditional jump to the top of the loop. Consider the
following example:
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 31
YOUR TURN . . .
Implement the following loop, using unsigned 32-bit integers:
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 32
EXAMPLE: IF STATEMENT NESTED IN A
LOOP
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 33
JMP AND LOOP INSTRUCTIONS
JMP Instruction
LOOP Instruction
LOOP Example
Copying a String
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 34
JMP INSTRUCTION
• JMP is an unconditional jump to a label that is usually within the same
procedure.
• Syntax: JMP target
• Logic: EIP target
• Example:
top:
.
.
jmp top
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 35
LOOP INSTRUCTION
• The LOOP instruction creates a counting loop
• Syntax: LOOP target
• Logic:
• ECX ECX – 1
• if ECX != 0, jump to target
• Implementation:
• The assembler calculates the distance, in bytes, between
the offset of the following instruction and the offset of the
target label. It is called the relative offset.
• The relative offset is added to EIP.
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 36
LOOP EXAMPLE
The following loop calculates the sum of the integers 5 + 4 + 3 +2 + 1:
(a) 128
(b) +127
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 38
YOUR TURN . . .
mov ax,6
mov ecx,4
What will be the final value of AX? L1:
inc ax
10 loop L1
mov ecx,0
How many times will the loop execute?
X2:
inc ax
4,294,967,296 loop X2
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 39
NESTED LOOP
If you need to code a loop within a loop, you must save the outer loop
counter's ECX value.
.data
count DWORD ?
.code
mov ecx,100 ; set outer loop count
L1:
mov count,ecx ; save outer loop count
mov ecx,20 ; set inner loop count
L2: .
.
loop L2 ; repeat the inner loop
mov ecx,count ; restore outer loop count
loop L1 ; repeat the outer loop
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 40
SUMMING AN INTEGER ARRAY
.data
intarray WORD 100h,200h,300h,400h
.code
mov edi,OFFSET intarray ; address of intarray
mov ecx,LENGTHOF intarray ; loop counter
mov ax,0 ; zero the accumulator
L1:
add ax,[edi] ; add an integer
add edi,TYPE intarray ; point to next integer
loop L1 ; repeat until ECX = 0
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 41
YOUR TURN . . .
Arithmatic and Data Transfer Computer Organization and Assembly Language – NUML
Asim Rehan 42