首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >nasm代码的实现

nasm代码的实现
EN

Stack Overflow用户
提问于 2016-11-06 13:33:15
回答 1查看 148关注 0票数 1

我想开始把一个小小的nasm项目{synth.asmcore.nh}转换成c,以了解更多关于这个小软合成器的知识。

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

另一种选择是手动执行asm->c转换,但我很难理解最简单的函数之一:(

ie:

代码语言:javascript
复制
;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

失败的尝试:

代码语言:javascript
复制
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清单是一个非常简单的循环,扭曲了一个简单的音频缓冲区,但是我不明白哪些是输入,哪些是输出,我甚至不理解循环条件:')

任何帮助上面的例行公事,以及如何取得进展,这个小教育项目将是非常感谢的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-06 17:10:03

这里有一些猜测:

代码语言:javascript
复制
;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:

代码语言:javascript
复制
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重新实现中,您可能希望更加明确,并修复零大小的缓冲区错误。不会花多少钱:

代码语言:javascript
复制
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;
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40449892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档