summaryrefslogtreecommitdiff
path: root/twasm/asm/main.asm
diff options
context:
space:
mode:
Diffstat (limited to 'twasm/asm/main.asm')
-rw-r--r--twasm/asm/main.asm77
1 files changed, 67 insertions, 10 deletions
diff --git a/twasm/asm/main.asm b/twasm/asm/main.asm
index 69708bc..9c052e1 100644
--- a/twasm/asm/main.asm
+++ b/twasm/asm/main.asm
@@ -90,6 +90,7 @@ assemble:
mov [.tokens_total], edi ; edi = total number of tokens in table
.loop:
+ call .flush_write_buffer
call .get_next_tte
; di = tte
call get_tte_type
@@ -129,7 +130,7 @@ assemble:
call get_opcode
; al = opcode
; dl = 0x00
- call .output_byte
+ call .write_byte
jmp .loop_next_token
@@ -143,7 +144,7 @@ assemble:
; al = opcode
; dl = op flag
push rdx
- call .output_byte
+ call .write_byte
pop rdx ; dl = op flag
call .next_token
@@ -165,7 +166,7 @@ assemble:
cmp al, 0x02 ; type: register
je .operator_1_register
- jmp .loop_next_token
+ jmp .unexpected_token
.operator_1_memory:
mov rsi, .msg_operator_1_memory
@@ -183,7 +184,7 @@ assemble:
mov edx, 11b ; dl = mod bits
call get_ModRM
; al = Mod R/M byte
- call .output_byte
+ call .write_byte
jmp .loop_next_token
@@ -212,7 +213,7 @@ assemble:
cmp al, 0x02 ; type: register
je .operator_2_register
- jmp .loop_next_token
+ jmp .unexpected_token
.operator_2_memory:
mov rsi, .msg_operator_2_memory
@@ -226,7 +227,7 @@ assemble:
call get_opcode
; al = opcode
; dl = op flag
- call .output_byte
+ call .write_byte
call .next_token
jge .break
@@ -264,7 +265,7 @@ assemble:
mov dl, 00b ; dl = mod bits
call get_ModRM
; al = Mod R/M byte
- call .output_byte
+ call .write_byte
jmp .loop_next_token
@@ -279,7 +280,7 @@ assemble:
; al = opcode
; dl = op flag
; TODO do something if the op flag is present
- call .output_byte
+ call .write_byte
pop rdi ; di = dst tte
mov si, di ; si = dst tte
@@ -324,7 +325,7 @@ assemble:
mov edx, 00b ; dl = mod bits
call get_ModRM
; al = Mod R/M byte
- call .output_byte
+ call .write_byte
jmp .loop_next_token
@@ -338,7 +339,7 @@ assemble:
mov edx, 11b ; dl = mod bits
call get_ModRM
; al = Mod R/M byte
- call .output_byte
+ call .write_byte
jmp .loop_next_token
@@ -348,6 +349,7 @@ assemble:
jmp .loop
.break:
+ call .flush_write_buffer
ret
.unexpected_token:
@@ -391,6 +393,59 @@ assemble:
.next_output_byte dd OUTPUT_ADDR ; next empty byte in output
; TODO get rid of this sketchy bit of state
+ ; al = byte to push
+ .push_byte:
+ push rcx
+ mov ecx, [.buffer_pointer]
+ push rcx
+ push rax
+ mov ecx, .buffer_end
+ mov [.buffer_pointer], ecx
+ .push_byte_loop:
+ dec ecx
+ cmp ecx, .buffer
+ jl .push_byte_break
+ mov al, [ecx]
+ mov [ecx + 1], al
+ jmp .push_byte_loop
+ .push_byte_break:
+ pop rax ; al = byte to push
+ mov [.buffer], al
+ pop rcx ; ecx = old buffer pointer
+ inc ecx
+ mov [.buffer_pointer], ecx
+ pop rcx
+ ret
+
+ ; al = byte to write
+ .write_byte:
+ mov edx, [.buffer_pointer]
+ mov [edx], al
+ inc edx
+ mov [.buffer_pointer], edx
+ ret
+
+ .flush_write_buffer:
+ push rcx
+ push rax
+ mov ecx, [.buffer_pointer]
+ .flush_write_buffer_loop:
+ dec ecx
+ cmp ecx, .buffer
+ jl .flush_write_buffer_break
+ mov al, [ecx]
+ call .output_byte
+ mov byte [ecx], 0x00
+ jmp .flush_write_buffer_loop
+ .flush_write_buffer_break
+ mov dword [.buffer_pointer], .buffer
+ pop rax
+ pop rcx
+ ret
+ .buffer dq 0, 0 ; octo word of space for max of 8 bytes per write
+ .buffer_end:
+ .buffer_pointer dd .buffer ; points to current byte in buffer
+
.msg_unexpected_token db "unexpected token, aborting", 0x0A, 0x00
.msg_unsupported_memory_access db "unsupported memory access, aborting", 0x0A, 0x00
.msg_operator_0 db "operator_0", 0x0A, 0x00
@@ -1991,6 +2046,8 @@ program:
db "; one line comment", 0x0A
db "mov rdx, [rax]", 0x0A
db "mov [rax], rdx", 0x0A
+ db "mov [rcx], rbx", 0x0A
+ db "mov rcx, [rbx]", 0x0A
db "hlt", 0x0A
.size dq $ - program