-
Notifications
You must be signed in to change notification settings - Fork 2
/
rtl64macOS.s
563 lines (442 loc) · 15.6 KB
/
rtl64macOS.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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# How to build:
# as rtl64.s -o rtl64.o
# ld rtl64.o -e _main -o rtl64 -lSystem
# ./rtl64
#
#==========================================
#------------------data--------------------
#==========================================
.section __DATA,__data
RTLWriteIntegerBuffer:
.byte 0x3c, 0x32, 0x30, 0x30, 0x36, 0x5f, 0x42, 0x54, 0x50, 0x43, 0x5f
.byte 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x5f, 0x42, 0x65, 0x6e, 0x6a
.byte 0x61, 0x6d, 0x69, 0x6e, 0x5f, 0x52, 0x6f, 0x73, 0x73, 0x65, 0x61
.byte 0x75, 0x78, 0x5f, 0x62, 0x65, 0x6e, 0x6a, 0x61, 0x6d, 0x69, 0x6e
.byte 0x40, 0x72, 0x6f, 0x73, 0x73, 0x65, 0x61, 0x75, 0x78, 0x2e, 0x63
.byte 0x6f, 0x6d, 0x3e, 0x3c, 0x32, 0x30, 0x31, 0x37, 0x5f, 0x4c, 0x69
.byte 0x6e, 0x75, 0x78, 0x50, 0x6f, 0x72, 0x74, 0x5f, 0x61, 0x75, 0x74
.byte 0x68, 0x6f, 0x72, 0x5f, 0x41, 0x6e, 0x74, 0x68, 0x6f, 0x6e, 0x79
.byte 0x5f, 0x42, 0x65, 0x6c, 0x79, 0x61, 0x65, 0x76, 0x5f, 0x61, 0x6e
.byte 0x74, 0x6f, 0x6e, 0x73, 0x64, 0x6c, 0x40, 0x67, 0x6d, 0x61, 0x69
.byte 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x3e
ReadCharBuffer:
.byte 0x3c
ReadCharInited:
.byte 0
IsEOF:
.byte 0
RTLFunctionTable:
.quad RTLHalt
.quad RTLWriteChar
.quad RTLWriteInteger
.quad RTLWriteLn
.quad RTLReadChar
.quad RTLReadInteger
.quad RTLReadLn
.quad RTLEOF
.quad RTLEOLN
Copyright:
#==========================================
#-------------------bss--------------------
#==========================================
.section __BSS,__bss
.macro pushall
pushq %rdi
pushq %rsi
pushq %rbp
pushq %rsp
pushq %rdx
pushq %rcx
pushq %rbx
pushq %rax
.endm
.macro popall
popq %rax
popq %rbx
popq %rcx
popq %rdx
popq %rsp
popq %rbp
popq %rsi
popq %rdi
.endm
.macro parleft
pushq $'{'
call RTLWriteChar
.endm
.macro parright
pushq $'}'
call RTLWriteChar
.endm
.macro space
pushq $' '
call RTLWriteChar
.endm
.macro raxchar
pushq %rax
call RTLWriteChar
.endm
# .macro raxint
# pushq %rax
# pushq $1
# #pushq $1
# call RTLWriteInteger
# #addq $16, %rsp
#.endm
#==========================================
#------------------text--------------------
#==========================================
.section __TEXT,__text
.global _main
_main:
jmp StubEntryPoint
/*
1. X RTLHalt — остановка программы,
2. X RTLWriteChar — запись char’а на stdout,
3. X RTLWriteInteger — запись целого на stdout, принимает два параметра: число и ширину вывода,
4. X RTLWriteLn — выводит на stdout символ новой строки (13, 10),
5. X RTLReadChar — считывает символ из stdin, результат кладёт в EAX,
6. X RTLReadInteger — считывает целое из stdin, результат кладёт в EAX,
7. X RTLReadLn — пропускает стандартный ввод до конца файла или ближайшего перевода строки,
8. X RTLEOF — возвращает в EAX число 1, если достигнут конец файла (следующий символ прочитать невозможно) или 0 в противном случае,
9. X RTLEOLN — возвращает в EAX число 1, если следующий символ \n, 0 — в противном случае.
**/
#------------------------------------------
#----------------WriteChar-----------------
#------------------------------------------
RTLWriteChar:
pushall
movq %rsp, %rbp #make stack frame
movq $0x2000004, %rax #syscall #4 == Write();
# movq $1, %rax #syscall #1 == Write();
movq $1, %rdi #param1 == write_to == 1 == stdout
movq %rbp, %rsi #p2 == write_from == %rbp == top_of_stack
addq $72, %rsi #reach top of stack:[0-ret,8-arg1,16-arg2]
#we have all regs pushed so top is really really far
movq $1, %rdx #p3 == count == single_byte
syscall
popall
ret $8
#------------------------------------------
#--------------WriteInteger----------------
#------------------------------------------
RTLWriteInteger:
pushq %rsi
#pushq %rbp
#movq %rsp, %rbp
movq 16(%rsp), %rbx #arg: count (stdout width). we do NOT care if it == 1
movq 24(%rsp), %rax #arg: num
cmpq $0, %rax
jnl RTLWriteIntegerNotSigned
negq %rax
decq %rbx
pushq $'-'
call RTLWriteChar
RTLWriteIntegerNotSigned:
xorq %rcx, %rcx
pushq %rax
pushq %rbx
RTLWriteIntegerPreCheckLoop:
testq %rax, %rax
jz RTLWriteIntegerPreCheckLoopDone
incq %rcx
movq $10, %rbx
xorq %rdx, %rdx
idiv %rbx
jmp RTLWriteIntegerPreCheckLoop
RTLWriteIntegerPreCheckLoopDone:
testq %rcx, %rcx
setz %dl #dl: (0 == rcx) ? 1 : 0
orb %dl, %cl
popq %rbx
popq %rax
subq %rcx, %rbx
cmpq $0, %rbx
jle RTLWriteIntegerNotPadding
pushq %rcx
RTLWriteIntegerPaddingLoop:
pushq $' '
call RTLWriteChar
decq %rbx
jnz RTLWriteIntegerPaddingLoop
popq %rcx
RTLWriteIntegerNotPadding:
#find last digit's address:
#we write from right to left cuz each time we divide number by 10, we only know it's right-most digit
#so we need to write right-most digit into right-most byte:
#e.g: num=-123; count=4
# | < | = | > | | < |...
#init buffer: |0x3b|0x3d|0x3e|0x00|0x3b|...
#1st iter: |0x3b|0x3d| 3d |0x00|0x3b|...
#2st iter: |0x3b| 2d | 3d |0x00|0x3b|...
#3st iter: | 1d | 2d | 3d |0x00|0x3b|...
movq RTLWriteIntegerBuffer@GOTPCREL(%rip), %rdi
# movq $RTLWriteIntegerBuffer, %rdi #leaq RTLWriteIntegerBuffer-1(%rcx), %rdi
addq %rcx, %rdi #-||-
decq %rdi #-||-
#LEA EDI,[OFFSET RTLWriteIntegerBuffer+ECX-1]
pushq %rcx #we dont care if (count < real_num_width)
RTLWriteIntegerLoop:
movq $10, %rsi
xorq %rdx, %rdx
idiv %rsi
#convert to string
movq %rdx, %rbx # ~= lea '0'(%rdx), %rbx
addq $'0', %rbx
movb %bl, (%rdi)
decq %rdi
loop RTLWriteIntegerLoop
popq %rcx
movq $0x2000004, %rax # syscall 4 == Write
movq $1, %rdi # p1, write_to == 1 == stdout
movq RTLWriteIntegerBuffer@GOTPCREL(%rip), %rsi # p2, write_from == buf addr
movq %rcx, %rdx # p3, count
syscall
#popq %rbp
popq %rsi
ret $16 #take ret addr from stack and remove 2 args
#------------------------------------------
#-----------------WriteLn------------------
#------------------------------------------
RTLWriteLn:
pushq $13 #13 == 0xD == CR
call RTLWriteChar
#addq $8, %rsp
pushq $10 #10 == 0xA == LF
call RTLWriteChar
#addq $8, %rsp
ret
ReadCharEx:
pushall
#movq %rsp, %rbp
#movq %rax, %rbx #copy to %rbx cuz %rax will be used for syscall
movq $0x2000003, %rax #syscall #3 == Read();
xorq %rdi, %rdi #p1 == read_from == 0 == stdin
movq ReadCharBuffer@GOTPCREL(%rip), %rsi #p2 == write_to == buffer
movq $1, %rdx #p3 == count == single_byte
syscall
#test against value read on prev. step
#beware of empty %rbx if ReadCharEx is the first function to be called
#testq %rbx, %rbx
#setz %bl #al: (0 == rbx) ? 1:0
#orb %bl, (IsEOF)
#test against num of bytes that were actually read
cmpq $0, %rax
setz %bl #bl: (0 == bytes_read) ? 1:0
mov IsEOF@GOTPCREL(%rip), %r8 # address of IsEOF in r8
xor %rcx, %rcx
orb %bl, (%r8)
popall
ret
ReadCharInit:
mov ReadCharInited@GOTPCREL(%rip), %r8 # address of ReadCharInited in r8
cmpb $0, (%r8)
jnz ReadInitDone
call ReadCharEx
movq ReadCharInited@GOTPCREL(%rip), %r8 # address of ReadCharInited in r8
movb $1, (%r8)
ReadInitDone:
ret
#------------------------------------------
#----------------ReadChar------------------
#------------------------------------------
RTLReadChar:
call ReadCharInit
xorq %rax, %rax
movq ReadCharBuffer@GOTPCREL(%rip), %r8
movb (%r8), %al # == movzxbl (ReadCharBuffer), %rax
call ReadCharEx
ret
#------------------------------------------
#--------------ReadInteger-----------------
#------------------------------------------
RTLReadInteger:
call ReadCharInit
pushall
#movq %rsp, %rbp
xorq %rax, %rax
#lea 1(%rax), %rcx
movq $1, %rcx
ReadIntegerSkipWhiteSpace:
mov IsEOF@GOTPCREL(%rip), %r8 # address of IsEOF in r8
cmpb $0, (%r8) #cmp with $1 and it works. no idea why {!}
jnz ReadIntegerDone #{!}{!!} why jz works and jnz
movq ReadCharBuffer@GOTPCREL(%rip), %r8
cmpb $0, (%r8)
je ReadIntegerSkipWhiteSpaceDone
movq ReadCharBuffer@GOTPCREL(%rip), %r8
cmpb $32, (%r8) #32d == 0x20 == space
ja ReadIntegerSkipWhiteSpaceDone
call ReadCharEx
jmp ReadIntegerSkipWhiteSpace
ReadIntegerSkipWhiteSpaceDone:
movq ReadCharBuffer@GOTPCREL(%rip), %r8
cmpb $'-', (%r8)
jne ReadIntegerNotSigned
negq %rcx #rcx stores -1 or 1 and will multiply the result
call ReadCharEx
ReadIntegerNotSigned:
ReadIntegerLoop:
xorq %rbx, %rbx
movq ReadCharBuffer@GOTPCREL(%rip), %r8
movb (%r8), %bl
cmpb $'0', %bl
jb ReadIntegerDone
cmpb $'9', %bl
ja ReadIntegerDone
imul $10, %rax #rax *= 10
#cast string to int:
subq $'0', %rbx # == lea -'0'(%rax,%rbx,1), %rax
addq %rbx, %rax
call ReadCharEx
jmp ReadIntegerLoop
ReadIntegerDone:
#movq $1, %rcx
imul %rcx #rax *= rcx (rcx={1;-1})
movq %rax, (%rsp) #rax is on top of stack (look at pushall())
popall
ret
#------------------------------------------
#------------------ReadLn------------------
#------------------------------------------
#it does NOT skip input if %RAX is nulled
#so put there some trash before calling it
#------------------------------------------
RTLReadLn:
call ReadCharInit
mov IsEOF@GOTPCREL(%rip), %r8 # address of IsEOF in r8
cmpb $0, (%r8)
jne ReadLnDone
movq ReadCharBuffer@GOTPCREL(%rip), %r8
movb (%r8), %bl
cmpb $10, %bl #cmp to LF
je ReadLnDone
call ReadCharEx
jmp RTLReadLn
ReadLnDone:
ret
#------------------------------------------
#-------------------EOF--------------------
#------------------------------------------
RTLEOF:
xorq %rax, %rax
mov IsEOF@GOTPCREL(%rip), %r8 # address of IsEOF in r8
movb (%r8), %al
ret
#------------------------------------------
#------------------EOLN--------------------
#------------------------------------------
RTLEOLN:
movq ReadCharBuffer@GOTPCREL(%rip), %r8
cmpb $10, (%r8) #cmp to LF
sete %dl #bero's legacy
ret
#------------------------------------------
#------------------Halt--------------------
#------------------------------------------
RTLHalt:
movq $0x2000001, %rax #syscall #1 == Exit()
movq $0, %rdi #exit process state
syscall
#------------------------------------------#
#- Tests -#
#------------------------------------------#
Test1:
#test 1: ReadInt, WriteChar, WriteInt, WriteLine
#test input(-1337 + space): "-1337 "
#expected:
#%entered_num%- 1234
#A
call RTLReadInteger
call RTLReadLn
pushq %rax
pushq $1
call RTLWriteInteger
#addq $16, %rsp
#space
pushq $-1234
pushq $6
call RTLWriteInteger
#addq $16, %rsp
call RTLWriteLn
pushq $'A'
call RTLWriteChar
call RTLWriteLn
call RTLHalt
Test2:
#test 2: ReadChar, EndOfLine, EndOfFile, WriteInt
#test input(B + linebreak): "B\n"
#expected
#{B66}{10}{1}{*1}
call RTLReadChar
parleft
raxchar #should be "B"
#raxint #should be 66(B)
pushq %rax
pushq $1
call RTLWriteInteger
parright
#
parleft
xorq %rax, %rax
movq ReadCharBuffer@GOTPCREL(%rip), %r8
movb (%r8), %al #should be 10 == LF
# raxint
pushq %rax
pushq $1
call RTLWriteInteger
parright
#
xorq %rdx, %rdx
call RTLEOLN
parleft
pushq %rdx
pushq $1
call RTLWriteInteger #should be 1 cuz of linebreak
#addq $16, %rsp
parright
call RTLEOF
parleft
raxchar
#raxint
pushq %rax
pushq $1
call RTLWriteInteger
parright
call RTLHalt
Test3:
#test3: ReadLn, ReadChar
#test input(linebreak + A):"\nA"
#expected
#{A 65}
#put smth into %rax in case IsEOF wont be triggered after first char
movq $1, %rax
call RTLReadLn
call RTLReadChar
parleft
raxchar #should be "A"
space
#raxint #should be 65(A)
pushq %rax
pushq $1
call RTLWriteInteger
parright
call RTLHalt
Test4:
pushq $-1234
pushq $6
call RTLWriteInteger
call RTLWriteLn
call RTLHalt
#//==----------------------------------==//
#//----------------ENTRY-----------------//
#//==----------------------------------==//
#call some tests here or post introduction
#dont need to allocate(prepare) stack pages
#------------------------------------------
StubEntryPoint:
movq %rsp, %rbp # bero's legacy
movq RTLFunctionTable@GOTPCREL(%rip), %rsi # store functionTable and don't change %rsi
ProgramEntryPoint:
#------------------------------------------
# code generated by btpc.pas goes here