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

如何将指定x86-64汇编代码转换为等价的C实现?

Equivalent C Code for x86-64 Assembly decode Function

Let's break down the assembly instruction by instruction and map each step to equivalent C code to complete your decode function:

Step-by-Step Assembly to C Mapping

  • addq %rsi, %rdi: Adds parameter y (stored in %rsi) to x (stored in %rdi) → x = x + y;
  • imulq %rdx, %rdi: Multiplies the updated x by parameter z (stored in %rdx) → x = x * z;
  • movq %rdi, %rax: Copies the computed x into %rax (the return register) → long w = x;
  • sarq $15, %rax: Performs an arithmetic right shift of 15 bits. For signed long variables in C, the >> operator automatically does sign-extended arithmetic shift → w = w >> 15;
  • salq $31, %rax: Performs a logical left shift of 31 bits (shifts bits left, fills lower bits with 0). The C << operator behaves like this for signed values here (since we're shifting a sign-extended value, hardware treats it as logical left shift) → w = w << 31;
  • andq %rdi, %rax: Computes the bitwise AND between the updated x and the shifted value → w = w & x;
  • ret: Returns the value in %rax, so we return w.

Complete C Code

long decode(long x, long y, long z) {
    x = x + y;
    x = x * z;
    long w = x;
    w = w >> 15;
    w = w << 31;
    w = w & x;
    return w;
}

Alternatively, you can condense it into a more concise form (while keeping the exact same logic):

long decode(long x, long y, long z) {
    long temp = (x + y) * z;
    return ((temp >> 15) << 31) & temp;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 10:31:22