I am new to assembly language programming. I am trying to follow the steps outlined here to get a better understanding of assembly and optimization. My operating system is Linux Mint, and I am trying to use the NASM assembler, albeit unsuccessfully.
As in the walkthrough, the code is:
BITS 32
GLOBAL main
SECTION .text
main:
mov eax, 42
ret
It compiles successfully with nasm using the command: nasm -f elf tiny.asm
But if I try to use gcc to link with the command: gcc -Wall -s tiny.o
I get the following error: /usr/bin/ld: i386 architecture of input file `tiny.o' is incompatible with i386:x86-64 output
A quick search told me that I should link using this ld command: ld -m elf_i386 -s -o tiny tiny.o
However, doing this gives me the following warning: ld: warning: cannot find entry symbol _start; defaulting to 0000000008048060
And if I ./tiny I get a Segmentation Fault. And ./tiny ; echo $? also returns the number '139' which is... unexpected.
Browsing around, I see that the problem is solved for some by passing 1 to the eax register and 0 to ebx, and using an int command I'm unfamiliar with to end the program... But considering my objective is to make the program as small as possible, I would rather not add additional lines of code.
I should add that compiling and linking this similar code (GAS):
.global main
.text
main:
mov $32, %eax
Using the gcc compiler seems to run flawlessly. I'm at a loss here. Any point in the right direction would be greatly appreciated.
sudo apt-get install gcc-multilib g++-multilibThen trynasm -f elf32 tiny.asmandgcc -m32 -Wall -s tiny.o -o tiny- Michael Petchsudo apt get...line simply downloads and installs the GCC files needed for 32-bit GCC development on 64-bit Linux Mint. Since you hadBITS 32at the top of assembler you want to compile that to a 32-bit ELF object which is whatnasm -f elf32 tiny.asmdoes )(it output by default a file calledtiny.o. Object files need to be linked to an executablegcc -m32 -Wall -s tiny.o -o tinydoes that. The-m32option that I added says we want to generate a 32-bit executable from 32-bit objects. - Michael Petchmainas if it was a C function. When you do theretin yourmainit returns back to the C runtime code and exits the program cleanly for you. - Michael Petch