strings, procedures and macros
DESCRIPTION
Strings, Procedures and Macros. The 8086 String Instructions. Moving a String : REPEAT MOVE BYTE FROM SOURCE STRING TO DESTINATION STRING UNTIL ALL BYTES MOVED. Contd. The more detailed algorithm is as follows: INITIALIZE SOURCE POINTER, SI INITIALIZE DESTINATION POINTER, DI - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/1.jpg)
1
Strings, Procedures and Macros
![Page 2: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/2.jpg)
2
The 8086 String Instructions
Moving a String:
REPEAT
MOVE BYTE FROM SOURCE STRING
TO DESTINATION STRING
UNTIL ALL BYTES MOVED
![Page 3: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/3.jpg)
3
Contd.. The more detailed algorithm is as follows:
INITIALIZE SOURCE POINTER, SIINITIALIZE DESTINATION POINTER, DIINITIALZE COUNTER, CXREPEAT
COPY BYTE FROM SOURCE TO DESTINATIONINCREMENT SOURCE POINTERINCREMENT DESTINATION POINTERDECREMENT COUNTER
UNTIL COUNTER = 0
![Page 4: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/4.jpg)
4
Contd.. 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
![Page 5: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/5.jpg)
5
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
![Page 6: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/6.jpg)
6
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
![Page 7: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/7.jpg)
7
Contd..
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
![Page 8: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/8.jpg)
8
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
![Page 9: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/9.jpg)
9
ExampleDATA SEGMENT
TEST_MSG DB ‘HELLO’DB 100 DUP(?)
NEW_LOC DB 5 DUP(0)DATA ENDS
CODE SEGMENTASSUME CS:CODE, DS:DATA, ES:DATA
START: MOV AX, DATAMOV DS, AXMOV ES, AX
![Page 10: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/10.jpg)
10
Contd..
LEA SI, TEST_MSGLEA DI, NEW_LOCMOV CX, 05CLD
REP MOVSBMOV AH, 4CHINT 21HCODE ENDSEND START
![Page 11: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/11.jpg)
11
Contd..Note: The MOVSW instruction can be 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
![Page 12: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/12.jpg)
12
Advantage of using 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
![Page 13: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/13.jpg)
13
Using the CMPSB (Compare String Byte)
The CMPSB instruction will compare the byte pointed to by SI with the byte pointed to by DI and 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
![Page 14: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/14.jpg)
14
Contd..
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
![Page 15: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/15.jpg)
15
Example
DATA SEGMENTSTR1 DB ‘HELLO’STR2 DB ‘HELLO’
DATA ENDS
CODE SEGMENTASSUME CS:CODE, DS:DATA, ES:DATA
START: MOV AX, DATAMOV DS, AXMOV ES, AX
![Page 16: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/16.jpg)
16
Contd..LEA SI, STR1LEA DI, STR2MOV CX, 05CLD
REPE CMPSB JNE NOTEQUAL; DISPLAY EQUALJMP EXIT
NOTEQUAL:; DISPLAY NOT EQUAL
EXIT: MOV AH, 4CHINT 21H
CODE ENDSEND START
![Page 17: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/17.jpg)
17
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
![Page 18: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/18.jpg)
18
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
![Page 19: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/19.jpg)
19
MAINLINE OR CALLING PROGRAM
NEXT MAINLINE INSTRUCTIONS
PROCEDUREINSTRUCTIONS
CALL
RET
Fig. Single Procedure Call
![Page 20: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/20.jpg)
20
Main Line Instructions
RET
Lower levelProcedure
CALL
RET
Procedure
CALL
Next Main LineInstructions
Fig. Nested Procedures Call
![Page 21: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/21.jpg)
21
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
![Page 22: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/22.jpg)
22
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
![Page 23: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/23.jpg)
23
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
![Page 24: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/24.jpg)
24
Contd..
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
![Page 25: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/25.jpg)
25
Direct within-segment NEAR CALL
Operation:
IP Disp16 -- (SP) Return Link
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
Opcode DispLow DispHigh
![Page 26: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/26.jpg)
26
Indirect within-segment NEAR CALL
Operation:
IP Reg16 --(SP) Return Link
IP Mem16 --(SP) Return Link
Opcode mod 010 r/m
![Page 27: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/27.jpg)
27
Direct Inter-Segment FAR CALL
Operation:
CS SegBase
IP Offset
Opcode Offset-low Offset-high
Seg-low Seg-high
![Page 28: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/28.jpg)
28
Indirect Inter-Segment FAR CALL
Operation:
CS SegBase
IP offset
New value of IP and CS is got from memory
Opcode Mem-low Mem-high
![Page 29: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/29.jpg)
29
Contd..
The first word from memory is put in the instruction pointer and the second word from memory is put in the code segment register
![Page 30: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/30.jpg)
30
The 8086 RET Instruction
A return at the end of the procedure copies this value from the stack back to the instruction pointer to return execution to the calling program
When the 8086 does a far call, it saves the contents of both the instruction pointer and the code segment register on the stack
![Page 31: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/31.jpg)
31
Contd..
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
![Page 32: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/32.jpg)
32
Contd..
Operation:
Intra-segment return Inter-segment return
Opcode
![Page 33: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/33.jpg)
35
The 8086 Stack 8086 lets us to set aside entire 64 KB segment of
memory as a stack
The stack segment register 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
![Page 34: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/34.jpg)
36
Contd..
SP register is automatically decremented by 2 before a word is written to the stack
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
![Page 35: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/35.jpg)
38
Contd..
STACK_SEG SEGMENT STACK
DW 40 DUP(0)
STACK_TOP LABEL WORD
STACK_SEG ENDS
CODE SEGMENT
ASSUME CS:CODE, SS:STACK_SEG
![Page 36: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/36.jpg)
39
Contd..
START:MOV AX, STACK_SEGMOV SS, AXLEA SP, STACK_TOP
.
.CODE ENDSEND START
![Page 37: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/37.jpg)
41
Passing Parameters to and from Procedures
Often when we call a procedure, we want to make some data values or addresses available to the procedure
Likewise, we often want a procedure to make some processed data values or addresses available to the main program
![Page 38: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/38.jpg)
42
Contd..
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
![Page 39: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/39.jpg)
43
Contd..
We use a simple BCD-to-Binary program to demonstrate all the 4 types of parameter passing techniques
![Page 40: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/40.jpg)
44
1. Passing Parameters in RegistersDATA SEGMENT
BCD_INPUT DB 17HBIN_VALUE DB ?
DATA ENDS
STACK_SEG SEGMENTDW 40 DUP(0)
TOP_STACK LABEL WORDSTACK_SEG ENDS
![Page 41: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/41.jpg)
45
Contd..
CODE SEGMENTASSUME CS:CODE, DS:DATA, SS:STACK_SEGSTART: MOV AX, DATA
MOV DS, AXMOV AX, STACK_SEGMOV SS, AXMOV SP, OFFSET TOP_STACK; SAME AS LEA SP, TOP_STACK
![Page 42: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/42.jpg)
46
Contd..
MOV AL, BCD_INPUT
CALL BCD_TO_BIN
MOV BIN_VALUE, AL
MOV AH, 4CH
INT 21H
![Page 43: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/43.jpg)
47
Contd..BCD_TO_BIN PROC NEAR
PUSHFPUSH BXPUSH CX; DO THE CONVERSIONMOV BL, ALAND BL, 0FHAND AL, 0F0HMOV CL, 04HSHR AL, CLMOV BH, 0AH
![Page 44: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/44.jpg)
48
Contd..
MUL BHADD AL, BL; End Of Conversion; Binary Result In AL
POP CXPOP BXPOPFRET
BCD_TO_BIN ENDPCODE ENDSEND START
![Page 45: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/45.jpg)
49
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
![Page 46: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/46.jpg)
50
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
![Page 47: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/47.jpg)
51
2. Passing Parameters in General Memory
We can directly access the parameters by name from the procedure
![Page 48: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/48.jpg)
52
Contd..
DATA SEGMENTBCD_INPUT DB 17HBIN_VALUE DB ?
DATA ENDS
STACK_SEG SEGMENTDW 40 DUP(0)
TOP_STACK LABEL WORDSTACK_SEG ENDS
![Page 49: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/49.jpg)
53
Contd..
CODE SEGMENT
ASSUME CS:CODE, DS:DATA, SS:STACK_SEG
START: MOV AX, DATA
MOV DS, AX
MOV AX, STACK_SEG
MOV SS, AX
MOV SP, OFFSET TOP_STACK
; SAME AS LEA SP, TOP_STACK
![Page 50: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/50.jpg)
54
Contd..
CALL BCD_TO_BIN
MOV AH, 4CH
INT 21H
![Page 51: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/51.jpg)
55
Contd..BCD_TO_BIN PROC NEAR
PUSHFPUSH AXPUSH BXPUSH CXMOV AL, BCD_INPUT; DO THE CONVERSIONMOV BL, ALAND BL, 0FHAND AL, 0F0HMOV CL, 04HSHR AL, CLMOV BH, 0AH
![Page 52: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/52.jpg)
56
Contd..MUL BHADD AL, BL; End Of Conversion; Binary Result In ALMOV BIN_VALUE, ALPOP CXPOP BXPOP AXPOPFRET
BCD_TO_BIN ENDPCODE ENDSEND START
![Page 53: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/53.jpg)
57
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
![Page 54: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/54.jpg)
58
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
![Page 55: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/55.jpg)
59
Contd..
DATA SEGMENTBCD_INPUT DB 17HBIN_VALUE DB ?
DATA ENDS
STACK_SEG SEGMENTDW 40 DUP(0)
TOP_STACK LABEL WORDSTACK_SEG ENDS
![Page 56: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/56.jpg)
60
Contd.
CODE SEGMENT
ASSUME CS:CODE, DS:DATA, SS:STACK_SEG
START: MOV AX, DATA
MOV DS, AX
MOV AX, STACK_SEG
MOV SS, AX
MOV SP, OFFSET TOP_STACK
; SAME AS LEA SP, TOP_STACK
![Page 57: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/57.jpg)
61
Contd..
MOV SI, OFFSET BCD_INPUT
; or LEA SI, BCD_INPUT
MOV DI, OFFSET BIN_VALUE
; or LEA DI, BIN_VALUE
CALL BCD_TO_BIN
MOV AH, 4CH
INT 21H
![Page 58: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/58.jpg)
62
Contd..BCD_TO_BIN PROC NEARPUSHFPUSH AXPUSH BXPUSH CXMOV AL, [SI]; DO THE CONVERSIONMOV BL, ALAND BL, 0FHAND AL, 0F0HMOV CL, 04HSHR AL, CLMOV BH, 0AH
![Page 59: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/59.jpg)
63
Contd..MUL BHADD AL, BL; End Of Conversion; Binary Result In ALMOV [DI], ALPOP CXPOP BXPOP AXPOPFRET
BCD_TO_BIN ENDPCODE ENDSEND START
![Page 60: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/60.jpg)
64
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
![Page 61: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/61.jpg)
65
Contd..
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
![Page 62: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/62.jpg)
66
Contd..
DATA SEGMENTBCD_INPUT DB 17HBIN_VALUE DB ?
DATA ENDS
STACK_SEG SEGMENTDW 40 DUP(0)
TOP_STACK LABEL WORDSTACK_SEG ENDS
![Page 63: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/63.jpg)
67
Contd..
CODE SEGMENT
ASSUME CS:CODE, DS:DATA, SS:STACK_SEG
START: MOV AX, DATA
MOV DS, AX
MOV AX, STACK_SEG
MOV SS, AX
MOV SP, OFFSET TOP_STACK
; SAME AS LEA SP, TOP_STACK
![Page 64: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/64.jpg)
68
Contd..
MOV AL, BCD_INPUTPUSH AXCALL BCD_TO_BINPOP AXMOV BIN_VALUE, AL
MOV AH, 4CHINT 21H
![Page 65: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/65.jpg)
69
Contd..BCD_TO_BIN PROC NEAR
PUSHFPUSH AXPUSH BXPUSH CXPUSH BPMOV BP, SPMOV AX, [BP+12]; DO THE CONVERSIONMOV BL, ALAND BL, 0FHAND AL, 0F0HMOV CL, 04HSHR AL, CLMOV BH, 0AH
![Page 66: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/66.jpg)
70
Contd..MUL BHADD AL, BL; End Of Conversion; Binary Result In ALMOV [BP+12], AXPOP BPPOP CXPOP BXPOP AXPOPFRET
BCD_TO_BIN ENDPCODE ENDSEND START
![Page 67: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/67.jpg)
71
REENTRANT PROCEDURESMAIN LINE
CALL MULTIPLY
MULTIPLY PROCEDURE
INTERRUPT PROCEDURE
CALL MULTIPLY
Return to Interrupted Program
Return to Calling Program
Interrupt Occurs here
Next Main LineInstruction after call
![Page 68: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/68.jpg)
72
Contd..
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
![Page 69: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/69.jpg)
73
Recursive Procedures
A recursive procedure is a procedure which calls itself
Recursive procedures are often used to work with complex data structures.
![Page 70: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/70.jpg)
74
Recursive Procedure Example Flow diagram for N=1
MAIN LINE
PROCEDURE FACTO
RET WITH 1
CALL FACTO
![Page 71: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/71.jpg)
75
Contd.. Flow diagram for N=3
MAIN LINE
CALL FACTO
Next Main LineInstruction
Procedure FACTO
Procedure FACTO
Procedure FACTO
RETWITH 1 !
RETWITH 3 !
RETWITH 2 !
CALL CALL
![Page 72: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/72.jpg)
76
Contd..PROCEDURE FACTO
IF N = 1FACTORIAL = 1RET
ELSEREPEAT
DECREMENT NCALL FACTO
UNTIL N=1MULTIPLY (N-1)! X PREVIOUS NRET
![Page 73: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/73.jpg)
77
Contd..
DATA SEGMENT
N DB 06H
FACT DW ?
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
![Page 74: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/74.jpg)
78
Contd..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
![Page 75: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/75.jpg)
79
Contd.. FACTORIAL PROC CMP BX, 1 JE L1
PUSH BX DEC BX CALL FACTORIAL
POP BX MUL BX
L1:RET FACTORIAL ENDP
CODE ENDS END START
![Page 76: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/76.jpg)
80
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
![Page 77: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/77.jpg)
81
Contd..
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
![Page 78: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/78.jpg)
82
Contd..
CODE SEGMENT
ASSUME CS:CODE, DS:DATA, SS:STCK_SEG
:
CALL MULTIPLY_32
:
CODE ENDS
![Page 79: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/79.jpg)
83
Contd..
PROCEDURES SEGMENTMULTIPLY_32 PROC FAR
ASSUME CS:PROCEDURES::
MULTIPLY_32 ENDPPROCEDURES ENDS
![Page 80: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/80.jpg)
84
Contd..
If a procedure is in a different segment from the CALL instruction, you must declare it far with the FAR Assembler Directive
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
![Page 81: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/81.jpg)
85
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
![Page 82: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/82.jpg)
86
Contd..
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
![Page 83: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/83.jpg)
87
Contd.. 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
![Page 84: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/84.jpg)
88
Contd..
To summarize, a procedure or variable declared PUBLIC in one module will be declared EXTRN in modules which access the procedure or variable
![Page 85: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/85.jpg)
89
Defining and Calling a Macro Without ParametersMacro Definition:
MACRO-NAME MACRO
; MACRO DEFINITION
;
ENDM
Invoking a Macro:
MACRO-NAME
![Page 86: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/86.jpg)
90
Example
Macro Definition:CLRSCR MACRO MOV AH, 00H MOV AL, 02H INT 10HENDM
Macro Invocation:CLRSCR
![Page 87: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/87.jpg)
91
Passing Parameters to Macros
Example:
Macro definition:
SETCURSOR MACRO X, Y
MOV DL, Y
MOV DH, X
MOV BH, 00H
MOV AH, 02H
INT 10H
ENDM
![Page 88: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/88.jpg)
92
Contd.. If the macro invocation statement is
SETCURSOR 12, 40 then the macro will be replaced by:
MOV DL, 40
MOV DH, 12
MOV BH, 00H
MOV AH, 02H
INT 10H
![Page 89: Strings, Procedures and Macros](https://reader036.vdocument.in/reader036/viewer/2022062322/568151dd550346895dc0173b/html5/thumbnails/89.jpg)
93
END OF UNIT 2