C nasm代码的实现

标签 c assembly nasm

我想开始转换一个小的 nasm 项目 { synth.asm , synth_core.nh } 到 c 以了解更多有关该小型软合成器的信息。

问题是我的 asm 知识非常生疏,我想知道从哪里开始。我想也许一个反编译器可以帮我解决问题,但我还没有找到任何能够将这些简单的 nasm 列表转换为 c 的开源软件。

另一种选择是手动进行转换 asm->c,但我正在努力理解最简单的函数之一 :(

即:

;distortion_machine
;---------------------------
;float a
;float b
;---------------------------
;ebp: distort definition 
;edi: stackptr
;ecx: length
section distcode code align=1
distortion_machine:
    pusha
    add ecx, ecx
    .sampleloop:
        fld dword [edi]
        fld dword [ebp+0]
        fpatan
        fmul dword [ebp+4]
        fstp dword [edi]
        scasd
    loop .sampleloop
    popa
    add esi, byte 8
ret

失败的尝试:

void distortion_machine(???) { // pusha; saving all registers
    int ecx = ecx+ecx; // add ecx, ecx; this doesn't make sense

    while(???) { // .sampleloop; what's the condition?
        float a = [edi];   // fld dword [edi]; docs says edi is stackptr, what's the meaning?
        float b = [ebp+0]; // fld dword [ebp+0]; docs says ebp is distort definition, is that an input parameter?
        float c = atan(a,b); // fpatan;
        float d = c*[ebp+4]; // fmul dword [ebp+4];
        // scasd; what's doing this instruction?
    }

    return ???;

    // popa; restoring all registers
    // add esi, byte 8;
}

我猜上面的 nasm 列表是一个非常简单的循环,它扭曲了一个简单的音频缓冲区,但我不明白哪些是输入,哪些是输出,我什至不明白循环条件 :')

对于上述例程以及如何推进这个小型教育项目的任何帮助,我们将不胜感激。

最佳答案

这里有一些猜测:

;distortion_machine
;---------------------------
;float a << input is 2 arrays of floats, a and b, successive on stack
;float b
;---------------------------
;ebp: distort definition  << 2 floats that control distortion
;edi: stackptr            << what it says
;ecx: length              << of each input array (a and b)
section distcode code align=1
distortion_machine:
    pusha        ; << save all registers
    add ecx, ecx ; << 2 arrays, so double for element count of both
    .sampleloop:
        fld dword [edi]    ; << Load next float from stack
        fld dword [ebp+0]  ; << Load first float of distortion control
        fpatan             ; << Distort with partial atan.
        fmul dword [ebp+4] ; << Scale by multiplying with second distortion float
        fstp dword [edi]   ; << Store back to same location
        scasd              ; << Funky way to incremement stack pointer
    loop .sampleloop       ; << decrement ecx and jump if not zero
    popa                   ; << restore registers
    add esi, byte 8        ; << See call site. si purpose here isn't stated 
ret

这是一个真实的猜测,但是esi可能是一个单独的参数堆栈指针,并且ab的地址已经被压入那里.此代码通过对数据堆栈布局做出假设来忽略它们,但它仍然需要从 arg 堆栈中删除这些指针。

近似 C:

struct distortion_control {
  float level;
  float scale;
};

// Input: float vectors a and b stored consecutively in buf.
void distort(struct distortion_control *c, float *buf, unsigned buf_size) {
  buf_size *= 2;
  do { // Note both this and the assembly misbehave if buf_size==0
    *buf = atan2f(*buf, c->level) * c->scale;
    ++buf;
  } while (--buf_size);
}

在 C 重新实现中,您可能希望更明确并修复零大小缓冲区错误。它不会花费太多:

void distort(struct distortion_control *c, float *a, float *b, unsigned size) {
  for (unsigned n = size; n; --n, ++a) *a = atan2f(*a, c->level) * c->scale;
  for (unsigned n = size; n; --n, ++b) *b = atan2f(*b, c->level) * c->scale;
}

关于C nasm代码的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40449892/

相关文章:

c - 如何正确管理 pthread

c++ - main in反汇编后的说明

macos - 如何在asm中使用malloc

linux - 为什么我无法成功将变量内容压入堆栈?

assembly - 如何用汇编语言打印字符串

使用 'sprintf' 将十六进制转换为字符串

c - 在 C 中将值发送到声音驱动程序(const void)

c - 如何检查指针 (void) 的值不为零(尝试取消引用通用指针)

visual-studio - 当不需要清理时避免 "push ebp, mov esp, ebp"序言

linux - 对汇编中ADD指令或程序导出值的误解