0

I copies it from here but compile fails:

[root@ test]# gcc -c test.S
test.S: Assembler messages:
test.S:8: Error: suffix or operands invalid for `pop'
[root@ test]# cat test.S
.text
    call start
    str:
        .string "test\n"
    start:
    movl    $4, %eax
    movl    $1, %ebx
    popl     %ecx
    movl    $5, %edx
    int     $0x80
    ret

Why?

OS:

CentOS release 5.5 (Final)
Kernel \r on an \m
Community
  • 1
  • 1
R__
  • 1,441
  • 5
  • 16
  • 22
  • 1
    This question http://stackoverflow.com/questions/5485468/x86-assembly-pushl-popl-dont-work explains this – Mike Kwan Jun 30 '11 at 13:35
  • 1
    @Mike ,changed `popl` to `popq`,still the error. – R__ Jun 30 '11 at 13:40
  • And changed %ecx to %rcx? As Mikes link explains you need to change a lot more than simply `popl` to `popq` to make it work on x86_64 (if that is indeed your target architecture -- your OS string is missing this crucial piece of information). – user786653 Jun 30 '11 at 14:08
  • @user786653 ,yes mine is x86_64. – R__ Jun 30 '11 at 14:32
  • Then Mikes link should explain everything. If you just want to compile the code as is use the `-m32` command line switch to compile in 32-bit mode. – user786653 Jun 30 '11 at 15:02
  • @user786653 ,now there's no syntax errors,but it doesn't function:( – R__ Jun 30 '11 at 15:07

1 Answers1

2

If you read that question again carefully, you'll notice that the user has written an application to load this code, which has been previously compiled into it's binary representation, at runtime. The code itself is not a full application. This code as it is, cannot generate a valid binary (executable) application.

When writing assembly code you have a few options as for what compiler and syntax you can use to write your programs. If you are interested in learning assembly, I recommend reading Programming from the Ground Up.

The example below came from here:

.section .data
hello:
   .ascii "Hello, World!\n\0"

.section .text
.globl _start
_start:
   movl $4, %eax
   movl $14, %edx
   movl $hello, %ecx
   movl $1, %ebx
   int $0x80
   movl $1, %eax
   movl $0, %ebx
   int $0x80

Compiled with:

as hello.s -o hello.o
ld hello.o -o hello
./hello
Hello, World!
Community
  • 1
  • 1
karlphillip
  • 92,053
  • 36
  • 243
  • 426