From a972f38bb636fdcb41a2bb7c856f7f546076b397 Mon Sep 17 00:00:00 2001 From: andromeda Date: Mon, 9 Mar 2026 23:00:39 +0100 Subject: [PATCH] add get_direct_addressing_ModRM and test_*, fix a couple bugs/typos, add 'reg value' as register metadata --- twasm/README.md | 16 +++++---- twasm/asm/main.asm | 84 ++++++++++++++++++++++++++++++++++++++------- twasm/asm/tests.asm | 53 ++++++++++++++++++++++++++-- 3 files changed, 132 insertions(+), 21 deletions(-) diff --git a/twasm/README.md b/twasm/README.md index e6f386e..ce6beec 100644 --- a/twasm/README.md +++ b/twasm/README.md @@ -127,13 +127,15 @@ type metadata for the different types is as follows: ``` ``` -+------------------+ -| type 0x2 | -+----------+-------+ -| 31 26 | 25 24 | -+----------+-------+ -| reserved | width | -+----------+-------+ ++------------------------------+ +| type 0x2 | ++----------+-----------+-------+ +| 31 29 | 28 26 | 25 24 | ++----------+-----------+-------+ +| reserved | reg value | width | ++----------+-----------+-------+ + +; reg is the value that cooresponds to the register in the ModR/M byte ; width: 00b ; 8 bit diff --git a/twasm/asm/main.asm b/twasm/asm/main.asm index 57af625..1dfad3b 100644 --- a/twasm/asm/main.asm +++ b/twasm/asm/main.asm @@ -187,6 +187,63 @@ get_tte_typed_metadata: mov al, [3 + tokens.by_id + rax * 4] ret +; ------------------------------------------------------------------------------ +; get_direct_addressing_ModRM +; +; description: +; given 2 register tokens, returns the ModR/M byte in direct addressing +; (mod = 11b) mode +; +; parameters: +; di = token table entry `reg` +; si = token table entry `R/M` +; +; returned: +; al = ModR/M byte +; ------------------------------------------------------------------------------ + +get_direct_addressing_ModRM: + ; TODO something is backwards in this function but I don't see it. If the test + ; suite fails, it's too far gone; rewrite it. + push rdi + push rsi + ; get metadata of reg + call get_tte_typed_metadata + ; al = typed metadata of reg + pop rsi + pop rdi + + mov bl, al ; bl = metadata of reg + + push rdi + push rsi + push rbx + + ; get metadata of R/M + mov di, si + call get_tte_typed_metadata + ; al = typed metadata of R/M + + pop rbx + pop rsi + pop rdi + + mov dl, al + + shr dl, 2 + and dl, 111b ; mask + + shr bl, 2 + and bl, 111b ; mask + shl bl, 3 + + xor eax, eax + or al, 11b << 6 ; mod bits + or al, dl ; reg bits + or al, bl ; R/M bits + and rax, 0xFF ; mask for byte + ret + ; ------------------------------------------------------------------------------ ; tokenising ; ------------------------------------------------------------------------------ @@ -872,30 +929,33 @@ tokens: dw 0x0059 .by_name_5: .by_id: + dw 0x0010 ; eax + db 0x02 ; type: register + db 00000010b ; reg: 000b + ; width: 10b (32 bits) + + dw 0x0000 ; rax + db 0x02 ; type: register + db 00000011b ; reg: 000b + ; width: 11b (64 bits) + + dw 0x0003 ; rdx + db 0x02 ; type: register + db 00001011b ; reg: 010b + ; width: 11b (64 bits) + dw 0x0053 ; xor db 0x01 ; type: operator db 0x02 ; # operands - dw 0x0010 ; eax - db 0x02 ; type: register - db 0x02 ; width: 32 bit - dw 0x0054 ; inc db 0x01 ; type: operator db 0x01 ; # operands - dw 0x0000 ; rax - db 0x02 ; type: register - db 0x03 ; width: 64 bit - dw 0x0056 ; mov db 0x01 ; type: operator db 0x02 ; # operands - dw 0x0003 ; rdx - db 0x02 ; type: register - db 0x03 ; width: 64 bit - dw 0x004F ; hlt db 0x01 ; type: operator db 0x00 ; # operands diff --git a/twasm/asm/tests.asm b/twasm/asm/tests.asm index f48d8bf..8480554 100644 --- a/twasm/asm/tests.asm +++ b/twasm/asm/tests.asm @@ -34,6 +34,9 @@ run_tests: call clear_test_arena call test_get_tte_typed_metadata + call clear_test_arena + call test_get_direct_addressing_ModRM + ret .msg db "running test suite...", 0x0A, 0x00 @@ -441,7 +444,8 @@ test_get_tte_typed_metadata: mov di, 0x0003 ; rdx call get_tte_typed_metadata - cmp al, 0x03 ; width: 64 bits + cmp al, 00001011b ; reg: 010b + ; width: 11b (64 bits) jne .fail mov di, 0x0056 ; mov @@ -462,7 +466,52 @@ test_get_tte_typed_metadata: mov rsi, msg_fail call print ret - .msg db "test_get_tte_type...", 0x00 + .msg db "test_get_tte_typed_metadata...", 0x00 + +; ------------------------------------------------------------------------------ +; test_get_direct_addressing_ModRM +; +; description: +; tests get_direct_addressing_ModRM described functionality +; ------------------------------------------------------------------------------ + +test_get_direct_addressing_ModRM: + mov rsi, .msg + call print + + mov di, 0x0000 ; rax + mov si, 0x0000 ; rax + call get_direct_addressing_ModRM + cmp al, 11000000b ; Mod Reg R/M: 11b 000b 000b + jne .fail + + mov di, 0x0000 ; rax + mov si, 0x0003 ; rdx + call get_direct_addressing_ModRM + cmp al, 11000010b ; Mod Reg R/M: 11b 000b 010b + jne .fail + + mov di, 0x0003 ; rdx + mov si, 0x0000 ; rax + call get_direct_addressing_ModRM + cmp al, 11010000b ; Mod Reg R/M: 11b 010b 000b + jne .fail + + mov di, 0x0003 ; rdx + mov si, 0x0003 ; rdx + call get_direct_addressing_ModRM + cmp al, 11010010b ; Mod Reg R/M 11b 010b 010b + jne .fail + + .pass: + mov rsi, msg_pass + call print + ret + .fail: + mov rsi, msg_fail + call print + ret + .msg db "test_get_direct_addressing_ModRM...", 0x00 msg_pass: db 0x0A