Monday, May 11, 2020

Using the stack in MIPS assember

This is MIPS assembler code that demonstrates how to use the stack when calling functions in MIPS. The code is fully documented.

Launch EzMIPSthe MIPS assembler simulator, copy the following MIPS code and paste it into EzMIPS. Assemble, Run.

# ---------------------- Using the stack in MIPS ----------------- #

#int foo (int x, int y, int var1, int var2, int var3)
#{
#    int a, b;
#    a = y;
#}

#int main () {
#    int c, d;
#    foo (3, 4, 43, 62, 1);
#}

# ---------------------------------------------------------------- #

.data

c: .word 3
d: .word 4

# ---------------------------------------------------------------- #

.text

    jal main
    li $v0, 10
    syscall

# ________________________ int main () ___________________________ #

main:

    la $t0, c
    lw $t0, 0($t0)       # $t0 holds the value of c (c=3)


    la $t1, d
    lw $t1, 0($t1)       # $t1 holds the value of d (d=4)

# ................................................................ #

    # make stack space for 8 words: $ra, c, d, and the args x, y, var1, var2, var3
    addi $sp, $sp, -32   # $sp = $sp - 32 = 0x7fffeffc - 32 = 0x7fffefdc

# ................................................................ #

    sw $ra, 28($sp)      # save $ra                     28($sp) = (0x7fffeff8) = $ra = 0
    sw $t0, 24($sp)      # save c before calling foo    24($sp) = (0x7fffeff4) = c = 3
    sw $t1, 20($sp)      # save d before calling foo    20($sp) = (0x7fffeff0) = d = 4

    add $a0, $0, $t0     # arg x = c = 3
    add $a1, $0, $t1     # arg y = d = 4
    addi $a2, $0, 43     # arg var1 = 43
    addi $a3, $0, 62     # var2 = 62
    addi $t0, $0, 1      # var3 = 1, but no more registers

    sw $t0, 16($sp)      # save var3 before calling foo 16($sp) = (0x7fffefec) = var3 = 1

    # call foo() function
    jal foo

# ................................................................ #

    addi $sp, $sp, 32    # restore stack space

# ................................................................ #

    lw $ra, -4($sp)      # reload return address
    jr $ra               # return to caller

# ____ int foo (int x, int y, int var1, int var2, int var3) ______ #

foo:

# ................................................................ #

    # make stack space for 3 words: $ra, a, b
    addi $sp, $sp, -12   # $sp = $sp - 12 = 0x7fffefdc - 12 = 0x7fffefd0

# ................................................................ #

    sw $ra, 8($sp)       # save $ra                     8($sp) = (0x7fffefd8) = $ra = 0x00400044
    add $t0, $0, $a1     # get argument y ($a1)
    
    # 12 (foo's frame) + 16 = 28 up on stack, fetched argument var3
    lw $t1, 28($sp)      #                              $t1 = 28($sp) = (0x7fffefec) = 1

# ................................................................ #

    addi $sp, $sp, 12    # restore stack space

# ................................................................ #

    lw $ra, -4($sp)      # reload return address
    jr $ra               # return to caller

# ________________________________________________________________ #

Please let me know of any suggestions or bugs regarding the code above.

Regards,

Antonis

No comments:

Post a Comment