您好,我对移位部分真的很困惑,我知道左移 N 会产生 2^N 的值,但是如何将乘数左移和被乘数右移,得到两者的乘积?
这是计算从控制台输入的两个数字的乘积的代码:
.data
string1: .asciiz "Enter multiplier : "
string2: .asciiz "\nEnter multiplicand : "
.text
li $s0, -1
la $a0, string1
li $v0, 4
syscall
li $v0, 5
syscall
move $a0, $v0
bltzal $a0, Negate
move $t0, $v0
la $a0, string2
li $v0, 4
syscall
li $v0, 5
syscall
move $a0, $v0
bltzal $a0, Negate
move $t1, $v0
li $t2, 1
li $t3, 0
loop:
andi $t5, $t1, 1
bnez $t5, addPartial
shift:
sll $t0,$t0,1
srl $t1,$t1,1
bgtz $t1, loop
done:
beqz $s0, negative_answer
bgtz $s0, positive_answer
bltz $s0, positive_values
Negate:
addiu $s0, $s0, 1
negu $v0, $a0
jr $ra
addPartial:
addu $t3, $t3, $t0
j shift
positive_answer:
move $a0, $t3
li $v0, 1
syscall
li $v0, 10
syscall
negative_answer:
negu $t3, $t3
move $a0, $t3
li $v0, 1
syscall
li $v0, 10
syscall
positive_values:
move $a0, $t3
li $v0, 1
syscall
li $v0, 10
syscall