简体   繁体   English

无法在MIPS上添加两个32位数字

[英]Can't add two 32 bit numbers on MIPS

I am trying to write a code that makes it possible to add 2 numbers, even if they are 32 bits long. 我正在尝试编写一个代码,可以添加2个数字,即使它们是32位长。 However my program won't work if I add two 32 bits numbers, or if I add two numbers that make a 32 bits number (like 2bi + 2bi). 但是,如果我添加两个32位数字,或者如果我添加两个32位数字(如2bi + 2bi),我的程序将无法工作。 In thw first case I get an "Invalid Interger Input" error, and in the second case I get an "Arithmetic Overflow" error. 在第一种情况下,我得到“无效的整数输入”错误,在第二种情况下,我得到“算术溢出”错误。 I figured the 32nd bit is being used for the "+" or "-" signal. 我认为第32位用于“+”或“ - ”信号。 How can I fix this? 我怎样才能解决这个问题?

    li $v0, 4  
la $a0, primeiro.num  #asks for the first number
syscall
li $v0, 5  
syscall
move $s0, $v0  

li $v0, 4  
la $a0, segundo.num  #asks for the second number
syscall
li $v0, 5 
syscall
move $s1, $v0 
blt $s1, $zero, erro_num_neg

add $s2, $s0, $s1

Use addu / addiu for plain binary add. 使用addu / addiu进行纯二进制添加。 The carry-out from the top bit is discarded, and no checking is done for 2's complement signed overflow, so it Just Works as wrapping unsigned or signed addition. 从顶部位进位输出被丢弃,并且没有对2的补码有符号溢出进行检查,因此它只是包含无符号或有符号加法。

You should only ever use add / addi if you specifically want it to raise an exception on signed overflow. 如果您特别希望它在签名溢出上引发异常,则应该只使用add / addi (eg to detect cases where this is an error and you want your program to abort instead of continuing with wrapped data.) (例如,检测出这一个错误并且您希望程序中止而不是继续包装数据的情况。)


C compilers don't use MIPS add even for signed arithmetic. 即使对于带符号的算术,C编译器也不使用MIPS add Signed overflow is undefined behaviour in C, so they could use it in some cases (when the asm operands have values that existed in the C abstract machine, not the result of reordering or something), but most compilers choose not to do. 带符号溢出是C中的未定义行为,因此它们可以在某些情况下使用它(当asm操作数具有C抽象机器中存在的值,而不是重新排序的结果或某些东西时),但大多数编译器选择不这样做。

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

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