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
|
code = []
code.append('format elf64 executable 3')
code.append('entry start')
code.append('read:')
code.append('xor rax, rax')
code.append('xor rdi, rdi')
code.append('mov rsi, int_buffer')
code.append('mov rdx, 20')
code.append('syscall')
code.append('xor rcx, rcx')
code.append('mov cl, [int_buffer + rax - 1]')
code.append('cmp rcx, 10')
code.append('jne newlineskip')
code.append('dec rax')
code.append('newlineskip:')
code.append('mov rsi, rax')
code.append('xor rax, rax')
code.append('cmp rsi, 0')
code.append('je skipread')
code.append('xor rcx, rcx')
code.append('mov rdi, 10')
code.append('keepreading:')
code.append('mul rdi')
code.append('mov dl, [int_buffer + rcx]')
code.append('sub rdx, 48')
code.append('add rax, rdx')
code.append('inc rcx')
code.append('cmp rcx, rsi')
code.append('jl keepreading')
code.append('skipread:')
code.append('ret')
code.append('print:')
code.append('mov [rsp - 8], rbx')
code.append('mov rax, [rsp + 8]')
code.append('mov rdi, 10')
code.append('xor rcx, rcx')
code.append('print_loop0:')
code.append('mov [int_buffer + rcx], 48')
code.append('inc rcx')
code.append('cmp rcx, 20')
code.append('jl print_loop0')
code.append('xor rbx, rbx')
code.append('xor rdx, rdx')
code.append('print_loop1:')
code.append('div rdi')
code.append('dec rcx')
code.append('inc rbx')
code.append('add [int_buffer + rcx], dl')
code.append('xor rdx, rdx')
code.append('cmp rax, 0')
code.append('jg print_loop1')
code.append('mov rax, 1')
code.append('mov rdi, 1')
code.append('lea rsi, [int_buffer + rcx]')
code.append('mov rdx, rbx')
code.append('syscall')
code.append('mov rbx, [rsp - 8]')
code.append('ret')
code.append('start:')
loop_stack = []
loop_index = -1
with open('input.stk', 'r') as f:
text = f.read()
words = text.replace('\n', ' ').strip().split(' ')
for word in words:
if word.isdecimal():
code.append('push ' + word)
elif word == '+':
code.append('pop rax')
code.append('pop rdi')
code.append('add rax, rdi')
code.append('push rax')
elif word == '-':
code.append('pop rdi')
code.append('pop rax')
code.append('sub rax, rdi')
code.append('push rax')
elif word == '++':
code.append('pop rax')
code.append('inc rax')
code.append('push rax')
elif word == '--':
code.append('pop rax')
code.append('dec rax')
code.append('push rax')
elif word == '*':
code.append('pop rax')
code.append('pop rdi')
code.append('imul rdi')
code.append('push rax')
elif word == 'pop':
code.append('pop rax')
elif word == 'dup':
code.append('pop rax')
code.append('push rax')
code.append('push rax')
elif word == 'print_number':
code.append('call print')
code.append('pop rax')
elif word == 'read_number':
code.append('call read')
code.append('push rax')
elif word == 'newline':
code.append('mov [int_buffer], 10')
code.append('mov rax, 1')
code.append('mov rdi, 1')
code.append('mov rsi, int_buffer')
code.append('mov rdx, 1')
code.append('syscall')
elif word == '[':
loop_index += 1
loop_stack.append(loop_index)
code.append('l' + str(loop_index) + ':')
code.append('cmp DWORD [rsp], 0')
code.append('je e' + str(loop_index))
elif word == ']':
loop_end = loop_stack.pop()
code.append('jmp l' + str(loop_end))
code.append('e' + str(loop_end) + ':')
code.append('pop rax')
code.append('mov rax, 60')
code.append('mov rdi, 0')
code.append('syscall')
code.append('segment writable')
code.append('int_buffer rb 20')
for line in code:
print(line)
|