-
Notifications
You must be signed in to change notification settings - Fork 14
/
code_c9.asm
executable file
·377 lines (368 loc) · 11.7 KB
/
code_c9.asm
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
;/***************************************
;|GROUP-09|
;Aditya Upadhyay - 2017A7PS0083P
;Harpider Jot Singh - 2017A7PS0057P
;Jaladi Lakshmi Teja - 2017A7PS0068P
;Vishal Mittal - 2017A7PS0080P
;Yash Vijay - 2017A7PS0072P
;*****************************************
extern printf, scanf, exit
section .data
ffour_reg_fmt: db `RAX = %ld, RBX = %ld, RCX = %ld, RDX = %ld`, 10, 0
lfour_reg_fmt: db `RSP = %ld, RBP = %ld, RSI = %ld, RDI = %ld\n`, 10, 0
int_fmt: db "%d", 0
real_fmt: db "%lf", 0
bool_fmt: db "%d", 0
str_fmt: db "%s",10, 0
str_no_new_line_fmt: db "%s",0
section .text
; Push all registers on stack to prevent value loss
%macro push_all 0
push RAX
push RBX
push RCX
push RDX
push RSP
push RBP
push RSI
push RDI
%endmacro
; Restore values of registers from stack
%macro pop_all 0
pop RDI
pop RSI
pop RBP
pop RSP
pop RDX
pop RCX
pop RBX
pop RAX
%endmacro
; Print RAX, RBX, RCX, RDX values
%macro print_first_four 0
push_all
push RDX
push RCX
push RBX
push RAX
mov RDI, ffour_reg_fmt ; first arg, format
pop RSI
pop RDX
pop RCX
pop R8
mov RAX, 0
align_16_rsp ;align RSP to 16 byte boundary
call printf
remove_align_16_rsp ;realign RSP
pop_all
%endmacro
; Print RSP, RBP, RSI, RDI values
%macro print_last_four 0
push_all
push RDI
push RSI
push RBP
push RSP
mov RDI, lfour_reg_fmt ; first arg, format
pop RSI
add RSI, 88 ; 88 because RSP has pushed additional 11 registers(8 in push_all, and 4 after that), so original value is 88 more than current
pop RDX
pop RCX
pop R8
mov RAX, 0
align_16_rsp ;align RSP to 16 byte boundary
call printf
remove_align_16_rsp ;realign RSP
pop_all
%endmacro
%macro print_str 1
section .data
%%str db %1,0 ;% is to make label local to this macro, to enable mutiple calls within same label
section .text
push_all
mov RDI, str_fmt ;"%s\n"
mov RSI, %%str
mov RAX, 0
align_16_rsp ;align RSP to 16 byte boundary
call printf
remove_align_16_rsp ;realign RSP
pop_all
%endmacro
%macro print_str_no_new_line 1
section .data
%%str db %1,0 ;% is to make label local to this macro, to enable mutiple calls within same label
section .text
push_all
mov RDI, str_no_new_line_fmt ;"%s"
mov RSI, %%str
mov RAX, 0
align_16_rsp ;align RSP to 16 byte boundary
call printf
remove_align_16_rsp ;realign RSP
pop_all
%endmacro
%macro align_16_rsp 0
push RBX
push RDX
push RAX
mov RDX, 0
mov RAX, RSP
mov RBX, 16
idiv RBX
mov RBX, RDX
pop RAX
pop RDX
sub RSP, RBX
%endmacro
%macro remove_align_16_rsp 0
add RSP, RBX
pop RBX
%endmacro
global main
main:
ENTER 104, 0
;reserve space for the input/output params of fn, later flush this space
sub RSP, 0; allocated space for i/o variables on stack
push_all
;Assignment to a INTEGER
mov RAX, 19
mov [RBP - 8], RAX
pop_all
label_1:
push_all
;Assignment to a INTEGER
mov RAX, 56
mov [RBP - 16], RAX
pop_all
label_3:
push_all
;Assignment to a INTEGER
mov RAX, 3
mov [RBP - 32], RAX
pop_all
label_4:
push_all
mov RCX, 0; initialize RCX with false
mov RDX, 1 ; assign RDX, a value true to assign to result if condition satisfies
;Comparison (LE) of integers
mov RAX, [RBP - 32]
mov RBX, 6
cmp RAX, RBX
cmovle RCX, RDX
mov [RBP - 40], RCX
pop_all
push_all
;jmp if true code
mov RAX, [RBP - 40]
sub RAX, 1
jz label_5
pop_all
jmp main_end
label_5:
; Code to take input value(s) of x
push_all
print_str ""
print_str "Input: Enter an integer value"
mov RDI, int_fmt ;first arg, format "%d"
mov RDX, RBP
sub RDX, 0 ; make RDX to point at location of variable on the stack
mov RAX, 0x0000_0000_ffff_ffff ;machine has sizeof(int) to be 4, for uniformity, We are taking
mov [RDX] , RAX ;our sizeof(int) to be 8, now scanf will just enter values in lower 32 bits
;So, we are firstly clearing upper 32 bits of memory so as to access data properly later
mov RSI, RDX
mov RAX, 0
align_16_rsp ;align RSP to 16 byte boundary for scanf call
call scanf
remove_align_16_rsp ;realign it to original position
pop_all
label_8:
jmp label_10
label_11:
push_all
;Multiplication of integers
mov RAX, [RBP - 0]
mov RBX, 2
mul RBX
mov [RBP - 48], RAX
pop_all
push_all
;Subtraction of integers
mov RAX, [RBP - 48]
mov RBX, [RBP - 8]
sub RAX, RBX
mov [RBP - 56], RAX
pop_all
push_all
;Addition of integers
mov RAX, [RBP - 56]
mov RBX, [RBP - 32]
add RAX, RBX
mov [RBP - 64], RAX
pop_all
push_all
;Assignment to a INTEGER
mov RAX, [RBP - 64]
mov [RBP - 8], RAX
pop_all
label_13:
; Code to output value(s) of y
print_str ""
print_str_no_new_line "Output: "
push_all
mov RDI, int_fmt ;first arg, format "%d"
mov RDX, RBP
sub RDX, 8 ; make RDX to point at location of variable on the stack
mov RSI, [RDX] ; for integer, value stored at offset should be passed to printf
mov RAX, 0
push_all
align_16_rsp ;align RSP to 16 byte boundary for printf call
call printf
remove_align_16_rsp ;realign RSP
pop_all
pop_all
print_str "" ;print a newline after output
jmp label_9
label_19:
push_all
;Multiplication of integers
mov RAX, [RBP - 0]
mov RBX, 3
mul RBX
mov [RBP - 72], RAX
pop_all
push_all
;Subtraction of integers
mov RAX, [RBP - 72]
mov RBX, [RBP - 16]
sub RAX, RBX
mov [RBP - 80], RAX
pop_all
push_all
;Addition of integers
mov RAX, [RBP - 80]
mov RBX, [RBP - 32]
add RAX, RBX
mov [RBP - 88], RAX
pop_all
push_all
;Assignment to a INTEGER
mov RAX, [RBP - 88]
mov [RBP - 16], RAX
pop_all
label_21:
; Code to output value(s) of z
print_str ""
print_str_no_new_line "Output: "
push_all
mov RDI, int_fmt ;first arg, format "%d"
mov RDX, RBP
sub RDX, 16 ; make RDX to point at location of variable on the stack
mov RSI, [RDX] ; for integer, value stored at offset should be passed to printf
mov RAX, 0
push_all
align_16_rsp ;align RSP to 16 byte boundary for printf call
call printf
remove_align_16_rsp ;realign RSP
pop_all
pop_all
print_str "" ;print a newline after output
jmp label_9
label_27:
; Code to output value(s) of x
print_str ""
print_str_no_new_line "Output: "
push_all
mov RDI, int_fmt ;first arg, format "%d"
mov RDX, RBP
sub RDX, 0 ; make RDX to point at location of variable on the stack
mov RSI, [RDX] ; for integer, value stored at offset should be passed to printf
mov RAX, 0
push_all
align_16_rsp ;align RSP to 16 byte boundary for printf call
call printf
remove_align_16_rsp ;realign RSP
pop_all
pop_all
print_str "" ;print a newline after output
jmp label_9
label_10:
push_all
mov RCX, 0; initialize RCX with false
mov RDX, 1 ; assign RDX, a value true to assign to result if condition satisfies
;Comparison (EQ) of integers
mov RAX, 1
mov RBX, [RBP - 0]
cmp RAX, RBX
cmove RCX, RDX
mov [RBP - 96], RCX
pop_all
push_all
;jmp if true code
mov RAX, [RBP - 96]
sub RAX, 1
jz label_11
pop_all
push_all
mov RCX, 0; initialize RCX with false
mov RDX, 1 ; assign RDX, a value true to assign to result if condition satisfies
;Comparison (EQ) of integers
mov RAX, 2
mov RBX, [RBP - 0]
cmp RAX, RBX
cmove RCX, RDX
mov [RBP - 96], RCX
pop_all
push_all
;jmp if true code
mov RAX, [RBP - 96]
sub RAX, 1
jz label_19
pop_all
jmp label_27
label_9:
; Code to output value(s) of y
print_str ""
print_str_no_new_line "Output: "
push_all
mov RDI, int_fmt ;first arg, format "%d"
mov RDX, RBP
sub RDX, 8 ; make RDX to point at location of variable on the stack
mov RSI, [RDX] ; for integer, value stored at offset should be passed to printf
mov RAX, 0
push_all
align_16_rsp ;align RSP to 16 byte boundary for printf call
call printf
remove_align_16_rsp ;realign RSP
pop_all
pop_all
print_str "" ;print a newline after output
label_31:
; Code to output value(s) of z
print_str ""
print_str_no_new_line "Output: "
push_all
mov RDI, int_fmt ;first arg, format "%d"
mov RDX, RBP
sub RDX, 16 ; make RDX to point at location of variable on the stack
mov RSI, [RDX] ; for integer, value stored at offset should be passed to printf
mov RAX, 0
push_all
align_16_rsp ;align RSP to 16 byte boundary for printf call
call printf
remove_align_16_rsp ;realign RSP
pop_all
pop_all
print_str "" ;print a newline after output
push_all
;Addition of integers
mov RAX, [RBP - 32]
mov RBX, 1
add RAX, RBX
mov [RBP - 32], RAX
pop_all
jmp label_4
main_end:
;Deallocate space given to I/O variables on stack
add RSP, 0
LEAVE
ret