Author Topic: Beginner Question.  (Read 10692 times)

Offline indieexe

  • New Member
  • Posts: 1
Beginner Question.
« on: April 23, 2013, 08:57:01 PM »
I'm on Ubuntu, followed a NASM tutorial online.
I told nasm to create an object file of the following code using the command: nasm -f elf64 01.asm.
I then linked the c libraries and told gcc to create an executable with the following command: gcc 01.o -o prog.
This apparently in order to use the printf subroutine in C.
The linking was successful however when i ran this program with ./prog i got a segmentation fault.
What is wrong about the code, why do i get a segmentation fault?
(The following code is identical to what the tuturial author wrote).
Code: [Select]
[section .data]

hello: db 'Hello, Wordl!',10,0

[section .text]

global main
extern printf

main:

push hello

call printf

add esp,4

mov eax,0
ret

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Beginner Question.
« Reply #1 on: April 23, 2013, 09:44:48 PM »
Your code is fine... but it's for 32bit. Easy way to fix it is to assemble it with "-f elf32" instead of "-f elf64", and tell gcc "-m32" so it will produce 32-bit code. This should run fine on your 64-bit system. If you want 64-bit code, the first parameter to "printf" goes in rsi (or is it rdi?), not on the stack. I think you need to zero rax to say that you haven't got any floating-point parameters. There may be some stack manipulation required, as well. I'm still running 32-bit hardware, and 64-bit code seems "weird" to me. I suppose I'll get used to it once I start using it. (obviously, I'm not in too much of a rush). You may find some 64-bit examples in the "Examples" section that will help... 32-bit examples (like what you've got) are easier to find, and telling gcc "-m32" doesn't seem too hard...

Best,
Frank


Offline Gerhard

  • Jr. Member
  • *
  • Posts: 51
Re: Beginner Question.
« Reply #2 on: April 23, 2013, 09:47:38 PM »
Hi indieexe,

I think your program won't work under Linux 64 bit. The ABI (Application Binary Interface) has changed. Please check out the following link. Your tutorial is for 32 bit.

Gerhard