forked from christianhujer/expensereport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpenseReport.S
290 lines (252 loc) · 5.46 KB
/
ExpenseReport.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
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
.equ STDOUT, 1
.equ EXIT_SUCCESS, 0
.equ write, 1
.equ exit, 60
.equ DINNER, 1
.equ BREAKFAST, 2
.equ CAR_RENTAL, 3
.text
.global _start
_start:
endbr64
leaq expenses(%rip),%rdi
call printReport
mov $EXIT_SUCCESS,%rdi
mov $exit,%rax
syscall
printReport:
endbr64
push %rbp // current expense pointer
push %r15 // current expense type
push %r14 // current expense amount
push %r13 // total
push %r12 // meals
mov %rdi,%rbp
xor %r13,%r13
xor %r12,%r12
mov $STDOUT,%rdi
leaq header(%rip),%rsi
mov $headerLen,%rdx
mov $write,%rax
syscall
.loop:
mov (%rbp),%r15
cmp $0,%r15
jz .loopDone
add $8,%rbp
mov (%rbp),%r14
add $8,%rbp
cmp $DINNER,%r15
jz .mealExpense
cmp $BREAKFAST,%r15
jz .mealExpense
jmp .notMeal
.mealExpense:
add %r14,%r12
.notMeal:
.printExpenseName:
lea expenseNames(%rip), %rsi
movq (%rsi, %r15, 8), %rsi
lea expenseNameLengths(%rip), %rdx
movq (%rdx, %r15, 8), %rdx
mov $STDOUT,%rdi
mov $write,%rax
syscall
mov $STDOUT,%rdi
leaq tab(%rip),%rsi
mov $tabLen,%rdx
mov $write,%rax
syscall
mov %r14,%rsi
leaq buffer(%rip),%rdi
call itoa10
mov %rax,%rsi
xor %al,%al
xor %rdx,%rdx
.lenLoop:
mov (%rsi,%rdx),%al
cmp $0, %al
jz .lenDone
inc %rdx
jmp .lenLoop
.lenDone:
mov $STDOUT,%rdi
mov $write,%rax
syscall
mov $STDOUT,%rdi
leaq tab(%rip),%rsi
mov $tabLen,%rdx
mov $write,%rax
syscall
cmp $DINNER,%r15
jnz .notDinner
.dinner:
cmp $5000,%r14
jg .mealOverExpense
jmp .mealNotOverExpense
.notDinner:
cmp $BREAKFAST,%r15
jnz .notBreakfast
cmp $1000,%r14
jg .mealOverExpense
jmp .mealNotOverExpense
.mealOverExpense:
leaq mealOverExpensesMarker(%rip), %rsi
jmp .printMealOverExpensesMarker
.notBreakfast:
.mealNotOverExpense:
leaq mealNotOverExpensesMarker(%rip), %rsi
.printMealOverExpensesMarker:
mov $1,%rdx
mov $STDOUT,%rdi
mov $write,%rax
syscall
mov $STDOUT,%rdi
leaq newline(%rip),%rsi
mov $newlineLen,%rdx
mov $write,%rax
syscall
add %r14,%r13
jmp .loop
.loopDone:
mov $STDOUT,%rdi
leaq meals(%rip),%rsi
mov $mealsLen,%rdx
mov $write,%rax
syscall
mov %r12,%rsi
leaq buffer(%rip),%rdi
call itoa10
mov %rax,%rsi
xor %al,%al
xor %rdx,%rdx
.lenLoop2:
mov (%rsi,%rdx),%al
cmp $0, %al
jz .lenDone2
inc %rdx
jmp .lenLoop2
.lenDone2:
mov $STDOUT,%rdi
mov $write,%rax
syscall
mov $STDOUT,%rdi
leaq newline(%rip),%rsi
mov $newlineLen,%rdx
mov $write,%rax
syscall
mov $STDOUT,%rdi
leaq total(%rip),%rsi
mov $totalLen,%rdx
mov $write,%rax
syscall
mov %r13,%rsi
leaq buffer(%rip),%rdi
call itoa10
mov %rax,%rsi
xor %al,%al
xor %rdx,%rdx
.lenLoop3:
mov (%rsi,%rdx),%al
cmp $0, %al
jz .lenDone3
inc %rdx
jmp .lenLoop3
.lenDone3:
mov $STDOUT,%rdi
mov $write,%rax
syscall
mov $STDOUT,%rdi
leaq newline(%rip),%rsi
mov $newlineLen,%rdx
mov $write,%rax
syscall
pop %r12
pop %r13
pop %r14
pop %r15
pop %rbp
ret
// Converts a number into a decimal string.
// @param %rdi buffer in which to convert the number.
// @param %rsi number to convert
// @return %rax buffer from %rdi
itoa10:
endbr64
push %rdi
cld
mov $10,%rcx
mov %rsi,%rax
mov %rdi,%rsi
.itoa10_loop:
xor %rdx,%rdx
div %rcx
add $'0', %rdx
mov %dl, (%rdi)
inc %rdi
cmp $0, %rax
jnz .itoa10_loop
xor %dl,%dl
mov %dl, (%rdi)
dec %rdi
.reverse:
cmp %rsi,%rdi
jle .reverseDone
mov (%rsi),%al
mov (%rdi),%ah
mov %ah,(%rsi)
mov %al,(%rdi)
dec %rdi
inc %rsi
jmp .reverse
.reverseDone:
pop %rax
ret
.data
header:
.ascii "Expense Report\n"
headerLen = . - header
meals:
.ascii "Meal Expenses: "
mealsLen = . - meals
total:
.ascii "Total Expenses: "
totalLen = . - total
tab:
.ascii "\t"
tabLen = . - tab
newline:
.ascii "\n"
newlineLen = . - newline
mealOverExpensesMarker:
.ascii "X"
mealNotOverExpensesMarker:
.ascii " "
dinner:
.ascii "Dinner"
dinnerLen = . - dinner
breakfast:
.ascii "Breakfast"
breakfastLen = . - breakfast
carRental:
.ascii "Car Rental"
carRentalLen = . - carRental
expenseNames:
.quad 0
.quad dinner
.quad breakfast
.quad carRental
expenseNameLengths:
.quad 0
.quad dinnerLen
.quad breakfastLen
.quad carRentalLen
expenses:
.quad DINNER, 5000
.quad DINNER, 5001
.quad BREAKFAST, 1000
.quad BREAKFAST, 1001
.quad CAR_RENTAL, 4
.quad 0, 0
buffer:
.fill 11,1,0