Assembly Language - CHAPTER 7
Assembly Language - CHAPTER 7
Assembly Language - CHAPTER 7
Boolean Operations
Shifting and Rotating Bits
The CMP Instruction
used to compare two numeric data values
compares values by subtracting the value of the second operand from the first one
unlike the SUB instruction, CMP instruction doesn’t affect the value of any of the
operands
Syntax
CMP reg/memory, reg/memory/immediate
Example: Given AX=0005 BX=0003 X=0007
CMP AX, BX ; AX – BX >0 AX > BX
CMP X,AX ; X – AX <0 X < AX , sets the sign flag
CMP BX, 0003 ; BX – 00003=0 BX ==0003, sets the zero flag
NB:
The two operands have to have the same size
The JMP Instruction
used to deviate from the normal flow of a program execution
without any conditions
Syntax
JMP short/near/far address
Short Jump mov ah,01
to a label within -128 to +127 bytes int 21h
Jmp exit
Near Jump
mov ah,02
Within 32k mov dl,al
FAR Jump int 21h
Exit:
A jump over 32k or in another segment
mov ah,4ch
int 21h
Conditional Jumps
In conditional jump,
transfer control to another instruction based on some conditions
Syntax
J<condition> destination_address
Conditional Jumps cont’d
Conditional jump instructions used on signed data
during arithmetic operations
Conditional Jumps cont’d
Conditional jump instructions used on unsigned data
during arithmetic operations
Conditional Jumps cont’d
Flag based conditional jump instructions
Conditional Jumps
mov ah,01
int 21h
mov ah,01 Mov bl,al
int 21h Int 21h
cmp BL, AL
cmp AL,’A’
JE equal
JE it_is_A
JG greater
JNE it_is_not_A
JL less
it_is_A:
equal:
….
….
it_is_not_A:
greater:
….
….
less:
….
The LOOP Instruction
generates a cycle in a program until it reaches a
particular condition
Syntax:
LOOP label
assumes that the loop count is stored in CX
for each iteration, automatically deducts 1 from CX and jumps to the
target instruction until CX equals zero
LOOPE/LOOPZ
Jumps if the counter is nonzero and the zero flag is set
LOONE/LOOPNZ
Jumps if the counter is nonzero and the zero flag is clear
Loop cont’d
INT 21H
MOV AH,02
DEC CX INT 21H
JNZ L1 LOOP L1
Boolean Operation
can be used to clear, set and test bits
Syntax
operation register/memory, register/memory/immediate
AND
OR
XOR
NEG
NOT
TEST
Example:
Given AL= 1010 , BL= 0011
AND AL, BL AL= 0010 , BL= 0011
Note:
AND CX, 0 ;clears CX
The OR Instruction
performs a disjunction between the two operands
Syntax
OR destination, source
Example:
Given AL= 1010 , BL = 0011 , CL = 03
OR AL, BL AL=1011, BL=0011
Note:
OR CL, 30h CL= 33 ; to change BCD data to ASCII
OR AX, AX ;to check if a register is empty
JZ lbl
The XOR Instruction
performs the logic exclusive disjunction between the two operands
Syntax
XOR destination, source
If the matched bits differ , the operation sets the result to 1, else the result is
0
Example
Given, AL = 1010 , BL = 0011 , CL = 03
XOR aL , bL => aL= 1001 , bL = 0011
Note
XOR AX, AX ;Xoring a register with itself clears the register
Given Binary code of ASCII characters
‘A’=0100 0001 and ‘a’=0110 0001
XOR A, 00100000 ;returns lowercase ‘a’
NOT and NEG instructions
Purpose: Purpose:
performs1’s performs2’s
complement complement
Syntax: Syntax:
NOT reg/memory NEG reg/memory
Ex. Ex.
AL= 0101 AL= 0101
NOT AL AL =1010 NEG AL AL =1011
TEST Instruction
Purpose:
checksif any corresponding bits of the two operands
are both 1. If so, sets the zero flag else it clears it
Syntax:
TEST destination, source
Ex.
Given,AX = 00111110B
TEST AX, 11000010B sets zero flag (ZR)
TEST AX, 00000001B clears zero flag(NZ)
Bit shifting operations
Shift Logical right/left
SHR & SHL
Syntax: Syntax:
CALL near/far procedure_name
RET[n] [immediate]
Pushes the value of IP to the stack
Pops the old IP value from
Stores the offset address of the
the stack and store it back
called procedure into IP
Stack
Passing parameters by value
Pass values in registers Pass value in stack
MAIN PROC FAR MAIN PROC FAR