Title: Assembly Language Week 10 ' Modular programming and Macro '
1Assembly LanguageWeek 10 .Modular
programming and Macro .
2Modular program
- Assembly Programming can use Modular
- Design which can help to develop a big
- program because it will separate into many
- part of procedure in many file that can link
- all together in the final.
- By using EXTRN and PUBLIC command
3How to use EXTRN and PUBLIC
Must Call P1 from other file
MAIN.ASM
P1.ASM
CODE SEGMENT ASSUME CSCODE MAIN PROC
FAR EXTRN P1FAR . . CALL P1 CALL
TERMINATE MAIN ENDP TERMINATE PROC
MOV AX,04C00H INT 21H TERMINATE
ENDP CODE ENDS END MAIN
CODE SEGMENT PUBLIC P1 ASSUME
CSCODE P1 PROC FAR . . . RET P1
ENDP CODE ENDS END P1
P1 in this file is public.
4How to Compile EXTRN and PUBLIC
Compile each ASM with TASM to get OBJ files
C\gtTASM MAIN.ASM C\gtTASM P1.ASM C\gtLINK
MAIN.OBJ P1.OBJ C\gtMAIN.EXE
Link all OBJ files together by LINK xx.obj
xx.obj ...
RUN
5Example of EXTRN and PUBLIC
STSEG SEGMENT STACK DB 64 DUP(?) STSEG
ENDS CODE SEGMENT ASSUME
CSCODE,SSSTSEG MAIN PROC FAR EXTRN
DISPBINFAR SUB AL,AL AGAIN PUSH AX
CALL DISPBIN MOV DL,20H
MOV AH,2 INT 21H MOV AH,2 INT
21H POP AX INC AL JNZ
AGAIN CALL TERMINATE MAIN
ENDP TERMINATE PROC MOV AX,04C00H
INT 21H TERMINATE ENDP CODE ENDS END MAIN
TESTBIN.ASM
"TESTBIN.ASM" is the program to print set of
BINARY number. This program cooperation with
DISPBIN.ASM Follow this to Run
this program C\gtTASM TESTBIN.ASM
C\gtTASM DISPBIN.ASM
C\gtLINK TESTBIN.OBJ
DISPBIN.OBJ C\gtTESTBIN.EXE
Call DISPBIN from other file
6Example of EXTRN and PUBLIC (cont.)
CODE SEGMENT PUBLIC DISPBIN
ASSUME CSCODE DISPBIN PROC FAR MOV
CX,8 NEXT SHL AL,1 PUSH AX
JC ITIS1 MOV DL,30H JMP
SAY01 ITIS1 MOV DL,'1' SAY01 MOV AH,2
INT 21H POP AX LOOP NEXT
RET DISPBIN ENDP CODE ENDS END
DISPBIN
DISPBIN.ASM
"DISPBIN.ASM" is the program to print set of
BINARY number. This program cooperation with
TESTBIN.ASM Follow this to Run this
program C\gtTASM TESTBIN.ASM
C\gtTASM DISPBIN.ASM
C\gtLINK TESTBIN.OBJ
DISPBIN.OBJ
C\gtTESTBIN.EXE
DISPBIN is public procedure
7Macro
- Macro is a set of assembly language statements
given a symbolic name. - Macro is invoked, not called. A copy of the macro
is inserted directly into the program - After being defined, the assembler will
substitute those statements whenever it finds the
symbolic name
8Example of Macro
- Lets define macros to input and print a
character - mInputChar macro mov ah, 1 int
21hendmmPutChar macro mov ah, 2 int
21hendm
Macro for get input a character and store in AL
Macro for print a character in dl to screen
9Example of Macro (cont.)
- Code segment
- .
- .
- mInputCharmov dl, almPutChar
- .
- .
- Code ends
- End start
Results in the code . . mov ah,
1 int 21h mov dl, al mov ah, 2
int 21h Observe the text substitution
10Macro with parameter
- Macros can have parameters. The text for the
actual parameter is substituted for the name of
the dummy parameter when the macro is invoked.
11Example of Macro with parameter
- mInputChar macro aChar push ax mov ah,
1 int 21h mov aChar, al pop ax - endm
12Example of Macro with parameter (cont.)
- Code segment
- .
- .
- mInputChar blmInputChar aCh
- .
- .
- Code ends
- End start
13Example of Macro with parameter
- This macro is used to create a buffer
- for function 0Ah, int 21h
- mInputBuf macro len InBuf label byte
MaxLen db len InLen db ? Buf
db len dup (?) endm - Then mInputBuf 20
- expands to InBuf label byte MaxLen db
20 InLen db ? Buf db 20 dup (?)
14Macros vs. Procedures
- Similarities Both
- Simplify program writing
- Encourage reuse of code
- Improve program readability
- Provide better structure to programs
15Macros vs. Procedures (cont.)
16Macros vs. Procedures (cont.)
- Use macros for short code segments, procedures
for longer ones.
17Local Label
- 8088 lacks a "near" jump loop instruction. Lets
try to create it. - mNearLoop macro repeatAt loop L1
jmp L2 L1 jmp repeatAt L2 - endm
18Local Label (cont.)
- Now lets use it Loop1
mNearLoop Loop1 - This expands to Loop1
loop L1 jmp L2 L1 jmp Loop1 L2
19Local Label (cont.)
- Now lets use it again in the same programLoop2
mNearLoop Loop2 - This expands to Loop2
loop L1 jmp L2 L1 jmp Loop2
L2
20Local Label (cont.)
- Solution
- mNearLoop macro repeatAt local L1, L2
loop L1 jmp L2 L1 jmp
repeatAt L2endm
21Local Label (cont.)
- Now lets use itLoop1
mNearLoop Loop1 - This expands to Loop1
loop ??0000 jmp ??0001 ??0000 jmp
Loop1 ??0001
22General format
- macroname macro param1, param2, statement
listendm - Use "" for comments that are not copied every
time the macro is used. - Use REQ for required parameters
- Terminology We may "invoke", "expand", or
"call" a macro