0% found this document useful (0 votes)
1 views60 pages

Modular Programming - LATEST

The document discusses the use of procedures and macros in modular programming, specifically focusing on the 8086 CALL and RET instructions. It explains how procedures can be called and returned from, including near and far calls, and the mechanics of the stack in relation to these operations. Additionally, it highlights the importance of writing reentrant procedures that can be interrupted and reused without data loss.

Uploaded by

jekele4499
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)
1 views60 pages

Modular Programming - LATEST

The document discusses the use of procedures and macros in modular programming, specifically focusing on the 8086 CALL and RET instructions. It explains how procedures can be called and returned from, including near and far calls, and the mechanics of the stack in relation to these operations. Additionally, it highlights the importance of writing reentrant procedures that can be interrupted and reused without data loss.

Uploaded by

jekele4499
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

Procedures and Macros

(Modular Programming)

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 1


Writing and using Procedures

 To avoid writing the sequence of instructions in


the program each time you need them, you can
write the sequence as a separate subprogram
called a procedure

 You use the CALL instruction to send the 8086


to the starting address of the procedure in
memory

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 2


 A RET instruction at the end of the procedure
returns execution to the next instruction in the
main line

 Procedures can even be nested

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 3


MAINLINE OR CALLING
PROGRAM
PROCEDURE
INSTRUCTIONS

CALL

NEXT MAINLINE
INSTRUCTIONS

RET

Fig. Single Procedure Call

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 4


Main Line Instructions

Lower level
Procedure Procedure

CALL CALL
Next Main Line
Instructions
RET
RET

Fig. Nested Procedures Call

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 5


8086 CALL and RET Instructions

 A CALL instruction in the mainline program


loads the Instruction Pointer and in some cases
also the code segment register with the starting
address of the procedure

 At the end of the procedure, a RET instruction


sends execution back to the next instruction
after the CALL in the mainline program

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 6


The CALL Instruction Overview

 The 8086 CALL instruction performs 2


operations when it executes

 First, it stores the address of the instruction after


the CALL instruction on the stack

 This address is called the return address


because it is the address that execution will
return to after the procedure executes

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 7


 If the CALL is to a procedure in the same code
segment, then the call is near, and only the
instruction pointer contents will be saved on the
stack

 If the CALL is to a procedure in another code


segment, the call is far ; In this case both the
instruction pointer and the code segment
register contents will be saved on the stack

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 8


 Second operation of the CALL instruction is to
change the contents of the instruction pointer
and, in some cases, the contents of the code
segment register to contain the starting address
of the procedure

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 9


Direct within-segment NEAR CALL
 Tells the 8086 to produce the starting
address of the procedure by adding 16-bit
signed displacement contained in the
instruction to the contents of the instruction
pointer

 The starting address of the procedure can


be anywhere in the range of -32,768 bytes
to +32,767 bytes from the address of the
instruction after the CALL
3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 10
Direct within-segment NEAR CALL
format
Opcode DispLow DispHigh

Operation:
IP  IP + Disp16 (SP) Return Link

 Eg: CALL delay

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 11


Indirect within-segment NEAR CALL
 The IP is replaced with a 16-bit value from
a specified register or memory location

Eg: CALL BP;offset is present in BP


register

 The value of BP will be put in the


instruction pointer

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 12


Indirect within-segment NEAR CALL
format
Opcode mod 010 r/m

Operation:
IP  Reg16 (SP)  Return Link
IP  Mem16 (SP)  Return Link

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 13


Direct Inter-Segment FAR CALL
 For this form of call instruction, the new value
for the IP is written in as bytes 2 and 3 of the
instruction code.

 Lower byte of the new IP value is written


before the high byte

 The new value for the CS register is written in


as bytes 4 and 5 of the instruction code

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 14


Direct Inter-Segment FAR CALL
format

Opcode Offset-low Offset-high Seg-low Seg-high

Operation:
CS  SegBase
IP  Offset
 The procedure is in a segment with a different
name from that where the CALL is located

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 15


Indirect Inter-Segment FAR CALL
 This form of CALL instruction replaces the IP
and CS register contents with two 16-bit
values from memory.

 Since two 16-bit values are needed ,the


values cannot come from a register

 The mod-r/m byte in the instruction is used to


specify the addressing mode for the memory
loc there the 8086 goes to get the new values
3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 16
Indirect Inter-Segment FAR CALL format

Opcode Mod 011 r/m Mem-low Mem-high

Operation:
CS  SegBase
IP  offset

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 17


 The first word from memory is put in the
instruction pointer and the second word from
memory is put in the code segment register

EX: CALL DWORD PTR[BX]

 Will get a new IP from [BX] and [BX+1] in data


segment and a new value for CS from offsets
[BX+2] and [BX+3] in the data segment

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 18


Writing and Calling near procedures
CODE SEGMENT
ASSUME CS:CODE
:
CALL DELAY
:
:
MOV AH,4CH
INT 21H
DELAY PROC NEAR
:
RET
ENDP
CODE ENDS

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 19


Writing and Calling Far Procedures

 A FAR procedure is one that is located in a


segment which has a different name from the
segment containing the CALL instruction

 To get the starting address of a far procedure,


the 8086 must change the contents of both the
Code Segment register and the Instruction
Pointer

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 20


 At the end of the far procedure, both the
contents of the code segment register and the
contents of the instruction pointer must be
popped off the stack to return to the calling
program

 If a procedure is in a different segment from the


CALL instruction, you must declare it far with the
FAR Assembler Directive

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 21


CODE SEGMENT
ASSUME CS:CODE
:
CALL MULTYIPLY_32
:
CODE ENDS

PROCEDURES SEGMENT
MULTIPLY_32 PROC FAR
ASSUME CS:PROCEDURES
:
:
RET
MULTIFLY_32 ENDP
PROCEDURES ENDS
3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 22
 Also you must put an ASSUME statement in the
procedure to tell the assembler what segment base to
use when calculating the offsets of the instructions in the
procedure

 When the assembler know that the procedure is far by


using the word FAR in the MULTIPLY_32 PROC FAR
statement

 It will automatically code the CALL instruction as an


intersegment call and the RET instruction as an
intersegment return

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 23


 When the assembler finally codes the CALL
instruction ,it will put the value of
PROCEDURES in CS

 The offset of first instruction of the procedure


in PROCEDURES in IP
The 8086 RET Instruction
 A return at the end of the procedure copies the
value from the stack back to the instruction
pointer to return execution to the calling program

 When the 8086 does a near call ,it saves the


instruction pointer value for the instruction after
the CALL on the stack

 A RET instruction at the end of the procedure


copies these values from the stack back into the
IP to return execution to the mainline program
3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 25
 When the 8086 does a far call, it saves the
contents of both the instruction pointer and the
code segment register on the stack

 A RET instruction at the end of the procedure


copies these values from the stack back into the
IP and CS register to return execution to the
mainline program

 The within segment (intra segment)form of RET


copies a word from the top of the stack to the
instruction pointer

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 26


RET FROM SUBROUTINE:

Opcode

Operation:

 Intra-segment return

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 27


 The intersegment form of RET instruction is
used to return from far procedures.

 When this form of the RET instruction executes,


it will copy the word from the top of the stack to
IP

 Then it will increment the stack pointer by 2 and


copy the next word from the stack to code
segment register

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 28


 The intersegment adding immediate to SP
form of the instruction also copies a new
value for IP and a new value for CS from the
stack.

 However it adds a 16-bit immediate number


contained in the instruction code to SP

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 29


RETURN AND ADD CONSTANTS TO SP:

Opcode DataL DataH

Operation:

 Intra-segment RET and ADD

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 30


The 8086 Stack
 8086 lets us to set aside entire 64 KB segment of
memory as a stack

 The stack segment register(SS) is used to hold the


upper 16 bits of the starting address you give to the
stack segment

 8086 produces the physical address for a stack


location by adding the offset contained in the SP
register to the stack segment base address
represented by the 16-bit number in the SS register

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 31


 SP register is automatically decremented by 2
before a word is written to the stack (PUSH)

 Word is copied from Stack and SP register is


automatically incremented by 2 (POP)

 This means that at the start of your program you


must initialize the SP register to point to the
top of the memory you set aside as a stack
rather than initializing it to point to the bottom
location
3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 32
 When a near RET instruction executes, the IP
value stored in the stack will be copied back to
the IP register and the SP register will be
automatically incremented by 2

 After a CALL-RET sequence, then, the SP


register is again pointing to the initial top-of-
stack location

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 33


 Before a call instruction ,assume that the SS
register contains 7000H and the SP register
contains 0050H.

 The physical address of the current top of the


stack will be 70050H

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 34


STACK_SEG SEGMENT
STACK DW 40 DUP(0)
STACK_TOP LABEL WORD
STACK_SEG ENDS

CODE SEGMENT
ASSUME CS:CODE, SS:STACK_SEG
START:
MOV AX, STACK_SEG
MOV SS, AX
LEA SP, STACK_TOP
.
.
CODE ENDS
END START

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 35


 For this example ,we set a length of 40 words
with the DW 40 DUP(0) statement

 40 words will occupy 80 addresses from


70000h to 7004fh

 The STACK_TOP is associated with address


70050h

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 36


Memory

70050H Initial top of stack and TOS after RET


7004FH IPH
7004EH IPL Top of stack after near CALL

Stack

70000H Start of stack segment

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 37


Effect on stack and stack pointer
Instruction sequence:
Multo proc near
SP SP
Pushf
Before CALL 0050 After RET 0050
Push ax
IP high
Push bx
After CALL 004E IP low After popf 004E
Push cx
Flag high
:
After PUSHF 004C Flag low After pop ax 004C
Pop cx
AH
Pop bx
After PUSH AX 004A AL After pop bx 004A
Pop ax
BH
popf
After PUSH BX 0048 BL After pop cx 0048
ret
CH
Multo endp CL
After PUSH CX 0046 Before pop cx

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 38


REENTRANT PROCEDURES
MAIN LINE MULTIPLY PROCEDURE
INTERRUPT PROCEDURE

CALL MULTIPLY Interrupt


CALL MULTIPLY
Occurs here
Return to Interrupted
Next Main Line
Program
Instruction after call

Return to Calling
Program

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 39


 For the program flow to work correctly ,the
multiply procedure must be written in such a
way that it can be interrupted ,used and
‘reentered’ without losing or writing over
anything

 A procedure which can function this way is


said to be reentrant
 To be reentrant, a procedure must first of all
push the flags and all registers used in the
procedure

 Also, to be reentrant, a program should use only


registers or the stack to pass parameters

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 41


Recursive Procedures

 A recursive procedure is a procedure which calls


itself

 Recursive procedures are often used to work


with complex data structures called trees

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 42


Factorial of a number
 5 factorial is equal to 5x4x3x2x1

 The word factorial is represented by ‘!’

 The basic algorithm is expressed as


If
N=1 then factorial =1
Else
factorial=Nx(factorial of(N-1))
Recursive Procedure Example
 Flow diagram for N=1

MAIN LINE

PROCEDURE FACTO

CALL FACTO

RET WITH 1

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 44


 Flow diagram for N=3
Procedure Procedure Procedure
MAIN LINE
FACTO FACTO FACTO

CALL CALL

CALL FACTO

Next Main Line


RET
Instruction
RET WITH 1 !
RET WITH 2 !
WITH 3 !

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 45


 If we call a procedure with N=3 ,the procedure will
call itself to compute (N-1)! Or 2!

 It will call again to compute the value of the next (N-


1)! Or 1!

 Since 1!=1 ,the procedure return this value to the


program that called it.

 In this case the program called it was a previous


execution of the same procedure that needed this
value to compute 2!
 It computes 2! and return the value to the
program that called it

 The program that called it was a previous


execution of the same procedure that needed 2!
to compute the 3!.

 Given the 2! Value ,call procedure will compute


3! And return to the program that called it

 Here the return now will be to the mainline


program
PROCEDURE FACTO
IF N = 1
FACTORIAL = 1
RET
ELSE
REPEAT
DECREMENT N
CALL FACTO
UNTIL N=1
MULTIPLY (N-1)! X PREVIOUS N
RET

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 48


DATA SEGMENT
N DB 06H
FACT DW ?
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX

MOV AX, 1
MOV BL, N
MOV BH, 0
CALL FACTORIAL

MOV FACT, AX
MOV AH, 4CH
INT 21H

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 49


FACTORIAL PROC NEAR
CMP BX, 1
JE L1

PUSH BX
DEC BX
CALL FACTORIAL

POP BX
MUL BX

L1:RET
FACTORIAL ENDP

CODE ENDS
END START

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 50


Accessing a Procedure and Data in a
Separate Assembly Module

 The best way to write a large program is to


divide it into a series of modules

 Each module can be individually written,


assembled, tested and debugged

 The object code files for the modules can then


be linked together

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 51


 In the module where a variable or procedure is
declared, you must use the PUBLIC directive to
let the linker know that the variable or procedure
can be accessed from other modules

 The statement PUBLIC DISPLAY, for example,


tells the linker that a procedure or variable
named DISPLAY can be legally accessed from
another assembly module

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 52


 In a module which calls a procedure or accesses
a variable in another module, you must use the
EXTRN directive to let the assembler know that
the procedure or variable is not in this module

 The statement EXTRN DISPLAY:FAR,


SECONDS:BYTE tells the linker that DISPLAY
is a far procedure and SECONDS is a variable
of type byte located in another assembly module

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 53


To summarize, a procedure or variable
declared PUBLIC in one module will be
declared EXTRN in modules which access
the procedure or variable

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 54


Writing and Using Assembler Macros

Assignment:

 Compare Macro with Procedure

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 55


Defining and Calling a Macro Without
Parameters
Macro Definition:
MACRO-NAME MACRO
; MACRO DEFINITION
;
ENDM
Invoking a Macro:
MACRO-NAME

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 56


Example
Macro Definition:
CLRSCR MACRO
MOV AH, 00H
MOV AL, 02H
INT 10H
ENDM

Macro Invocation:
CLRSCR

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 57


Passing Parameters to Macros
Example:
Macro definition:
SETCURSOR MACRO X, Y
MOV DL, X ;COLUMN
MOV DH, Y ;ROW
MOV BH, 00H
MOV AH, 02H
INT 10H
ENDM
3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 58
 If the macro invocation statement is
SETCURSOR 12, 40 then the macro will be
replaced by:

MOV DL, 12
MOV DH, 40
MOV BH, 00H
MOV AH, 02H
INT 10H

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 59


Data Conversions
 ASCII to BCD/HEXA

 BCD/HEXA to ASCII

 2/4 digit(8/16bit) BCD(Decimal) to Binary(Hexa)

 2/4 digit (8/16bit) Binary(Hexa) to BCD(Decimal))

3/11/2024 ROSHAN FERNANDES, DEPT OF CSE 60

You might also like