0

I am new to MIPS and the stack is a bit puzzling to me. I am trying to swap the char values between two strings. This is also my first time posting here, if I am violating etiquette let me know!

My swap method --

copyString:  # back up all registers you will use!
        push($t0)
        push($t1)    
        push($t2)
        push($t3)   
        push($t6)
        #  retrieve values from the stack!!!
        lw   $t1,  20($sp)
        lw   $t0,  24($sp)      
        li   $t6,  0    #  LCV for the loop
   copyStringBytes:
        bge   $t6, 6, endCopyString  # Leave the loop when we have looped 6 times
        lb    $t2,    ($t0)
        lb    $t3,    ($t1)
        sb    $t3,    ($t0)
        sb    $t2,    ($t1)    
        addi  $t0,    $t0,    1
        addi  $t1,    $t1,    1     
        addi  $t6,    $t6,    1
        b     copyStringBytes          
endCopyString:
        pop($t6)
        pop($t3)
        pop($t2)    
        pop($t1)
        pop($t0) 
        addi  $sp,    $sp,   8    # deallocate 2 parameters
        jr    $ra

Im not sure if I am sending the correct registers to the stack before implementing the swap function? Any help is appreciated and if there are tutors with time this evening please message me.

Erik Eidt
  • 23,049
  • 2
  • 29
  • 53
  • 1
    You are using a non standard and undocumented calling convention, which makes it difficult to say if you're doing it right or wrong. Without knowing it, the code looks ok to me though. Just use `addiu` instead of `addi` when doing pointer arithmetic (as with adjusting `$sp`, `$t0`, and `$t1`). – Erik Eidt Nov 21 '21 at 23:30
  • 1
    The standard calling convention for MIPS would 1. pass arguments in `$a` registers (and only use stack memory for arguments after the first 4 had been passed in registers), 2. use `$t` registers without save & restore b/c they are classified as call clobbered, and 3. save only `$s` registers before using them (which you wouldn't need/use in this function, of course), and lastly, would adust the stack only once, instead of pushing and popping -- the adjustment for all those pushes would be done only once and then the pushes replaced with simple stores. – Erik Eidt Nov 22 '21 at 14:34
  • If you want further help with this then cite your calling convention documentation. – Erik Eidt Nov 22 '21 at 14:36
  • Normally you wouldn't save/restore `$t` registers at all; just clobber them without saving/restoring. That's the purpose they're named for in the standard calling convention: T stands for Temporary. See [call-preserved vs. call-clobbered registers](https://stackoverflow.com/questions/9268586/what-are-callee-and-caller-saved-registers/56178078#56178078) – Peter Cordes Nov 22 '21 at 20:38

0 Answers0