You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求可在C++中实现x64汇编字符串转字节编码的工具

x64 Tools to Convert Assembly Strings to Hex Bytes

Got it, let's tackle this—you're looking for x64 equivalents of asmpure that can turn assembly strings into hex byte sequences, right? Here are some solid options that work great for x86-64 platforms, whether you need a command-line tool or a library to embed in your C++ code:

1. NASM (Netwide Assembler)

NASM is a tried-and-true assembler with full x86-64 support, and it can output raw binary bytes that you can easily convert to hex. Here's how to use it:

  • Create a small .asm file with your instruction, e.g., mov rdx, rax
  • Run this command to generate a raw binary file:
    nasm -f bin -o output.bin input.asm
    
  • Convert the binary to hex using a tool like xxd:
    xxd -p output.bin
    
    For your examples:
    • "mov rdx, rax" will output 488bd0 (or formatted as 48 8B D0 if you use xxd -c 20)
    • "add ax, 17h" gives 6683c017
    • "sub cl, 5Bh" gives 80e95b

You can even pipe everything together to skip the file step:

echo 'mov rdx, rax' | nasm -f bin -o - - | xxd -p -c 20

2. Keystone Engine (Perfect for C++ Integration)

If you need to do this directly within a C++ program (which it sounds like you might, given your mention of C++), Keystone Engine is your best bet. It's a lightweight open-source assembly library with full x86-64 support and official C++ bindings.

Here's a quick example that converts your sample instructions to hex bytes:

#include <keystone/keystone.h>
#include <iostream>
#include <iomanip>

int main() {
    ks_engine *ks;
    ks_err err;
    unsigned char *code;
    size_t code_size;

    // Initialize Keystone for x86-64 mode
    err = ks_open(KS_ARCH_X86, KS_MODE_64, &ks);
    if (err != KS_ERR_OK) {
        std::cerr << "Failed to initialize Keystone: " << ks_strerror(err) << std::endl;
        return 1;
    }

    // Test your first instruction
    const char *asm1 = "mov rdx, rax";
    err = ks_asm(ks, asm1, 0, &code, &code_size, NULL);
    if (err == KS_ERR_OK) {
        std::cout << "\"mov rdx, rax\" => ";
        for (size_t i = 0; i < code_size; i++) {
            std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(code[i]) << " ";
        }
        std::cout << "\n";
        ks_free(code);
    }

    // Test the second instruction
    const char *asm2 = "add ax, 17h";
    err = ks_asm(ks, asm2, 0, &code, &code_size, NULL);
    if (err == KS_ERR_OK) {
        std::cout << "\"add ax, 17h\" => ";
        for (size_t i = 0; i < code_size; i++) {
            std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(code[i]) << " ";
        }
        std::cout << "\n";
        ks_free(code);
    }

    // Test the third instruction
    const char *asm3 = "sub cl, 5Bh";
    err = ks_asm(ks, asm3, 0, &code, &code_size, NULL);
    if (err == KS_ERR_OK) {
        std::cout << "\"sub cl, 5Bh\" => ";
        for (size_t i = 0; i < code_size; i++) {
            std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(code[i]) << " ";
        }
        std::cout << "\n";
        ks_free(code);
    }

    // Cleanup
    ks_close(ks);
    return 0;
}

When you compile this (linking against the Keystone library), e.g.:

g++ -o asm_to_hex asm_to_hex.cpp -lkeystone

Running it will output exactly the hex sequences you're looking for:

"mov rdx, rax" => 48 8b d0 
"add ax, 17h" => 66 83 c0 17 
"sub cl, 5Bh" => 80 e9 5b 

3. YASM

YASM is another popular assembler that's syntax-compatible with NASM and fully supports x86-64. Its usage is nearly identical to NASM—just replace nasm with yasm in the commands above, and you'll get the same results. It's a good alternative if you prefer or need compatibility with certain build systems.


内容的提问来源于stack exchange,提问作者Eduardo Magalhaes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:24:23