Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Alexander,</p> <p>What Tom said is pretty much correct the important thing to note with doing programming in assembly is everything is by convention. While in MIPS the common convention is what Tom noted it is not the only convention. For instance if you are using macro's (which if you are going to be writing more than 1 or 2 functions in assembly it is a much easier to use macros) then you can define your calling convention in the macro. The easiest way to do this is instead of having the callee save the stack is to have the caller save the stack. This is less efficient because sometimes (perhaps many times) unused registers will be saved, however it will save you a lot of heartache because your convention is applied consistently.</p> <p>Caller Stack Save:</p> <pre><code> sw $fp 0($sp) # save the old frame pointer addu $fp $sp $0 # move the frame pointer to point at top of frame subu $sp $sp 44 # move the stack pointer down 44 sw $fp 40($sp) # save the old stack pointer sw $ra 36($sp) # save the return address sw $s0 32($sp) # save registers $s0 - $s7 sw $s1 28($sp) sw $s2 24($sp) sw $s3 20($sp) sw $s4 16($sp) sw $s5 12($sp) sw $s6 8($sp) sw $s7 4($sp) </code></pre> <p>Function call</p> <pre><code> jal my_func </code></pre> <p>Caller Stack Restore</p> <pre><code> subu $sp $fp 44 # move the stack pointer to the orginal unmodified bottom lw $ra 36($sp) # load the return address lw $s0 32($sp) # load registers $s0 - $s7 lw $s1 28($sp) lw $s2 24($sp) lw $s3 20($sp) lw $s4 16($sp) lw $s5 12($sp) lw $s6 8($sp) lw $s7 4($sp) lw $fp 44($sp) # load the old frame pointer lw $sp 40($sp) # load the old stack pointer </code></pre> <p>Your function:</p> <pre><code>my_func: do some stuff jr $ra # return to the previous function </code></pre> <p>As I said the best way to apply this convention is to use macros. I did a project in SPIM (which you may being using or you may not be) for a class. As part of the project we wrote a macro engine (it does other cool stuff as well) for SPIM you can get it here: <a href="http://github.com/timtadh/mpp" rel="nofollow noreferrer">http://github.com/timtadh/mpp</a> I would also recommend checking out <a href="http://github.com/timtadh/jist" rel="nofollow noreferrer">http://github.com/timtadh/jist</a> which is a toy operating system written on top of SPIM. It will give you a sense of how to use the macro engine.</p> <p>cheers</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload