CS2340-14-Macros

Page 1: Introduction to Macros

Overview

  • Subject: CS2340 Computer Architecture

  • Focus on Macros


Page 2: Macros, Defined

Definition

  • A macro (macro-instruction) defines a single line of code to assemble into one or more machine instructions.

  • Macros can be parameterized, allowing formal parameters to be replaced with actual values when invoked.

  • Differentiation from functions:

    • Macros generate code inline rather than performing function calls.


Page 3: Why Use Macros?

Advantages

  • Efficient for repetitive code: Useful when coding the same few lines frequently.

  • No execution overhead: Unlike function calls, macros do not incur call overhead.

  • Reduces typing: Macros can reduce the amount of code the programmer has to write.

  • Decreases errors: Fewer typed lines lead to less chance of mistakes.

  • Improves readability: Well-placed macros can enhance program clarity.

Caution

  • Avoid overusing macros:

    • Excessive use can result in complex and hard-to-follow code.


Page 4: Creating Macros

Directives

  • Two MIPS directives for defining macros:

    • .macro - Starts the macro definition.

    • .end_macro - Ends the macro definition.


Page 5: Simple Example of a Macro

Non-Parameterized Macro

  • Example for printing a string:

.macro PrintString
 li  $v0,SysPrintString
 syscall
.end_macro

Page 6: Using the Macro

Invocation

  • To print a string, load $a0 with the address of the string, then call the macro:

 la  $a0, prompt
 PrintString
  • Macro expansion translates this to:

 li $v0, SysPrintString
 syscall

Page 7: Better Example

Improved Macro Definition

  • A macro that includes the address load directly:

.macro PrintString(%string)
 la $a0, %string
 li  $v0, SysPrintString
 syscall
.end_macro

Page 8: Using the Improved Macro

Invocation with Parameter

  • This allows for a specific prompt definition:

 PrintString(prompt)
  • Expansion generates:

 la $a0, prompt
 li  $v0, SysPrintString

Page 9: Notes on Macros

Key Points

  • Must define macros before using them.

  • Macro definitions are local to the file: Best to store them in their own file and include as needed.

  • Example of including macros:

 .include "SysCalls.asm"
 .include "demoinc.asm"
 .include "macros.asm"

Page 10: Nested Macros

Calling Other Macros

  • Macros can call other previously defined macros.

  • Name differentiation:

    • Two macros with the same name but different parameter counts are treated as distinct.


Page 11: Label Naming Conventions

Unique Labeling

  • Labels in a macro will change during expansion.

  • The name will append _M#, where # is a unique identifier for each expansion.

  • Example of expanded code:

    • The original macro may not show, but its effects will be visible in the debugger.


Page 12: Example Macro Definition

Clear Buffer Macro

  • Macro for clearing a buffer:

.macro clear_buf(%buf,%len)
 li $t0, ' '
 la $t1, %buf
 li $t2, %len
 clearl:
 beqz $t2, clearx
 sb $t0, ($t1)
 addi $t1, $t1, 1
 subi $t2, $t2, 1
 b clearl
 clearx:
.end_macro
  • Use:

 clear_buf buffer, BUFSIZE

Page 13: Macro Expansion Notes

Label Changes and Substitutions

  • Labels within the macro are altered during expansion.

  • Values, such as BUFSIZE, are replaced with their definitions before macro expansion, enhancing clarity and maintainability.