NASM - Hello, World¶
2010
Hello, World [windows]¶
This snippet of assembly language code prints the message “Hello, World!” using a dynamically linked library (for the _printf
function). The arguments to the _printf
function are pushed onto the stack.
global _main
extern _printf
extern _exit
section .data
message db 'Hello, World', 10, 0
section .text
_main:
push message
call _printf
xor eax, eax
push eax
call _exit
In link time, libcrtdll.a
is adding, this library contains _printf
and _exit
function.
nasm -f win32 hellworld.asm
ld hellworld.o libcrtdll.a -o hellworld
Alternatively, you can compile using gcc
instead of ld
.
nasm -f win32 hellworld.asm
gcc hellworld.o
Static and dynamic library¶
Create liberary¶
Writing function
myFunction
in assembler
; file : libHello.asm
BITS 64
section .data
hello db \"Hello World !\", 0xa
len equ $ - hello
section .text
global myFunction:function
myFunction:
mov rax, 4
mov rbx, 1
mov rcx, QWORD hello
mov rdx, len
int 0x80
ret
Compiling the library statically
nasm -f elf64 libHello.asm
ar rcs libHello.a libHello.o
Compiling the dynamic library
nasm -f elf64 libHello.asm
ld -shared -o libHello.so libHello.o
Example of library uses¶
C code using a library function
// file : main.c
int main(int argc, char **argv) {
myFunction();
return 0;
}
Compiling with the static library
gcc -static main.c -L. libHello.a -o statically_linked
Compiling with the dynamic library
gcc main.c -o dynamically_linked -L -l c_printf.so.1.0.1