nasm代码的C实现

C Implementation of nasm code

我想开始将一个小 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;
}