Linux build and print

I am trying to write a simple build of a program with the printf function. I will compile it nasm -f elf 64 and the link using gcc . After starting, I see a segmentation fault . What's wrong?

 [Bits 32] extern printf global main section .data hello: db "Hello", 0xa, 0 section .text main: push hello call [printf] add esp, 4 mov eax, 1 mov ebx, 0 int 80h 
+6
source share
2 answers

Linux on ia32 does not use the same calling convention as on amd64. Since your code uses the first, you should compile it as 32 bits and associate it with a 32-bit libc. On debian, you'll need the libc6-dev-i386 package.

You should also replace 'call [printf]' with 'call printf', which is an error.

Please also note that when using the main interface, you must return from the main interface and not make a system call to exit to allow C start-up code to run.

Hello World example for x86-32 with assembly instructions .

If you are working on amd64, you can learn how to write a 64-bit assembly.

Hello World example for x86-64 with assembly instructions .

+7
source

If you really want to get a 32-bit binary, as the title of your code shows, you just need to fix the line:

 call [printf] 

changing it to:

 call printf 

When you execute call [printf] , you do not call printf, and the address indicated by the first byte of the printf code, this construct ( [address] ) is called the effective address .

+3
source

Source: https://habr.com/ru/post/921990/


All Articles