Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- .data
- num: .word , -32000, 0 #a, b and result ## 32767 is max 16 bit number ## 2147483647 is max 32 bit number
- result: .asciiz "The result is: "
- error: .asciiz "Multiplication error."
- .text
- main:
- la $a0, num #Load base addres of the array (num)
- jal multiplication #Jump to multiplication procedure
- #When multiplication ends, program returns to this point
- beq $v0, $zero, multIsNotOk #If multiplication procedure returns zero, program jumps to multIsNotOk label
- #Otherwise(which means it returns 1) continue
- lw $t6, 8($a0) #Load the result of multiplication from the memory
- li $v0, 4 #Printing the array "The result is: "
- la $a0, result
- syscall
- li $v0, 1 #Printing the result
- move $a0, $t6
- syscall
- li $v0, 10 #End
- syscall
- multIsNotOk:
- li $v0, 4 #Printing the array "Multiplication error."
- la $a0, error
- syscall
- li $v0, 10 #End
- syscall
- multiplication:
- lw $t0, 0($a0) #Load first number
- lw $t1, 4($a0) #Load second number
- lw $t5, 8($a0) #Load result
- add $t3, $zero, $t0 #Copy "original" value of t0 in case it is negative and we need change result sign
- ##If the first number is negative, we take absolute vale of it. That's becouse if we want to handle with negative values
- ## a problem with sll may occur.
- blt $t0, -32768, exitIfMultIsNotOk
- blt $t1, -32768, exitIfMultIsNotOk
- bgt $t0, 32767, exitIfMultIsNotOk
- bgt $t1, 32767, exitIfMultIsNotOk
- bgt $t0, $zero, isPositive
- abs $t0, $t0
- isPositive:
- while:
- andi $v0, $t0, 1 #Check if first number is even
- beq $v0, 0, even
- add $t5, $t5, $t1 #If the number is even skip this step, go to procedure "even"
- even:
- beq $t0 1 exitIfMultIsOk #Check if first number is equal to one, if true exit loop
- srl $t0 $t0 1 #Divide t0 by 2 without remainder
- sll $t1 $t1 1 #Multiply t1 by 2
- j while #Back to the beginning of the loop
- exitIfMultIsOk:
- addi $v0 $zero 1
- bgt $t3, $zero, wasPositive #Checking if first value is positive. If it is skip this step, otherwise we need to change result sign
- sub $t5, $zero, $t5
- wasPositive:
- sw $t5 8($a0) #Save the result to the array
- jr $ra #Back to the main procedure
- exitIfMultIsNotOk:
- move $v0 $zero
- jr $ra
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement