-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_without_stack.asm
70 lines (54 loc) · 1.48 KB
/
function_without_stack.asm
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
.data
x: .word 5
y: .word 0
msg1: .asciiz "y="
lf: .asciiz "\n"
# Simple routine to demo functions
# NOT using a stack in this example.
# Thus, the function does not preserve values
# of calling function!
# ------------------------------------------------------------------
.text
main:
# Register assignments
# $s0 = x
# $s1 = y
# Initialize registers
la $t0, x
la $t1, y
lw $s0, 0($t0) # Reg $s0 = x
lw $s1, 0($t1) # 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: # 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
# Return from function
jr $ra # Jump to addr stored in $ra
# ------------------------------------------------------------------