Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail to assemble mov al, -1 #327

Closed
ghost opened this issue Apr 20, 2022 · 1 comment · Fixed by #336
Closed

Fail to assemble mov al, -1 #327

ghost opened this issue Apr 20, 2022 · 1 comment · Fixed by #336

Comments

@ghost
Copy link

ghost commented Apr 20, 2022

#include <Zydis/Zydis.h>
#include <Zycore/LibC.h>

int main()
{
    ZyanU8 encoded_instruction[ZYDIS_MAX_INSTRUCTION_LENGTH];
    ZyanUSize encoded_length = sizeof(encoded_instruction);
    ZydisEncoderRequest req;
    ZYAN_MEMSET(&req, 0, sizeof(req));
    req.mnemonic = ZYDIS_MNEMONIC_MOV;
    req.machine_mode = ZYDIS_MACHINE_MODE_LONG_64;
    req.operand_count = 2;
    req.operands[0].type = ZYDIS_OPERAND_TYPE_REGISTER;
    req.operands[0].reg.value = ZYDIS_REGISTER_AL;
    req.operands[1].type = ZYDIS_OPERAND_TYPE_IMMEDIATE;
    req.operands[1].imm.s = -1;
    if (ZYAN_FAILED(ZydisEncoderEncodeInstruction(&req, encoded_instruction, &encoded_length)))
    {
        ZYAN_PUTS("Failed to encode instruction");
        return 1;
    }
    for (ZyanUSize i = 0; i < encoded_length; ++i)
    {
        ZYAN_PRINTF("%02X ", encoded_instruction[i]);
    }
    ZYAN_PUTS("");
    return 0;
}

Should assemble, but fails. It works if req.operands[1].imm.u = 0xFF; instead.

@mappzor
Copy link
Contributor

mappzor commented Apr 20, 2022

That's because all gpr8, imm have immediate value marked as unsigned. I mentioned that problem in #192. Some months ago we agreed to change several instruction definitions like this one (where signedness doesn't really exist) to "signed by default". This change will improve consistency with other definitions and automatically fix such encoding problems. I've started this effort but never finished it. It's a tedious process and it can be automated only partially. I still intend to fix it one day. For now your workaround is the intended way of handling this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant