-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnestedFunctions.asm
More file actions
87 lines (55 loc) · 1.44 KB
/
nestedFunctions.asm
File metadata and controls
87 lines (55 loc) · 1.44 KB
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
85
86
87
#Nested Functions
.data
newLine: .asciiz "\n"
test: .asciiz "test"
.text
#Main function
main:
addi $s0, $zero, 63
jal displayValue
#Prints a new line
li $v0, 4
la $a0, newLine
syscall
#'Declaring' a value
addi $s0, $zero, 10
#Calling function to increase value in register s0 and display
jal increaseMyRegister
#Prints a new line
li $v0, 4
la $a0, newLine
syscall
#Calling function to display original value
jal displayValue
#Closing statement for main function
li $v0, 10
syscall
#Function to increase value in register s0
increaseMyRegister:
#Nested function here returns to main, but we want it to return to
#increaseMyRegister function. Need to save the old address to the stack.
#Freeing up 8 bytes of memory in stack for or old value in s0 and
#for the old address from main
addi $sp, $sp, -8
#Storing value in stack
sw $s0, 0($sp)
#Storing address
sw $ra, 4($sp)
#Manipulate value in register
addi $s0, $s0, 30 #30 + value in s0, stored in s0
#Nested function
jal displayValue
#Load original value and address from memory to be accessible in main
lw $s0, 0($sp)
lw $ra, 4($sp)
addi $sp, $sp, 4 #Restoring the stack
#Closing statement, returns to main
jr $ra
#Function to decrease value in register s1
#Function to print value in register
displayValue:
li $v0, 1
move $a0, $s0
syscall
#return to the caller
jr $ra