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 …

Programming
22 February 2011 8 Comments

Static and Dynamic Linking of C Runtime Libraries with VS2010

Linking Types

A C or C++ program compiled on Windows almost invariably uses the C Runtime Libary (CRT). This set of functions comes with your default Windows installation and provides some of the most basic operations, read this page for details. Now, remember that the last step of creating an EXE is called linking — the process that combines one or more compiled objects and combines them into a single executable.This post describes how to change your CRT linking method to control the size of your EXE file. There are two ways to link an external library such as a DLL.

  • Static linking: All library routines used in the program are copied into the final EXE file. This means there is no dependency on some file existing at run-time.
  • Dynamic linking: Both the name of a shareable library and the functions you use from that library are placed in the EXE file. When the executable is loaded, the operating system needs to do some extra work to locate this library and map it into your program’s virtual memory.

There is actually a third, “super-dynamic” way of loading DLL’s : calling a loadLibrary() function in your code. This means that you can load different libraries…