diff options
-rw-r--r-- | input.stk | 6 | ||||
-rw-r--r-- | main.py | 76 |
2 files changed, 62 insertions, 20 deletions
@@ -1,3 +1,3 @@ -5 dup * dup * dup * print -34 35 + print -1 0 - print +Loops ten times asking for user input. If the input is 0 or nothing, print 69420, else print back the number from the input. +10 [ 0 1 , [ ~ . ! -- ] [ 69420 . ! ] -- ] + @@ -1,18 +1,37 @@ -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('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]') @@ -48,6 +67,8 @@ 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(' ') @@ -60,25 +81,46 @@ with open('input.stk', 'r') as f: code.append('add rax, rdi') code.append('push rax') elif word == '-': - code.append('pop rax') 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('mul rdi') code.append('push rax') - elif word == 'pop': + elif word == '!': code.append('pop rax') - elif word == 'dup': + elif word == '~': code.append('pop rax') code.append('push rax') code.append('push rax') - elif word == 'print': + elif word == '.': code.append('call print') - else: - print('Unknown word:', word) + code.append('pop rax') + elif word == ',': + code.append('call read') + code.append('push rax') + 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') |