Lecture35 - 0306-250 Assembly Language Programming Lecture...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 0306-250 Assembly Language Programming Lecture Thirty-Five: Macros Assembly Language Macro • Similar to C/C++ macro – Provides intelligent text substitution • Expanded inline in code • Not called like a subroutine • Provided by most assemblers but syntax varies with each assembler 2 Macro Assembler Directives • MACRO—begin macro definition <label> MACRO <label>—name used to invoke the macro • ENDM—end macro definition ENDM 3 Simple Macro MacroName MACRO Macro instructions and text ENDM • Simple –No labels in macro –No parameters for macro • Creates pseudoinstruction 4 Pseudoinstruction MacroName MACRO Macro instructions and text ENDM • Sequence of instructions –Assembler expands pseudoinstruction (simple macro) in code –Example: Two instances of a macro with 20 instructions ® 40 instructions in program 5 Simple Macro Example diva4: MACRO ; Divides number in A by 4 ; Result is in A asra ; A divided by 2 asra ; A divided by 4 ENDM 6 Simple Macro Usage Example ;Scale array by ¼ using diva4 macro ldx #Array ;Ptr <-- &Array[0] ldab #NUM_ELEMENTS ;Counter <-- NUM_ELEMENTS Loop: ;repeat { ldaa 0,x ; A <--*Ptr diva4 ; A /= 4 staa 1,x+ ; *Ptr <-- *Ptr / 4; Ptr++ dbne b,Loop ;} until (Counter-- == 0) 7 Macro Parameters • Passing parameters offers flexibility Contrast with predetermined, fixed registers of simple macro • Macro parameters are positional Example of usage in macro: • First parameter: \1 • Second parameter: \2 • Up to 35 parameters may be specified \1 – \9 and then \A – \Z Warning: Macro parameters are tricky to read and write 8 Parameter Macro Example div4: MACRO ; Divides number in ; \1 must be A or B ; Result is in \1 asr\1 asr\1 ENDM \1 by 4 for HCS12 instructions ; \1 divided by 2 ; \1 divided by 4 9 Parameter Macro Usage Example ;Scale array by ¼ using div macro ldx #Array ;Ptr <-- &Array[0] ldab #NUM_ELEMENTS ;Counter <-- NUM_ELEMENTS Loop: ;repeat { ldaa 0,x ; A <--*Ptr div4 a ; A /= 4 staa 1,x+ ; *Ptr <-- *Ptr / 4; Ptr++ dbne b,Loop ;} until (Counter-- == 0) 10 Using Labels in Macro • Each label in program must be unique Issue: Macro text repeats for each macro use •Append \@ suffix to label used in macro –Assembler expands each instance of \@ –Expands to unique 5-digit suffix: _nnnnn –Example: Loop\@ • Loop_00001 for Loop\@ in first instance of macro • Loop_00002 for Loop\@ in second instance of macro 11 Example Using Labels in Macro div: MACRO ;Divides number in \1 by power of two \2 ;\1 must be A or B for HCS12 instructions ;Result is in \1 pshy ;save y to use for Count ldy \2 ;Count ß /2 divLoop\@: ;repeat { asr\1 ; \1 divided by 2 dbne y,divLoop\@ ;} until (Count-- == 0) puly ;restore y ENDM 12 Macro Label Usage Example ;Scale array by ¼ using div macro ldx #Array ;Ptr <-- &Array[0] ldab #NUM_ELEMENTS ;Counter <-- NUM_ELEMENTS Loop: ;repeat { ldaa 0,x ; A <--*Ptr div a,#2 ; A /= 4 staa 1,x+ ; *Ptr <-- *Ptr / 4; Ptr++ dbne b,Loop ;} until (Counter-- == 0) 13 ...
View Full Document

This note was uploaded on 05/06/2010 for the course EECC 0306-250 taught by Professor Roymelton during the Fall '10 term at RIT.

Ask a homework question - tutors are online