-
Notifications
You must be signed in to change notification settings - Fork 0
/
highscore.s.save.3
380 lines (310 loc) · 7.12 KB
/
highscore.s.save.3
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
.include "mov32.inc"
@ System Calls
.set EXIT, 1
.set READ, 3
.set WRITE, 4
.set OPEN, 5
.set CLOSE, 6
.set MMAP2, 192
@ Constants for System Calls
.set STDIN, 0
.set STDOUT, 1
.set MAP_SHARED, 1
.set MUNMMAP, 91
.set PROT_READ, 1
.set PROT_WRITE, 2
.set MAP_PROT, (PROT_READ | PROT_WRITE)
.set O_WRONLY, 1
.set O_RDWR , 2
.set O_CREAT, 64
.set O_TRUNC, 512
.set O_APPEND, 1024
.set FILE_FLAGS, (/*O_WRONLY*/O_RDWR | O_APPEND | O_CREAT/* | O_TRUNC*/)
.balign 4
.data
filename:
.ascii "highscore.txt\0"
.set filename_Len, .-filename
newline:
.ascii "\n\0"
.set newline_Len, .-newline
empty_mess:
.ascii "Empty File::::\n\0"
.set empty_mess_Len, .-empty_mess
you_placed:
.ascii "Congragulations you placed in the top 3.\n\0"
.ascii " Please enter your name: \0"
.set you_placed_Len, .-you_placed
sorry_message:
.ascii "Sorry but you suck\n\0"
.set sorry_message_Len, .-sorry_message
// Delete later and add the one below
user_score:
.word 19
.balign 4
.bss // any label created after this point will be be zeroed
user_name:
.space 10
//user_score:
//.space 4
user_score_ascii:
.space 4
file_info:
.space 10 // Name 1
.word 5 // Score 1
.space 10 // Name 2
.word 5 // Score 2
.space 10 // Name 3
.word 5 // Score 3
.balign 4
.text
.global get_name
get_name:
push {r0, lr}
// Move cursor to middle of screen
@ mov r0, #9
@ mov r1, #25
@ bl locate
// Print a message to the user for input
mov r0, #STDOUT
mov32 r1, you_placed
mov r2, #you_placed_Len
mov r7, #WRITE
svc #0
// Get input from user
mov r0, #STDIN
mov32 r1, user_name
mov r2, #20
mov r7, #READ
svc #0
cmp r0, #10
bge finish_spaces
subs r0, #1
add_filler_spaces:
mov r2, #32
strb r2, [r1, r0]
add r0, #1
cmp r0, #10
blt add_filler_spaces
finish_spaces:
pop {r0, pc}
/* // Write to File Save for later
mov r0, r4
mov32 r1, userName
mov r2, #6
mov r7, #WRITE
svc #0*/
.balign 4
.text
.global hit
hit:
push {r0, r1}
// This function will count a kill point :)
mov32 r0, user_score
ldr r1, [r0] @ Here we are De-Referencing
add r1, r1, #1 @ Adding a Kill point
@ Now add the new value back into label
str r1, [r0]
pop {r0, r1}
bx lr
.balign 4
.text
.global score_to_ascii
score_to_ascii:
push {r3-r7, lr}
// r4 already has score in decimal already
mov r5, #0x3
mov r6, #10
loop:
mov r8, r2
sdiv r2, r2, r6
mul r7, r6, r2
subs r7, r8, r7
add r7, #48
strb r7, [r3, r5]
subs r5, #1
cmp r2, #1
bge loop
mov r6, #32
cmp r5, #0
strgeb r6, [r3, r5]
sub r5, #1
cmp r5, #0
strgeb r6, [r3, r5]
sub r5, #1
cmp r5, #0
strgeb r6, [r3, r5]
// This will add a newline athe end of the line
mov r2, #10
add r3, #4
str r2, [r3]
add r3, #1
pop {r3-r7, pc}
.balign 4
.text
.global _start
_start:
push {r4-r9}
bl open_file // returns the file descriptor in r0
mov r6, r0 // Need to save the file descriptor else where
bl unload_file // load file descriptor and content into mmap2. Also returns the mmap address in r4
blne display_scores
bleq display_empty_message
bl did_user_place // Did the user place in the top three
cmp r1, #1
bleq get_name
mov r1, r3 // move counter ro r1 instead
mov r2, r4 // move usersScore to r2 instead
blne sorry_prompt // if r1 != 1 then they did place into top score file
bleq add_new_score
bl display_scores
bl close_all_files
pop {r4-r9}
mov r7, #EXIT
svc #0
open_file:
mov32 r0, filename
movw r1, #FILE_FLAGS
movw r2, #00644 // Permission for File number 644 ultimate power
mov r7, #OPEN
svc #0
bx lr
unload_file:
// First allocate 2k of memory
mov r0, #0
mov r1, #2000
mov r2, #MAP_PROT
mov r3, #MAP_SHARED
mov r4, r6
mov r5, #0
mov r7, #MMAP2
svc #0
mov r4, r0 // Keep the address of mmap.
mov r5, r0 // delete later maybe this is for the display message
// We can close the file here and keep mmap open.
mov r0, r6 // r6 = file descriptor
mov r7, #CLOSE
svc #0
// Load first byte from file and check to see if there is information
ldrb r1, [r4]
// If r0 = 0, then file is empty :: if r0 = any #, then file contains something
cmp r1, #0
bx lr
@ This functio sets variables some variables will change others wont
did_user_place:
push {r4, lr}
mov r0, r4 // mmap memory address
mov r1, #0 // This is a flag if r1 = 0 then did not make top 3, else if r1 = 1 then user placed in top
mov r2, #0 // This will contain the score in file byte at a time
mov r3, #0 // This is our counter
mov32 r4, user_score // This is used to compare scores
ldr r4, [r4]
mov r5, #0 // number from file after r2 conversion
mov r6, #10 // For multiplication purposes
mov r7, #0
while_loop:
ldrb r2, [r0, r3]
cmp r2, #57
movgt r2, #100
cmp r2, #32
moveq r2, #100
// If at the end of the line then lets compare scores
cmp r2, #10
bleq compare_scores
// If Equal then we are at end of File
cmp r2, #0
beq finish_loop
add r3, #1 // add counter
// Loop from the begining if number did not exist
cmp r2, #100
beq while_loop
// Go ahead and convert user score into a register
mul r5, r6, r5
subs r2, #48
add r5, r2, r5
b while_loop
finish_loop:
pop {r2, pc}
compare_scores:
push {r4, lr}
cmp r4, r5 // r4 = user score, r5 = file score "one of the file scores"
movle r2, #100 // This will kick out of the while_loop
movgt r2, #0
movgt r1, #1 // This indicates that the user placed in the top 3
mov r5, #0 // reset this value otherwise things will go off when converting
pop {r4, pc}
@ At this point r0 = initial memory in mmap
@ r1 = counter
@ r2 = users score
add_new_score:
push {r0, lr}
mov32 r3, file_info // Temporary memory holder in .bss section
mov32 r4, user_name // Get user name in ascii style memory aka sucks
mov r5, #0
sub r1, r1, #14 // This shifts the pointer of memory to the begining of a line
adding_score_loop:
cmp r1, r5
bne copy_old_info
beq add_new_info
done_adding_score:
mov r6, #0
str r6, [r3]
pop {r0, pc}
copy_old_info:
cmp r5, #45
bge done_adding_score
ldm r0, {r8-r11} // Load 16 Bytes BUT our line is only 15 bytes
stm r3, {r8-r11} // Store 16 bytes
add r5, #15 // This will take care of the extra byte above
add r0, #15 // Move pointer from mmap
add r3, #15 // Move pointer from .bss section
cmp r5, #45
b adding_score_loop
add_new_info:
ldm r4, {r8-r10}
stm r3, {r8-r10}
add r3, #10
bl score_to_ascii // returns address of ascii number in r2
add r5, #15
add r3, #5
cmp r5, #45
bge done_adding_score
b adding_score_loop
sorry_prompt:
mov r0, #STDOUT
mov32 r1, sorry_message
mov r2, #sorry_message_Len
mov r7, #WRITE
svc #0
display_scores:
push {r0, lr}
mov r0, #20 // Middle
mov r1, #30 // Screen
@Add this later bl locate
// Print top 3 score from file pulled from memory for now.
mov r0, #STDOUT
mov32 r1, file_info//r5
mov r2, #100
mov r7, #WRITE
svc #0
pop {r0, pc}
display_empty_message:
push {r4, lr}
mov r0, #20 // Middle
mov r1, #30 // Screen
@Add this later bl locate
mov r0, #STDOUT
mov32 r1, empty_mess
mov r2, #empty_mess_Len
mov r7, #WRITE
svc #0
pop {r4, pc}
close_all_files:
sub r3, #45
ldm r3, {r4-r11}
stm r0, {r4-r11}
mov r0, r0
movw r1, #2000
mov r7, #MUNMMAP
svc #0
bx lr