Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. .data
  2. num: .word , -32000, 0 #a, b and result ## 32767 is max 16 bit number ## 2147483647 is max 32 bit number
  3. result: .asciiz "The result is: "
  4. error: .asciiz "Multiplication error."
  5. .text
  6.  
  7. main:
  8.  
  9. la $a0, num #Load base addres of the array (num)
  10. jal multiplication #Jump to multiplication procedure
  11.  
  12. #When multiplication ends, program returns to this point
  13.  
  14. beq $v0, $zero, multIsNotOk #If multiplication procedure returns zero, program jumps to multIsNotOk label
  15. #Otherwise(which means it returns 1) continue
  16.  
  17. lw $t6, 8($a0) #Load the result of multiplication from the memory
  18. li $v0, 4 #Printing the array "The result is: "
  19. la $a0, result
  20. syscall
  21.  
  22. li $v0, 1 #Printing the result
  23. move $a0, $t6
  24. syscall
  25. li $v0, 10 #End
  26. syscall
  27.  
  28. multIsNotOk:
  29. li $v0, 4 #Printing the array "Multiplication error."
  30. la $a0, error
  31. syscall
  32. li $v0, 10 #End
  33. syscall
  34.  
  35. multiplication:
  36.  
  37. lw $t0, 0($a0) #Load first number
  38. lw $t1, 4($a0) #Load second number
  39. lw $t5, 8($a0) #Load result
  40.  
  41. add $t3, $zero, $t0 #Copy "original" value of t0 in case it is negative and we need change result sign
  42.  
  43. ##If the first number is negative, we take absolute vale of it. That's becouse if we want to handle with negative values
  44. ## a problem with sll may occur.
  45.  
  46. blt $t0, -32768, exitIfMultIsNotOk
  47. blt $t1, -32768, exitIfMultIsNotOk
  48. bgt $t0, 32767, exitIfMultIsNotOk
  49. bgt $t1, 32767, exitIfMultIsNotOk
  50.  
  51. bgt $t0, $zero, isPositive
  52.  
  53. abs $t0, $t0
  54.  
  55. isPositive:
  56.  
  57.  
  58.  
  59.  
  60. while:
  61.  
  62. andi $v0, $t0, 1 #Check if first number is even
  63. beq $v0, 0, even
  64.  
  65. add $t5, $t5, $t1 #If the number is even skip this step, go to procedure "even"
  66.  
  67. even:
  68.  
  69. beq $t0 1 exitIfMultIsOk #Check if first number is equal to one, if true exit loop
  70. srl $t0 $t0 1 #Divide t0 by 2 without remainder
  71. sll $t1 $t1 1 #Multiply t1 by 2
  72. j while #Back to the beginning of the loop
  73.  
  74. exitIfMultIsOk:
  75.  
  76. addi $v0 $zero 1
  77. bgt $t3, $zero, wasPositive #Checking if first value is positive. If it is skip this step, otherwise we need to change result sign
  78. sub $t5, $zero, $t5
  79. wasPositive:
  80. sw $t5 8($a0) #Save the result to the array
  81. jr $ra #Back to the main procedure
  82.  
  83. exitIfMultIsNotOk:
  84.  
  85. move $v0 $zero
  86. jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement