diff --git a/twasm/README.md b/twasm/README.md index a0a1c39..6509be7 100644 --- a/twasm/README.md +++ b/twasm/README.md @@ -272,33 +272,47 @@ entries are as follows: +----------+---------------+----------------------------------+ 16 bytes -+----------------------------------------------+ -| 2 operand operators | -+----------------------------------------------+ -| 127 96 | -+----------------------------------------------+ -| reserved | -+-------------------+-------+-------+----------+ -| 95 80 | 79 76 | 75 72 | 71 64 | -+-------------------+-------+-------+----------+ -| reserved | op3&8 | op2&8 | reserved | -+-------------------+-------+-------+----------+ -| 63 48 | 47 40 | 39 32 | -+-------------------+---------------+----------+ -| reserved | opcode | opcode | -| | dst=r/m | dst=r/m | -| | src=imm8 | src=imm | -+---------+---------+---------------+----------+ -| 31 24 | 23 16 | 15 0 | -+---------+---------+--------------------------+ -| opcode | opcode | token ID | -| dst=r | dst=r/m | | -| src=r/m | src=r | | -+---------+---------+--------------------------+ ++-----------------------------------------------+ +| 2 operand operators | ++-----------------------------------------------+ +| 127 96 | ++-----------------------------------------------+ +| reserved | ++---------+----------+-------+-------+----------+ +| 95 88 | 87 80 | 79 76 | 75 72 | 71 64 | ++---------+----------+-------+-------+----------+ +| flags | reserved | op3&8 | op2&8 | reserved | ++---------+----------+-------+-------+----------+ +| 63 48 | 47 40 | 39 32 | ++--------------------+---------------+----------+ +| reserved | opcode | opcode | +| | dst=r/m | dst=r/m | +| | src=imm8 | src=imm | ++---------+----------+---------------+----------+ +| 31 24 | 23 16 | 15 0 | ++---------+----------+--------------------------+ +| opcode | opcode | token ID | +| dst=r | dst=r/m | | +| src=r/m | src=r | | ++---------+----------+--------------------------+ + +1 byte ++-----------------+ +| flags byte | ++----------+------+ +| 95 89 | 88 | ++----------+------+ +| reserved | 8bit | ++----------+------+ + +; flags key: +8bit ; tte has opcodes for r/m8 and r8 instead of r/m and r respectively ; key: r/m ; r/m 16/32/64 +r/m8 ; r/m 8 r ; r 16/32/64 +r8 ; r 8 imm ; imm 16/32 imm8 ; imm 8 rel ; rel 16/32 diff --git a/twasm/asm/main.asm b/twasm/asm/main.asm index 6310bbc..7f09981 100644 --- a/twasm/asm/main.asm +++ b/twasm/asm/main.asm @@ -374,8 +374,8 @@ assemble: and al, 11b ; al = register width - cmp al, 00b ; 8 bit - je .unexpected_token ; TODO handle 8 bit opcodes + cmp al, 00b ; 8 bit + je .operator_2_register_8 cmp al, 01b ; 16 bit je .operator_2_register_16 @@ -385,11 +385,16 @@ assemble: cmp al, 11b ; 64 bit je .operator_2_register_64 + .operator_2_register_8: + mov bl, 1b ; operator flag 8bit + jmp .operator_2_register_continue .operator_2_register_16: + xor ebx, ebx ; no operator flags mov al, 0x66 call .push_byte jmp .operator_2_register_continue .operator_2_register_64: + xor ebx, ebx ; no operator flags mov al, 0x48 call .push_byte jmp .operator_2_register_continue @@ -430,6 +435,7 @@ assemble: push rsi mov di, cx ; di = tte of operator mov sil, 1 ; dst = reg + mov bl, 1 ; bl = operator flag byte call get_opcode ; al = opcode ; dl = op flag @@ -484,6 +490,7 @@ assemble: push rsi mov di, cx ; di = tte of operator mov sil, 1 ; dst = reg + ; bl = operator flag byte call get_opcode ; al = opcode ; dl = op flag @@ -606,6 +613,17 @@ assemble: push rsi mov di, cx ; di = tte of operator mov sil, 2 ; dst=r/m,src=imm + ; bl = operator flag byte + ; TODO change sil based on whether bl is 8 bit or not + push rbx + and ebx, 1 + cmp bl, 1 ; bit8 flag + pop rbx + je .operator_2_register_const_get_opcode_8 + jmp .operator_2_register_const_get_opcode_continue + .operator_2_register_const_get_opcode_8: + mov sil, 3 ; dst=r/m,src=imm8 + .operator_2_register_const_get_opcode_continue: call get_opcode ; al = opcode ; dl = op flag @@ -616,6 +634,12 @@ assemble: call .next_token jge .break + push rbx + and ebx, 1 + cmp bl, 1 ; bit8 flag + pop rbx + je .operator_2_register_const_8 + push rdi push rsi mov edi, .buffer_end - .buffer ; length of buffer @@ -639,6 +663,11 @@ assemble: je .operator_2_register_const_16 jmp .operator_2_register_const_32 + .operator_2_register_const_8: + mov ecx, [.tokens_processed] + mov al, [TOKEN_TABLE_ADDR + 2 * rcx] ; get the next byte from the tt + call .write_byte ; and add it to the buffer + jmp .operator_2_register_const_continue .operator_2_register_const_16: mov ecx, [.tokens_processed] mov ax, [TOKEN_TABLE_ADDR + 2 * rcx] ; get the next 2 bytes from the tt @@ -981,6 +1010,7 @@ get_ModRM: ; di = token table entry ; sil = offset within opcode entry. 0 is the first opcode, 1 the second, and so ; on +; bl = flag byte ; ; returned: ; al = opcode; the rest of rax is zeroed. @@ -989,6 +1019,7 @@ get_ModRM: get_opcode: and edi, 0xFFFF ; di = token table entry + and ebx, 0xFF ; bl = flag byte add esi, 2 and esi, 111b ; offset within opcode entry @@ -1005,10 +1036,20 @@ get_opcode: shr eax, 4 cmp cx, di - je .found + je .maybe_found inc eax jmp .loop + + .maybe_found: + shl eax, 4 + mov cl, [opcodes.by_id + 11 + eax] + shr eax, 4 + cmp cl, bl + je .found + inc eax + jmp .loop + .not_found: xor eax, eax mov eax, UNRECOGNISED_ID_OPCODE @@ -2677,6 +2718,22 @@ opcodes: dd 0x00000000 + ; mov bit8 + dw 0x0056 + db 0x88 ; r/m8 <- r8 + db 0x8A ; r8 <- r/m8 + + db 0x00 + db 0xC6 ; r/m8 <- imm8 + dw 0x0000 + + dd 0x01000000 ; 000: + ; 0: r/m8 <- imm8 op flag + ; 00: + ; 01: bit8 flag + + dd 0x00000000 + ; add dw 0x0057 db 0x01 ; r/m <- r @@ -2747,6 +2804,20 @@ opcodes: dd 0x00000000 + ; cmp bit8 + dw 0x005B + db 0x38 ; r/m8 <- r8 + db 0x3A ; r8 <- r/m8 + + db 0x00 + db 0x80 ; r/m8 <- imm8 + dw 0x0000 + + dd 0x01007000 ; 000: + ; 7: r/m8 <- imm8 op flag + ; 00: + ; 01: bit8 flag + ; jmp dw 0x005C db 0xFF ; r/m diff --git a/twasm/asm/tests.asm b/twasm/asm/tests.asm index 980d53d..f0ee54a 100644 --- a/twasm/asm/tests.asm +++ b/twasm/asm/tests.asm @@ -295,6 +295,7 @@ test_get_opcode: mov di, 0x0053 ; xor mov sil, 0 + mov bl, 0 call get_opcode cmp al, 0x31 jne .fail @@ -303,6 +304,7 @@ test_get_opcode: mov di, 0x0053 ; xor mov sil, 1 + mov bl, 0 call get_opcode cmp al, 0x33 jne .fail @@ -311,6 +313,7 @@ test_get_opcode: mov di, 0x0053 ; xor mov sil, 2 + mov bl, 0 call get_opcode cmp al, 0x81 jne .fail @@ -319,6 +322,7 @@ test_get_opcode: mov di, 0x0053 ; xor mov sil, 3 + mov bl, 0 call get_opcode cmp al, 0x83 jne .fail @@ -327,6 +331,7 @@ test_get_opcode: mov di, 0x0054 ; inc mov sil, 0 + mov bl, 0 call get_opcode cmp al, 0xFF jne .fail @@ -335,6 +340,7 @@ test_get_opcode: mov di, 0x0055 ; dec mov sil, 0 + mov bl, 0 call get_opcode cmp al, 0xFF jne .fail @@ -343,6 +349,7 @@ test_get_opcode: mov di, 0x004F ; hlt mov sil, 0 + mov bl, 0 call get_opcode cmp al, 0xF4 jne .fail @@ -351,6 +358,7 @@ test_get_opcode: mov di, 0x0059 ; call mov sil, 0q0 + mov bl, 0 call get_opcode cmp al, 0xFF jne .fail @@ -359,6 +367,7 @@ test_get_opcode: mov di, 0x0003 ; rdx (not an operator) mov sil, 0q0 + mov bl, 0 call get_opcode cmp al, UNRECOGNISED_ID_OPCODE jne .fail diff --git a/twasm/package.nix b/twasm/package.nix index ee4e250..c96b1bc 100644 --- a/twasm/package.nix +++ b/twasm/package.nix @@ -14,7 +14,8 @@ stdenv.mkDerivation { asm/main.asm \ -o out.bin \ -w+all \ - -w-reloc-abs + -w-reloc-abs \ + -O0 ''; installPhase = '' dd if=/dev/zero of=disk bs=512 count=2880