Strings, Procedures and Macros
Strings, Procedures and Macros
1
The 8086 String Instructions
Moving a String:
REPEAT
2
Contd..
◼ The more detailed algorithm is as follows:
UNTIL COUNTER = 0
3
MOVS/MOVSB/MOVSW
◼ The single 8086 string instruction MOVSB, will perform all the
actions in the REPEAT-UNTIL loop
5
Contd..
◼ Also, the number of string elements to be moved must be loaded into the CX
register
◼ 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
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;
9
Advantage of using MOVS/MOVSB/MOVSW:
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)
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
17
The 8086 Stack
◼ Max 64KB memory can be used as 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
◼ 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
21
PUSHF and POPF
PUSHF
◼ SP← SP – 2
POPF
◼ (flag register) ← (SS:SP)
◼ SP← SP + 2
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
24
Contd..
◼ A RET instruction at the end of the procedure returns
execution to the next instruction in the main line
25
MAINLINE OR CALLING
PROGRAM
PROCEDURE
INSTRUCTIONS
my_proc PROC near
---
CALL
---
---
NEXT MAINLINE
INSTRUCTIONS ---
RET My_proc ENDP
26
Main Line Instructions
Lower level
Procedure
Procedure
CALL CALL
Next Main Line
Instructions
RET
RET
27
The CALL Instruction Overview
◼ The 8086 CALL instruction performs 2 operations when it
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
30 30
Intersegment CALL
Physical Offset
Address Address
31
Direct within-segment NEAR CALL
(Intrasegment – Direct Call)
Opcode DispLow DispHigh
E8 DispLow DispHigh
Operation:
IP IP + Disp16 and (SP) Return address
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
Operation:
SP Return address (CS:IP)
CS SegBase
IP Offset
34
Indirect Inter-Segment FAR CALL
Opcode 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
35
Near RET Instruction
RET Opcode
C3 Physical Offset
(IP) ← (SS:SP) Address Address
SP ← SP+2 SS=6200 62000h 0000h
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
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
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..
44
Contd..
◼ The disadvantage of using registers to pass parameters
is that the number of registers limits the number of
parameters you can pass
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
48
3. Passing Parameters Using Pointers
◼ This pointer approach is more versatile because you can pass the
procedure pointers to data anywhere in memory
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
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
PUSHF MUL BH
PUSH AX ADD AL, BL BP LOW 0056h SP=0056
PUSH BX ; End Of Conversion BP High 0057h
54
REENTRANT PROCEDURES
MAIN LINE MULTIPLY PROCEDURE
INTERRUPT PROCEDURE
Return to Calling
Program
55
Contd..
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
◼ 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 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
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
67
◼ unsigned division
DIV instruction
◼ Divide a word by a byte.
❑ Divided:16-bit must be in AX
◼ 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
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.
78
Defining and Calling a Macro Without Parameters
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