Assembly error when compiling with GCC

10,214

I think the following code will compile ("gcc" can compile .s and .S files and link them with C library by default but "as" do the same and don't link code with C library) as :

.section .text
    .global _start
_start:
    mov $4,%eax
    mov $1,%ebx
    mov $message,%ecx
    mov msglength,%edx
    int  $0x80

    mov $1, %eax
    mov $0, %ebx
    int $0x80
.section .data
    message: .ascii "Hello world!"
    msglength: .word 12

gcc:

.section .text
    .global main
main:
    mov $4,%eax
    mov $1,%ebx
    mov $message,%ecx
    mov msglength,%edx
    int  $0x80

    mov $1, %eax
    mov $0, %ebx
    int $0x80
.section .data
    message: .ascii "Hello world!"
    msglength: .word 12
Share:
10,214
nelthas
Author by

nelthas

Updated on August 21, 2022

Comments

  • nelthas
    nelthas over 1 year

    I'm getting "no such instruction" errors when compiling a .s file with this command:

    $ gcc -s -o scall scall.s
    scall.s: Assembler messages:
    scall.s:2: Error: no such instruction: `section '
    scall.s:4: Error: no such instruction: `global _start'
    scall.s:7: Error: unsupported instruction `mov'
    scall.s:8: Error: unsupported instruction `mov'
    scall.s:11: Error: operand size mismatch for `int'
    scall.s:13: Error: no such instruction: `section .data'
    scall.s:15: Error: no such instruction: `msglength .word 12'
    

    Here is the code of the file:

    section .text
        global _start
    
    _start:
        mov 4,%eax
        mov 1,%ebx
        mov $message,%ecx
        mov $msglength,%edx
        int  $0x80
    
    section .data
       message: .ascii "Hello world!"
       msglength .word 12
    

    How can I get rid of the errors?

  • nelthas
    nelthas about 9 years
    Yes that got rid of the no such errors, but instead I get In function _start': (.text+0x0): multiple definition of _start' /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
  • Parham Alvani
    Parham Alvani about 9 years
    if you use gcc you must use main instead of _start.
  • nelthas
    nelthas about 9 years
    Thanks, that did it. When I run it now though I get Segmentation fault(core dumped). Am I pointing at something that doesn't exist?
  • Parham Alvani
    Parham Alvani about 9 years
    I think you must correct following line mov $msglength, %edx to mov msglength, %edx.
  • nelthas
    nelthas about 9 years
    No I still get the same error. Maybe I should make a new thread for this or mods will be mad.
  • Parham Alvani
    Parham Alvani about 9 years
    You MUST call exit system call (eax = 1) after finishing. i correct the above code for you.
  • nelthas
    nelthas about 9 years
    Yes, now it works. Thanks alot! So AT&T syntax requires $ infront of numbers and variables then I guess. Is the exit system call always required for all system calls or is it only for writing/reading operations?
  • Parham Alvani
    Parham Alvani about 9 years
    Yes, AT&T syntax require $ infront of numbers and variables that you need their address. you MUST exit from every assembly program.
  • Peter Cordes
    Peter Cordes almost 6 years
    You don't have to use main with gcc. You can use gcc -static -nostdlib start.s to assemble and link a static executable without linking CRT startup files (which defines a conflicting _start).
  • Peter Cordes
    Peter Cordes almost 6 years
    Also, the better fix for msglength would be .equ msglength, . - message to make the assembler calculate the length for you, and use it as an immediate constant instead of loading it.
  • Peter Cordes
    Peter Cordes almost 6 years
    The right extension for -c is .o. You're just assembling without linking here. And you didn't fix any of the bugs in the asm, and you changed the OP's _start to main: use gcc -m32 -nostdlib -static test.s -o test to assemble+link a 32-bit static executable.