Coal Lab-08
Coal Lab-08
Computer Conditional
Organization & Processing
Assembly Language
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.
NOTE: The All other instructions use the same operand combinations and sizes as
the AND instruction.
- 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.
mov al,11100011b
or al,00000100b ; result in AL = 11100111
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.
Page | 1
- 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.
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.
- 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.
Testing Multiple Bits: The TEST instruction can check several bits at once. Suppose 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
call DumpRegs
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
Flags: The CMP instruction changes the Overflow, Sign, Zero, Carry, Auxiliary Carry,
and Parity flags according to the value the destination operand.
When two unsigned operands are compared, the Zero and Carry flags indicate
the following relations between operands:
When two signed operands are compared, the Sign, Zero, and Overflow flags indicate the
following relations between operands:
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
Page | 3
mov ax, 1000
cmp ax, 1000 ;ZF=1 and CF =0
- Conditional Jumps
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 | 4
Jumps based on unsigned comparisons
EXAMPLE
; This program compares and finds larger of the two integers
INCLUDE Irvine32.inc
.data
var1 DWORD 500
var2 DWORD 125
larger DWORD ?
.code
main PROC
mov eax, var1
mov larger, eax
mov ebx, var2
cmp eax, ebx
jae L1
mov larger, ebx
L1:
exit
main ENDP
END main
Page | 5
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
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
The syntax is
LOOPZ destination
The LOOPE (loop if equal) instruction is equivalent to LOOPZ and they share the
same opcode.
Page | 6
- 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 LOOPNE (loop if not equal) instruction is equivalent to LOOPNZ and they share the
same opcode.
Example:
.code
mov ecx,5
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 | 7
If-else structure: In High level Vs Assembly Language
Page | 8
- 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 | 9
ACTIVITY:
Task#1
Write a program that takes four input integers from the user. Then compare and display a
message whether these integers are equal or not.
Task#2
Use cmp and jumps to find the first non-zero value in the given array:
intArr SWORD 0, 0, 0, 150, 120, 35, -12, 66, 4, 0
Task#3 Implement the following given code in Assembly and Consider var = 5 , edx =
var+1 and counter value from array initialized in task#2.
Task#6
Write a program for bubble sort on this array.
arr WORD 10, 4, 7, 14, 299, 156, 3, 19, 29, 300, 20
Task#7
Write a program to print weekday based on given number.
Task#8
Write a program to check whether a character is alphabet or not.
Page | 10