Author Topic: Quick guide to cross compiling with NASM, GCC, Mingw  (Read 28915 times)

Offline Brainiac

  • New Member
  • Posts: 1
Quick guide to cross compiling with NASM, GCC, Mingw
« on: May 09, 2010, 02:04:40 AM »
Hello, first post on the forum.

Hopefully this isn't too basic. I haven't seen an example quite like this before, and it kind of bothers me that most of the examples I have seen are somewhat platform dependent. Especially since NASM supports both ELF and PE executable formats natively.

This is a quick guide to getting NASM with the C library to cross compile, inspired partially by the fact that I haven't seen anyone doing it.. Like Nathan's previous example on this board (guest) was set up to compile depending on which OS your on. Also another example I saw, might have been on yahoo nasm group forum it was with importing SDL dll functions from the dll which is windows specific. Not to throw off on these guys, it's good examples, it's just I don't personally see any good reason not to have a cross compiling set up if it's possible.

Okay, so on with it!  8)

I'm on the Ubuntu distribution of Linux, so you may get packages a little differantly than me. If you do, then that's fine, just look up the appropriate packages.

First your going to need the "build-essential" package. If you already have gcc maybe some other things, you may not need build-essential, nevertheless it is handy to have. Also you will need the "mingw" package. Simply use sudo apt-get for the appropriate packages like below.
Code: [Select]
sudo apt-get install build-essential
sudo apt-get install mingw32
Now that you have both GCC and mingw32 set up, it's time to cross compile!

Save a main.asm in a new empty folder and put the following code in it:
Code: [Select]
SECTION .data ; data section
extern exit,printf
mystring dd 'Hello World!', 0
SECTION .text ; code section
global main ;make main: label be the global entry point
main:
push mystring
call printf
call exit

now save this below code as "makefile"
Code: [Select]
##### Makefile #####
all: main main.exe

main.obj: main.asm
nasm -f win32 --prefix _ -o main.obj main.asm
nasm -f elf main.asm

main:  main.obj
gcc -o main main.o

main.exe: main.obj
i586-mingw32msvc-gcc -o main.exe main.obj

clean:
rm -f *.o *.obj main main.exe
##### End Makefile #####
So that's it, run "make" and it will cross compile main.asm. You can test it in wine or Virtualbox with a windows installation or other virtual machine software. To clean the produced files up, merely run "make clean"

As a side note, if gcc or mingw ever gives trouble, you should be able to run nasm through wineconsole and use alink or another linker as long as you have a C compiler (or library?) to link it with.
« Last Edit: May 09, 2010, 03:08:47 AM by Brainiac »