简体   繁体   English

MIPS大会中的除法(和负数),如何计算余数并打印结果?

[英]Division (and negative numbers) in MIPS Assembly, how to work out remainder and print it with result?

I am sceptical about dealing with negative numbers so positive for now, but at the moment when I enter 54 as my first integer and 8 as my second integer, I get a result of 6 rather than 6.75.我现在对处理如此积极的负数持怀疑态度,但是当我输入 54 作为我的第一个 integer 和 8 作为我的第二个 integer 时,我得到的结果是 6 而不是 6.75。 How can I get both 6 to be outputted and something like Remainder equals 0.75 in my case?在我的情况下,我怎样才能同时输出 6 和剩余值等于 0.75?

.data
msg1:   .asciiz "\nEnter the first integer"
msg2:   .asciiz "Enter the second integer"
msg3:   .asciiz "Result"

.text
li $v0, 4   # syscall to print string
la $a0, msg1
syscall

li $v0, 5   # syscall to read an integer
syscall
add $t1, $zero, $v0

li $v0, 4   # syscall to print a string
la $a0, msg2
syscall

li $v0, 5   # syscall to read an integer
syscall
add $t2, $zero, $v0

add $t0, $zero, $zero

LOOP:
slt $t3, $t1, $t2
bne $t3, $zero, DONE
sub $t1, $t1, $t2
addi $t0, $t0, 1
j LOOP

DONE:
li $v0, 4   # syscall to print a string
la $a0, msg3
syscall

li $v0, 1   # syscall to print an integer
add $a0, $t0, $zero
syscall

li $v0, 10   # syscall code to exit
syscall
'Template'
main:
    #initialize registers

loop:
    #dividend -= divsor
    #quotient++

    #temp = dividend - divsor
    #if temp < 0 jump to done
    #jump to loop

done:
    #remainder = temp

Division by repetitive subtraction and the div instruction on MIPS are doing integer division .重复减法除法和 MIPS 上的div指令正在执行integer 除法 That means that the quotient is an integer and so is the remainder — there is no rounding of either the quotient or the remainder, these are exact as per integer division.这意味着商是 integer,余数也是 - 商或余数都没有四舍五入,这些与 integer 除法完全相同

In your example, both the quotient and the remainder happen to be 6. In integer division, if you did 55 / 8 you would have a quotient of 6 and remainder of 7. And 53 / 8 would yield quotient of 6 and remainder of 5. (The remainder is what's left after the quotient, 6, is multiplied by the divisor, 8, and subtracted from the original dividend, eg here, 53-48, 54-48, 55-48.)在您的示例中,商和余数恰好都是 6。在 integer 除法中,如果您执行 55 / 8,则商数为 6,余数为 7。而 53 / 8 的商数为 6,余数为 5 . (余数是商 6 乘以除数 8 并从原始被除数中减去的余数,例如此处为 53-48、54-48、55-48。)

However, it sounds like perhaps you don't want integer division.但是,听起来您可能不希望 integer 划分。

If you use real division instead of integer division, 6.75 is the quotient and there is no concept of remainder, just quotient — with potential for some of it to be after the decimal point.如果您使用实数除法而不是 integer 除法,则 6.75 是商,没有余数的概念,只是商 — 其中一些可能在小数点后。

(0.75 would be the integer remainder, 6, divided by the divisor, 8, using real division instead of integer division; I don't know a name for this so I've simply called it "the rest" of the quotient.) (0.75 是 integer 余数,6 除以除数 8,使用实数除法而不是 integer 除法;我不知道这个名称,所以我简单地称它为商的“余数”。)

Real division is approximated on processors using floating point.实际除法在使用浮点的处理器上进行近似。 MIPS has a floating point coprocessor that will give you 6.75 for the answer in a floating point register. MIPS 有一个浮点协处理器,它会在浮点寄存器中给出 6.75 的答案。 In order to use floating point arithmetic, we place values into floating point registers, and then use floating point instructions, which return values to floating point registers — these are named $f0 - $f31 to differentiate from the integer registers.为了使用浮点运算,我们将值放入浮点寄存器,然后使用浮点指令将值返回到浮点寄存器——这些寄存器被命名为$f0 - $f31以区别于 integer 寄存器。 If you're using MARS you can also print the 6.75 after so dividing.如果您使用 MARS,您也可以在除法后打印 6.75。


Still, there is a way to get the digits 7 and 5 out of the integer computation (though this is fairly uncommon, since it is a bit like building your own floating point..).尽管如此,还是有一种方法可以从 integer 计算中获取数字 7 和 5(尽管这相当罕见,因为它有点像构建自己的浮点......)。

Take the remainder, 6, and multiply by 10 to get 60. Then do integer division again, 60 / 8 to get 7 quotient and 4 remainder.取余数 6,乘以 10 得到 60。然后再做 integer 除法,60 / 8 得到 7 商和 4 余数。 Print the 7, and, with the 4 do the above again: multiply by 10 to get 40 and integer divide by 8: 40 / 8 = 5, so print the 5 — you have printed 75 — if you had started with 54 / 8 you would get 675 out as digits, where the decimal point should be placed after the printing the first quotient: 6.75.打印 7,然后用 4 再次执行上述操作:乘以 10 得到 40 和 integer 除以 8:40 / 8 = 5,所以打印 5——你已经打印了 75——如果你从 54 / 8 开始你会得到 675 作为数字,小数点应该放在打印第一个商之后:6.75。 Knowing where to stop printing is a trick because 1/3 would always have a remainder of 1, and thus go on forever with 3's: 0.33333333333...知道在哪里停止打印是一个技巧,因为 1/3 的余数总是 1,因此 go 永远是 3:0.33333333333...

Alternatively, multiply the dividend by 100 to start with: 5400 / 8 = 675, or, multiply the remainder after the first integer division, 6, by 100 then divide by 8 = 75.或者,将被除数乘以 100 以开始:5400 / 8 = 675,或者,将第一次 integer 除法后的余数 6 乘以 100,然后除以 8 = 75。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM