-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathmacros.asm
More file actions
161 lines (134 loc) · 3.59 KB
/
Copy pathmacros.asm
File metadata and controls
161 lines (134 loc) · 3.59 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# ============================================================================= #
# NOTE: For this lab, the macros have been modified to save and restore
# $a0 in the stack.
# Thus, our macros will not clobber $a0, which may be used by your code.
# For example, if $a0 holds the value 1 when you invoke a macro,
# $a0 will continue to hold the value 1 after the macro body is done executing.
# See the updated clobbers: ... comments.
# Although this makes coding easier, note that this
# does not follow MIPS caller-callee contracts (macro is like a callee).
# ============================================================================= #
# ==================== #
# Do not modify the
# given macros
# ==================== #
# syscall with the immediate %service_num in $v0
#
# %service_num: immediate that is put in $v0 for the syscall
#
# clobbers: $v0
.macro syscall_val (%service_num)
li $v0, %service_num
syscall
.end_macro
# >>> New macros >>>
# for bingen
# print string in .data given its label using system call 4
# different from print_str because print_str takes
# a string literal
#
# %label: label of string (array of chars) in .data
#
# clobbers: $v0
.macro print_str_label (%label)
.text
addi $sp, $sp, -4
sw $a0, 0($sp)
la $a0, %label
syscall_val (4) # print string
lw $a0, 0($sp)
addi $sp, $sp, 4
.end_macro
.eqv NULL 0 # NULL is 0, see usage in new_node below
# similar function to C's malloc (malloc uses sbrk internally)
# allocate %bytes bytes in heap memory and return a pointer
# to allocated memory using system call 9 (sbrk)
#
# %bytes: register or constant representing the
# number of bytes to allocate
#
# returns: pointer to allocated memory in $v0
#
# clobbers: $v0
.macro sbrk (%bytes)
addi $sp, $sp, -4
sw $a0, 0($sp)
add $a0, $0, %bytes # %bytes can be register or constant
syscall_val (9) # sbrk (allocate heap memory)
lw $a0, 0($sp)
addi $sp, $sp, 4
.end_macro
# translation of bst.c new_node
# return pointer to new BST node initialized
# with data %x and left and right fields NULL
#
# %x: register or constant representing
# node data (int)
#
# returns: pointer to initialized BST node in $v0
#
# clobbers: $v0
.macro new_node (%x)
addi $sp, $sp, -4
sw $a0, 0($sp)
sbrk (12) # $v0 = pointer to newly
# allocated 12 bytes in heap
# self-check: Why 12 per BST struct?
add $a0, $0, %x # %x can be register or constant
# use $a0 to avoid clobbering
# another register
# $a0 is saved in stack anyway
sw $a0, 0($v0) # root->data = %x
# self-check: Why 0($v0)?
li $a0, NULL
sw $a0, 4($v0) # root->left = NULL
# self-check: Why 4($v0)?
sw $a0, 8($v0) # root->right = NULL
# self-check: Why 8($v0)?
lw $a0, 0($sp)
addi $sp, $sp, 4
.end_macro
# <<< New macros <<<
# print string literal using system call 4
#
# %str: ASCII string literal
#
# clobbers: $v0
.macro print_str (%str)
.data
str_label: .asciiz %str # store %str in .data section
.text
addi $sp, $sp, -4
sw $a0, 0($sp)
la $a0, str_label # $a0 = address of %str
syscall_val (4) # print string
lw $a0, 0($sp)
addi $sp, $sp, 4
.end_macro
# print contents of %reg as integer using system call 1
#
# %reg: register with contents to be printed as integer
#
# clobbers: $v0
.macro print_reg_int (%reg)
addi $sp, $sp, -4
sw $a0, 0($sp)
add $a0, $0, %reg
syscall_val (1) # print integer
lw $a0, 0($sp)
addi $sp, $sp, 4
.end_macro
# read integer from stdin into $v0 using system call 5
#
# returns: integer read from stdin in $v0
#
# clobbers: $v0
.macro read_int
syscall_val (5) # read integer
.end_macro
# ==================== #
# End of given code
# You may add macros
# after here, if you
# wish
# ==================== #