Lab 02

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Lab 02: Linux Assembly Tutorial

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.

The Template for an assembly source program consists of several sections:

• Data section where the initialized variables are declared.


• BSS section where the uninitialized variables are declared.
• Text section where the code placed.

2. Objectives
This lab aims to provide students with ability:

a) To get used to formatted assembly source code (.asm)


b) To compile asm source code with nasm;
c) To view listing file, identifying various memory segment of a program in object code;
d) To link object code with library to create executable code;
e) To load, set breakpoints, run program step-by-step, examining CPU registers, memory areas
in gdb.

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

nasm x86 syntax highlighting Code highlighting

Remote - WSL Code in WSL remotely

Remote - SSH Connect to VM via ssh to code remotely

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

4.2. Compile source code and link to create executable file

Compile source code with nasm

nasm -g -f elf first.asm -l f.lst

If no error, the following files will be generated:

first.o – object code

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

~$ ld -m elf_i386 -o first first.o

output file “first” will be created

run executable file:

~$ ./first

Hello world

program output as expected!

4.3. Load, debug, disassemble code

Load program in gdb

~$ gdb first -q

Disassemble program from start

gdb-peda$ disas _start

4.4. Examine the memory (*)

Answer the following questions:


a) Where are the locations (addresses) of a, b, c, d, hello variables?
b) Verify the value at those addresses?
c) Address of the first instruction?
4.5. Debug program (*)

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.

You might also like