DevDungeon
- Labs
Knowledge
Social
DevDungeon
Knowledge
Social
This is an old revision of the document!
There are many Assembly compilers out there. nasm
is one compiler that is available across platforms and can be used to program x86 processors.
Use interrupt 0x80
to make Linux system calls like reading and writing to file descriptors and sockets.
For a list of system calls in Linux, refer to https://man7.org/linux/man-pages/man2/syscalls.2.html
Here is an example of compiling with nasm
:
nasm -f elf64 file.asm # or for 32-bit nasm -f elf file.asm
If you need to link against something you can use:
ln -d -o outfile file.o
Here is a Hello World example:
SECTION .DATA hello: db 'Hello world!',10 helloLen: equ $-hello SECTION .TEXT GLOBAL _start _start: mov eax,4 ; 'write' system call = 4 mov ebx,2 ; file descriptor 1 = STDOUT mov ecx,hello ; string to write mov edx,helloLen ; length of string to write int 80h ; call the kernel ; Terminate program mov eax,1 ; 'exit' system call mov ebx,0 ; exit with error code 0 int 80h ; call the kernel
Use interrupt 0x21
to make DOS system calls like reading and writing files.
For a list of DOS system calls, refer to https://en.wikipedia.org/wiki/DOS_API
When interacting with the BIOS, you use a different system call for each function. BIOS will have less system calls available than an kernel like DOS or Linux, but it gives you the tools you need to build an operating system. For example, BIOS will let you change the video mode, get input from keyboard, write text to screen, and draw pixels on the screen.
For a list of BIOS system calls, refer to https://en.wikipedia.org/wiki/BIOS_interrupt_call.
You can write and compile libraries that can be linked against by other programs.
This example shows how to create a function called print_hello()
that can be used from other Assembly or C programs.
; Compile this program using ; nasm -f elf64 static_lib.asm ; gcc myprogram.c static_lib.o ; ./a.out SECTION .DATA hello: db 'Hello world!',10 helloLen: equ $-hello SECTION .TEXT GLOBAL print_hello print_hello: mov eax,4 ; 'write' system call = 4 mov ebx,2 ; file descriptor 1 = STDOUT mov ecx,hello ; string to write mov edx,helloLen ; length of string to write int 80h ; call the kernel ; Terminate program mov eax,1 ; 'exit' system call mov ebx,0 ; exit with error code 0 int 80h ; call the kernel
// Compile with // gcc call_c.c static_lib.o // Automatically links the elf binary .o generated by // the assembly compiler to resolve references #include <stdio.h> int main(int argc, char *argv[]) { // Define external from a separate library // Defined in something.o from a compiled // assembly program extern print_hello(); // Call external assembly function print_hello(); }
; Because we have a reference to 'main' ; we can compile with nasm to create the ; .o object file, and then compile that with ; gcc. Example ; nasm -f elf64 c_main.asm ; gcc c_main.o ; ./a.out SECTION .DATA hello: db 'Hello world!',10 helloLen: equ $-hello SECTION .TEXT GLOBAL main main: mov eax,4 ; 'write' system call = 4 mov ebx,2 ; file descriptor 1 = STDOUT mov ecx,hello ; string to write mov edx,helloLen ; length of string to write int 80h ; call the kernel ; Terminate program mov eax,1 ; 'exit' system call mov ebx,0 ; exit with error code 0 int 80h ; call the kernel