From 31a438d1eebaebcf00e49035ce6c73817c5fee96 Mon Sep 17 00:00:00 2001 From: andromeda Date: Wed, 25 Mar 2026 17:48:58 +0100 Subject: [PATCH] prettify some printing, tidy loop --- twasm/asm/main.asm | 156 ++++++++++++-------------------------------- twasm/asm/tests.asm | 8 ++- 2 files changed, 46 insertions(+), 118 deletions(-) diff --git a/twasm/asm/main.asm b/twasm/asm/main.asm index ee1854e..7552c38 100644 --- a/twasm/asm/main.asm +++ b/twasm/asm/main.asm @@ -85,13 +85,12 @@ start: ; ------------------------------------------------------------------------------ assemble: - xor eax, eax ; rax = number of tokens processed - ; rdi = number of tokens in table + xor eax, eax ; rax = number of tokens processed + mov [.tokens_total], edi ; rdi = number of tokens in table + .loop: - push rdi xor edi, edi mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; next tte - push rax ; di = tte of operator .operator: ; if next tte's type is an operator: @@ -134,12 +133,7 @@ assemble: pop rsi pop rdi - pop rax ; rax = number of tokens processed - pop rdi ; rdi = total number of tokens - inc rax - cmp rax, rdi - jge .break - jmp .loop + jmp .loop_next_token ; di = tte of operator .operator_1: @@ -161,13 +155,8 @@ assemble: pop rsi pop rdi ; di = tte of operator - pop rax ; rax = number of tokens processed - pop rdi ; rdi = total number of tokens - inc rax - cmp rax, rdi + call .next_token jge .break - push rdi - push rax xor edi, edi mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte @@ -186,14 +175,8 @@ assemble: cmp al, 0x02 ; type: register je .operator_1_register - pop rax ; rax = number of tokens processed - pop rdi ; rdi = total number of tokens - inc rax - cmp rax, rdi - jge .break - jmp .loop + jmp .loop_next_token - ; TODO figure out if this is relevant .operator_1_memory: push rsi mov rsi, .msg_operator_1_memory @@ -216,12 +199,7 @@ assemble: ; al = Mod R/M byte call .output_byte - pop rax ; rax = number of tokens processed - pop rdi ; rdi = total number of tokens - inc rax - cmp rax, rdi - jge .break - jmp .loop + jmp .loop_next_token .operator_2: push rsi @@ -231,13 +209,8 @@ assemble: mov cx, di ; cx = tte of operator - pop rax - pop rdi - inc rax - cmp rax, rdi + call .next_token jge .break - push rdi - push rax xor edi, edi mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte @@ -256,12 +229,7 @@ assemble: cmp al, 0x02 ; type: register je .operator_2_register - pop rax ; rax = number of tokens processed - pop rdi ; rdi = total number of tokens - inc rax - cmp rax, rdi - jge .break - jmp .loop + jmp .loop_next_token .operator_2_memory: push rsi @@ -283,25 +251,15 @@ assemble: call .output_byte pop rdi - pop rax - pop rdi - inc rax - cmp rax, rdi + call .next_token jge .break - push rdi - push rax xor edi, edi mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte mov si, di ; si = dst tte - pop rax - pop rdi - inc rax - cmp rax, rdi + call .next_token jge .break - push rdi - push rax xor edi, edi mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte @@ -318,12 +276,7 @@ assemble: cmp al, 0x02 je .operator_2_memory_register - pop rax ; rax = number of tokens processed - pop rdi ; rdi = total number of tokens - inc rax - cmp rax, rdi - jge .break - jmp .loop + jmp .loop_next_token .operator_2_memory_register: push rsi @@ -338,12 +291,7 @@ assemble: ; al = Mod R/M byte call .output_byte - pop rax ; rax = number of tokens processed - pop rdi ; rdi = total number of tokens - inc rax - cmp rax, rdi - jge .break - jmp .loop + jmp .loop_next_token .operator_2_register: push rsi @@ -363,13 +311,8 @@ assemble: mov si, di ; si = dst tte - pop rax - pop rdi - inc rax - cmp rax, rdi + call .next_token jge .break - push rdi - push rax xor edi, edi mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte @@ -386,12 +329,7 @@ assemble: cmp al, 0x02 je .operator_2_register_register - pop rax ; rax = number of tokens processed - pop rdi ; rdi = total number of tokens - inc rax - cmp rax, rdi - jge .break - jmp .loop + jmp .loop_next_token .operator_2_register_memory: push rsi @@ -402,13 +340,8 @@ assemble: cmp di, 0x1000 ; check if token is addressing to a register jne .unsupported_memory_access ; if not, unsupported :/ - pop rax - pop rdi - inc rax - cmp rax, rdi + call .next_token jge .break - push rdi - push rax xor edi, edi mov di, [rax * TOKEN_TABLE_ENTRY_SIZE + TOKEN_TABLE_ADDR] ; di = next tte @@ -422,12 +355,7 @@ assemble: ; al = Mod R/M byte call .output_byte - pop rax ; rax = number of tokens processed - pop rdi ; rdi = total number of tokens - inc rax - cmp rax, rdi - jge .break - jmp .loop + jmp .loop_next_token .operator_2_register_register: push rsi @@ -443,10 +371,10 @@ assemble: ; al = Mod R/M byte call .output_byte - pop rax ; rax = number of tokens processed - pop rdi ; rdi = total number of tokens - inc rax - cmp rax, rdi + jmp .loop_next_token + + .loop_next_token: + call .next_token jge .break jmp .loop @@ -465,6 +393,18 @@ assemble: ; 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 .output_byte: mov edx, [.next_output_byte] ; get output byte's address @@ -597,20 +537,10 @@ get_ModRM: call get_reg_bits ; al = reg bits - push rsi - mov rsi, .msg_normal_ModRM - call print.debug - pop rsi - mov bl, al ; bl = reg bits jmp .continue .pass_di_as_op_flag: - push rsi - mov rsi, .msg_op_flag - call print.debug - pop rsi - mov bl, dil ; bl = op flag and bl, 111b ; mask @@ -633,9 +563,6 @@ get_ModRM: pop rbx ret - .msg_op_flag db "get_ModRM op_flag", 0x0A, 0x00 - .msg_normal_ModRM db "get_ModRM normal_ModRM", 0x0A, 0x00 - ; ------------------------------------------------------------------------------ ; get_opcode ; @@ -693,8 +620,7 @@ get_opcode: jne .found_continue shr dl, 4 ; if so, actually 1 further on dl byte - - .found_continue + .found_continue: mov al, [rsi + 2 + opcodes.by_id + rax] and rax, 0xFF ; mask and rdx, 0x0F ; mask @@ -1441,10 +1367,10 @@ print: call print pop rsi jmp print ; tail call - .debug_msg db "[DEBUG]: ", 0x00 - .error_msg db "[ERROR]: ", 0x00 - .test_msg db "[TEST]: ", 0x00 - .warn_msg db "[WARN]: ", 0x00 + .debug_msg db 0x1B, "[36m", "[DEBUG]: ", 0x1B, "[0m", 0x00 + .error_msg db 0x1B, "[1;31m", "[ERROR]: ", 0x1B, "[0m", 0x00 + .test_msg db 0x1B, "[1;33m", "[TEST]: ", 0x1B, "[0m", 0x00 + .warn_msg db 0x1B, "[1;35m", "[WARN]: ", 0x1B, "[0m", 0x00 ; ------------------------------------------------------------------------------ ; halt @@ -1623,7 +1549,7 @@ clear_output_arena: ; data ; ------------------------------------------------------------------------------ -align 4 +align 16 ; for readability in hexdump tokens: .by_id: dw 0x0000 ; rax @@ -1935,7 +1861,7 @@ tokens: dw 0x003F .registers_end: -align 16 +align 16 ; for readability in hexdump opcodes: .by_id: ; hlt @@ -2079,7 +2005,7 @@ opcodes: dd 0x00000000 .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 whitespace_2 db " ", 0x0D diff --git a/twasm/asm/tests.asm b/twasm/asm/tests.asm index bc52041..dd82315 100644 --- a/twasm/asm/tests.asm +++ b/twasm/asm/tests.asm @@ -654,11 +654,13 @@ test_evaluate_operand: msg_pass: 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." - .end db 0x0A, 0x00 + .end db 0x1B, "[0m", 0x0A, 0x00 msg_fail: db 0x0A times (TEST_LINE_LENGTH + .start - .end) db " ", + db 0x1B, "[31m" .start db "failed." - .end db 0x0A, 0x00 + .end db 0x1B, "[0m", 0x0A, 0x00