Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.99 KB | None | 0 0
  1. sprintf1:
  2. li $v0,1 # return: length of string is 1
  3. jr $ra
  4.  
  5. sprintf:
  6. move $s0, $a0
  7. move $s1, $a1
  8. move $s2, $a2
  9. move $s3, $a3
  10. li $s4, 0
  11. j sprintfloop
  12.  
  13. sprintfloop:
  14. lb $t0, 0($s1)
  15. addi $s1, $s1, 1
  16. beq $t0, '%', checker
  17.  
  18. sb $t0, 0($s0)
  19. addi $s0, $s0, 1
  20. addi $s4, $s4, 1
  21. bne $t0, $0, sprintfloop
  22. jr $ra
  23.  
  24. checker:
  25. lb $t0, 0($s1)
  26.  
  27. beq $t0, 'd', printd
  28. beq $t0, 'x', sprintx
  29. beq $t0, 's', strcpy
  30. #beq $t0, 'c' sputc
  31. #beq $t0, '%', sputc
  32. j sprintfloop
  33.  
  34. #######
  35. # Helper functions
  36.  
  37. # void printd(int n)
  38. #printd:
  39. # addi $sp,$sp,-12 #
  40. # sw $ra,8($sp) #
  41. # sw $s0,4($sp) # save the old value of $s0
  42. # sw $s1,0($sp) # save the old value of $s1
  43. #
  44. # li $s1,10
  45. # rem $s0,$a0,$s1 # value % 10
  46. # addi $s0,$s0,'0' # convert that to a digit
  47. #
  48. # div $a0,$a0,$s1 # value / 10
  49. #
  50. # beqz $a0,onedig # is it non-zero?
  51. # jal printd # if so, print it
  52.  
  53.  
  54. #onedig: move $a0,$s0 # print the digit we built
  55. # li $v0,11 # print_char
  56. # syscall
  57. # lw $s1,0($sp)
  58. # lw $s0,4($sp)
  59. # lw $ra,8($sp)
  60. # addi $sp,$sp,12
  61. # jr $ra
  62. # end of printd
  63.  
  64. printd:
  65. addi $sp, $sp, -8 #
  66. sw $ra, 0($sp) #
  67. sw $s0, 4($sp) # save the old value of $s0
  68.  
  69. li $t9, 10 # constant, used for div and rem
  70. rem $s0, $s2, $t9 # value % 10
  71. addi $s0, $s0, '0' # convert that to a digit
  72.  
  73. div $a0, $a0, $t9 # value / 10
  74.  
  75. beqz $a0, sonedig # is it non-zero?
  76. jal printd # if so, print it
  77.  
  78. sonedig:
  79.  
  80. li $v0, 11 # print_char
  81. move $a0, $s0
  82. syscall
  83.  
  84. lw $s0, 4($sp)
  85. lw $ra, 0($sp)
  86. addi $sp, $sp, 8
  87. # move $s2, $s3
  88. jr $ra
  89.  
  90. strcpy:
  91. lb $t0, 0($s1)
  92. addi $s1, $s1, 1
  93. sb $t0, 0($s0)
  94. addi $s0, $s0, 1
  95. bne $t0, $0, strcpy
  96. jr $ra
  97. # void printx(int value)
  98. #
  99. sprintx:
  100. addi $sp, $sp, -8 #
  101. sw $ra, 0($sp) #
  102. sw $s0, 4($sp) # save the old value of $s0
  103.  
  104. li $t9, 16 # constant, used for div and rem
  105. rem $s0, $a0, $t9 # value % 16
  106. addi $s0, $s0, '0' # convert that to a digit
  107.  
  108. div $a0, $a0, $t9 # value / 16
  109.  
  110. beqz $a0, xonedig # is it non-zero?
  111. jal sprintx # if so, print it
  112.  
  113. xonedig: li $v0, 11 # print_char
  114. move $a0, $s0
  115. syscall
  116.  
  117. lw $s0, 4($sp)
  118. lw $ra, 0($sp)
  119. addi $sp, $sp, 8
  120. jr $ra
  121. # void bzero(char *buf, int n)
  122. bzero: beq $a1,$0,bzexit # while(n>0) {
  123. sb $0,0($a0) # *s = 0
  124. addi $a0,$a0,1 # s++
  125. addi $a1,$a1,-1 # n--
  126. j bzero # }
  127. bzexit: jr $ra
  128. # end of bzero
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement