0% found this document useful (0 votes)
41 views83 pages

Strings, Procedures and Macros

Uploaded by

Yug Patel
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)
41 views83 pages

Strings, Procedures and Macros

Uploaded by

Yug Patel
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/ 83

Strings, Procedures and Macros

Prepared By: Sunil K. Vithlani


Assistant Professor,
Department of Information Technology,
DDU, Nadiad

1
The 8086 String Instructions
Moving a String:

REPEAT

MOVE BYTE FROM SOURCE STRING


TO DESTINATION STRING

UNTIL ALL BYTES MOVED

2
Contd..
◼ The more detailed algorithm is as follows:

INITIALIZE SOURCE POINTER, SI


INITIALIZE DESTINATION POINTER, DI
INITIALZE COUNTER, CX
REPEAT

-COPY BYTE FROM SOURCE TO DESTINATION


-INCREMENT SOURCE POINTER
-INCREMENT DESTINATION POINTER
-DECREMENT COUNTER

UNTIL COUNTER = 0

3
MOVS/MOVSB/MOVSW
◼ The single 8086 string instruction MOVSB, will perform all the
actions in the REPEAT-UNTIL loop

◼ The MOVSB instruction will copy a byte from the location


pointed to by the SI register to a location pointed to by DI
register

◼ It will then automatically increment/decrement SI and DI to


point to the next source location

◼ (ES:DI) <- (DS:SI)


4
Contd..
◼ If you add a special prefix called the repeat prefix ( REP ) in front of
the MOVSB instruction, the MOVSB instruction will be repeated
and CX decremented until CX is counted down to zero

◼ In order for the MOVSB instruction to work correctly, the source


index register, SI, must contain the offset of the start of the source
string and the destination index register, DI, must contain the
offset of the start of the destination location

5
Contd..
◼ Also, the number of string elements to be moved must be loaded into the CX
register

◼ The string instructions will automatically increment or decrement the


pointers after each operation, depending on the state of the direction flag
DF

◼ If the direction flag is cleared with a CLD instruction, then the pointers in SI
and DI will be automatically be incremented after each string operation

◼ If the direction flag is set with an STD instruction, then the pointers in SI
and DI will be automatically be decremented after each string operation

6
Contd..
Note:
◼ For String instructions, an offset in DI is added to the
segment base represented by the number in the ES register
to produce a physical address

◼ If DS and ES are initialized with the same value, then SI


and DI will point to locations in the same segment

7
DATA SEGMENT
Example
SOURCE_MSG DB ‘HELLO’;
NEW_LOC DB 5 DUP(0)
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA, ES:DATA
START: MOV AX, DATA
MOV DS, AX
MOV ES, AX
LEA SI, SOURCE_MSG
LEA DI, NEW_LOC
MOV CX, 05
CLD
REP MOVSB
INT 3
CODE ENDS
END START

8
MOVSW
Note:
◼ Used to move a string of words;

◼ Depending on the state of the direction flag, SI and DI will


automatically be incremented or decremented by 2 after
each word move

◼ If the REP prefix is used, CX will be decremented by 1


after each word move, so CX should be initialized with the
number of words in the string

9
Advantage of using MOVS/MOVSB/MOVSW:

◼ The string instruction is much more efficient than using a


sequence of standard instructions, because the 8086 only
has to fetch and decode the REP MOVSB instruction once

◼ A standard instruction sequence such as MOV, MOV, INC,


INC, LOOP etc would have to be fetched and decoded each
time around the loop

10
Loading string
◼ LODS/LODSB/LODSW
◼ Load a string byte or word
◼ AL or AX <- (DS:SI)

11
Storing string
◼ STOS/STOSB/STOSW
◼ (ES:DI) <- AL or AX

12
Using the CMPSB (Compare String Byte)

◼ Compare the byte pointed to by SI with the byte pointed to


by DI
◼ set the flags according to the result
◼ It also increment/decrement the pointers SI and DI
(depending on DF) to point to the next string elements.
◼ The REPE prefix in front of this instruction tells the 8086
to decrement the CX register after each compare, and
repeat the CMPSB instruction if the compared bytes were
equal AND CX is not yet decremented down to zero

13
Example
DATA SEGMENT CLD
STR1 DB ‘HELLO’ REPE CMPSB
STR2 DB ‘HELLO’ JNE NOTEQUAL
DATA ENDS ; DISPLAY EQUAL
JMP EXIT
CODE SEGMENT NOTEQUAL: ….
ASSUME CS:CODE, DS:DATA, ES:DATA …..
START: MOV AX, DATA ; DISPLAY NOT EQUAL
MOV DS, AX
EXIT: INT 3
MOV ES, AX
CODE ENDS
LEA SI, STR1
END START
LEA DI, STR2
MOV CX, 05

14
Scan String
◼ SCAS/SCASB/SCASW
◼ Scan a string byte or word stored in AL/AX in the
destination string (ES:DI)

15
8086 STACK

16
Applications of stack in 8086
◼ To store values during execution of program

◼ While calling a procedure, the return address needs to


stored on the stack

◼ Pass the parameters to the procedure while calling and


getting the return values from the procedure

17
The 8086 Stack
◼ Max 64KB memory can be used as stack

◼ SS holds segment address of the stack.

◼ SP holds offset address of top of the stack.

◼ PA=>SS*10h + SP

18
Contd.. Physical
Address
Offset
Address
STACK_SEG SEGMENT STACK SS=6200 62000h 0000h
DW 50 DUP(0) 62001h 0001h
STACK_TOP LABEL WORD 62002h 0002h
STACK_SEG ENDS 0003h
CODE SEGMENT 0004h
ASSUME CS:CODE, SS:STACK_SEG
START: MOV AX,
STACK_SEG
005Fh
MOV SS, AX
0060h
LEA SP, STACK_TOP
0061h
.
. 0062h
CODE ENDS 62063h 0063h
SP=0064
END START 0064h Initially

19
PUSH instruction
Physical Offset
◼ PUSH source SS=6200 Address Address
62000h 0000h
◼ SP← SP – 2 62001h 0001h

◼ (SS:SP) ← (SOURCE) 62002h 0002h


0003h
0004h

◼ E.g. PUSH AX
005Fh
PUSH DS
36 0060h SP=0060
PUSH word ptr [SI]
A2 0061h
12 0062h SP=0062
PUSH 3212h
62063h 32 0063h
PUSH A236h
0064h

20
POP instruction
Physical Offset
Address Address
◼ POP destination SS=6200 62000h 0000h

(Destination)←
62001h 0001h

62002h 0002h
(SS:SP) 0003h

◼ SP← SP + 2 0004h

005Fh

◼ E.g. POP BX 36 0060h SP=0060


A2 0061h
pop word ptr [BX+SI]
12 0062h SP=0062
62063h 32 0063h
POP AX AX=A236h
0064h

21
PUSHF and POPF
PUSHF
◼ SP← SP – 2

◼ (SS:SP) ← (flag register)

◼ Generally used in starting of procedure, to store flag register’s status of


calling prog.

POPF
◼ (flag register) ← (SS:SP)

◼ SP← SP + 2

◼ Generally used at the end of procedure, to retrive flag register’s status of


calling prog.

22
Procedure

23
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

◼ Procedure is a self-contained program that represent a


specified task.

◼ CALL instruction is used to call procedure.

24
Contd..
◼ A RET instruction at the end of the procedure returns
execution to the next instruction in the main line

◼ Procedures can even be nested

◼ PROC and ENDP directives are used to define the


procedure.

25
MAINLINE OR CALLING
PROGRAM
PROCEDURE
INSTRUCTIONS
my_proc PROC near
---
CALL
---
---
NEXT MAINLINE
INSTRUCTIONS ---
RET My_proc ENDP

Fig. Single Procedure Call

26
Main Line Instructions

Lower level
Procedure
Procedure

CALL CALL
Next Main Line
Instructions
RET
RET

Fig. Nested Procedures Call

27
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

28
Contd..
◼ 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
◼ 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

29
Intrasegment CALL
Physical Offset
Address Address
CALL proc_name 62000h 0000h
SS=6200

0061h
IP LOW 0062h SP=0062
62063h IP HIGH 0063h
SP ← SP-2 0064h SP=0064

(SS:SP) ← IP ;push the return address

(IP) ← offset address of the proc_name

30 30
Intersegment CALL
Physical Offset
Address Address

CALL proc_name SS=6200 62000h 0000h

IP LOW 0060h SP=0060


IP HIGH 0061h
CS LOW 0062h
62063h CS HIGH 0063h
SP ← SP-2 0064h SP=0064
(SS:SP) ← (CS) ;push the CS of return address
SP ← SP-2
(SS:SP) ← (IP) ;push the IP of return address
(CS) ← segment base of proc_name
(IP) ← offset address of the proc_name

31
Direct within-segment NEAR CALL
(Intrasegment – Direct Call)
Opcode DispLow DispHigh
E8 DispLow DispHigh

Operation:
IP  IP + Disp16 and (SP) Return address

◼ 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
◼ E.g CALL proc_name

32
Indirect within-segment NEAR CALL
(Intrasegment – Indirect Call)
Opcode mod 010 r/m
FF mod 010 r/m

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

E.g. CALL AX
CALL word ptr [SI]
33
Direct Inter-Segment FAR CALL

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


9A Offset-low Offset-high Seg-low Seg-high

Operation:
SP  Return address (CS:IP)
CS  SegBase
IP  Offset

◼ E.g. CALL proc_name

34
Indirect Inter-Segment FAR CALL
Opcode mod 010 r/m Mem-low Mem-high

FF mod 010 r/m Mem-low Mem-high

Operation:
SP  Return address (CS:IP)
CS  SegBase
IP  offset
◼ New value of IP and CS is got from memory

E.g. CALL DWORD PTR [SI]


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

35
Near RET Instruction
RET Opcode

C3 Physical Offset
(IP) ← (SS:SP) Address Address
SP ← SP+2 SS=6200 62000h 0000h

◼ A return at the end of the procedure


0061h
copies this value from the stack back SP=0062
IP LOW 0062h
to the instruction pointer to return
62063h IP HIGH 0063h
execution to the calling program.
0064h SP=0064

36
Far RET Instruction
RET Opcode

(IP) ← (SS:SP) C8
SP ← SP+2 Physical Offset
(CS) ← (SS:SP) Address Address
SP ← SP+2 SS=6200 62000h 0000h

IP LOW 0060h SP=0060


◼ A RET instruction at the end of the IP HIGH 0061h
procedure copies these values from CS LOW 0062h
the stack back into the IP and CS 62063h CS HIGH 0063h
register to return execution to the
0064h SP=0064
mainline program.

37
RET n instruction SS=6200
Offset
Address
0000h
Opcode DataL DataH
005Bh
RET n ;POP return address first and then SP←SP+n
IP LOW 005Ch SP=005C
◼Here n is an integer value IP HIGH 005Dh
Operation: SP=005E
........ 005Eh
◼ Intra-segment RET and ADD (Opcode: C2) ........ 005Fh + 0003
........ 0060h
◼ Inter-segment RET and ADD (Opcode: CA)
........ 0061h SP=0061
• It will copy the word at the top of the stack to the IP ........ 0062h
and CS and also add an immediate number contained ........ 0063h
in the instruction to the contents of SP
0064h
• Generally used to remove the parameters from the
stack automatically after returning from the
procedure RET 0003
38
Passing parameters to and from
procedures

39
Passing Parameters to and from Procedures

◼ Often we want to pass some data values or addresses to the procedure.

◼ Likewise, we often want a procedure to make some processed data values


or addresses available to the main program.

◼ The 4 major ways of passing parameters to and from a procedure are:


1. In Registers
2. In dedicated memory locations accessed by name
3. With pointers passed in registers
4. With the stack
40
BCD-to-Binary program
BCD NO: 0100 0101 1001 0110 =>4596 Decimal ◼ Algorithm for two-
digit BCD number to
Binary No: 0001 0001 1111 0100 =>11F4H
convert into binary
4596=(4*1000)+(5*100)+(9*10)+(6*1)
1. Separate nibbles
2. Save lower nibble (don’t
1=0001H therefore 6= 6 * 0001H = 0006H need to multiply by 1)
10=000AH therefore 90=9 * 000AH = 005AH 3. Multiply upper nibble
100=0064H therefore 500=5 * 0064H = 01F4H by 0AH
1000=03E8H therefore 4000=4 * 03E8H = 0FA0H 4. Add lower nibble to
4596 11F4 H result of multiplication.

41
1. Passing Parameters in Registers
DATA SEGMENT CODE SEGMENT
BCD_INPUT DB 17H ASSUME CS:CODE, DS:DATA, SS:STACK_SEG
BIN_VALUE DB ? START: MOV AX, DATA
DATA ENDS MOV DS, AX
MOV AX, STACK_SEG
STACK_SEG SEGMENT MOV SS, AX
DW 40 DUP(0) MOV SP, OFFSET TOP_STACK
TOP_STACK LABEL WORD MOV AL, BCD_INPUT
STACK_SEG ENDS CALL BCD_TO_BIN
MOV BIN_VALUE, AL
INT 3H

42
Contd..
BCD_TO_BIN PROC NEAR MUL BH
PUSHF ADD AL, BL
PUSH BX ; End Of Conversion
PUSH CX ; Binary Result In AL
; DO THE CONVERSION
POP CX
MOV BL, AL
POP BX
AND BL, 0FH POPF
AND AL, 0F0H RET
MOV CL, 04H BCD_TO_BIN ENDP
SHR AL, CL CODE ENDS
MOV BH, 0AH END START

43
Contd..

Note that we don’t push the AX register because we


are using it to pass a value to the procedure and
expecting the procedure to pass a different value
back to the calling program in it

44
Contd..
◼ The disadvantage of using registers to pass parameters
is that the number of registers limits the number of
parameters you can pass

◼ You can’t, for example, pass an array of 100 elements to a


procedure in registers

45
2. Passing Parameters in General Memory
DATA SEGMENT CODE SEGMENT
BCD_INPUT DB 17H
BIN_VALUE DB ?
ASSUME CS:CODE, DS:DATA, SS:STACK_SEG
DATA ENDS START: MOV AX, DATA
MOV DS, AX
STACK_SEG SEGMENT
MOV AX, STACK_SEG
DW 40 DUP(0)
TOP_STACK LABEL WORD MOV SS, AX
STACK_SEG ENDS MOV SP, OFFSET TOP_STACK
; SAME AS LEA SP, TOP_STACK

CALL BCD_TO_BIN
INT 3H
46
Contd..
BCD_TO_BIN PROC NEAR MUL BH
PUSHF ADD AL, BL
PUSH AX ;End Of Conversion; Binary Result In AL
PUSH BX MOV BIN_VALUE, AL
PUSH CX POP CX
MOV AL, BCD_INPUT POP BX
; DO THE CONVERSION POP AX
MOV BL, AL POPF
AND BL, 0FH RET
AND AL, 0F0H BCD_TO_BIN ENDP
MOV CL, 04H CODE ENDS
SHR AL, CL END START
MOV BH, 0AH
47
Contd..
◼ The limitation of this method is that this procedure will
always look to the memory location named BCD_INPUT to
get it data and will always put its result in the memory
location called BIN_VALUE

◼ In other words, the way it is written we cant easily use


this procedure to convert a BCD number in some other
memory location

48
3. Passing Parameters Using Pointers

◼ A parameter-passing method which overcomes the disadvantage of


using data item names directly in a procedure is to use registers to
pass the procedure pointers to the desired data

◼ This pointer approach is more versatile because you can pass the
procedure pointers to data anywhere in memory

◼ You can pass pointers to individual values or pointers to arrays or


strings

49
Contd..
DATA SEGMENT CODE SEGMENT
BCD_INPUT DB 17H ASSUME CS:CODE, DS:DATA, SS:STACK_SEG
BIN_VALUE DB ? START: MOV AX, DATA
DATA ENDS MOV DS, AX
MOV AX, STACK_SEG
STACK_SEG SEGMENT
MOV SS, AX
DW 40 DUP(0)
TOP_STACK LABEL WORD MOV SP, OFFSET TOP_STACK
STACK_SEG ENDS ; SAME AS LEA SP, TOP_STACK
MOV SI, OFFSET BCD_INPUT
; or LEA SI, BCD_INPUT
MOV DI, OFFSET BIN_VALUE
; or LEA DI, BIN_VALUE
CALL BCD_TO_BIN
INT 3H
50
Contd..
BCD_TO_BIN PROC NEAR MUL BH
PUSHF ADD AL, BL
PUSH AX ; End Of Conversion; Binary Result In AL
PUSH BX MOV [DI], AL
PUSH CX POP CX
MOV AL, [SI] POP BX
; DO THE CONVERSION POP AX
MOV BL, AL POPF
AND BL, 0FH RET
AND AL, 0F0H BCD_TO_BIN ENDP
MOV CL, 04H CODE ENDS
SHR AL, CL END START
MOV BH, 0AH
51
4. Passing Parameters Using the Stack

◼ To pass parameters to a procedure using the stack, we push the


parameters on the stack somewhere in the mainline program before
we call the procedure

◼ Instructions in the procedure then read the parameters from the


stack as needed

◼ Likewise, parameters to be passed back to the calling program are


written to the stack by instructions in the procedure and read off
the stack by instructions in the mainline program

52
Contd..
DATA SEGMENT CODE SEGMENT
BCD_INPUT DB 17H ASSUME CS:CODE, DS:DATA, SS:STACK_SEG
BIN_VALUE DB ? START: MOV AX, DATA
DATA ENDS MOV DS, AX
MOV AX, STACK_SEG
STACK_SEG SEGMENT
MOV SS, AX
DW 40 DUP(0)
TOP_STACK LABEL WORD MOV SP, OFFSET TOP_STACK
STACK_SEG ENDS ; SAME AS LEA SP, TOP_STACK
MOV AL, BCD_INPUT
PUSH AX
CALL BCD_TO_BIN
POP AX
MOV BIN_VALUE, AL
INT 3H

53
Offset

BCD_TO_BIN PROC NEAR


Contd.. Address

PUSHF MUL BH
PUSH AX ADD AL, BL BP LOW 0056h SP=0056
PUSH BX ; End Of Conversion BP High 0057h

PUSH CX ; Binary Result In AL CL 0058h

PUSH BP MOV [BP+12], AX CH 0059h


BL 005Ah
MOV BP, SP POP BP
BH 005Bh
MOV AX, [BP+12] POP CX AL 005Ch
12 d
or
; DO THE CONVERSION POP BX AH 005Dh 0CH
MOV BL, AL POP AX Flag Low 005Eh

AND BL, 0FH POPF Flag High 005Fh


IP LOW 0060h
AND AL, 0F0H RET
IP HIGH 0061h
MOV CL, 04H BCD_TO_BIN ENDP AL 0062h BP=00062
SHR AL, CL CODE ENDS AH 0063h

MOV BH, 0AH END START 0064h

54
REENTRANT PROCEDURES
MAIN LINE MULTIPLY PROCEDURE
INTERRUPT PROCEDURE

CALL MULTIPLY Interrupt


CALL MULTIPLY
Occurs here

Next Main Line Return to Interrupted


Instruction after call Program

Return to Calling
Program

55
Contd..

◼ The reentrant procedure is the procedure which can be


interrupted, used and reentered without losing or writing
over anything.

◼ 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
56
Recursive Procedures

◼ A recursive procedure is a procedure which calls itself

◼ Recursive procedures are often used to work with complex


data structures.

57
Recursive Procedure Example
◼ Flow diagram for N=1

MAIN LINE

PROCEDURE FACTO

CALL FACTO

RET WITH 1

58
Contd..
◼ Flow diagram for N=3
PROCEDURE FACTO
Procedure Procedure
MAIN LINE
FACTO FACTO Procedure IF N = 1
FACTO FACTORIAL = 1
RET
ELSE
CALL CALL
REPEAT
CALL FACTO DECREMENT N
CALL FACTO
RET UNTIL N=1
Next Main Line
WITH 1 ! MULTIPLY (N-1)! *PREVIOUS N
Instruction
RET RET
RET WITH 2 !
WITH 3 !

59
Contd..
DATA SEGMENT CODE SEGMENT
N DB 04H ASSUME CS:CODE, DS:DATA, SS:STACK_SEG
FACT DW ? START: MOV AX, DATA
MOV DS, AX
DATA ENDS
MOV AX, STACK_SEG
MOV SS, AX
STACK_SEG SEGMENT
MOV SP, OFFSET TOP_STACK
DW 50 DUP(0) Off. Add.
TOP_STACK LABEL WORD
STACK_SEG ENDS MOV AX, 1 005Fh
MOV BL, N 0060h
MOV BH, 0 0061h
CALL FACTORIAL IP LOW 0062h
(main prog.)
MOV FACT, AX IP HIGH 0063h
INT 3H 0064h

60
Procedure Procedure Procedure Procedure
MAIN LINE
FACTO FACTO FACTO FACTO
Off. Add.
FACTORIAL PROC
CMP BX, 1 IP LOW
CALL FACTO CALL CALL CALL (n=2)
JE L1 POP BX
POP BX POP BX IP HIGH
MOV FACT, AX 02
PUSH BX RET 00
DEC BX RET WITH 1 !
RET RET IP LOW
WITH 2 ! (n=3)
CALL FACTORIAL WITH 4 ! WITH 3 ! Off. Add.
IP HIGH
IP LOW (n=3)
03
IP HIGH
POP BX Off. Add. 00
03
MUL BX IP LOW (n=4) IP LOW
00 (n=4)
IP HIGH 005Fh
IP LOW (n=4)
L1:RET 04 0060h IP HIGH 005Fh
IP HIGH 005Fh
FACTORIAL ENDP 00 0061h 04 0060h
04 0060h 00 0061h
IP LOW 0062h
00 0061h
CODE ENDS (main prog.) IP LOW 0062h
IP LOW (main prog.) 0062h (main prog.)
END START IP HIGH 0063h
0064h IP HIGH 0063h IP HIGH 0063h

After call Factorial(3) After call Factorial(2) After call Factorial(1)


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

◼ 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
62
Contd..
CODE SEGMENT ◼ If a procedure is in a different
ASSUME CS:CODE, DS:DATA,
SS:STCK_SEG
segment from the CALL
: instruction, you must declare it far
CALL MULTIPLY_32 with the FAR Assembler Directive
:
CODE ENDS
◼ Also you must put an ASSUME
PROCEDURES SEGMENT statement in the procedure to tell
MULTIPLY_32 PROC FAR the assembler what segment base
ASSUME CS:PROCEDURES
:
to use when calculating the offsets
: of the instructions in the procedure
MULTIPLY_32 ENDP
PROCEDURES ENDS

63
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

64
PUBLIC Directive
◼ 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

◼ E.g. PUBLIC DISPLAY, tells the linker that a procedure


or variable named DISPLAY can be legally accessed from
another assembly module

65
EXTRN Directive
◼ 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

◼ E.g. EXTRN DISPLAY:FAR, SECONDS:BYTE, PI:ABS

◼ Tells the linker that DISPLAY is a far procedure and


SECONDS is a variable of type byte and PI is constant
defined with EQU and located in another assembly
module.
66
Contd..

To summarize, a procedure or variable declared


PUBLIC in one module will be declared EXTRN in
modules which access the procedure or variable

67
◼ unsigned division
DIV instruction
◼ Divide a word by a byte.
❑ Divided:16-bit must be in AX

❑ Divisor:8-bit from register or memory

❑ After division operation AL:8-bit quotient , AH: 8-bit reminder.

◼ Divide a double word by a word.


❑ Divided (32 bit): most significant word of the double word must be in DX. least
significant word of the double word must be in AX
❑ Divisor:16-bit from register or memory

❑ After division operation AX:16-bit quotient , DX: 16-bit reminder.

◼ If an attempt is made to divide by 0 or if the quotient is too large to fit in the destination
(greater than FFH / FFFFH), the 8086 will generate a type 0 interrupt.

68
◼ A assembly
language
program to
divide 32-bit
number by a 16-
bit number and
return a 32bit
quotient.

69
div_main.asm
data segment word public
dividend DW 4038h, 8C72h ;dividend=8c724038 (32-bit)
divisor DW 5692h ;16-bit
data ends
more_data segment word
quotient dw 2 dup (0)
reminder dw 0
more_data ends
my_stack segment stack
dw 30 dup (0)
tos label word
my_stack ends
public divisor

procedures segment public


extrn smart_divide:far
procedures ends

70
div_main.asm
code segment word public
save_all: assume ds:more_data
assume cs:code,ds:data,ss:my_stack
push ds
start: mov ax,data
mov bx,more_data
mov ds,ax
mov ds,bx
mov ax,my_stack
mov ss,ax
mov quotient,ax
mov sp, offset tos
mov quotient+2,dx
;passing parameters using registers
mov reminder,cx
mov ax,dividend
mov dx,dividend+2
assume ds:data
mov cx,divisor
pop ds
stop: int 3
call smart_divide
code ends
end start
jnc save_all
jmp stop

71
div_smartdivproc.asm
data segment public mov bp,ax ;save 16-bit quotient into register
extrn divisor:word mov ax,bx ;dx=reminder,ax=lower 16-bit dividend
data ends div cx ;dx:ax/cx, quotient in ax, reminder in dx
mov cx,dx ;reminder in cx
public smart_divide mov dx,bp ;high order result in dx
CLC
procedures segment public jmp exit
smart_divide proc far error_exit: STC
assume cs:procedures,ds:data exit: ret
cmp divisor,0
je error_exit smart_divide endp
mov bx,ax
mov ax,dx procedures ends
mov dx,0000h end
div cx ;dx:ax/cx= 0000ax/cx, quotient in
;ax, reminder in dx

72
Mainline program module.asm

73
Mainline program module.asm

74
Mainline program module.asm

75
Procedure module.asm

76
Procedure module.asm

77
MACRO
◼ When repeated group of instructions is too short or not
appropriate to write as a procedure, we use a macro.

◼ Macro is group of instructions.

78
Defining and Calling a Macro Without Parameters

Macro Definition: Macro Definition:


MACRO-NAME MACRO CLRSCR MACRO
; MACRO DEFINITION MOV AH, 00H
; MOV AL, 02H
INT 10H
ENDM
ENDM
Invoking a Macro:
MACRO-NAME Macro Invocation:
CLRSCR

79
example
PUSH_ALL MACRO Multiply proc far
pushf assume: cs: procedures, ds:data
push ax PUSH_ALL
push bx mov ax,data
push cx mov ds,ax
push dx …..
push bp ….
push si Multiply endp
push di another proc near
push ds PUSH_ALL
push es …..
push ss ….
ENDM

80
Passing Parameters to Macros
◼ If the macro invocation
Example: statement is SETCURSOR
Macro definition: 12, 40 then the macro will be
SETCURSOR MACRO X, Y replaced by:
MOV DL, Y MOV DL, 40
MOV DH, X MOV DH, 12
MOV BH, 00H MOV BH, 00H
MOV AH, 02H MOV AH, 02H
INT 10H INT 10H
ENDM
81
Difference between procedure and macro
◼ Procedure
❑ Assessed by CALL and RET
❑ Machine code for instructions put in memory only once.
❑ Parameters passed in registers, memory locations or stack.
◼ Macro
❑ Assessed during assembly with name given to macro
❑ Machine code generated for instructions each time called.
❑ Parameters passed as part of statement which calls macro.

82
END OF CHAPTER 5

83

You might also like