From ffff6d9bb31efd2b47bedc9ee07cd68b50c12f47 Mon Sep 17 00:00:00 2001 From: Spiderpig86 Date: Tue, 7 Aug 2018 21:48:42 -0400 Subject: feat(mips.html.markdown): Added examples of macros --- mips.html.markdown | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'mips.html.markdown') diff --git a/mips.html.markdown b/mips.html.markdown index 9b176842..e56c9fce 100644 --- a/mips.html.markdown +++ b/mips.html.markdown @@ -287,5 +287,47 @@ hello_world .asciiz "Hello World\n" # Declare a null terminated string jr $ra +## MACROS ## + _macros: + # Macros are extremly useful for substituting repeated code blocks with a + # single label for better readability + # These are in no means substitutes for functions + # These must be declared before it is used + + # Macro for printing new lines (since these can be very repetitive) + .macro println() + la $a0, newline # New line string stored here + li $v0, 4 + syscall + .end_macro + + println() # Assembler will copy that block of + # code here before running + + # Parameters can be passed in through macros. + # These are denoted by a '%' sign with any name you choose + .macro print_int(%num) + li $v0, 1 + lw $a0, %num + syscall + .end_macro + + li $t0, 1 + print_int($t0) + + # We can also pass in immediates for macros + .macro immediates(%a, %b) + add $t0, %a, %b + .end_macro + + immediates(3, 5) + + # Along with passing in labels + .macro print(%string) + la $a0, %string + li $v0, 4 + syscall + .end_macro + print(hello_world) ``` -- cgit v1.2.3