prettify some printing, tidy loop

This commit is contained in:
andromeda
2026-03-25 17:48:58 +01:00
parent 20162ab02c
commit 31a438d1ee
2 changed files with 46 additions and 118 deletions

View File

@@ -85,13 +85,12 @@ start:
; ------------------------------------------------------------------------------ ; ------------------------------------------------------------------------------
assemble: assemble:
xor eax, eax ; rax = number of tokens processed xor eax, eax ; rax = number of tokens processed
; rdi = number of tokens in table mov [.tokens_total], edi ; rdi = number of tokens in table
.loop: .loop:
push rdi
xor edi, edi xor edi, edi
mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; next tte mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; next tte
push rax
; di = tte of operator ; di = tte of operator
.operator: ; if next tte's type is an operator: .operator: ; if next tte's type is an operator:
@@ -134,12 +133,7 @@ assemble:
pop rsi pop rsi
pop rdi pop rdi
pop rax ; rax = number of tokens processed jmp .loop_next_token
pop rdi ; rdi = total number of tokens
inc rax
cmp rax, rdi
jge .break
jmp .loop
; di = tte of operator ; di = tte of operator
.operator_1: .operator_1:
@@ -161,13 +155,8 @@ assemble:
pop rsi pop rsi
pop rdi ; di = tte of operator pop rdi ; di = tte of operator
pop rax ; rax = number of tokens processed call .next_token
pop rdi ; rdi = total number of tokens
inc rax
cmp rax, rdi
jge .break jge .break
push rdi
push rax
xor edi, edi xor edi, edi
mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte
@@ -186,14 +175,8 @@ assemble:
cmp al, 0x02 ; type: register cmp al, 0x02 ; type: register
je .operator_1_register je .operator_1_register
pop rax ; rax = number of tokens processed jmp .loop_next_token
pop rdi ; rdi = total number of tokens
inc rax
cmp rax, rdi
jge .break
jmp .loop
; TODO figure out if this is relevant
.operator_1_memory: .operator_1_memory:
push rsi push rsi
mov rsi, .msg_operator_1_memory mov rsi, .msg_operator_1_memory
@@ -216,12 +199,7 @@ assemble:
; al = Mod R/M byte ; al = Mod R/M byte
call .output_byte call .output_byte
pop rax ; rax = number of tokens processed jmp .loop_next_token
pop rdi ; rdi = total number of tokens
inc rax
cmp rax, rdi
jge .break
jmp .loop
.operator_2: .operator_2:
push rsi push rsi
@@ -231,13 +209,8 @@ assemble:
mov cx, di ; cx = tte of operator mov cx, di ; cx = tte of operator
pop rax call .next_token
pop rdi
inc rax
cmp rax, rdi
jge .break jge .break
push rdi
push rax
xor edi, edi xor edi, edi
mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte
@@ -256,12 +229,7 @@ assemble:
cmp al, 0x02 ; type: register cmp al, 0x02 ; type: register
je .operator_2_register je .operator_2_register
pop rax ; rax = number of tokens processed jmp .loop_next_token
pop rdi ; rdi = total number of tokens
inc rax
cmp rax, rdi
jge .break
jmp .loop
.operator_2_memory: .operator_2_memory:
push rsi push rsi
@@ -283,25 +251,15 @@ assemble:
call .output_byte call .output_byte
pop rdi pop rdi
pop rax call .next_token
pop rdi
inc rax
cmp rax, rdi
jge .break jge .break
push rdi
push rax
xor edi, edi xor edi, edi
mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte
mov si, di ; si = dst tte mov si, di ; si = dst tte
pop rax call .next_token
pop rdi
inc rax
cmp rax, rdi
jge .break jge .break
push rdi
push rax
xor edi, edi xor edi, edi
mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte
@@ -318,12 +276,7 @@ assemble:
cmp al, 0x02 cmp al, 0x02
je .operator_2_memory_register je .operator_2_memory_register
pop rax ; rax = number of tokens processed jmp .loop_next_token
pop rdi ; rdi = total number of tokens
inc rax
cmp rax, rdi
jge .break
jmp .loop
.operator_2_memory_register: .operator_2_memory_register:
push rsi push rsi
@@ -338,12 +291,7 @@ assemble:
; al = Mod R/M byte ; al = Mod R/M byte
call .output_byte call .output_byte
pop rax ; rax = number of tokens processed jmp .loop_next_token
pop rdi ; rdi = total number of tokens
inc rax
cmp rax, rdi
jge .break
jmp .loop
.operator_2_register: .operator_2_register:
push rsi push rsi
@@ -363,13 +311,8 @@ assemble:
mov si, di ; si = dst tte mov si, di ; si = dst tte
pop rax call .next_token
pop rdi
inc rax
cmp rax, rdi
jge .break jge .break
push rdi
push rax
xor edi, edi xor edi, edi
mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte
@@ -386,12 +329,7 @@ assemble:
cmp al, 0x02 cmp al, 0x02
je .operator_2_register_register je .operator_2_register_register
pop rax ; rax = number of tokens processed jmp .loop_next_token
pop rdi ; rdi = total number of tokens
inc rax
cmp rax, rdi
jge .break
jmp .loop
.operator_2_register_memory: .operator_2_register_memory:
push rsi push rsi
@@ -402,13 +340,8 @@ assemble:
cmp di, 0x1000 ; check if token is addressing to a register cmp di, 0x1000 ; check if token is addressing to a register
jne .unsupported_memory_access ; if not, unsupported :/ jne .unsupported_memory_access ; if not, unsupported :/
pop rax call .next_token
pop rdi
inc rax
cmp rax, rdi
jge .break jge .break
push rdi
push rax
xor edi, edi xor edi, edi
mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte
@@ -422,12 +355,7 @@ assemble:
; al = Mod R/M byte ; al = Mod R/M byte
call .output_byte call .output_byte
pop rax ; rax = number of tokens processed jmp .loop_next_token
pop rdi ; rdi = total number of tokens
inc rax
cmp rax, rdi
jge .break
jmp .loop
.operator_2_register_register: .operator_2_register_register:
push rsi push rsi
@@ -443,10 +371,10 @@ assemble:
; al = Mod R/M byte ; al = Mod R/M byte
call .output_byte call .output_byte
pop rax ; rax = number of tokens processed jmp .loop_next_token
pop rdi ; rdi = total number of tokens
inc rax .loop_next_token:
cmp rax, rdi call .next_token
jge .break jge .break
jmp .loop jmp .loop
@@ -465,6 +393,18 @@ assemble:
; procedures ; procedures
; add the line `jge .break` after call site
.next_token:
mov eax, [.tokens_processed]
mov edi, [.tokens_total]
inc eax
mov [.tokens_processed], eax
cmp eax, edi
ret
.tokens_processed dd 0
.tokens_total dd 0
; al = byte to write ; al = byte to write
.output_byte: .output_byte:
mov edx, [.next_output_byte] ; get output byte's address mov edx, [.next_output_byte] ; get output byte's address
@@ -597,20 +537,10 @@ get_ModRM:
call get_reg_bits call get_reg_bits
; al = reg bits ; al = reg bits
push rsi
mov rsi, .msg_normal_ModRM
call print.debug
pop rsi
mov bl, al ; bl = reg bits mov bl, al ; bl = reg bits
jmp .continue jmp .continue
.pass_di_as_op_flag: .pass_di_as_op_flag:
push rsi
mov rsi, .msg_op_flag
call print.debug
pop rsi
mov bl, dil ; bl = op flag mov bl, dil ; bl = op flag
and bl, 111b ; mask and bl, 111b ; mask
@@ -633,9 +563,6 @@ get_ModRM:
pop rbx pop rbx
ret ret
.msg_op_flag db "get_ModRM op_flag", 0x0A, 0x00
.msg_normal_ModRM db "get_ModRM normal_ModRM", 0x0A, 0x00
; ------------------------------------------------------------------------------ ; ------------------------------------------------------------------------------
; get_opcode ; get_opcode
; ;
@@ -693,8 +620,7 @@ get_opcode:
jne .found_continue jne .found_continue
shr dl, 4 ; if so, actually 1 further on dl byte shr dl, 4 ; if so, actually 1 further on dl byte
.found_continue:
.found_continue
mov al, [rsi + 2 + opcodes.by_id + rax] mov al, [rsi + 2 + opcodes.by_id + rax]
and rax, 0xFF ; mask and rax, 0xFF ; mask
and rdx, 0x0F ; mask and rdx, 0x0F ; mask
@@ -1441,10 +1367,10 @@ print:
call print call print
pop rsi pop rsi
jmp print ; tail call jmp print ; tail call
.debug_msg db "[DEBUG]: ", 0x00 .debug_msg db 0x1B, "[36m", "[DEBUG]: ", 0x1B, "[0m", 0x00
.error_msg db "[ERROR]: ", 0x00 .error_msg db 0x1B, "[1;31m", "[ERROR]: ", 0x1B, "[0m", 0x00
.test_msg db "[TEST]: ", 0x00 .test_msg db 0x1B, "[1;33m", "[TEST]: ", 0x1B, "[0m", 0x00
.warn_msg db "[WARN]: ", 0x00 .warn_msg db 0x1B, "[1;35m", "[WARN]: ", 0x1B, "[0m", 0x00
; ------------------------------------------------------------------------------ ; ------------------------------------------------------------------------------
; halt ; halt
@@ -1623,7 +1549,7 @@ clear_output_arena:
; data ; data
; ------------------------------------------------------------------------------ ; ------------------------------------------------------------------------------
align 4 align 16 ; for readability in hexdump
tokens: tokens:
.by_id: .by_id:
dw 0x0000 ; rax dw 0x0000 ; rax
@@ -1935,7 +1861,7 @@ tokens:
dw 0x003F dw 0x003F
.registers_end: .registers_end:
align 16 align 16 ; for readability in hexdump
opcodes: opcodes:
.by_id: .by_id:
; hlt ; hlt
@@ -2079,7 +2005,7 @@ opcodes:
dd 0x00000000 dd 0x00000000
.by_id_end: .by_id_end:
msg_welcome db "Welcome to Twasm", 0x0A, 0x00 msg_welcome db 0x1B, "[35m", "Welcome to Twasm", 0x1B, "[0m", 0x0A, 0x00
msg_halt db "halted.", 0x0A, 0x00 msg_halt db "halted.", 0x0A, 0x00
whitespace_2 db " ", 0x0D whitespace_2 db " ", 0x0D

View File

@@ -654,11 +654,13 @@ test_evaluate_operand:
msg_pass: msg_pass:
db 0x0A db 0x0A
times (TEST_LINE_LENGTH + .start - .end) db " ", ; right align times (TEST_LINE_LENGTH + .start + 5 - .end) db " ", ; right align
db 0x1B, "[32m"
.start db "passed." .start db "passed."
.end db 0x0A, 0x00 .end db 0x1B, "[0m", 0x0A, 0x00
msg_fail: msg_fail:
db 0x0A db 0x0A
times (TEST_LINE_LENGTH + .start - .end) db " ", times (TEST_LINE_LENGTH + .start - .end) db " ",
db 0x1B, "[31m"
.start db "failed." .start db "failed."
.end db 0x0A, 0x00 .end db 0x1B, "[0m", 0x0A, 0x00