在MIPS汇编中使用逻辑移位进行乘法 [英] Multiplication using Logical shifts in MIPS assembly
问题描述
有人可以给我一些指导,告诉我如何制作使用MIPS汇编中的移位倍增的代码吗?我不明白拥有2 ^ n的数字如何帮助我使用奇数被乘数进行乘法
Can someone please give me pointers on how I can go about making a code that multiplies using shifts in MIPS assembly? I don't understand how having a number 2^n can help me multiply using an odd multiplicand
我目前有此代码,我正在尝试制作一个计算器
I currently have this code, I'm trying to make a calculator
.text
li $v0, 4
la $a0, ask_1
syscall
li $v0,5
syscall
move $s1, $v0
li $v0, 4
la $a0, ask_2
syscall
li $v0,5
syscall
move $s2, $v0
#sll $s2, $s2, 3 #$s2 * $s2^3 = result
srl $s2, $s2, 1
li $v0, 1
la $a0, ($s2)
syscall
.data
ask_1: .asciiz "Enter Multiplier\n"
ask_2: .asciiz "Enter Multiplicand\n"
result: .asciiz "The Answer is:\n"
推荐答案
将数字左移n位将数字乘以2 n .例如n << 3 = n*2^3 = n*8
.相应的说明是
Shifting a number n bits left multiples the number by 2n. For example n << 3 = n*2^3 = n*8
. The corresponding instruction is
SLL $s1, $s2, 1
要乘以任何数字,您可以将数字分成2s的幂.例如:
To multiply any number you can split the number into sum of power of 2s. For example:
n * 10 = n * 8 + n * 2 = n< 3 + n<< 1
n*10 = n*8 + n*2 = n << 3 + n << 1
SLL $t1, $s2, 1
SLL $t2, $s2, 3
ADD $s2, $t1, $t2
如果速度更快,您也可以使用减法
You can also use a subtraction if it's faster
n * 15 = n * 16-n = n<< 4-n
n*15 = n*16 - n = n << 4 - n
SLL $t1, $s2, 4
SUB $s1, $t1, $s2
这篇关于在MIPS汇编中使用逻辑移位进行乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!