0

I am wondering how I would deal with adding two 64-bit numbers. I currently have this code, made in MARS Simulator:

#A*B
    multu $s0, $s1
    mfhi $t4
    mflo $t5
#C*D
    multu $s2, $s3
    mfhi $t6
    mflo $t7

The values $s0-3 have already been set previously in the code via user input and are unsigned(but their max values are 2147483647 due to how syscall works).
Essentially, I'm trying to do ($s0$s1 + $s2$s3) without causing overflow by returning a 64-bit number in the form of two 32-bit values representing the most and least significant bits.

  • 1
    Which part is causing you problem? Use two 32 bit additions, and handle the carry. – Jester Feb 13 '22 at 23:38
  • 1
    Compiler output using `uint64_t` should make a good example of how to manually handle carry-out. https://godbolt.org/ I assume you're using 32-bit MIPS, so you can't just use `daddu`. – Peter Cordes Feb 13 '22 at 23:39
  • How would I go about carrying the bit, avoiding an overflow error? I have heard that SLTU is good since when overflowing, the result is a number that is less than the two numbers added. – Bradley Lund Feb 14 '22 at 00:38
  • Yes, use `sltu`. That will directly produce a carry bit that you can add to the top half. – Jester Feb 14 '22 at 00:41

0 Answers0