![Ubuntu 上的汇编语言](https://linux22.com/image/917722/Ubuntu%20%E4%B8%8A%E7%9A%84%E6%B1%87%E7%BC%96%E8%AF%AD%E8%A8%80.png)
所以我目前正在研究汇编语言,并且在终端上遇到了这个很棒的命令,gcc -S filename.cpp
它可以生成 C++ 源文件的汇编代码。
但是在我安装了 NASM 并尝试链接和编译 .S 文件之后(我也尝试通过从 .s 文件复制粘贴代码来使用 .asm),我最终只在终端中遇到错误。
我确信 C++ 源文件中没有错误,因为它只是一个 hello world 程序,但我知道我遗漏了一些东西,我希望有人能为我指出来。
以下是 C++ 源代码:
#include <iostream>
using namespace std;
int main ()
{
cout<<"wtf";
return 0;
}
以下是.s文件代码:
.file "try1.cpp"
.text
.section .rodata
.type _ZStL19piecewise_construct, @object
.size _ZStL19piecewise_construct, 1
_ZStL19piecewise_construct:
.zero 1
.local _ZStL8__ioinit
.comm _ZStL8__ioinit,1,1
.LC0:
.string "wtf"
.text
.globl main
.type main, @function
main:
.LFB1493:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
leaq .LC0(%rip), %rsi
leaq _ZSt4cout(%rip), %rdi
call _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@PLT
movl $0, %eax
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1493:
.size main, .-main
.type _Z41__static_initialization_and_destruction_0ii, @function
_Z41__static_initialization_and_destruction_0ii:
.LFB1977:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $16, %rsp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
cmpl $1, -4(%rbp)
jne .L5
cmpl $65535, -8(%rbp)
jne .L5
leaq _ZStL8__ioinit(%rip), %rdi
call _ZNSt8ios_base4InitC1Ev@PLT
leaq __dso_handle(%rip), %rdx
leaq _ZStL8__ioinit(%rip), %rsi
movq _ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rax
movq %rax, %rdi
call __cxa_atexit@PLT
.L5:
nop
leave
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1977:
.size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii
.type _GLOBAL__sub_I_main, @function
_GLOBAL__sub_I_main:
.LFB1978:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
movl $65535, %esi
movl $1, %edi
call _Z41__static_initialization_and_destruction_0ii
popq %rbp
.cfi_def_cfa 7, 8
ret
.cfi_endproc
.LFE1978:
.size _GLOBAL__sub_I_main, .-_GLOBAL__sub_I_main
.section .init_array,"aw"
.align 8
.quad _GLOBAL__sub_I_main
.hidden __dso_handle
.ident "GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0"
.section .note.GNU-stack,"",@progbits
以下是来自终端的错误消息:
oliver@Nimitz:~$ nasm -f elf64 try1.asm
try1.asm:1: error: attempt to define a local label before any non-local labels
try1.asm:1: error: parser: instruction expected
try1.asm:2: warning: label alone on a line without a colon might be in error [-w+orphan-labels]
try1.asm:2: error: attempt to define a local label before any non-local labels
try1.asm:3: error: attempt to define a local label before any non-local labels
try1.asm:3: error: parser: instruction expected
try1.asm:4: error: attempt to define a local label before any non-local labels
try1.asm:4: error: parser: instruction expected
try1.asm:5: error: attempt to define a local label before any non-local labels
try1.asm:5: error: parser: instruction expected
try1.asm:7: error: parser: instruction expected
try1.asm:8: error: parser: instruction expected
try1.asm:9: error: parser: instruction expected
try1.asm:11: error: parser: instruction expected
try1.asm:12: warning: label alone on a line without a colon might be in error [-w+orphan-labels]
try1.asm:13: error: parser: instruction expected
try1.asm:14: error: parser: instruction expected
try1.asm:17: warning: label alone on a line without a colon might be in error [-w+orphan-labels]
try1.asm:18: error: parser: instruction expected
try1.asm:19: error: parser: instruction expected
try1.asm:20: error: parser: instruction expected
try1.asm:21: error: expression syntax error
try1.asm:22: error: parser: instruction expected
try1.asm:23: error: parser: instruction expected
try1.asm:24: error: symbol `leaq' redefined
try1.asm:24: error: parser: instruction expected
try1.asm:26: error: parser: instruction expected
try1.asm:27: error: parser: instruction expected
try1.asm:28: error: parser: instruction expected
try1.asm:30: warning: label alone on a line without a colon might be in error [-w+orphan-labels]
try1.asm:32: error: parser: instruction expected
try1.asm:33: error: parser: instruction expected
try1.asm:36: warning: label alone on a line without a colon might be in error [-w+orphan-labels]
try1.asm:37: error: symbol `pushq' redefined
try1.asm:37: error: parser: instruction expected
try1.asm:38: error: parser: instruction expected
try1.asm:39: error: parser: instruction expected
try1.asm:40: error: expression syntax error
try1.asm:41: error: parser: instruction expected
try1.asm:42: error: parser: instruction expected
try1.asm:43: error: symbol `movl' redefined
try1.asm:43: error: parser: instruction expected
try1.asm:44: error: symbol `movl' redefined
try1.asm:44: error: parser: instruction expected
try1.asm:45: error: parser: instruction expected
try1.asm:47: error: symbol `cmpl' redefined
try1.asm:47: error: parser: instruction expected
try1.asm:49: error: symbol `leaq' redefined
try1.asm:49: error: parser: instruction expected
try1.asm:51: error: symbol `leaq' redefined
try1.asm:51: error: parser: instruction expected
try1.asm:52: error: symbol `leaq' redefined
try1.asm:52: error: parser: instruction expected
try1.asm:53: error: comma, colon, decorator or end of line expected after operand
try1.asm:53: error: expression syntax error
try1.asm:54: error: expression syntax error
try1.asm:59: error: parser: instruction expected
try1.asm:61: warning: label alone on a line without a colon might be in error [-w+orphan-labels]
try1.asm:63: error: parser: instruction expected
try1.asm:64: error: parser: instruction expected
try1.asm:67: warning: label alone on a line without a colon might be in error [-w+orphan-labels]
try1.asm:68: error: symbol `pushq' redefined
try1.asm:68: error: parser: instruction expected
try1.asm:69: error: parser: instruction expected
try1.asm:70: error: parser: instruction expected
try1.asm:71: error: expression syntax error
try1.asm:72: error: parser: instruction expected
try1.asm:73: error: symbol `movl' redefined
try1.asm:73: error: parser: instruction expected
try1.asm:74: error: symbol `movl' redefined
try1.asm:74: error: parser: instruction expected
try1.asm:76: error: symbol `popq' redefined
try1.asm:76: error: parser: instruction expected
try1.asm:77: error: parser: instruction expected
try1.asm:79: warning: label alone on a line without a colon might be in error [-w+orphan-labels]
try1.asm:81: error: parser: instruction expected
try1.asm:82: error: parser: instruction expected
try1.asm:83: error: parser: instruction expected
try1.asm:84: error: parser: instruction expected
try1.asm:85: error: parser: instruction expected
try1.asm:86: error: parser: instruction expected
try1.asm:87: error: symbol `.section' redefined
try1.asm:87: error: parser: instruction expected
oliver@Nimitz:~$