Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        2年前关闭。
                                                                                            
                
        
我在理解如何将此汇编代码转换为C方面遇到麻烦。它非常简短,只有几行,而且答案应该是一个衬里。

char_out:
    subq   $8, %esp
    movsbl  %dil, %edi    # Parameter 1 in %dil / %edi
    call    putchar       # put char is a C function that writes (prints)
    addq    $8, %rsp      # a single char to stdout (the screen).
    ret




void char_out(char P1)
{
    //insert here
}

最佳答案

char_out:
    # Allocate 8 bytes on the stack by subtracting 8 from the stack pointer.
    #
    # This is done for conformance to the calling convention expected by the
    # 'putchar' function, which we're about to call.
    subq   $8, %esp

    # This is the weirdo AT&T mnemonic for the MOVSX instruction, which does a
    # move with sign extension. This means that it extends a small value
    # (here, a BYTE-sized value in DIL) into a larger value (here, a
    # DWORD-sized value in EDI), in a way that properly accounts for the
    # value's sign bit.
    #
    # This is necessary because the 'putchar' function expects to be passed a
    # 32-bit 'int' parameter.
    movsbl  %dil, %edi

    # It's obvious what this does: it calls the 'putchar' function.
    call    putchar

    # Clean up the stack, undoing what we previously did to the stack pointer
    # at the top of the function (the 'subq' instruction).
    addq    $8, %rsp


正如Lashane所说,此汇编代码等效于以下C代码:

void char_out(char P1)
{
    putchar(P1);
}


或者,我想你也可以说:

void char_out(char P1)
{
    int temp = (int)P1;     // movsbl
    putchar(temp);
}


但是C编译器会为您隐式地执行此操作,因此不必显式显示这种扩展转换。

关于c - 如何将汇编代码转换为C? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43573694/

10-11 23:03
查看更多