Assembly Language - CHAPTER 7

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

CHAPTER SEVEN

PROGRAM LOGIC AND


CONTROL
Compiled by: Seble N.
Outline
 Compare Operations
 CMP

 Program Logic & Control


 Conditionaljump
 Unconditional jump
 Loop

 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

Using Jnnn instructions Using Loop instruction

MOV CX,04 MOV CX, 04


L1:
L1:
MOV DL,’A’
MOV DL, ‘A’
MOV AH,02

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

 The result is stored on the first operand


 Affects the Flags registers
The AND Instruction

 performs a conjunction between the two operands


 Syntax
 AND destination, source

 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

NOT instruction NEG instruction

 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

 Shift Arithmetic right/left


 SAR & SAL

 Rotate bits right/left


 ROR/ROL
SHR(Shift Logical Right) &
SAR(Shift Arithmetic Right)
 used to shift the position of bits to the right
 SHR is used with unsigned data & clears the most significant bit
 SAR is used with signed data & sets or clears the most significant
bit to correspond to the sign of the original value
 Syntax
 SHR/SAR reg/mem , CL/immediate
 Ex. Given, AL = 1001 BL=0101
 SHR AL, 01 ;1001  0100=AL
 SAR AL, 01 ;1001  1100=AL
 SAR BL, 01 ;0101  0010=BL
 NB: SHR & SAR can be used for halving values
SHR(Shift Logical Left) &
SAR(Shift Arithmetic Left)
 used to shift the position of bits to the left
 SHL & SAL perform the same operations
 Syntax
 SHL/SAL reg/mem , CL/immediate
 Ex. Given, AL = 1001 BL=0101
 SHL AL, 01 ;1001  0010=AL
 SAL AL, 01 ;1001  0010=AL
 SAL BL, 01 ;0101  1010=BL

 NB: SHL & SAL can be used for doubling values


ROR & ROL
 Rotate instructions are similar to shift instructions, except the rotate
instructions are circular, with the bits shifted out one end returning to the
other end
 ROR (Rotate Logical Right)
 rotates contents of a data register or memory location to the right (towards the
least significant bit) by a specified amount
 ROL (Rotate Logical Left)
 rotates contents of a data register or memory location to the left (towards the
most significant bit) by a specified amount
 Syntax:
 ROR/ROL reg/mem , CL/immediate
 Ex. AX = 1001
 ROR AX, 01 ;1001  1100= AX
 ROL AX, 01 ;1001  0011= AX
Working with Procedures
 Defining a procedure
 FAR and NEAR operands
 Calling a procedure
 CALL instruction
 Returning to a procedure
 RET instructions
 Passing parameters by value
 Using registers
 Using stack
 Passing parameters by reference
 Using registers
 Using stack
Defining a procedure
 Basic structure of a procedure
proc_name PROC FAR/NEAR
; your code goes here
proc_name ENDP
 PROC: defines a procedure
 ENDP: indicates end of a procedure
 FAR: indicates that the procedure is the entry point for program
execution
 NEAR: defines procedures other than FAR
 NB:
 Only one FAR procedure can be defined within one code segment
 Any number of functions can be done within a segment
 All procedures must be defined before program end instruction “ END
MAIN“
CALL & RET
Calling a Procedure Returning to a Procedure

 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

 Two types of calls


into IP that is pushed by

 A NEAR Call : pushes IP to the stack the CALL operator


 A FAR Call : pushes CS:IP to the
stack
CALL & RET cont’d

.MODEL SMALL IP INSTRUCTION


.STACK 100 0100 MOV DL, 02
.CODE
MAIN PROC FAR 0103 CALL display
MOV DL, 02 0105 MOV Ax, 4c00h
CALL DISPLAY

MOV AX,4C00H
INT 21H 010A display proc
MAIN ENDP 010C MOV AH,0
DISPLAY PROC

MOV AH,02
INT 21H 0120 RET
RET 0122 display endp
DISPLAY ENDP
END MAIN ….
Passing Parameters

 A program may pass parameters


 By value: the actual data item

 By reference: the address of the data

 These values can be passed through parameters


 Register

 Stack
Passing parameters by value
Pass values in registers Pass value in stack
MAIN PROC FAR MAIN PROC FAR

MOV AX,0002 MOV AX,0002


MOV BX,0003 STACK : SP = 0066
MOV BX,0003
PUSH AX
CALL ADDITION
PUSH BX 0002 0064
MOV DX, BX
CALL ADDITION 0003 0062
MOV AH,02 MOV DX, BX IP 0060
INT 21H MOV AH,02 005E
MOV AX,4C00H INT 21H
INT 21H ….. SP=0060

MAIN ENDP MAIN ENDP


ADDITION PROC
ADDITION PROC
MOV BP,SP ;BP=0060
ADD BX,AX
MOV AX,[BP + 4] ;[BP + 4]=0064
ADD BX,0030H
MOV BX,[BP+ 2] ;[BP + 2]=0062
RET ADD BX, AX
ADDITION ENDP RET 4 POP VALUE AND SET IT TO IP AND
END MAIN ADD 4 TO SP =>SP=0064
Passing Parameters by Reference
Addresses in registers Addresses in stack
.DATA
.DATA
X DB 0002
X DB 0002
Y DB 0003
Y DB 0003
.CODE
.CODE MAIN PROC FAR
MAIN PROC FAR LEA SI, X
LEA SI, X LEA DI,Y
LEA DI,Y PUSH SI
CALL ADDITION PUSH DI
MOV DL, [DI] CALL ADDITION
MOV AH,02 ….
INT 21H MAIN ENDP
MAIN ENDP ADDITION PROC
ADDITION PROC MOV BP,SP
MOV AL,[BP+4]
MOV AL,[SI]
ADD [BP+2],AL
ADD [DI],AL
RET
RET ADDITION ENDP
ADDITION ENDP END MAIN
END MAIN

You might also like