-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinter.s
277 lines (236 loc) · 6.46 KB
/
printer.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
NULL EQU 0
TRUE EQU 1
FALSE EQU 0
NEW_LINE_TERMINATOR EQU 10
NULL_TERMINATOR EQU 0
STK_UNIT EQU 4
%define align_on(n, base) (((n)-1)-(((n)-1)%(base)))+(base)
%define align_on_16(n) align_on(n, 16)
; SIGNATURE: func_entry(n = 0)
; n - locals size (in bytes). default is 0
; DESCRIPTION: Prepares the stack frame before executing the function
; esp -= n
%macro func_entry 0-1 0
%push func_entry
%define %$_stack_reserve_size align_on_16(%1)
push ebp
mov ebp, esp
%if %$_stack_reserve_size
sub esp, %$_stack_reserve_size
%endif
pushfd
pushad
%endmacro
; SIGNATURE: func_exit(p_ret_val = eax)
; p_ret_val - A place to put function return value. default is eax
; DESCRIPTION: cleans stack frame before exiting the function (after function execution)
%macro func_exit 0-1 eax
popad
popfd
%ifidn %1, eax
%else
mov eax, %1
%endif
%if %$_stack_reserve_size
mov esp, ebp
%endif
pop ebp
ret
%pop
%endmacro
; SIGNATURE: func_call(p_ret_val, func, ... args)
; p_ret_val - A place to put function return value
; func - the function to call
; args - list of arguments to pass to the function
; DESCRIPTION: calls the function <func> with args <args> and puts the return value in <p_ret_val>
; EXAMPLE:
; func_call [r], fgets, ebx, MAX_LINE_LENGTH, [stdin]
; the above is semantically equivalent to:
; [r] = fgets(ebx, MAX_LINE_LENGTH, [stdin])
%macro func_call 2-*
%push
%define %$args_size (%0-2)*STK_UNIT
%define %$args_size_aligned align_on_16(%$args_size)
%define %$push_size_aligned_complement (%$args_size_aligned - %$args_size)
%if %$push_size_aligned_complement
sub esp, %$push_size_aligned_complement
%endif
%rep %0-2
%rotate -1
push dword %1
%endrep
%rotate -1
call %1
%rotate -1
%ifidn %1, eax
%else
mov %1, eax
%endif
%if %$args_size_aligned
add esp, %$args_size_aligned
%endif
%pop
%endmacro
%macro void_call 1-*
func_call eax, %{1:-1}
%endmacro
; SIGNATURE: printf_inline(format_str, ... args)
; DESCRIPTION: calls printf with format_str followed by a null terminator with the specified args
; EXAMPLE:
; printf_inline "%d", [r]
; the above is semantically equivalent to:
; printf("%d", [r])
%macro printf_inline 1-*
section .rodata
%%format: db %1, NULL_TERMINATOR
section .text
%if %0-1
void_call printf, %%format, %{2:-1}
%else
void_call printf, %%format
%endif
%endmacro
; SIGNATURE: printf_line(format_str, ... args)
; DESCRIPTION: calls printf with format_str followed by new line and null terminator with the specified args
; EXAMPLE:
; printf_line "%d", [r]
; the above is semantically equivalent to:
; printf("%d\n", [r])
%macro printf_line 1-*
%if %0-1
printf_inline {%1, NEW_LINE_TERMINATOR}, %{2:-1}
%else
printf_inline {%1, NEW_LINE_TERMINATOR}
%endif
%endmacro
%macro fprintf_inline 2-*
section .rodata
%%format: db %2, NULL_TERMINATOR
section .text
%if %0-2
void_call fprintf, %1, %%format, %{3:-1}
%else
void_call fprintf, %1, %%format
%endif
%endmacro
%macro fprintf_line 2-*
%if %0-2
fprintf_inline %1, {%2, NEW_LINE_TERMINATOR}, %{3:-1}
%else
fprintf_inline %1, {%2, NEW_LINE_TERMINATOR}
%endif
%endmacro
; SIGNATURE: mem_mov(m1, m2, r)
; DESCRIPTION: m1 = r = m2
; EXAMPLE: mem_mov ebx, [ebp-4], [ebp+8]
; This will copy the value at the memory address ebp+8 to ebp-4
; while using ebx as an intermediate place to store the result of [ebp+8]
; NOTES:
; * This can be used to transfer from memory to memory
; while specifying the intermediate register used
; (but can also be used with any arbitrary combination of registers and memory)
; * If used for transfer for memory to memory,
; the register implicitly determines the operand's sizes
; * Operand sizes can also be specified explicitly
%macro mem_mov 3
mov %3, %2
mov %1, %3
%endmacro
%macro mem_mov 2
mem_mov %1, %2, eax
%endmacro
%macro dbg_print_line 1-*
%%dbg_print:
; if (DebugMode) printf(args);
cmp dword [DebugMode], FALSE
je %%not_debug_mode
; print info
fprintf_line [stderr], %{1:-1}
%%not_debug_mode:
%endmacro
%macro mem_double_mov 2-3 eax
mem_mov [%1], [%2], %3
mem_mov [%1+4], [%2+4], %3
%endmacro
%macro print_double 1
void_call printf, FloatPrintFormat, [%1], [%1+4]
%endmacro
%macro dbg_print_double 2
%%dbg_print_double:
cmp dword [DebugMode], FALSE
je %%not_debug_mode
fprintf_line [stderr], {%1, "%.2f"}, [%2], [%2+4]
%%not_debug_mode:
%endmacro
%macro dbg_print_double_st 0
%%dbg_print_double_st:
cmp dword [DebugMode], FALSE
je %%not_debug_mode
sub esp, 8
fst qword [esp]
push FloatPrintFormat_NewLine
call printf
add esp, 12
%%not_debug_mode:
%endmacro
%define drone_x(d) d+0
%define drone_y(d) d+8
%define drone_speed(d) d+16
%define drone_angle(d) d+24
%define drone_score(d) d+32
%define drone_is_active(d) d+36
sizeof_drone EQU 40
section .rodata
f_printer: db "Printer %d", 10, 0
section .text
extern printf
extern fprintf
extern sscanf
extern stdin
extern stdout
extern stderr
extern malloc
extern calloc
extern free
extern resume
extern CoId_Scheduler
extern N
extern TargetPosition
extern DronesArr
global printer_co_func
printer_co_func:
push ebp
mov ebp, esp
sub esp, 4
%define $index ebp-4
.start_print:
; print target location x,y
printf_line "%.2f,%.2f", [TargetPosition], [TargetPosition+4], [TargetPosition+8], [TargetPosition+12]
mov dword [$index], 0
;while(ecx < N)
.print_drones_data_start:
mov ecx, dword [$index]
cmp ecx, dword [N]
jge .print_drones_data_start_end
; eax = drone id
mov eax, ecx
inc eax
; ebx = DroneArr[ecx]
mov ebx, dword [DronesArr]
mov ebx, [ebx + 4*ecx]
printf_inline "%d,", eax
printf_inline "%.2f,", [drone_x(ebx)], [drone_x(ebx) + 4]
printf_inline "%.2f,", [drone_y(ebx)], [drone_y(ebx) + 4]
printf_inline "%.2f,", [drone_angle(ebx)], [drone_angle(ebx) + 4]
printf_inline "%.2f,", [drone_speed(ebx)], [drone_speed(ebx) + 4]
printf_inline "%d", [drone_score(ebx)]
printf_line ""
.check_loop_a:
inc dword [$index]
jmp .print_drones_data_start
.print_drones_data_start_end:
mov ebx, [CoId_Scheduler]
call resume
jmp .start_print
add esp, 4
pop ebp