如何在不使用内置指令的情况下在MIPS汇编中实现乘法和除法? [英] How do I implement multiplication and division in MIPS assembly without using the built in instructions?

查看:269
本文介绍了如何在不使用内置指令的情况下在MIPS汇编中实现乘法和除法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,这是问题所在.我必须编写一个MIPS程序,该程序可以从用户那里获得2个输入数字.然后,我必须编写一个代码,以输出用户输入的2个数字的乘积,商和余数.

Ok, here is the problem. I had to write a MIPS program that got 2 input numbers from the user. Then, I had to write a code that would output the product, quotient and remainder for the 2 numbers entered by the user.

现在,这很简单.但是,我没有意识到我们不能在程序中使用乘法和除法操作数.现在我不知道该怎么办,因为我对没有乘法和除法操作数的情况感到困惑.我在整个程序中只使用了两次,它可以正常工作,但是我的教授不会接受它,但现在我很难过.任何帮助,将不胜感激.谢谢

Now, that was pretty straight forward. However, I didnt realize that we could NOT use the multiply and divide operands in the program. Now I dont know what to do because I am confused on how to do it without the multiply and division operands. I only used it twice in the whole program, it works fine but my professor wont accept it and now im getting sad. Any help would be appreciated. Thank you

这是我的代码

# Given positive integers a and b, output a/b and a%b.
  .data
str1: .asciiz "Enter a: "
str2: .asciiz "Enter b: "
str3: .asciiz "a/b = "
str4: .asciiz "a%b = "
str5: .asciiz "a*b = "
newline: .asciiz "\n"
  .text

main: li   $v0, 4            # system call code for print_string
  la   $a0, str1         # address of str1
  syscall                # print str1

#get the first number from user, put it into $s0

li   $v0, 5            # system call code for read_int
  syscall                # read an integer into $v0 from console
  add  $s0, $v0, $zero   # copy $v0 into $s0 (a)


#read print_string for str2
li   $v0, 4            # system call code for print_string
  la   $a0, str2         # address of str1
  syscall                # print str1

# get second number from user, put it into $t1  
li  $v0, 5      #load syscall for read_int
syscall         #make the syscall
move $s1, $v0       #move the number read into $s1(b)

#DO THE CALCULATIONS................................................
div $s0, $s1        #diving $s0 by $s1
mflo    $t0         #storing value of lo(quotient) in
                #register $t0
mfhi    $t1         #storing value of hi(remainder) in
                #register $t1

mult $s0, $s1
mflo $t2


li $v0,1
move $a0, $t2
syscall

li $v0,4
la $a0, str5
syscall

#read print_string for str3
li   $v0, 4            # system call code for print_string
  la   $a0, str3         # address of str1
  syscall                # print str1   

#print a/b
li  $v0, 1      #load syscall print_int into $v0
move $a0, $t0      #move the number to print into $t2
syscall
# read print string for str4
li $v0, 4
    la $a0, str4
    syscall
# print remainder
li $v0, 1
move $a0, $t1
syscall

#end of program
li  $v0, 10     #system call code for exit
syscall

推荐答案

您正在寻找的是按位乘法/除法.

我不确定是否可以简要总结一下,但是在这里我举一个例子:

I'm not sure I can summarize it succinctly, but here I go with an example:

假设您希望将数字6乘以数字5.
如果a =数字6,则(简化的8位)为:
a=00000110

Let's say you wish to multiply the number 6 by the number 5.
If a = number 6, then (in simplified 8-bit) this is:
a=00000110

如果b =数字5,则(简化的8位)为:
b=00000101

If b = the number 5, then (in simplified 8-bit) this is:
b=00000101

要将这些数字相乘,您需要将数字下移至最接近的整数倍,然后相加.

To multiply these numbers, you need to shift by the nearest multiple of two below the nummber, then add.

例如,5下方的2的最接近倍数是4,也就是说它是2 ^ 2;
因此,我们按位左移a(数字6)2次:
a << 2
现在变成00011000

For example, the nearest multiple of 2 below 5 is 4, that is to say it's 2^2;
So we bitwise shift left a (the number 6) 2 times:
a << 2
Which now makes it 00011000

这意味着我们现在乘以4;现在将其乘以5,我们只需再次添加a:

This means we've now multiplied by 4; to now make this multiplied by 5, we simply add a again:

     00011000  
    +00000110  
    =00011110  
    =30 (base 10)

哪个是6 * 5.

让我们再次尝试使用12 * 11 11以下的2的最接近倍数是8(2 ^ 3).这意味着我们需要将数字按位移位12 3次,然后再将其添加到自身3次.

Let's try this again with 12*11 The nearest multiple of 2 below 11 is 8 (2^3). This means we'll need to bitwise shift the number 12 3 times, then add it to itself another 3 times.

    00001100 = 12
    //Let's bitshift by 3
    01100000 = 96
    //Let's add 12 3 times
    01100000
   +00001100
   =01101100 = 108
   +00001100
   =01111000 = 120
   +00001100
   =10000100 = 132 = 12*11

除法

将12除以11,则采用另一种方法.从132减去12,共3次,然后向右移3次(除以8)

To Divide

To divide 12 by 11, you go the other way; subtract 12 from 132, 3 times, then bitshift to the right 3 times (to divide by 8)

这是我目前能做的最好的事情;如果您想要更多,请使用C中的相关算法,请参阅

That's the best I can do right at this moment; if you want more, with relevant algorithms in C, take a look at http://www.programmersheaven.com/mb/CandCPP/295363/295363/bitwise-multiplication--division/

如果您希望扩展任何答案,请在下面评论;

If you want any of this answer expanded, comment below;

P.S.我为您展示了两个带有质数(讨厌)的示例,但是如果您得到一个像12这样的数字怎么办? 逻辑上,根据我所说的,最接近的2的倍数是8(2 ^ 3),因此您必须向左移3位,然后将12加到数字上4次.

P.S. I've shown you two examples with prime numbers (nasty) but what if you get a number like 12? Logically, based on what I've said, the nearest multiple of 2 is 8 (2^3), so you'd have to bitshift left 3, then add 12 to the number 4 times.

但是,如果您进行数学运算,您会发现12实际上是=(2 ^ 3 + 2 ^ 2)...这意味着您可以获得(12<< 3)(即左移12位) 3次)​​,然后将其添加到(12<< 2)(左移12位2次)...魔术!

But if you do the maths, you'd find that 12 is actually = (2^3 + 2^2)... which means you can get (12 << 3) (that's 12 bitshift-left 3 times), then add it to (12 << 2) (that's 12 bitshift left 2 times)... magic!

这篇关于如何在不使用内置指令的情况下在MIPS汇编中实现乘法和除法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆