-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisaproject_template.s
165 lines (156 loc) · 4.5 KB
/
isaproject_template.s
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
162
163
164
165
// The code you must implement is provided in ISAproject.pdf
// Define stack start address
.stack 0x5000
// Define address of printf
.text 0x7000
.label printf
.data 0x100
.label sia
50
43
100
-5
-10
50
0
.label sib
500
43
100
-5
-10
50
0
.label fmt1
.string //sia[%d]: %d
.label fmt2
.string //Something bad
.text 0x300
// r0 has ia - address of null terminated array
// sum_array is a leaf function
// If you only use r0, r1, r2, r3; you do not need a stack
.label sum_array
mov r0, 2 // hardcode to return a 2
mov r15, r14 // return
.text 0x400
// r0 has ia1 - address of null terminated array
// r1 has ia1 - address of null terminated array
// cmp_arrays must allocate a stack
// Save lr on stack and allocate space for local vars
.label cmp_arrays
// Allocate stack
// Call sum_array two times
mov r0, -1 // hardcode to return -1
// Deallocate stack
mov r15, r14 // return
.text 0x500
// r0 has ia - address of null terminated array
// numelems is a leaf function
// If you only use r0, r1, r2, r3; you do not need a stack
.label numelems
mov r0, 0xa // hardcode to return a 10
.label break
mov r15, r14 // return
.text 0x600
// r0 has ia - address of null terminated array
// sort must allocate a stack
// Save lr on stack and allocate space for local vars
.label sort
// Allocate stack
// blr numelems // count elements in ia[]
// create nested loops to sort
// Deallocate stack
mov r15, r14 // return - sort is a void function
.text 0x700
// r0 has ia - address of null terminated array
// smallest must allocate a stack
// Save lr on stack and allocate space for local vars
.label smallest
// Allocate stack
// blr numelems // count elements in ia[]
// create loop to find smallest
mov r0, 2 // hardcode to return a 2
// Deallocate stack
mov r15, r14 // return
.text 0x800
// r0 has an integer
// factorial must allocate a stack
// Save lr on stack and allocate space for local vars
.label factorial
// Allocate stack
// implement algorithm
//blr factorial // factorial calls itself
// Deallocate stack
mov r0, 120 // hardcode 5! as return value
mov r15, r14 // return
.text 0x900
// This sample main implements the following
// int main() {
// int n = 0, l = 0, c = 0;
// printf("Something bad");
// for (int i = 0; i < 3; i++)
// printf("ia[%d]: %d", i, sia[i]);
// n = numelems(sia);
// sm1 = smallest(sia);
// cav = cmp_arrays(sia, sib);
// }
.label main
sbi sp, sp, 16 // allocate space for stack
// [sp,0] is int cav
// [sp,4] is int n
// [sp,8] is int sm1
str lr, [sp, 12] // [sp,12] is lr (save lr)
mov r0, 0
str r0, [sp, 0] // cav = 0;
str r0, [sp, 4] // n = 0;
str r0, [sp, 8] // sm1 = 0;
// printf("Something bad");
// Kernel call to printf expects parameters
// r1 - address of format string - "Something bad"
// mva r1, bad
// ker #0x11
// The os has code for printf at address 0x7000
// The code there generates the ker instruction
// You call printf with
// r0 - has address of format string - "Something bad"
mva r0, fmt2
blr printf
//
// for (int i = 0; i < 4; i++)
// printf("ia[%d]: %d", i, sia[i]);
mov r4, 0 // i to r4
mva r5, sia // address is sia to r5
.label loop4times // print 3 elements if sia
cmp r4, 4
bge end_loop4times
// Kernel call to printf expects parameters
// r1 - address of format string - "ia[%d]: %d"
// r2 - value for first %d
// r3 - value for second %d
mva r1, fmt1 // fmt1 to r1
mov r2, r4 // i to r2
ldr r3, [r5], 4 // sia[i] to r3
ker #0x11 // Kernel call to printf
adi r4, r4, 1 // i++
bal loop4times
.label end_loop4times
// int n = numelems(sia);
mva r0, sia // put address of sia in r0
blr numelems // n = numelems(sia)
str r0, [sp, 4]
// int sm1 = smallest(sia);
mva r0, sia // put address of sia in r0
blr smallest // sm1 = smallest(sia)
str r0, [sp, 8] // store return value in sm1
// cav = cmp_arrays_sia, sib);
mva r0, sia // put address of sia in r0
mva r1, sib // put address of sib in r1
blr cmp_arrays
str r0, [sp, 0]
// Do not deallocate stack.
// This leaves r13 with an address that can be used to dump memory
// > d 0x4ff0
// Shows the three hardcoded values stored in cav, n, and sm1.
// 0x4ff0 (0d20464) 0xffffffff 0x0000000a 0x00000002
.label end
bal end // branch to self