SHARE
TWEET

Untitled

a guest Oct 17th, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ################################ data segment
  2. .data
  3. name: .asciiz "CSE3666: Lab 6: Saamiya Bhura (sab16198)\n\n"
  4. errmsg: .asciiz "The number is too large.\n"
  5. nl: .asciiz "\n"
  6. str:    .space 128
  7.  
  8. ################################ code segment
  9. .text
  10. .globl main # declare main to be global
  11.  
  12. main:
  13. la $a0, name # load the address of "name" into $a0
  14. li $v0, 4 # system call, type 4, print an string
  15. syscall # system call
  16.  
  17. main_loop:
  18. # use a system call to read a string into str
  19. la $a0, str
  20. li $a1, 128
  21. li $v0, 8
  22. syscall
  23.  
  24. # TODO
  25. # if str[0] is '\n' or 0, exit from the loop.
  26. lb $t0, ($a0) # loading str[0]
  27. beq $t0, 48, Exit # if str[0] = '0' (48 in ASCII), go to Exit
  28. beq $t0, 10, Exit # if str[0] = '\n' (10 in ASCII), go to Exit
  29.  
  30. jal myatoi # call myatoi(str)
  31. # if the return value is the error code, print error message
  32. # otherwise, print the return value in 3 formats
  33.  
  34. # print errmsg
  35. la $a0, errmsg # load address of errmsg in $a0
  36. addi $v0, $0, 4 # add 4 to $v0 to print string
  37. syscall # system call to print string
  38.  
  39. la $a0, nl # load address of n1 into $a0
  40. addi $v0, $0, 4 # add 4 to $v0 to print
  41. syscall # system call to print string
  42.  
  43. j main_loop # jump to main loop
  44.  
  45. no_error:
  46. # print return value in three different formats, separated by a space.
  47. # syscall 11 can be used to print a character (e.g., a space, a new line).
  48.  
  49. addi $s0, $v0, 0 # save return value in $s0
  50. addi $a0, $s0, 0 # $a0 = v
  51. addi $v0, $0, 34 # add 34 to $v0
  52. syscall # system call - print hex
  53.  
  54. addi $a0, $0, 32 # add 32 to $a0 (space in ASCII)
  55. addi $v0, $0, 11 # add 11 to $v0
  56. syscall # system call to print character
  57.  
  58. addi $a0, $s0, 0 # $a0 = v
  59. addi $v0, $0, 36 # add 36 to $v0
  60. syscall # system call to print decimal
  61.  
  62. addi $a0, $0, 32 # add 32 to $a0 (space in ASCII)
  63. addi $v0, $0, 11 # add 11 to $v0
  64. syscall # system call to print character
  65.  
  66. addi $a0, $s0, 0 # $a0 = v
  67. addi $v0, $0, 1 # add 1 to $v0
  68. syscall # system call to print unsigned int
  69.  
  70. la $a0, nl # load address of new line into $a0
  71. addi $v0, $0, 4 # add 4 to $v0
  72. syscall # system call to print string
  73.  
  74. la $a0, nl # load address of new line into $a0
  75. addi $v0, $0, 4 # add 4 to $v0
  76. syscall # system call to print string
  77.  
  78.  
  79. main_continue:
  80. b main_loop
  81.  
  82. Exit: li $v0, 10 # System call, type 10, standard exit
  83. syscall # ...and call the OS
  84.  
  85. # TODO
  86. # your implementation of myatoi
  87. myatoi:
  88. addi $s0, $0, 0 # $s0 = v = 0
  89. addi $s1, $0, 0 # $s1 = i = 0
  90. addi $s2, $0, 0 # $s2 = c = 0
  91. addi $s3, $0, 10 # $s3 = 10
  92.  
  93. sll $t0, $s1, 2 # $t0 = i*4
  94. add $t0, $a0, $t0 # $t0 = &str[i]
  95. lb $s2, ($t0) # $s2 = c = str[i]
  96.  
  97. while:
  98. slti $t0, $s2, 48 # check is c < '0'
  99. bne $t0, $0, my_exit # if true, go to my_exit
  100.  
  101. slti $t0, $s2, 58 # check is c < '9'
  102. beq $t0, $0, my_exit # if false, go to my_exit
  103.  
  104. multu $s0, $s3 # v*10
  105. addi $t1, $s2, -48 # $t1 = c - '0'
  106. mflo $t3 # $t3 = v*10 - lo
  107. mfhi $t4 # t4 = hi
  108. bne $t4, $0, overflow # if $t4 != 0, go to overflow
  109.  
  110. add $s0, $t3, $t1 # v = (v*10) + (c - '0')
  111. sltu $at, $s0, $t0 # $t0 < (v*10)
  112. bne $at, $0, overflow # if true, go to overflow
  113.  
  114. addi $s1, $s1, 1 # i++
  115. add $t0, $a0, $s1 # $t0 = &str[i]
  116. lb $s2, ($t0) # $s2 = c = &str[i]
  117.  
  118. j while # loop
  119.  
  120. overflow:
  121. addi $s0, $0, -1 # add -1 to $s0 (return -1)
  122.  
  123.  
  124. my_exit:
  125. add $v0, $s0, $0 # add $s0 to $v0 (return v)
  126. bne $v0, -1, no_error # if v != -1, go to no error
  127.         jr $ra             # return to calling routine
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top