0% found this document useful (0 votes)
59 views15 pages

Lecture 15

The document discusses subroutines, CALL and RET instructions, PUSH and POP instructions, and loop handling instructions. It provides examples of how to implement an absolute difference calculation between AX and BX registers using CMP and SUB instructions, and how CALL and RET instructions can implement near, register-pointer, and far procedure calls. It also explains how PUSH and POP instructions preserve register contents during subroutine calls, and how the LOOP instruction uses the CX register to repeat a loop the number of times it is preloaded with.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views15 pages

Lecture 15

The document discusses subroutines, CALL and RET instructions, PUSH and POP instructions, and loop handling instructions. It provides examples of how to implement an absolute difference calculation between AX and BX registers using CMP and SUB instructions, and how CALL and RET instructions can implement near, register-pointer, and far procedure calls. It also explains how PUSH and POP instructions preserve register contents during subroutine calls, and how the LOOP instruction uses the CX register to repeat a loop the number of times it is preloaded with.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Example:

Write a program to move a block of data stored at memory area


[1000:100 through 1000:1FF], to another memory area starting at
address [1000:200].
Solution:

MOV AX,1000
MOV DS, AX

MOV SI,100
MOV DI,200
MOV CX,100

TOP: MOV AL,[SI]


MOV [DI],AL

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

 Subroutine- Handling Instructions


◦ CALL and RET instructions
◦ PUSH and POP instructions

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

◦ Far-proc and Memptr32


Call [BX] : Subroutine offset at the memory location is derived
from contents of DS and BX

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

MOV DL, 05H


MOV AX, 0A00H
MOV DS, AX
MOV SI, 0000H
MOV CX, 000FH
AGAIN: INC SI
CMP [SI], DL
LOOPNE AGAIN

42

You might also like