2

I'm new to MIPS and while I sort of understand how to do basic tasks with it, I'm struggling heavily with procedures/functions.

I was hoping someone could explain how to solve a simple CPP program such as

void swap( int & a, int & b )
{
  register int t;
  t = a; a = b; b = t;
}

Into MIPS. In class we talked about frame pointers and stack pointers, offsets, and I'm just lost. How do you pass int a, and int b to the function? How do you return a value and use it in MIPS? I've seen similar problems with arrays but was hoping if someone could help me with one with just two ints to be swapped. Thanks.

LogwanaMan
  • 21
  • 1
  • 1
  • 3
  • Unclear what do you want as an answer. What does "solve a simple CPP program" mean? - Explain how it works on C level? How it would look at asm level? How it would work at asm level? – ivan_pozdeev Dec 08 '15 at 17:39
  • 1
    Sorry, I mean how would you implement it in assembly language/MIPS. – LogwanaMan Dec 08 '15 at 17:39
  • Why not [compile it and see for yourself](http://stackoverflow.com/questions/137038/how-do-you-get-assembler-output-from-c-c-source-in-gcc)? You probably need to turn any optimizations off so they don't cloud the picture. – ivan_pozdeev Dec 08 '15 at 17:41
  • You may find this easier if you use pointers rather than reference variables. i.e. void swap(int *a,int *b){register int t; t=*a;*a=*b;*b=t;} to call use swap(&c,&d); – PeterI Dec 08 '15 at 17:59

2 Answers2

4

First, you should not use the keyword register in C++, as it is deprecated and in most cases as meaningful as whitespace.

How do you pass int a, and int b to the function?

You store a and b in the registers you will use in your subroutine.

If you want to swap two values in an array, let's say at 0x80080000:

lui $t0, 0x8008     ;$t0 = 0x80080000
lw  $t1 0($t0)      ;load (0x80080000) in $t1
lw  $t2 4($t0)      ;load (0x80080004) in $t2
sw  $t1 4($t0)      ;store $t1 in 0x80080004
sw  $t2 0($t0)      ;store $t2 in 0x80080000

If you want to swap the values of two registers (xor swap algorithm):

xor $t0, $t0, $t1
xor $t1, $t0, $t1
xor $t0, $t0, $t1

Or, if you care about readability:

add $t2, $r0, $t0
add $t0, $r0, $t1
add $t1, $r0, $t2
Méga Lag
  • 386
  • 5
  • 10
  • Don't use xor to swap values --- if `$t0` and `$t1` contain the same value, you'll end up with `$t0=$t1=0`! – David Given Sep 03 '18 at 19:55
  • $t0=9 ; $t1=9 ; $t0=$t0^$t1=9^9=0 ; $t1=$t0^$t1=0^9=9 ; $t0=$t0^$t1=0^9=9. So no, you will not end up with $t0=$t1=0. Please remove your unjustified -1. – Méga Lag Sep 12 '18 at 08:57
  • I beg your pardon, yes. The problem only occurs if `$t0` and `$t1` refer to the same _location_ (which of course they don't as they're distinct registers). Unfortunately I can't remove the downvote until the post is edited (no idea why). – David Given Sep 13 '18 at 09:07
  • I've edited the answer to add a link to the xor algorithm wikipedia page. It is not necessary, makes the answer a bit longer than it needs to be, but it seems to be the only way for people to remove -1s on a post. – Méga Lag Sep 25 '18 at 14:32
1

You can create a function that do swap. Something like that :

swap:                # Swap function

addi    $sp,$sp,-4  
sw  $t0, 0($sp)      # Stack[0] = $t0
    
add $t0,$a0,$zero    # $t0 = $a0
add $a0,$a1,$zero    # $a0 = $a1
add $a1,$t0,$zero    # $a1 = $t0 (swapping $a0 and $a1)

lw  $t0,0($sp)
addi    $sp,$sp,4   # $t0 = Stack[0]
jr  $ra             # Return to the code 

And use the function Swap, like this:

li  $a0,100    # $a0 = 100
li  $a1,33     # $a1 = 33

jal swap       # Swapping $a0 & $a1

Note: The names of the registers aren't important, you can use other.

Ariel
  • 31
  • 4