Skip to content

Commit

Permalink
Fix memory scale defaulting to 0 rather than 1 when index is supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
ZehMatt committed May 12, 2024
1 parent 012062f commit 841038a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
4 changes: 2 additions & 2 deletions include/zasm/x86/memory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace zasm::x86
// ex.: mov eax, ptr [ecx+edx]
static constexpr Mem ptr(BitSize bitSize, const Gp& base, const Gp& index) noexcept
{
return Mem(bitSize, Seg{}, base, index, 0, 0);
return Mem(bitSize, Seg{}, base, index, 1, 0);
}

// ptr [base + index * scale + disp]
Expand Down Expand Up @@ -96,7 +96,7 @@ namespace zasm::x86
// ex.: mov eax, ptr:ds [edx+ecx]
static constexpr Mem ptr(BitSize bitSize, const Seg& seg, const Gp& base, const Gp& index) noexcept
{
return Mem(bitSize, seg, base, index, 0, 0);
return Mem(bitSize, seg, base, index, 1, 0);
}

// ptr : seg [base + index * scale + disp]
Expand Down
25 changes: 22 additions & 3 deletions src/tests/tests/tests.serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1104,9 +1104,7 @@ namespace zasm::tests
Serializer serializer;
ASSERT_EQ(serializer.serialize(program, 0x0000000000401000), Error::None);

const std::array<std::uint8_t, 7> expected = {
0x48, 0x8B, 0x15, 0xF9, 0xFF, 0xFF, 0xFF
};
const std::array<std::uint8_t, 7> expected = { 0x48, 0x8B, 0x15, 0xF9, 0xFF, 0xFF, 0xFF };
ASSERT_EQ(serializer.getCodeSize(), expected.size());

const auto* data = serializer.getCode();
Expand Down Expand Up @@ -1192,4 +1190,25 @@ namespace zasm::tests
ASSERT_EQ(serializer.getLabelAddress(label.getId()), 0x140015000 + 13);
}

TEST(SerializationTests, TestMemBaseIndex)
{
Program program(MachineMode::AMD64);

x86::Assembler a(program);
ASSERT_EQ(a.mov(x86::rax, x86::qword_ptr(x86::rax, x86::rbp)), Error::None);

Serializer serializer;
ASSERT_EQ(serializer.serialize(program, 0x140015000), Error::None);

const std::array<uint8_t, 4> expected = { 0x48, 0x8B, 0x04, 0x28 };
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

0 comments on commit 841038a

Please sign in to comment.