A lightweight system resource monitor written in pure x86-64 assembly language for Linux. This tool displays real-time memory and CPU usage statistics by directly interfacing with Linux system calls and the /proc filesystem.
-
Memory Usage Monitoring
- Total system memory
- Free memory
- Used memory calculation
- Values displayed in KB
-
CPU Usage Monitoring
- Real-time CPU activity percentage
- Reads directly from
/proc/stat - Calculates active vs idle time
-
Pure Assembly Implementation
- No external libraries or dependencies
- Direct system calls for maximum performance
- Minimal memory footprint (~4KB buffer)
- Assembler: NASM (Netwide Assembler)
- Architecture: x86-64 (64-bit)
- Operating System: Linux
- Linker: ld (GNU linker)
# Debian/Ubuntu
sudo apt-get install nasm
# Arch Linux
sudo pacman -S nasm
# Fedora/RHEL
sudo dnf install nasm# Assemble the source code
nasm -f elf64 monitor.asm -o monitor.o
# Link the object file
ld -o monitor monitor.o
# Make executable (if needed)
chmod +x monitornasm -f elf64 monitor.asm -o monitor.o && ld -o monitor monitor.oSimply run the compiled binary:
./monitor=== Memory Usage ===
Total Memory: 16384000 KB
Free Memory: 8192000 KB
Used Memory: 8192000 KB
=== CPU Usage ===
CPU Active: 35%
The program uses the sysinfo system call (syscall number 99) to retrieve system memory information:
- System Call:
sys_sysinfo - Data Structure: 112-byte sysinfo struct
- Fields Used:
totalram(offset 8): Total usable RAMfreeram(offset 16): Available RAM
- Calculation:
used_memory = totalram - freeram
CPU statistics are obtained by reading and parsing /proc/stat:
- Open
/proc/statusingsys_open(syscall 2) - Read the first line containing CPU time values (syscall 0)
- Parse the values:
cpu user nice system idle iowait... - Calculate CPU usage percentage:
active_time = user + nice + system total_time = active_time + idle cpu_usage = (active_time * 100) / total_time - Close the file using
sys_close(syscall 3)
| Syscall Number | Name | Purpose |
|---|---|---|
| 0 | sys_read | Read from file descriptor |
| 1 | sys_write | Write to stdout |
| 2 | sys_open | Open /proc/stat |
| 3 | sys_close | Close file descriptor |
| 60 | sys_exit | Exit program |
| 99 | sys_sysinfo | Get system information |
monitor.asm
├── .data section # Static strings and file paths
├── .bss section # Uninitialized buffers
└── .text section # Code and functions
├── _start # Entry point
├── print_number # Convert and display numbers
├── parse_cpu_stats # Parse /proc/stat
├── read_number # Parse ASCII to integer
└── print_cpu_usage # Display CPU percentage
- sysinfo_buffer: 112 bytes for system info struct
- file_buffer: 4096 bytes for reading
/proc/stat - num_buffer: 20 bytes for number-to-string conversion
- Format: NASM (Intel syntax)
- Target: ELF64 (64-bit Linux executable)
- Registers: Uses 64-bit registers (rax, rbx, rcx, etc.)
mov rax, 99 ; sys_sysinfo syscall
mov rdi, sysinfo_buffer ; pointer to buffer
syscall ; execute system callThe program includes a custom integer-to-ASCII conversion routine:
- Divides by 10 repeatedly
- Converts remainders to ASCII characters
- Builds string in reverse order
All file operations are performed using raw system calls:
- No libc or standard library
- Direct kernel interface
- Maximum efficiency
- Startup Time: < 1ms
- Memory Usage: ~112 bytes + 4KB buffers
- Binary Size: ~2-3 KB (after linking)
- CPU Overhead: Negligible (single snapshot read)
- Platform: Linux x86-64 only
- Accuracy: CPU usage is a single-point snapshot (not averaged over time)
- Display: Text-only output
- Real-time: No continuous monitoring (runs once and exits)
Wrap the main logic in a loop with sleep delays:
; Add sys_nanosleep syscall to pause between updates
; Loop back to monitoring code- Network: Parse
/proc/net/dev - Disk I/O: Read
/proc/diskstats - Processes: Count entries in
/proc - Uptime: Use data from sysinfo struct
- Add ANSI color codes for visual formatting
- Create progress bars using ASCII characters
- Display values in MB/GB for larger systems
- Linux System Call Table
- x86-64 Assembly Guide
- NASM Documentation
- Linux /proc Filesystem
- OSDev Wiki - x86
chmod +x monitor- Ensure you're running on a 64-bit Linux system
- Check that NASM version supports ELF64 format
- Verify buffer sizes in
.bsssection - Check for integer overflow on systems with >2TB RAM
This project is open source and available for educational purposes. Feel free to modify and distribute.
Contributions are welcome! Areas for improvement:
- Cross-platform support (BSD, macOS)
- Real-time continuous monitoring
- Better error handling
- More system metrics
Created as an educational project to demonstrate low-level system programming and direct kernel interaction using x86-64 assembly language.
- Linux kernel documentation
- NASM community
- x86-64 assembly learning resources
Happy Assembly Coding! 🚀