7

I'd like to pass a character as an argument to a function in MIPS. Do I do this by storing the character into register $a0, use jal to move to the function, then extract what's in $a0 into a separate register using lw?

If someone could give me an example of passing an argument or two in MIPS, I'd really appreciate it. I've found a lot of info on the MIPS calling conventions, but not any simple and succinct examples.

Victor Brunell
  • 5,668
  • 10
  • 30
  • 46

2 Answers2

4

No need to use lw which is for extracting words from memory. You can simply use $a0 in the sub-routine.

Take a look at this example of a "print-char" function:

.text

main:

    #save $ra on stack
    addi $sp $sp -4
    sw   $fp 0($sp)
    move $fp $sp
    addi $sp $sp -4
    sw   $ra  -4($fp)

    #call sub-routine
    addi $a0 $zero 'A'
    jal printchar

    #restore and shrink stack
    lw $ra  -4($fp)
    lw $fp   0($fp)
    addi $sp $sp 8

    jr $ra

#prints a char and then a new line
printchar:

    #call print-char syscall
    addi $v0 $zero 11
    syscall

    addi $a0 $zero 10
    syscall

    jr $ra

As demonstrated, you the value of the $a0 register is just used in the sub-routine as it returns the value that it was given before the jal.

Also demonstrated is proper expansion and shrinking of the stack as is necessary for calling a sub-routing. As you will notice, the sub-routine does not perform this operation as it does not call a sub-routine and there-fore does not need to save the $ra. Stack manipulations would also be required in the sub-routine if it were to use an $s register as the MIPS calling convention specifies these as callee saved.

Konrad Lindenbach
  • 4,911
  • 1
  • 26
  • 28
  • Why are you doing manipulations on the stack? is it essential? – BananaBuisness Aug 19 '16 at 19:37
  • 1
    @AsafFisher You must store the value of the `$ra` somewhere when you call a subroutine as `jal` overwrites it.This could be done by using an `$s` register, but I was just demonstrating the general method for saving `$ra` on the stack with manipulation of `$sp` and `$fp`. – Konrad Lindenbach Aug 20 '16 at 05:26
  • Ok so why not using only sp? Why are you using also fp? – BananaBuisness Aug 20 '16 at 07:26
  • 1
    @AsafFisher Using `$fp`, the frame pointer, allows for dynamic allocation on the stack within the function. I've heard it said that `$fp` is rarely used in hand-written assembly, and you're certainly free to leave it out -- it's not necessary in this example. – Konrad Lindenbach Aug 30 '16 at 21:06
  • 1
    What about if you have to pass in a lot of parameters? – john k Apr 04 '18 at 03:06
  • @KonradLindenbach _Stack manipulations would also be required in the sub-routine if it were to use an $s register as the MIPS calling convention specifies these as caller saved._ I think you mean _callee_ saved here since the function that was called is required to preserve the `$s` registers. Your [other answer](https://stackoverflow.com/questions/20111326/mips-assembly-language-temporary-register-vs-saved-registers/20112240#20112240) correctly says "`$s` registers are callee saved." – ggorlen Dec 09 '20 at 00:07
  • @johnktejik See [MIPS function call with moe than four arguments](https://stackoverflow.com/questions/2298838/mips-function-call-with-more-than-four-arguments) – ggorlen Dec 09 '20 at 00:08
0

a very easy way to go about it would be to load the argument in a temp register and then just use:

move $a0,$t0

this way the argument stored in the temp register will be given as an argument

Simas Joneliunas
  • 2,890
  • 20
  • 28
  • 35
Kash'
  • 1