Advertisement
CSenshi

MIPS - macros

May 11th, 2019
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # macro to end program
  2. .macro done
  3.     li $v0,10
  4.     syscall
  5. .end_macro
  6.  
  7. # print int
  8. .macro print_int ($addr)
  9.     li $v0, 1
  10.     add $a0, $zero, $addr
  11.         syscall
  12. .end_macro
  13.  
  14. # print char
  15. .macro print_char ($addr)
  16.     li $v0, 11
  17.     add $a0, $zero, $addr
  18.         syscall
  19. .end_macro
  20.  
  21. # print string from data
  22. .macro print_str ($str)
  23.     li $v0, 4
  24.     la $a0, $str
  25.     syscall
  26. .end_macro
  27.  
  28. # print string from register
  29. .macro print_str_addr (%str)
  30.     li $v0, 4
  31.     la $a0, (%str)
  32.     syscall
  33. .end_macro
  34.  
  35. .macro read_char($str, $ind, $sv)
  36.     la $s2, ($str)
  37.     addu $s2,$s2,$ind   # $a1 = &str[x].  assumes x is in $s0
  38.     lbu $sv,($s2)      # read the character
  39. .end_macro
  40.  
  41.  
  42. .macro strlen(%str, $addr)
  43.     la $s0 (%str)
  44.  
  45.     loop:
  46.         lb   $s1 0($s0)
  47.         beq  $s1 $zero end
  48.    
  49.         addi $s0 $s0 1
  50.         addi $addr $addr 1
  51.         j loop
  52.    
  53.     end:
  54.  
  55.     addi $addr $addr -1
  56. .end_macro
  57.  
  58. # read register and save in register
  59. .macro read_str($addr)
  60.     li $v0, 8       # take in input
  61.  
  62.         la $a0, buffer  # load byte space into address
  63.         li $a1, 200      # allot the byte space for string
  64.     move $t0, $a0   # save string to t0
  65.     syscall
  66. .end_macro
  67.  
  68. .macro set_char($addr, $chr, $offset)
  69.     sb $chr, buffer($offset)
  70.  
  71. .end_macro
  72.  
  73. # generic looping mechanism
  74. # for ($t0, 1, 10, body)
  75. .macro for (%regIterator, %from, %to, %bodyMacroName)
  76.     add %regIterator, $zero, %from
  77.     Loop:
  78.     %bodyMacroName ()
  79.     add %regIterator, %regIterator, 1
  80.     ble %regIterator, %to, Loop
  81. .end_macro
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement