Iduoad
Iduoad

Reputation: 985

Nasm code running on windows but not on linux

I try to assemble and link my teacher's NASM code, but it does not work on my linux (Ubuntu 16.03) while it's working on her pc (Windows)

segment .data
a dw 10
segment .bss
segment .text
global _main:
extern _printf
_main:
_b100: mov eax, 10
_b150: mov eax, a
_b200: mov ebx, eax
fin:
ret

Those are the instructions i follow to assemble the code

nasm -g -f elf32 test.asm;ld -m elf_i386 -s -o demo *.o

ld returns an error

ld: warning: cannot find entry symbol _start; defaulting to 0000000008048080

Regardless the effect of errors the executable is generated every time I run the commands but when I want to execute breakpoints on the program with gdb I can't.

Upvotes: 0

Views: 754

Answers (1)

Ped7g
Ped7g

Reputation: 16596

First the code needs some patching for linux:

-global _main:
+global main

-_main:
+main:

Remove the underscore from main symbol. Also in the global directive don't add the colon, that's needed when you specify new label.

The removal of underscore will apply also to other external symbols, like printf or when you will publish function from your asm to the C with global.

Compiling:

nasm -g -felf32 -Fdwarf test.asm; gcc -m32 -o demo test.o

And you need to have nasm, gcc and 32 bit libraries installed, not sure what is the minimal set of packages, but going by sudo apt-get install nasm gcc gcc-multilib may be enough even on clean install of *buntu.

Upvotes: 3

Related Questions