COAL Chapter No 6
COAL Chapter No 6
COAL Chapter No 6
Computer Organization
& Assembly Language
INSTRUCTOR
CONDITIONAL PROCESSING
BIT-WISE
OPERATIONS
Status Flags (Revision)
• The Zero flag is set when the result of an operation equals zero.
• The Carry flag is set when an instruction generates a result that is too large
(or too small) for the destination operand.
• The Sign flag is set if the destination operand is negative, and it is clear if
the destination operand is positive.
• The Overflow flag is set when an instruction generates an invalid signed
result.
• The Parity flag is set when an instruction generates an even number of 1
bits in the low byte of the destination operand.
• The Auxiliary Carry flag is set when an operation produces a carry out from
bit 3 to bit 4.
NOT INSTRUCTION
Performs a bitwise Boolean NOT operation on a single destination operand
Syntax: (no flag affected)
NOT destination
Example:
mov al 11110000b
NOT al
AND INSTRUCTION
Performs a bitwise Boolean AND operation between each pair of matching bits in two
operands
AND instruction always clears Overflow and Carry flag. Also can modify Sign, Zero, and
Parity in a way that is consistent with the value assigned to the destination operand.
Syntax: AND destination, source
Example:
mov al, 00111011b
and al, 00001111b
OR INSTRUCTION
Performs a bitwise Boolean OR operation between each pair of matching bits in two
operands
Syntax: Clears Overflow, Cary . Modifies Sign, Zero, and Parity in a way that is
consistent with the value assigned to the destination operand
Syntax: OR destination, source
Example:
mov al, 00111011b
or al, 00001111b
XOR INSTRUCTION
Performs a bitwise Boolean XOR operation between each pair of matching bits in two
operands
The XOR instruction always clears the Overflow and Carry flags.
Syntax: XOR destination, source
Example:
mov al, 00111011b
xor al, 00001111b
Unsigned ZF CF
destination < source 0 1
destination > source 0 0
destination = source 1 0
Signed FLAGS
destination < source SF != OF
destination > source SF == OF
destination = source ZF = 1
SETTING AND CLEARING INDIVIDUAL FLAGS
and al , 0 ; set Zero
or al , 1 ; clear Zero
or al , 80h ; set Sign
and al , 7Fh ; clear Sign
stc ; set Carry
clc ; clear Carry
mov al , 7Fh
inc al ; set Overflow
or eax , 0 ; clear Overflow
CONDITIONAL
JUMPS
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 changes the execution flow
accordingly
Example:
cmp eax,0 and dl,10110000b
jz L1 ; jump if ZF = 1 jnz L2 ; jump if ZF = 0
.. ….
L1: L2:
JCOND INSTRUCTIONS
A conditional jump instruction branches to a label when specific register or flag
conditions are met
Jcond Destination
Instruction Description
JE Jump if equal (left OP = right OP)
JNE Jump if not equal (left OP ≠ right OP)
JCXZ Jump if CX = 0
JECXZ Jump if ECX = 0
JUMPS BASED ON UN-SIGNED COMPARISON
Condition
JNE esle
Then
JMP endif
else:
Else
endif:
BLOCK-STRUCTURED IF STATEMENTS
Assembly language programmers can easily translate logical statements written in
C++ into assembly language. For example
Else MOV X , 2
Endif: Endif:
EXERCISE
Implement the following pseudocode in assembly language, all values are unsigned:
Endif:
EXERCISE
Implement the following pseudocode in assembly language, all values are 32-bit
signed integer
Endif: Endif:
COMPOUND EXPRESSION WITH AND
When implementing the logical AND operator, consider that HLLs use short-circuit
evaluation
In the following example, if the first expression is false, the second expression is skipped
Endif:
WHILE LOOP
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
_While:
Jmp _while
_endwhile:
WHILE LOOP
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
_While:
Jmp _while
_endwhile:
EXERCISE
Implement the following loop, using unsigned 32-bit integer
_While:
Jmp _while
_endwhile:
DO LOOP
A DO loop is really an IF statement, here the body of the loop followed by an IF
statement to unconditional jump
Consider the following example
_do:
Inc EAX
Do
{
eax = eax + 1;
}while( eax < ebx)
Jmp _do
_enddo:
LOOPZ AND LOOPE INSTRUCTION
Syntax:
LOOPE destination
LOOPZ destination
Logic:
ECX ← ECX – 1
if ECX != 0 and ZF=1, jump to destination
The destination label must be between -128 and +127 bytes from the location of the
following instruction
Useful when scanning an array for the first element that meets some condition
LOOPNZ AND LOOPNE INSTRUCTION
Syntax:
LOOPNE destination
LOOPNZ destination
Logic:
ECX ← ECX – 1
if ECX != 0 and ZF=0, jump to destination
The destination label must be between -128 and +127 bytes from the location of the
following instruction
Useful when scanning an array for the first element that meets some condition
EXAMPLES
The following code finds the first positive value in an array:
Solution:
.data
array SWORD -3,-6,-1,-10,10,30,40,4
sentinel SWORD 0
.code
mov esi , OFFSET array
mov ecx , LENGTHOF array
next:
test WORD PTR [esi] , 8000h ; test sign bit
pushfd ; push flags on stack
add esi , TYPE array
popfd ; pop flags from stack
loopnz next ; continue loop
jnz quit ; none found
sub esi,TYPE array ; ESI points to value
quit:
EXAMPLES
Locate the first nonzero value in the array. If none is found, let ESI point to the sentinel value
Solution:
.data
array SWORD 50 DUP (?)
sentinel SWORD 0
.code
mov esi , OFFSET array
mov ecx , LENGTHOF array
next:
cmp WORD PTR [esi] , 0 ; check for zero
pushfd ; push flags on stack
add esi , TYPE array
popfd ; pop flags from stack
loopnz next ; continue loop
jnz quit ; none found
sub esi,TYPE array ; ESI points to value
quit:
BT (BIT TEST) INSTRUCTION
Copies 𝑛𝑡ℎ bit from an operand into the Carry flag
Syntax:
BT reg/mem16 , reg16/reg32/imm
BT reg/mem32 , reg16/reg32/imm
Example: jump to label L1 if bit 9 is set in the AX register
bt AX , 9 ; CF = bit 9
jc L1 ; jump if Carry
There are three more BT instructions:
BTC bitBase, n ; bit test and complement
BTR bitBase, n ; bit test and reset (clear)
BTS bitBase, n ; bit test and set