Learn 32 Bit Assembly
Download Debian image
curl -Lo debian.amd64.qcow2 https://cloud.debian.org/images/cloud/bookworm/latest/debian-12-nocloud-amd64.qcow2
Resize to desired disk size
qemu-img resize debian.qcow2 16G
Run the machine
./start.sh
On the virtual machine guest, add virtual drive via:
vim /etc/fstab
then add the following line
Project /root/ground-up 9p _netdev,trans=virtio,version=9p2000.u,msize=104857600 0 0
Install development toolchains
dpkg --add-architecture i386
apt update
apt upgrade
apt install -y build-essential gdb vim
apt install libc6:i386
Exit the virtual machine
Crtl-A x
Login: root
The Makefile
allows building the examples, e.g:
make build src=00_exit.s
make build
unites the following build process:
- Assemble/complie with
as
(using-32
option to build 32 bit assembler, since Programming from the ground up covers 32 bit assembly) toobject
(.o
) file ino/
directory. - Link with
ld
(using-m elf_i386
option to use the 32 bit emulation toolchain) to create a runnable binary inbin/
folder
- General Purpose Registers:
%eax, %ebx, %ecx, %edx, %edi, %esi
- Special Purpose Registers:
%ebp, %esp, %eip, %eflags
%ebp
: base pointer, you access specific data in memory by offsetting this base pointer address, e.g.-4(%ebp)
%esp
: always contains a pointer to the top of the stack
- Function parameters are pushed to the stack before function calls!
%esp
is the stack pointer and holds the address of the stack
Using gdb you can step through the program after building it by running, e.g:
gdb bin/00_exit
Helpful commands
list $line_number
, e.g.list 20
to show the source code (shortcutl
)breakpoint $line_number
, e.g.breakpoint 20
to set a breakpoint (shortcutb
)run
to start the program (shortcutr
)continue
to continue after halt on a breakpoint (shortcutc
)step
to step through line by line after halt on a breakpoint (shortcuts
)info register $register
, e.g.info register eax
to list registers and their values (shortcuti r
) - you can add more than one register, e.g.i r eax ebx esi
info frame
shows the stack frame info (shortcuti f
)p $sp
print stack pointerx/10d $sp
display 10 memory blocks (as decimal) at stack pointer, see x commandbacktrace
shows the call stack (shortcutbt
)