Advertisement
Guest User

minus 1

a guest
Mar 18th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. .include "./cs47_macro.asm"
  2.  
  3. .data
  4. .align 2
  5. format1: .asciiz "My string is %s and integer is %d \n"
  6. int_a: .word 0x10
  7. str_b: .asciiz "'test printf'"
  8. format2: .asciiz "My name is %s\nI am %d year old\nI love to eat %s\nI will graduate in %d\n"
  9. str_name: .asciiz "John Adams"
  10. int_age: .word 26
  11. str_food: .asciiz "pizza"
  12. int_year: .word 2015
  13. format3: .asciiz "My name is \\%s\nI am \\%d year old\nI love to eat \\%s\nI will graduate in \\%d\n"
  14.  
  15. .text
  16. #-----------------------------------------------
  17. # C style signature 'printf(<format string>,<arg1>,
  18. # <arg2>, ... , <argn>)'
  19. #
  20. # This routine supports %s and %d only
  21. #
  22. # Argument: $a0, address to the format string
  23. # All other addresses / values goes into stack
  24. #-----------------------------------------------
  25. #-----------------------------------------------
  26. # C style signature 'printf(<format string>,<arg1>,
  27. # <arg2>, ... , <argn>)'
  28. #
  29. # This routine supports %s and %d only
  30. #
  31. # Argument: $a0, address to the format string
  32. # All other addresses / values goes into stack
  33. #-----------------------------------------------
  34. printf:
  35. #store RTE - 5 *4 = 20 bytes
  36. addi $sp, $sp, -24
  37. sw $fp, 24($sp)
  38. sw $ra, 20($sp)
  39. sw $a0, 16($sp)
  40. sw $s0, 12($sp)
  41. sw $s1, 8($sp)
  42. addi $fp, $sp, 24
  43. # body
  44. move $s0, $a0 # save the argument $s0 = argument
  45. add $s1, $zero, $zero # store argument index $s1 = 0
  46. printf_loop:
  47. lbu $a0, 0($s0) # $a0 = character stored in address at $s0
  48. beqz $a0, printf_ret # if ($a0 == '\0') goto printf_ret
  49. beq $a0, '\\' printf_raw
  50. beq $a0, '%', printf_format # if (a0 == '%') goto printf_format
  51.  
  52. li $v0, 11 # print the character
  53. syscall
  54. j printf_last
  55. printf_raw:
  56. li $v0, 11
  57. syscall
  58.  
  59. addi $s0, $s0, 1
  60. lbu $a0, 0($s0)
  61. beqz $a0, printf_ret
  62. j printf_raw
  63. printf_format:
  64. addi $s1, $s1, 1 # increase argument index ($s1 = $s1 + 1)
  65. mul $t0, $s1, 4 # ($t0 = $s1 * 4)
  66. add $t0, $t0, $fp # all print type assumes ($t0 = $t0 + $fp)
  67. # the latest argument pointer at $t0 = ($s1 * 4) + $fp
  68. addi $s0, $s0, 1 # $s0 = $s0 + 1
  69. lbu $a0, 0($s0) # $a0 = character as pointed by $s0
  70. beq $a0, 'd', printf_int # if($a0 == 'd') goto printf_int
  71. beq $a0, 's', printf_str # if($a0 == 's') goto printf_str
  72. printf_int:
  73. lw $a0, 0($t0) # $a0 = Read from memory at $t0
  74. li $v0, 1
  75. syscall # print integer value at $a0
  76. j printf_last
  77. printf_str:
  78. lw $a0, 0($t0) # ($t0 is pointing toward memory) (syscall code 4 is printing a string) $a0 = Read from memory at $t0
  79. li $v0, 4
  80. syscall
  81. j printf_last
  82. printf_last:
  83. addi $s0, $s0, 1 # move to next character ($s0 = $s0 + 1)
  84. j printf_loop
  85. printf_ret:
  86. #restore RTE
  87. lw $fp, 24($sp)
  88. lw $ra, 20($sp)
  89. lw $a0, 16($sp)
  90. lw $s0, 12($sp)
  91. lw $s1, 8($sp)
  92. addi $sp, $sp, 24
  93. jr $ra
  94. .globl main
  95. main:
  96. # push the arguments
  97. # in reverse order of the sequence
  98. # in the format
  99. lw $t0, int_a
  100. push($t0)
  101. la $t0, str_b
  102. push ($t0)
  103. # load the format in argument
  104. # and call printf
  105. la $a0, format1
  106. jal printf
  107. # pop the arguments
  108. pop($t0)
  109. pop($t0)
  110.  
  111. # print the next string
  112. lw $t0, int_year
  113. push($t0)
  114. la $t0, str_food
  115. push($t0)
  116. lw $t0, int_age
  117. push($t0)
  118. la $t0, str_name
  119. push($t0)
  120. la $a0, format2
  121. jal printf
  122.  
  123. la $a0, format3
  124. jal printf
  125. pop($t0)
  126. pop($t0)
  127. pop($t0)
  128. pop($t0)
  129.  
  130. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement