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
Author by
nelthas
Updated on August 21, 2022Comments
-
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 about 9 yearsYes 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 about 9 yearsif you use gcc you must use main instead of _start.
-
nelthas about 9 yearsThanks, 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 about 9 yearsI think you must correct following line
mov $msglength, %edx
tomov msglength, %edx
. -
nelthas about 9 yearsNo I still get the same error. Maybe I should make a new thread for this or mods will be mad.
-
Parham Alvani about 9 yearsYou MUST call exit system call (eax = 1) after finishing. i correct the above code for you.
-
nelthas about 9 yearsYes, 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 about 9 yearsYes, AT&T syntax require $ infront of numbers and variables that you need their address. you MUST exit from every assembly program.
-
Peter Cordes almost 6 yearsYou don't have to use
main
with gcc. You can usegcc -static -nostdlib start.s
to assemble and link a static executable without linking CRT startup files (which defines a conflicting_start
). -
Peter Cordes almost 6 yearsAlso, 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 almost 6 yearsThe 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
tomain
: usegcc -m32 -nostdlib -static test.s -o test
to assemble+link a 32-bit static executable.