Search code examples
unixassemblyprintfscanfatt

First assembler program


I am trying to compile my 1st Assembler program for UNIX, but get a lot of errors. For example, this code (expected to read and write a number from keyboard) gives me "Segmentation fault" message:

.data
.code32
printf_format:
  .string "%d\n"
scanf_format:
  .string "%d"
number:
  .space 4

.text 
.globl main
 main:

 pushl $number
 pushl $scanf_format
 call scanf
 addl $8, %esp

 pushl number
 pushl $printf_format
 call printf
 addl $8, %esp
 movl $0, %eax
 ret

Solution

  • Your example works fine, if you compile it with gcc -m32 example.s. If you use a GAS/LD-combination, you cannot terminate it with ret. Use instead:

    pushl $0
    call exit