Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 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
  45. add $s1, $zero, $zero # store argument index
  46. printf_loop:
  47. lbu $a0, 0($s0)
  48. beqz $a0, printf_ret
  49. beq $a0, '\\', printf_format
  50. # print the character
  51. li $v0, 11
  52. syscall
  53. j printf_last
  54. printf_format:
  55. addi $s1, $s1, 1 # increase argument index
  56. mul $t0, $s1, 4
  57. add $t0, $t0, $fp # all print type assumes
  58. # the latest argument pointer at $t0
  59. addi $s0, $s0, 1
  60. lbu $a0, 0($s0)
  61. beq $a0, 'd', printf_int
  62. beq $a0, 's', printf_str
  63. printf_int:
  64. lw $a0, 0($t0)
  65. li $v0, 1
  66. syscall
  67. j printf_last
  68. printf_str:
  69. lw $a0, 0($t0)
  70. li $v0, 4
  71. syscall
  72. j printf_last
  73. printf_last:
  74. addi $s0, $s0, 1 # move to next character
  75. j printf_loop
  76. printf_ret:
  77. #restore RTE
  78. lw $fp, 24($sp)
  79. lw $ra, 20($sp)
  80. lw $a0, 16($sp)
  81. lw $s0, 12($sp)
  82. lw $s1, 8($sp)
  83. addi $sp, $sp, 24
  84. jr $ra
  85. .globl main
  86. main:
  87. # push the arguments
  88. # in reverse order of the sequence
  89. # in the format
  90. lw $t0, int_a
  91. push($t0)
  92. la $t0, str_b
  93. push ($t0)
  94. # load the format in argument
  95. # and call printf
  96. la $a0, format1
  97. jal printf
  98. # pop the arguments
  99. pop($t0)
  100. pop($t0)
  101.  
  102. # print the next string
  103. lw $t0, int_year
  104. push($t0)
  105. la $t0, str_food
  106. push($t0)
  107. lw $t0, int_age
  108. push($t0)
  109. la $t0, str_name
  110. push($t0)
  111. la $a0, format2
  112. jal printf
  113. pop($t0)
  114. pop($t0)
  115. pop($t0)
  116. pop($t0)
  117.  
  118. la $a0, format3
  119. jal printf
  120. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement