5

So I was currently studying about assembly language and I came across this awesome command on terminal, gcc -S filename.cpp, that produces the assembly code of a C++ source file.

But after I installed NASM and tried to link and compile the .S file (I also tried to use .asm by copy-pasting the code from the .s file), I only ended up facing errors in the terminal.

I am sure there were no errors in the C++ source file because its just a hello world program, but I know that I am missing something and Im hoping someone can point it out for me.

Here is the c++ source code:

#include <iostream>

using namespace std;


int main ()
{

    cout<<"wtf";

    return 0;

}

Here is the .s file code:

.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

Here is the error message from the terminal:

    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:~$ 

screenshot of terminal

Fortem
  • 51
  • 6
  • 1
    Thanks for the edit. Do you specifically need to use `nasm`, or is your goal just to produce a binary from your assembly code (with any tool)? I recommend that you [edit] again to clarify this. – Eliah Kagan Nov 04 '19 at 22:14
  • 2
    The assembly language code produced by the GNU compiler is intended for the GNU assembler, [`as`](https://manpages.ubuntu.com/manpages/bionic/en/man1/as.1.html)(1). If you have `gcc` you most likely also have `as`. It is not a trivial task to convert GNU assembly source to NASM assembly source. In particular, the two assemblers have very different syntax and opposing opinions on the order of operands: `movl $0, %eax` in Gnu Assembler would be `mov eax, 0` in NASM... – AlexP Nov 04 '19 at 23:09
  • 2
    You may find the information here helpful: [How to generate assembly code with gcc that can be compiled with nasm](https://stackoverflow.com/questions/35102193/how-to-generate-assembly-code-with-gcc-that-can-be-compiled-with-nasm) – steeldriver Nov 04 '19 at 23:14
  • Thanks for the help! – Fortem Nov 05 '19 at 19:48

0 Answers0