Passing Arguments: MIPS

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

If someone can give me an example of passing an argument or two to MIPS, I would really appreciate it. I found a lot of information about MIPS calling conventions, but not simple and concise examples.

+4
source share
1 answer

No need to use lwone that is designed to extract words from memory. You can just use $a0in a routine.

Take a look at this print-char function example:

.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

, $a0 , , jal.

, -. , , , , $ra. , $s, MIPS .

+3

Source: https://habr.com/ru/post/1526651/


All Articles