15

So I am learning MIPS using the SPIM simulator and im stuck on this problem.

I want to add two 64 bit numbers which are stored in four 32 bit registers. So I add the LO bytes and then the carry and the HI bytes. But there is no adc/addc command i.e. add with carry.

So I would have to add the carry bit in the status register. But, how exactly do I read this register?

If $t0 is temporary register 1, then what is the equivalent of the status register which holds the carry flag?

I have googled a lot I still can't find any examples that even use the status register.

laalto
  • 150,114
  • 66
  • 286
  • 303

1 Answers1

22

Add $t2 $t3 + $t4 $t5, result in $t0 $t1

addu  $t1, $t3, $t5    # add least significant word
sltu  $t0, $t1, $t5    # set carry-in bit 
addu  $t0, $t0, $t2    # add in first most significant word
addu  $t0, $t0, $t4    # add in second most significant word

For the second part of your question, there is no status register. None at all. Nada.

Gunther Piez
  • 29,760
  • 6
  • 71
  • 103
  • Thanks. It makes sense why I couldn't find the status register :) Your answer works perfectly. Is there any place with tips on better MIPS coding? –  Aug 15 '09 at 21:28
  • There are few. For me, meditation over other peoples source or looking a objdump -S does works. You can learn from the rare occassions, where a compiler does something right, but you can learn even more from the places, where they produce utter crap, as usual ;-) http://www.cs.unibo.it/~solmi/teaching/arch_2002-2003/AssemblyLanguageProgDoc.pdf does give some useful tips too. – Gunther Piez Aug 16 '09 at 10:41
  • Great solution. I love it when solutions to apparently diificult (IMHO) problems are this elegant. – Derek Nov 06 '09 at 16:17
  • 1
    This is pretty much the definitive way of doing this (if you want to see if if the two 64-bit numbers overflow, you can still do it in four instructions, just another register for the second result, and changes the registers around). In terms of a good reference, See MIPS Runs by Dominic Sweetman is my goto book. Though it doesn't hold your hand if you don't know general assembly programming, it coverts MIPS in and out including lots of specific chip variations. – D'Nabre Apr 23 '12 at 19:43
  • 3
    it may be faster to do `addu $t1, $t3, $t5` and `addu $t0, $t2, $t4` before adding the carry `sltu $at, $t1, $t5; addu $t0, $t0, $at` since most modern CPUs have multiple ALUs/FPUs that can run many instructions simultaneously as long as they're not depend on each other and on previous results – phuclv Dec 24 '13 at 11:42