EL213 Computer Organization & Assembly Language Lab 08: Conditional Processing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

EL213 Lab 08

Computer Conditional
Organization & Processing
Assembly Language

Instructor: Ms. Atiya


atiya.jokhio@nu.edu.pk

NATIONAL UNIVERSITY OF COMPUTER AND EMERGING SCIENCES


LAB 08
Learning Objectives
- Boolean and Comparison Instructions
- Conditional Jumps
- Conditional Loop Instructions
- Conditional Structures

A programming language that permits decision making lets you alter the flow of control,
using a technique known as conditional branching.

- AND Instruction
It is boolean AND operation between a source operand and destination operand. If both
bits equal 1, the result bit is 1; otherwise, it is 0.

Syntax: AND reg, reg


AND reg, mem
AND reg, imm
AND mem, reg
AND mem, imm

The AND instruction always clears the Carry and Overflow flags. It modifies the Sign,
Zero,
and Parity flags in a way that is consistent with the value assigned to the destination
operand.

- OR Instruction
It is boolean AND operation between a source operand and destination operand. For each
matching bit in the two operands, the output bit is 1 when at least one of the input bits is 1.

Syntax: OR reg, reg


OR reg, mem
OR reg, imm
OR mem, reg
OR mem, imm

mov al,11100011b
or al,00000100b ; result in AL = 11100111
Page | 1
The OR instruction always clears the Carry and Overflow flags. It modifies the Sign, Zero,
and Parity flags in a way that is consistent with the value assigned to the destination
operand.

- XOR Instruction
The XOR instruction performs a boolean exclusive-OR operation between each pair of
matching bits in two operands and stores the result in the destination operand. If both bits
are the same (both 0 or both 1), the result is 0; otherwise, the result is 1.

Syntax: XOR reg, reg


XOR reg, mem
XOR reg, imm
XOR mem, reg
XOR mem, imm

The XOR instruction always clears the Overflow and Carry flags. XOR modifies the Sign,
Zero, and Parity flags.

- NOT Instruction
The NOT instruction toggles (inverts) all bits in an operand. The result is called the one’s
complement.

Syntax: NOT reg


NOT mem

- TEST Instruction
The TEST instruction performs an implied AND operation between each pair of matching
bits in two operands and sets the Sign, Zero, and Parity flags based on the value assigned
to the destination operand.
The only difference between TEST and AND is that TEST does not modify the destination
operand.
Syntax: TEST reg, reg
TEST reg, mem
TEST reg, imm
TEST mem, reg
TEST mem, imm

Page | 2
Testing Multiple Bits: The TEST instruction can check several bits at once. Sup-pose we
want to know whether bit 0 or bit 3 is set in the AL register. We can use the following
instruction to find this out:

EXAMPLE

.code
mov al, 10101110b ; Clear only bit 3
and al, 11110110b ; AL = 10100110

mov al, 11100011b ; set bit 2


or al, 00000100b ; AL = 11100111

mov al, 10110101b ; 5 bits means odd parity


xor al, 0 ; PF = 0 (PO)

mov al, 10100101b ; 4 bits means even parity


xor al, 0 ; PF = 1 (PE)

mov al, 11110000b


not al ; AL = 00001111b

mov al, 00100101b


test al, 00001001b ; ZF = 0

mov al, 00100101b


test al, 00001000b ; ZF = 1

call DumpRegs
exit

- Set Operations (using Boolean instructions)

Set Complement
The complement of a set can be generated using the NOT instruction, which reverses all
bits.

Set Intersection
The AND instruction produces a bit vector that represents the intersection of two sets.

Page | 3
Set Union
The OR instruction produces a bit map that represents the union of two sets.

EXAMPLE
.data
A DWORD 10000000000000000000000000000111b
B DWORD 10000001010100000000011101100011b
msg1 BYTE "A intersection B is: ", 0
msg2 BYTE "A union B is: ", 0
msg3 BYTE "Complement of A is: ", 0
.code
mov eax, A
and eax, B ; A intersection B
mov edx, OFFSET msg1
call WriteString
mov ebx, TYPE DWORD
call WriteBinB
call Crlf

mov eax, A
or eax, B ; A union B
mov edx, OFFSET msg2
call WriteString
mov ebx, TYPE DWORD
call WriteBinB
call Crlf

mov eax, A
not eax ; A complement
mov edx, OFFSET msg3
call WriteString
mov ebx, TYPE DWORD
call WriteBinB
exit

- CMP Instruction
CMP (compare) instruction performs an implied subtraction of a source operand from
a destination operand for comparison. Neither operand is modified.

Syntax:
CMP destination, source

Page | 4
Flags: The CMP instruction changes the Overflow, Sign, Zero, Carry, Auxiliary Carry,
and Parity flags according to the value the destination operand.

For unsigned operands:

• Destination < source ZF = 0 CF = 1


• Destination > source ZF = 0 CF = 0
• Destination = source ZF = 1 CF = 0

For signed operands:

• Destination < source SF ! = OF


• Destination > source SF = OF
• Destination = source ZF = 1

Examples: Let’s look at three code fragments showing how flags are affected by the CMP
instruction.

.code
mov ax, 5
cmp ax, 10 ; ZF = 0 and CF = 1

mov ax, 1000


cmp ax, 1000 ; ZF = 1 and CF =0

mov si, 106


cmp si, 0 ; ZF = 0 and CF = 0

- Conditional Jumps
Two steps are involved in executing a conditional statement:
1. an operation such as CMP, AND, or SUB modifies the CPU status flags.
2. a conditional jump instruction tests the flags and causes a branch to a new address.

- Jcond Instruction
A conditional jump instruction branches to a destination label when a status flag condition
is true.
Syntax:

Page | 5
The conditional jump instructions can be divided into four groups:

• Jumps based on Flag values

• Jumps based on Equality

• Jumps based on unsigned comparisons

Page | 6
• Jumps based on signed comparisons

EXAMPLE
; This program compares and finds larger of the two integers

.data
var1 DWORD 250
var2 DWORD 125
larger DWORD ?
.code
mov eax, var1
mov larger, eax
mov ebx, var2
cmp eax, ebx
jae L1
mov larger, ebx
L1:
exit

EXAMPLE
; This program compares and finds smallest of the three integers

.data
var1 DWORD 50
var2 DWORD 25
var3 DWORD 103
msg BYTE "The smallest integer is: ", 0
.code
mov eax, var1

Page | 7
cmp eax, var2
jbe L1
mov eax, var2
L1:
cmp eax, var3
jbe L2
mov eax, var3
L2:
mov edx, OFFSET msg
call WriteString
call crlf
call WriteDec
call crlf
exit

EXAMPLE
; The following program continues a loop until an alphanumeric key is pressed

.data
char BYTE ?
.code
L1:
mov eax, 10 ; create 10ms delay
call Delay
call ReadKey ; reads a key input
jz L1 ; repeat if no key is pressed
mov char, al ; saves the character

- Conditional Loop Instructions

- LOOPZ and LOOPE Instructions


The LOOPZ (loop if zero) instruction works just like the LOOP instruction except that it
has one additional condition: The Zero flag must be set in order for control to transfer to
the destination label.

The syntax is

LOOPZ destination

Page | 8
The LOOPE (loop if equal) instruction is equivalent to LOOPZ and they share the same
opcode.

- LOOPNZ and LOOPNE Instructions


The LOOPNZ (loop if not zero) instruction is the counterpart of LOOPZ. The loop
continues while the unsigned value of ECX is greater than zero (after being decremented)
and the Zero flag is clear.

The syntax is

LOOPNZ destination

The LOOPNE (loop if not equal) instruction is equivalent to LOOPNZ and they share the
same opcode.

Example:

; The following take input from user until user press 0


.code
L1:
CALL readInt
cmp eax, 0
LOOPNZ L1
call DumpRegs

- Conditional Structures
We define a conditional structure to be one or more conditional expressions that trigger
a choice between different logical branches. Each branch causes a different sequence
of instructions to execute.

- Block-Structured IF Statements
An IF structure implies that a boolean expression is followed by two lists of statements;
one per-formed when the expression is true, and another performed when the expression
is false.

Page | 9
If structure: In High level Vs Assembly Language

If-else structure: In High level Vs Assembly Language

Page | 10
- Compound Expression with AND
When implementing the logical AND operator in compound expression, if the first
expression is false, the second expression is skipped.

- Compound Expression with OR


When implementing the logical OR operator in compound expression, if the first
expression is true, the second expression is skipped.

- While Loops
A WHILE loop is really an IF statement followed by the body of the loop,
followed byan unconditional jump to the top of the loop.

Page | 11
Page | 12
ACTIVITIES:

1. Translate the following pseudo-code to Assembly Language:

(a)
var = 5
if ( var < ecx ) AND (ecx >= edx) then
x=0
else
x=1

(b)
var = 0
while ( var <= 10)
if (var % 2 == 0)
Print “Hello”
else
Print “World”
var = var + 1
end while
3. Use cmp and jumps to find the first non-zero value in the given array:
intArr SWORD 0, 0, 0, 0, 1, 20, 35, -12, 66, 4, 0

4. Write a program that takes four input integers from the user. Then compare and display
a message whether these integers are equal or not.

6. Write a program for sequential search. Take an input from the user and find if it occurs
in the following array:
arr WORD 10, 4, 7, 14, 299, 156, 3, 19, 29, 300, 20

7. Translate the following pseudo-code to Assembly Language:

Swap_Count = 0
for all elements of list
if list[i] > list[i+1]
swap(list[i], list[i+1])
Swap_Count = Swap_Count + 1
end if
end for
Print Swap_Count

Page | 13

You might also like