summaryrefslogtreecommitdiff
path: root/tests/examplefiles/mips/function_with_stack.mips
blob: b9d606f66e66d5a1caea37e571160ded45b49922 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Simple routine to demo functions
# USING a stack in this example to preserve
# values of calling function

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

    .text

    .globl  main
main:
    # Register assignments
    # $s0 = x
    # $s1 = y

    # Initialize registers
    lw  $s0, x      # Reg $s0 = x
    lw  $s1, y      # Reg $s1 = y

    # Call function
    move    $a0, $s0    # Argument 1: x ($s0)
    jal fun     # Save current PC in $ra, and jump to fun
    move    $s1,$v0     # Return value saved in $v0. This is y ($s1)

    # Print msg1
    li  $v0, 4      # print_string syscall code = 4
    la  $a0, msg1
    syscall

    # Print result (y)
    li  $v0,1       # print_int syscall code = 1
    move    $a0, $s1    # Load integer to print in $a0
    syscall

    # Print newline
    li  $v0,4       # print_string syscall code = 4
    la  $a0, lf
    syscall

    # Exit
    li  $v0,10      # exit
    syscall

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

    # FUNCTION: int fun(int a)
    # Arguments are stored in $a0
    # Return value is stored in $v0
    # Return address is stored in $ra (put there by jal instruction)
    # Typical function operation is:

fun:    # This function overwrites $s0 and $s1
    # We should save those on the stack
    # This is PUSH'ing onto the stack
    addi $sp,$sp,-4     # Adjust stack pointer
    sw $s0,0($sp)       # Save $s0
    addi $sp,$sp,-4     # Adjust stack pointer
    sw $s1,0($sp)       # Save $s1

    # Do the function math
    li $s0, 3
    mul $s1,$s0,$a0     # s1 = 3*$a0  (i.e. 3*a)
    addi $s1,$s1,5      # 3*a+5

    # Save the return value in $v0
    move $v0,$s1

    # Restore saved register values from stack in opposite order
    # This is POP'ing from the stack
    lw $s1,0($sp)       # Restore $s1
    addi $sp,$sp,4      # Adjust stack pointer
    lw $s0,0($sp)       # Restore $s0
    addi $sp,$sp,4      # Adjust stack pointer

    # Return from function
    jr $ra          # Jump to addr stored in $ra

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

    # Start .data segment (data!)
    .data
x:  .word 5
y:  .word 0
msg1:   .asciiz "y="
lf:     .asciiz "\n"