Author Topic: Newbie question  (Read 11064 times)

Offline merovich

  • Jr. Member
  • *
  • Posts: 2
Newbie question
« on: October 09, 2012, 01:35:24 PM »
Hi

I am having my first steps trying to use NASM with Linux RHEL6 and I am trying to compile a small test program

Code: [Select]
section .text
global _start
_start:
;Aquí va el código
;escribe (EAX=4) en la salida estándar (EBX=1) la cadena apuntada por ECX de longitud EDX.
mov edx,longitud
mov ecx,cadena
mov ebx,1
mov eax,4 ; es el system call WRITE
int 80h
;terminar la ejecución (EAX=1)
mov eax,1 ; es el system call EXIT
int 80h
section .data
;Aquí los datos inicializados
cadena db "Hola mundo",10,"$"
longitud equ $-cadena

I compile it without errors
Code: [Select]
[hmerovich@oc8467616104 ~]$ nasm -f elf ej1tp6.asm -o ej1tp6.o
[hmerovich@oc8467616104 ~]$

but when I try to link

Code: [Select]
[hmerovich@oc8467616104 ~]$ nasm -f elf ej1tp6.asm -o ej1tp6.o
[hmerovich@oc8467616104 ~]$
as the object format is elf32-i386 , i try
Code: [Select]
[hmerovich@oc8467616104 ~]$ ld -o ej1tp6 -b elf32-i386 ej1tp6.o
ej1tp6.o: could not read symbols: File format not recognized
Which format can I use to have LD recognize the NASM output?

Thanks you very much



Code: [Select]
[hmerovich@oc8467616104 ~]$ nasm -f elf ej1tp6.asm -o ej1tp6.o
[hmerovich@oc8467616104 ~]$





« Last Edit: October 09, 2012, 03:30:01 PM by Frank Kotler »

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Newbie question
« Reply #1 on: October 09, 2012, 04:04:00 PM »
I think a minor change to the ld command line will fix it. "-m" rather than "-b", and "_" rather than "-"...
Code: [Select]
ld -o ej1tp6 -m elf_i386 ej1tp6.o
Although when I try the command line you're using, it seems to work for me(?). I'm still running a 32-bit system, so I don't really need that at all, just...
Code: [Select]
ld -o myfile myfile.o
...works for me. If I make a typo, and type...
Code: [Select]
ld -o myfile myfile.asm
I get a similar error.

There are a lot of options to ld, and it seems pretty flexible about what it will accept, so I'm a little confused about what you're seeing. Your code looks right, so that's a good start! If you can't get it to work, get back to us!

Best,
Frank


Offline merovich

  • Jr. Member
  • *
  • Posts: 2
Re: Newbie question
« Reply #2 on: October 10, 2012, 12:43:29 AM »
Frank

Thanks you very much

I try the command ld -o ej1tp6 -m elf_i386 ej1tp6.o

and it worked fine

Meanwhile I have tried to compile the asm using the NASM option ELF64 and I could link the obejct file without the -m option


Thanks again

Regards

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Newbie question
« Reply #3 on: October 10, 2012, 07:55:42 AM »
Good! Yeah, "-f elf64" should work for "native" 64-bit ld, but I think you'll find it requires different code. I'm told you can still use "int 80h" and the old 32-bit system call numbers, but there are different numbers and "syscall" for 64-bit code. Your example may work, but something as simple as "push eax" will be illegal in 64-bit code. So I'm told... There are a few 64-bit examples in the "examples" section. Someday, 32-bit code will be as obsolete as 16-bit code is today, I imagine. When I upgrade my system (Real Soon Now, I keep saying), I'm going to be very busy getting back up to speed with 64-bit code! You might as well start learning it, but 32-bit code still works... for now...

Best,
Frank