I am trying to write a code that makes it possible to add 2 numbers, even if they are 32 bits long. 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). 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. 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. 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.You should only ever use
add
/addi
if you specifically want it to raise an exception on signed overflow. (e.g. 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. 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.