Reversing
26 February 2011 6 Comments

Assembler Tutorial: Hello World with NASM and CL.EXE or LINK.EXE

Introduction

If learning assembler with the NASM.exe assembler peaks your interest, you might be interested in this extensive tutorial by Paul Carter. Annoyingly, finding a linker on Windows that plays nice with NASM took me a while. The tutorial by P. Carter is not very clear on this matter. I wanted to use the linker link.exe that comes with Visual Studio 2010, because the free DJGPP linker that is mentioned by Mr Carter generates badly formatted .exe files. In this post I will show how to compile and link a Hello World assembler program with nasm.exe and link.exe.

Compiling Assembly Code

Let’s compile and link this Hello World program (courtesy of Ray Toal) in a file named helloworld.asm.

; This is a Win32 console program that writes "Hello, World" on one line and
; then exits.  It needs to be linked with a C library.
 
global  _main
extern  _printf
 
section .text
_main:
push    message
call    _printf
add     esp, 4
ret
message:
db      'Hello, World', 10, 0

As you can see we use the printf() function to print the text “Hello, World”. This function is marked as extern, because it is an imported function (it resides in the C Runtime Library).

The tutorial …