Professional Documents
Culture Documents
MOV AX,1000
MOV DS, AX
MOV SI,100
MOV DI,200
MOV CX,100
INC SI
INC DI
DEC CX
JNZ TOP
HLT 28
Control Flow and Jump Instructions
EXAMPLE:
Implement an instruction sequence that calculates the absolute
difference between the contents of AX and BX and places it in
DX
Solution:
CMP AX, BX
JC DIFF2
DIFF1: MOV DX, AX
SUB DX, BX ; (DX)=(AX)-(BX)
JMP DONE
DIFF2: MOV DX, BX
SUB DX, AX ; (DX)=(BX)-(AX)
DONE: NOP
29
Subroutines and Subroutine- Handling
Instructions
Subroutine (Procedure) : a special program that can
be called for execution from any point in a program
Return instruction must be included at the end of the
subroutine to initiate the return sequence to the main
program environment
30
Subroutines and Subroutine- Handling
Instructions
Subroutine concept
31
Subroutines and Subroutine- Handling
Instructions
CALL and RET instructions
32
Subroutines and Subroutine- Handling
Instructions
Call allows implementation of two types of operations:
intrasegment call and intersegment call
◦ Near-proc
Call 1234H : 1234H is loaded into IP
◦ Regptr16
Call BX : Contents of BX are loaded into IP
33
Subroutines and Subroutine- Handling
Instructions
CALL and RET instructions
34
Subroutines and Subroutine- Handling
Instructions
RET instruction causes IP or both IP and CS that were
saved on the stack to be returned back to their
corresponding registers
35
Subroutines and Subroutine- Handling
Instructions
PUSH and POP instructions
◦ Original contents are kept intact in the stack segment of a
memory during the execution of the subroutine
36
Subroutines and Subroutine- Handling
Instructions
PUSH and POP instructions
37
Subroutines and Subroutine- Handling
Instructions
PUSH and POP instructions
◦ PUSH AX (SS:SP)
((SP) – 1) (AH)
((SP) – 2) (AL)
(SP) (SP) - 2
◦ POP AX (SS:SP)
(AL) ((SP))
(AH) ((SP) + 1)
(SP) (SP) + 2
38
Loop and the Loop-handling Instructions
LOOP instructions
◦ It uses the contents of the CX register
◦ CX must be preloaded with a count that represents the number of
times the loop is to repeat
◦ Contents of CX are decremented by one and then checked to
determine if they are equal to zero
◦ If it is equal to zero, the loop is complete and the instruction
following LOOP is executed
39
Loop and the Loop-handling Instructions
LOOP instructions
40
Loop and the Loop-handling Instructions
LOOP instructions
41
Loop and the Loop-handling Instructions
EXAMPLE: Given the following sequence of instructions,
explain what happens as they are executed
42