Title: Computer Systems
1Computer Systems
2Macros
- Macro a group of instructions that can be
invoked as needed. - Similar to subroutine, isnt it?
- Difference between macro and subroutine
- Macro Code is in-line expanded at assembly time
for each call to the macro - Subroutine Invoked at run time using call/return
mechanism, only one copy of code is generated
3Macros (contd)
- Written similar to a subroutine
- Invocation Use the name (label) of macro
- First line Name MACRO
- At the end ENDM instead of RTS is used
- Parameters
- Labels (special treatment needed)
- Conditional Assembly
- Structured Control (not supported by our
simulator, though ( )
4Macro Example
- Example 1 (No parameter, no label)
- Definition
- D0 ? (D0(07)7) x 12
- AddMul MACRO
- ADD.B 7,D0
- AND.W 00FF,D0
- MULU 12,D0
- ENDM
- Invocation
- MOVE.B VAL2,D0
- AddMul
- MOVE.B VAL3,D0
- AddMul
5Macro Example (contd)
- Example 1 (No parameter, no label)
- Expansion Code of AddMul expanded at the place
of invocation -
- MOVE.B VAL2,D0
- ADD.B 7,D0
- AND.W 00FF,D0
- MULU 12,D0
- MOVE.B VAL3,D0
- ADD.B 7,D0
- AND.W 00FF,D0
- MULU 12,D0
6Macro Example (contd)
- Example 2 (Parameter, no label)
- Definition \1, \2, , \n 1st, 2nd, , nth
parameter, resp. - Dx(07) ? (Dx(07)7) x 12
- AddMul MACRO
- ADD.B 7,\1
- AND.W 00FF,\1
- MULU 12,\1
- ENDM
- Invocation
- MOVE.B VAL2,D0
- AddMul D0 D0?(D07)x12
- MOVE.B VAL3,D1
- AddMul D1 D1?(D17)x12
7Macro Example (contd)
- Example 2 (Parameter, no label)
- Expansion Code of AddMul expanded at the place
of invocation, \1 is replaced by the 1st
parameter -
- MOVE.B VAL2,D0
- ADD.B 7,D0
- AND.W 00FF,D0
- MULU 12,D0
- MOVE.B VAL3,D1
- ADD.B 7,D1
- AND.W 00FF,D1
- MULU 12,D1
8Macros and Labels
- Labels can be used in the definition of a macro.
- Problem
- When the macro is invoked more than once, we will
have an error - Why?
- Solution
- Write a label as ltstringgt\_at_
- The label will be replaced by ltstringgt.nnn by
assembler - ? No duplicate
9Macros and Labels - Example (P. 343)
- OPT MEX
- ORG 400
- SUM MACRO
- CLR.W \3
- ADDQ.W 1,\2
- SUM1\_at_ ADD.W \1,\3
- ADD.W 1,\1
- CMP.W \1,\2
- BNE SUM1\_at_
- ENDM
- MOVE.W 1,D1
- MOVE.W 10,D2
- SUM D1,D2,D3
- MOVE.W 5,D3
- MOVE.W 10,D4
- SUM D3,D4,D5
- STOP 2700
- end 400
10- 1 ( 1)
OPT MEX - 2 00000400 ( 2)
ORG 400 - 3 ( 3) SUM
MACRO - 4 ( 4)
CLR.W \3 - 5 ( 5)
ADDQ.W 1,\2 - 6 ( 6) SUM1\_at_
ADD.W \1,\3 - 7 ( 7)
ADD.W 1,\1 - 8 ( 8)
CMP.W \1,\2 - 9 ( 9)
BNE SUM1\_at_ - 10 ( 10)
ENDM - 11 00000400 323C0001 ( 11)
MOVE.W 1,D1 - 12 00000404 343C000A ( 12)
MOVE.W 10,D2 - 13 00000408 ( 13)
SUM D1,D2,D3 - 14 00000408 4243 ( 13)
CLR.W D3 - 15 0000040A 5242 ( 13)
ADDQ.W 1,D2 - 16 0000040C D641 ( 13) SUM1.000
ADD.W D1,D3 - 17 0000040E 5241 ( 13)
ADD.W 1,D1 - 18 00000410 B441 ( 13)
CMP.W D1,D2 - 19 00000412 66F8 ( 13)
BNE SUM1.000
11Assembler
- Two-pass assembler
- Source program scanned twice before producing
the object code - Pass I
- Search source program for symbol definitions and
enter these into symbol table - Pass II
- Use symbol table constructed in Pass I and
op-code table to generate machine code equivalent
to source
12Pass I (Simplified)
START
LC lt- 0
Fetch Next Instruction
Y
END?
PASS II
N
Add Label to Symbol Table w/ LC as its
value (or EQU value)
Y
Label?
Increment LC Accordingly
N
13Pass II (Simplified)
START
LC lt- 0
Fetch Next Instruction
Y
END?
STOP
N
Increment LC Accordingly
Generate Machine Code
Symbol Table Lookup
14Example
1 OPT CRE
2 00000019 A EQU 25
3 00001000 ORG 1000
4 00001000 00000004 M DS.W 2
5 00001004 00001008 N DC.L EXIT
6 00001008 2411 EXIT MOVE.L
(A1),D2 7 0000100A 139A2000
MOVE.B (A2),(A1,D2) 8 0000100E 06450019
ADDI.W A,D5 9 00001012
67000008 BEQ DONE 10
00001016 90B81004 SUB.L N,D0 11
0000101A 60EC BRA EXIT
12 0000101C 4E722700 DONE STOP
2700 13 00001000 END
1000 Lines 13, Errors 0, Warnings
0. SYMBOL TABLE INFORMATION Symbol-name Type
Value Decl Cross reference line
numbers A EQU 00000019 2
8. DONE LABEL 0000101C 12
9. EXIT LABEL 00001008 6 5,
11. M LABEL 00001000 4
NOT USED N LABEL 00001004
5 10.