From 942831934dfff9d4c8dd3d86a13fb454d7f2d09f Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 26 Oct 2026 17:07:18 +0000 Subject: [PATCH] Assembler: push of a constant, cld and std --- diff --git a/doc/compiler.md b/doc/compiler.md index 6a01b09..8f27133 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -1175,7 +1175,7 @@ `BitOps` the bit scans and counts: `bsf`, `bsr`, `tzcnt`, `lzcnt`, `popcnt`). A kernel's instructions are there too, though the code generator never makes them: `cli`, `sti`, `iretq`, `swapgs`, `rdmsr`, -`wrmsr` and `wbinvd` in `Plain`; `lgdt`, `lidt`, `invlpg`, `xsave` and +`wrmsr`, `wbinvd`, `cld` and `std` in `Plain`; `push` of a constant (6A and a byte when it fits a signed byte, else 68 and four, sign-extended to 64 bits); `lgdt`, `lidt`, `invlpg`, `xsave` and `xrstor`, of one memory operand, in `MemOps`; `ltr` of a 16-bit register; `mov` to and from `cr0`, `cr2`, `cr3`, `cr4` and `cr8` (an operand of its own kind, `oCr`); and `in` and `out`, of `al`, `ax` or diff --git a/src/X64.raz b/src/X64.raz index 6d70cd5..f2c05d6 100644 --- a/src/X64.raz +++ b/src/X64.raz @@ -1141,13 +1141,13 @@ BitOps = " bsf bsr tzcnt lzcnt popcnt "; BitPfx: [5]INT = [0, 0, 0F3H, 0F3H, 0F3H]; BitCodes: [5]INT = [0BC0FH, 0BD0FH, 0BC0FH, 0BD0FH, 0B80FH]; - (* the instructions of no operands; the last seven are a kernel's *) - Plain: [22][]const U8 = ["ret", "leave", "syscall", "cpuid", "rdtsc", + (* the instructions of no operands; the last nine are a kernel's *) + Plain: [24][]const U8 = ["ret", "leave", "syscall", "cpuid", "rdtsc", "pause", "nop", "ud2", "int3", "hlt", "cqo", "cdq", "mfence", "lfence", "sfence", - "cli", "sti", "iretq", "swapgs", "rdmsr", "wrmsr", "wbinvd"]; - PlainOp: [22]INT = [0C3H, 0C9H, 050FH, 0A20FH, 310FH, + "cli", "sti", "iretq", "swapgs", "rdmsr", "wrmsr", "wbinvd", "cld", "std"]; + PlainOp: [24]INT = [0C3H, 0C9H, 050FH, 0A20FH, 310FH, 90F3H, 90H, 0B0FH, 0CCH, 0F4H, 9948H, 99H, 0F0AE0FH, 0E8AE0FH, 0F8AE0FH, - 0FAH, 0FBH, 0CF48H, 0F8010FH, 320FH, 300FH, 090FH]; + 0FAH, 0FBH, 0CF48H, 0F8010FH, 320FH, 300FH, 090FH, 0FCH, 0FDH]; (* the privileged instructions of one memory operand, 0F 01 /digit but for xsave and xrstor, 0F AE /digit *) MemOps = " lgdt lidt invlpg xsave xrstor "; @@ -1317,6 +1317,17 @@ return bad(); end; elsif (aw = "push") or (aw = "pop") then + if (aw = "push") and (an = 1) and (ao[0].kind = oImm) then + (* a constant, sign-extended to 64 bits: 6A and a byte if it fits + one, else 68 and four bytes *) + if (ao[0].v < -80000000H) or (ao[0].v > 7FFFFFFFH) then return bad(); end; + if (ao[0].v >= -128) and (ao[0].v <= 127) then + Chunk.put1(6AH); Chunk.put1(INT(ao[0].v & 0FFH)); + else + Chunk.put1(68H); Chunk.put4(INT(ao[0].v & 0FFFFFFFFH)); + end; + return nil; + end; if (an # 1) or (ao[0].kind # oReg) or not awide then return bad(); end; if aw = "push" then push(ao[0].r); else pop(ao[0].r); end; elsif (aw = "call") or (aw = "jmp") then diff --git a/test/run/AsmKernel.expect b/test/run/AsmKernel.expect index 0a37d3d..a0bfc6e 100644 --- a/test/run/AsmKernel.expect +++ b/test/run/AsmKernel.expect @@ -34,3 +34,12 @@ out dx, al: EE out dx, ax: 66 EF out dx, eax: EF +push 0: 6A 00 +push 127: 6A 7F +push -128: 6A 80 +push 128: 68 80 00 00 00 +push -129: 68 7F FF FF FF +push 7FFFFFFFH: 68 FF FF FF 7F +push -80000000H: 68 00 00 00 80 +cld: FC +std: FD diff --git a/test/run/AsmKernel.raz b/test/run/AsmKernel.raz index 8b5ec89..cc2b62c 100644 --- a/test/run/AsmKernel.raz +++ b/test/run/AsmKernel.raz @@ -6,7 +6,7 @@ import SYSTEM; const - Count = 36; + Count = 45; Names: [Count][]const U8 = ["cli", "sti", "iretq", "swapgs", "rdmsr", "wrmsr", "wbinvd", "lgdt [rdi]", "lidt [rsi + 8]", "invlpg [rax]", "invlpg [r12 + rcx * 8 + 400]", "xsave [rdi]", @@ -15,10 +15,13 @@ "mov cr8, r11", "mov rax, cr0", "mov r9, cr3", "mov rbx, cr8", "in al, 60H", "in ax, 61H", "in eax, 0FFH", "in al, dx", "in ax, dx", "in eax, dx", "out 80H, al", "out 80H, ax", - "out 0, eax", "out dx, al", "out dx, ax", "out dx, eax"]; + "out 0, eax", "out dx, al", "out dx, ax", "out dx, eax", + "push 0", "push 127", "push -128", "push 128", "push -129", + "push 7FFFFFFFH", "push -80000000H", "cld", "std"]; Sizes: [Count]INT = [1, 1, 2, 3, 2, 2, 2, 3, 4, 3, 9, 3, 4, 3, 4, 3, 3, 4, 3, 4, 4, 3, 4, 4, - 2, 3, 2, 1, 2, 1, 2, 3, 2, 1, 2, 1]; + 2, 3, 2, 1, 2, 1, 2, 3, 2, 1, 2, 1, + 2, 2, 2, 5, 5, 5, 5, 1, 1]; proc kernel; code @@ -58,6 +61,15 @@ out dx, al; out dx, ax; out dx, eax; + push 0; + push 127; + push -128; + push 128; + push -129; + push 7FFFFFFFH; + push -80000000H; + cld; + std; ret; end kernel;