Lab 02
Lab 02
Lab 02
1. Overview
Linux is a true, 32-bit protected mode operating system, this enables us to do real, up to date 32-bit
assembly. This 32-bit code runs in the flat memory model, which basically means you don't have to
worry about segments at all. This makes life a lot easier, because you never need to use a segment
override or modify any segment register. Every address is 32-bits long and contains only an offset part.
This lab illustrates the sections for an assembly-language program. The formats are specific to the
nasm assembler. Other assemblers might be slightly different.
2. Objectives
This lab aims to provide students with ability:
3. Lab Environment
Students can either practice the labs with Ubuntu linux VM (VirtualBox) or WSL2 (preferable).
On either way, install nasm, gcc, gdb-peda by doing the following commands at linux prompt:
~# sudo apt update
~# sudo apt install git nasm gcc gcc-multilib
~# git clone https://github.com/longld/peda.git ~/peda
~# echo "source ~/peda/peda.py" >> ~/.gdbinit
For writing code, students are recommended to install VSCode with the following extensions:
Extensions Description
4. Tasks
4.1. Compose a simple nasm program in vscode
~$ code first.asm
Enter code:
section .data
SYS_EXIT equ 1
a db 17
b db 9
c db 0
d dw 4096
hello db ‘Hello world’,10
len equ $-hello
section .text
global _start
_start:
mov al, [a]
add al, [b]
mov [c], al
mov eax, 4
mov ebx, 1
mov ecx, hello
mov edx, len
int 0x80
last:
mov eax, SYS_EXIT
int 0x80
f.lst –The list file shows the line number, the relative address, the machine language version of the
instruction (including variable references), and the original source line. The list file can be useful when
debugging. A section of list file may look like this:
1 section .data
3 SYS_EXIT equ 1
4 00000000 11 a db 17
5 00000001 09 b db 9
6 00000002 00 c db 0
7
8 00000003 0010 d dw 4096
9 00000005 48656C6C6F20776F72- hello db 'Hello world',10
10 0000000E 6C640A
11 len equ $-hello
12
13 section .text
14 global _start
15 _start:
16 00000000 A0[00000000] mov al, byte [a]
17 00000005 0205[01000000] add al, byte [b]
18 0000000B A2[02000000] mov byte [c], al
19 00000010 B804000000 mov eax, 4
20 00000015 BB01000000 mov ebx, 1
Link object code to create executable file
~$ ./first
Hello world
~$ gdb first -q
a) Execute the program step by step (step i) then examine the value of variables.
b) Try to set a, b to new values while executing program then check the final result.