简体   繁体   中英

Assembly error when compiling with GCC

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?

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

Correct it as follows and compile it with -c param gcc -c test.s -o test

.text

_start:
.global main

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

.data
   message: .ascii "Hello world!"
   msglength: .word 12

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM