This project has NOT been created as part of the 42 curriculum by madelwau
LibASM is a small library written in pure 64-bit Assembly (Intel syntax). The goal of this project is to get familiar with assembly language (x86_64), the System V AMD64 ABI calling convention, CPU registers, stack frame alignment, and direct Linux system calls (syscall).
Recreation of standard C library functions:
ft_strlen— Calculates the length of a null-terminated string (man 3 strlen).ft_strcpy— Copies a string from source to destination (man 3 strcpy).ft_strcmp— Compares two strings lexicographically (man 3 strcmp).ft_write— Writes data to a file descriptor and setserrnoon failure (man 2 write).ft_read— Reads data from a file descriptor and setserrnoon failure (man 2 read).ft_strdup— Duplicates a string using heap allocation (man 3 strdup).
- Integer/Pointer Arguments:
RDIRSIRDXRCXR8R9
- Return Value:
RAX(andRDXfor 128-bit values). - Callee-Saved Registers (must be preserved across calls):
RBX,RSP,RBP,R12,R13,R14,R15. - Caller-Saved Registers (can be overwritten by calls):
RAX,RCX,RDX,RSI,RDI,R8,R9,R10,R11. - Stack Alignment: The stack pointer (
RSP) must be aligned to a 16-byte boundary before issuing anycallinstruction.
Linux system calls return an error code between -1 and -4095 directly in RAX on failure (representing -errno).
To match libc behavior:
- Detect negative return values (
rax < 0). - Negate
RAXto get the positive error code. - Call
__errno_location wrt ..pltto obtain the memory address oferrno. - Store the error code at
[rax]. - Return
-1.
nasm(Netwide Assembler)gcc/clangmake
% make% gcc -Wall -Werror -Wextra -no-pie test.c -L. -lasm
% ./a.out- [Books] Art Of Intel x86 Assembly (No Starch Press)
- [YouTube] Mxy - Learn Assembly for Beginners
AI was used for documentation, debug and to generate tests.