Assembly Language Part 3

Programming 5: Boolean and Conditional Processing

Logical Instructions 

The processor instruction set provides the instructions AND, OR, XOR, TEST and NOT Boolean logic, which tests, sets and clears the bits according to the need of the program. These instructions set the CF, OF, PF, SF and ZF flags. The format for these instructions:

Conditional Instructions 

Sometimes a program needs to do different things depending on the result of an operation. As shown in Figure 1, if the conditions are met then do process A. Otherwise, proceed with process B. This is conditional branching. This is different from unconditional branching (the JMP instruction) previously studied.

 Compare (CMP) instruction 

First, let us look at the compare (CMP) instruction.
This instruction is used in to test branching conditions.
• The CMP instruction compares two operands.
 • This instruction basically subtracts one operand from the other for comparing whether the operands are equal or not.
• It does not disturb the destination or source operands (i.e. these does not change)
• The instruction format:
o The destination operand can be either register or memory.
o The source operand can be register, memory or immediate value.
Conditional Branching or Conditional Jump

This is performed by a set of jump instructions depending upon the condition. The conditional instructions transfer the control by breaking the sequential flow and they do it by changing the offset value in IP (Instruction Pointer).
 • Written in the form J<condition>. Example: JE, JNZ, JL, JG
• There are different groups of conditional jump instructions: o Jumps based on specific flag values
4



J<condition> TARGET Examples:
 JE TARGET
JNZ TARGET
JL TARGET
o Jumps based on equality between operands or the value of (E)CX
o Jumps based on comparisons of unsigned operands
o Jumps based on comparisons of signed operands
• The instruction format
Logical statements and compound expressions
 Assembly language can easily represent logical statements and compound expressions. This can be done through smart manipulations of branching instructions (both conditional and unconditional).

Question(Lets try by your own)

Solution

TITLE LAB2            (main.asm)

INCLUDE Irvine32.inc
.data

X word ?

.code
MAIN PROC

mov ax,dx
cmp ax,cx
jle L1 ;after signed comparison
mov x,2
jmp L2
L1:mov x,1
L2:

exit

MAIN ENDP

END main

TITLE LAB5 Q3 (main.asm)

INCLUDE Irvine32.inc
.data
x WORD ?
val1 WORD ?

.code
main PROC

mov ax,bx
cmp ax,cx
jg L1
cmp ax,val1
jg L1
mov x,2
jmp L2
L1:mov x,1
L2

exit

MAIN ENDP

END main

Question 4

1-JL L2 because 5 is less than 20 as its only operate in JL (jump if eax= 5 < 20)
instruction which as final answer if ebx is 0

2-jump outt is needed so that the program can terminate if it has complete of its instruction based on
 its instruction based on its condition if the true condition instruction above the false condition instruction,if there is no
no jmp outt,the program will operate  all of the instruction even the condition is false.

Post a Comment

0 Comments