summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuan Manuel Tomás <jtomas1815@gmail.com>2022-07-04 03:44:29 -0300
committerJuan Manuel Tomás <jtomas1815@gmail.com>2022-07-04 03:44:29 -0300
commit653fdc8cdc1040cc5647a7cebfd274bf49bd8a21 (patch)
tree18c4cc0697c3b89eae3b797817ad0973e58b2c08
downloadstk-653fdc8cdc1040cc5647a7cebfd274bf49bd8a21.tar.gz
stk-653fdc8cdc1040cc5647a7cebfd274bf49bd8a21.zip
Initial Commit
-rw-r--r--.gitignore2
-rw-r--r--Makefile5
-rw-r--r--input.stk3
-rw-r--r--main.py90
4 files changed, 100 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..81235ba
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+main
+main.asm
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..a380159
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,5 @@
+main: main.asm
+ fasm main.asm
+
+main.asm: main.py input.stk
+ python3 main.py > main.asm
diff --git a/input.stk b/input.stk
new file mode 100644
index 0000000..51e3270
--- /dev/null
+++ b/input.stk
@@ -0,0 +1,3 @@
+5 dup * dup * dup * print
+34 35 + print
+1 0 - print
diff --git a/main.py b/main.py
new file mode 100644
index 0000000..b2fd03e
--- /dev/null
+++ b/main.py
@@ -0,0 +1,90 @@
+parameter_registers = ['rdi', 'rsi', 'rdx', 'rcx', 'r8', 'r9']
+return_registers = ['rax', 'rdx']
+
+syscall_registers = ['rdi', 'rsi', 'rdx', 'r10', 'r8', 'r9']
+static_chain_pointer = 'r10'
+
+fparameter_registers = ['xmm0', 'xmm1', 'xmm2', 'xmm3', 'xmm4', 'xmm5', 'xmm6', 'xmm7']
+freturn_registers = ['xmm0', 'xmm1']
+
+callee_must_clean = ['rbx', 'rsp', 'rbp', 'r12', 'r13', 'r14', 'r15']
+
+code = []
+
+code.append('format elf64 executable 3')
+code.append('entry start')
+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 [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')
+code.append('mov rbx, [rsp - 8]')
+code.append('ret')
+
+code.append('start:')
+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 rax')
+ code.append('pop rdi')
+ code.append('sub rax, rdi')
+ code.append('push rax')
+ elif word == '*':
+ code.append('pop rax')
+ code.append('pop rdi')
+ code.append('mul 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':
+ code.append('call print')
+ else:
+ print('Unknown word:', word)
+
+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)