diff --git a/tests/src/tests/tests.serialization.cpp b/tests/src/tests/tests.serialization.cpp index 788b807..df4f944 100644 --- a/tests/src/tests/tests.serialization.cpp +++ b/tests/src/tests/tests.serialization.cpp @@ -1365,4 +1365,27 @@ namespace zasm::tests } } + TEST(SerializationTests, Issue_138) + { + Program program(zasm::MachineMode::AMD64); + x86::Assembler assembler(program); + + assembler.mov(x86::ecx, Imm(0xFFFFFFFF)); + + zasm::Serializer serializer{}; + ASSERT_EQ(serializer.serialize(program, 0x140015000), ErrorCode::None); + + const std::array expected = { + 0xB9, 0xFF, 0xFF, 0xFF, 0xFF, + }; + ASSERT_EQ(serializer.getCodeSize(), expected.size()); + + const auto* data = serializer.getCode(); + ASSERT_NE(data, nullptr); + for (std::size_t i = 0; i < expected.size(); i++) + { + ASSERT_EQ(data[i], expected[i]); + } + } + } // namespace zasm::tests diff --git a/zasm/include/zasm/base/immediate.hpp b/zasm/include/zasm/base/immediate.hpp index 5769c0d..8f37363 100644 --- a/zasm/include/zasm/base/immediate.hpp +++ b/zasm/include/zasm/base/immediate.hpp @@ -8,69 +8,65 @@ namespace zasm { - class Imm + class Imm { - union - { - std::int64_t s; - std::uint64_t u; - }; + std::int64_t _value; public: constexpr Imm() noexcept - : s{} + : _value{} { } constexpr Imm(std::uint32_t imm) noexcept - : u{ imm } + : _value{ static_cast(imm) } { } constexpr Imm(std::int32_t imm) noexcept - : s{ imm } + : _value{ imm } { } constexpr Imm(std::int64_t imm) noexcept - : s{ imm } + : _value{ imm } { } constexpr Imm(std::uint64_t imm) noexcept - : u{ imm } + : _value{ static_cast(imm) } { } constexpr bool operator==(const Imm& other) const noexcept { - return u == other.u; + return _value == other._value; } constexpr bool operator!=(const Imm& other) const noexcept { - return u != other.u; + return _value != other._value; } template constexpr T value() const noexcept { - return static_cast(s); + return static_cast(_value); } template Imm& setValue(const T val) { - s = static_cast(val); + _value = static_cast(val); return *this; } constexpr BitSize getBitSize() const noexcept { - if (math::abs(s) > std::numeric_limits::max()) + if (math::abs(_value) > std::numeric_limits::max()) { return BitSize::_64; } - if (math::abs(s) > std::numeric_limits::max()) + if (math::abs(_value) > std::numeric_limits::max()) { return BitSize::_32; } - if (math::abs(s) > std::numeric_limits::max()) + if (math::abs(_value) > std::numeric_limits::max()) { return BitSize::_16; } @@ -85,7 +81,7 @@ namespace zasm namespace detail { - template class ImmT final : public Imm + template class ImmT final : public Imm { public: template