From df43ce6916b72d7b2567c32e737d64ec8b5384b3 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 15 Jul 2026 17:22:14 +0200 Subject: [PATCH] Fixed incorrect conditional set encoding on x86-64 --- src/x86/Set.go | 20 ++++++++++---------- src/x86/Set_test.go | 2 ++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/x86/Set.go b/src/x86/Set.go index 9217a46..a004525 100644 --- a/src/x86/Set.go +++ b/src/x86/Set.go @@ -4,50 +4,50 @@ import "git.urbach.dev/cli/q/src/cpu" // SetIfEqual sets the register to 1 if the result was equal, otherwise to 0. func SetIfEqual(code []byte, destination cpu.Register) []byte { - return append(code, 0x0F, 0x94, ModRM(AddressDirect, 0, byte(destination))) + return encode(code, AddressDirect, 0, destination, 1, 0x0F94) } // SetIfNotEqual sets the register to 1 if the result was not equal, otherwise to 0. func SetIfNotEqual(code []byte, destination cpu.Register) []byte { - return append(code, 0x0F, 0x95, ModRM(AddressDirect, 0, byte(destination))) + return encode(code, AddressDirect, 0, destination, 1, 0x0F95) } // SetIfGreater sets the register to 1 if the result was greater, otherwise to 0. func SetIfGreater(code []byte, destination cpu.Register) []byte { - return append(code, 0x0F, 0x9F, ModRM(AddressDirect, 0, byte(destination))) + return encode(code, AddressDirect, 0, destination, 1, 0x0F9F) } // SetIfGreaterEqual sets the register to 1 if the result was greater or equal, otherwise to 0. func SetIfGreaterEqual(code []byte, destination cpu.Register) []byte { - return append(code, 0x0F, 0x9D, ModRM(AddressDirect, 0, byte(destination))) + return encode(code, AddressDirect, 0, destination, 1, 0x0F9D) } // SetIfLess sets the register to 1 if the result was less, otherwise to 0. func SetIfLess(code []byte, destination cpu.Register) []byte { - return append(code, 0x0F, 0x9C, ModRM(AddressDirect, 0, byte(destination))) + return encode(code, AddressDirect, 0, destination, 1, 0x0F9C) } // SetIfLessEqual sets the register to 1 if the result was less or equal, otherwise to 0. func SetIfLessEqual(code []byte, destination cpu.Register) []byte { - return append(code, 0x0F, 0x9E, ModRM(AddressDirect, 0, byte(destination))) + return encode(code, AddressDirect, 0, destination, 1, 0x0F9E) } // SetIfUnsignedGreater sets the register to 1 if the result was greater using unsigned comparison, otherwise to 0. func SetIfUnsignedGreater(code []byte, destination cpu.Register) []byte { - return append(code, 0x0F, 0x97, ModRM(AddressDirect, 0, byte(destination))) + return encode(code, AddressDirect, 0, destination, 1, 0x0F97) } // SetIfUnsignedGreaterEqual sets the register to 1 if the result was greater or equal using unsigned comparison, otherwise to 0. func SetIfUnsignedGreaterEqual(code []byte, destination cpu.Register) []byte { - return append(code, 0x0F, 0x93, ModRM(AddressDirect, 0, byte(destination))) + return encode(code, AddressDirect, 0, destination, 1, 0x0F93) } // SetIfUnsignedLess sets the register to 1 if the result was less using unsigned comparison, otherwise to 0. func SetIfUnsignedLess(code []byte, destination cpu.Register) []byte { - return append(code, 0x0F, 0x92, ModRM(AddressDirect, 0, byte(destination))) + return encode(code, AddressDirect, 0, destination, 1, 0x0F92) } // SetIfUnsignedLessEqual sets the register to 1 if the result was less or equal using unsigned comparison, otherwise to 0. func SetIfUnsignedLessEqual(code []byte, destination cpu.Register) []byte { - return append(code, 0x0F, 0x96, ModRM(AddressDirect, 0, byte(destination))) + return encode(code, AddressDirect, 0, destination, 1, 0x0F96) } \ No newline at end of file diff --git a/src/x86/Set_test.go b/src/x86/Set_test.go index b0a06d6..10df804 100644 --- a/src/x86/Set_test.go +++ b/src/x86/Set_test.go @@ -34,6 +34,8 @@ func TestSet(t *testing.T) { {x86.R1, 9, []byte{0x0F, 0x92, 0xC1}}, {x86.R0, 10, []byte{0x0F, 0x96, 0xC0}}, {x86.R1, 10, []byte{0x0F, 0x96, 0xC1}}, + {x86.R8, 1, []byte{0x41, 0x0F, 0x94, 0xC0}}, + {x86.R9, 1, []byte{0x41, 0x0F, 0x94, 0xC1}}, } for _, pattern := range usagePatterns { -- 2.51.2