|
|
|
|
|
|
|
|
MIPS Multiplication
To multiply two numbers in assembly we can use right and left shifts.
You can see the procedure below: Multiplication: # $a0 - Multiplicand # $a1 - Multiplier # $v0 - Result # $t0 - The mask for the right bit # $t1 - The LSB of the multiplier li $v0, 0 # Initialize the result register li $t0, 1 # Initialize the mask li $t1, 0 # Initialize the LSB result Multiplication_loop: beq $a1, $zero, Multiplication_end # If the multiplier is zero we finished and $t1, $t0, $a1 # Get the LSB beq $t1, 1, Multiplication_do_add # If the LSB is not zero add the multiplicand to the result beq $t1, 0, Multiplication_do_shift # If the LSB is zero add just do the shifts Multiplication_do_add: addu $v0, $v0, $a0 Multiplication_do_shift: sll $a0, $a0, 1 # Shift left the multiplicand srl $a1, $a1, 1 # Shift right the multiplier j Multiplication_loop # Back to the loop Multiplication_end: jr $ra # Return with the result in $v0 register |
All rights reserved to Tovi Levis. DO NOT copy or redistribute any content from this site.
|
Direct link: http://www.tovilevis.com
|