Lab 03

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

Lab 03: NASM Basics

1. Overview
A properly formatted assembly source file consists of several sections;

• Data section where initialized data is declared and defined.


• BSS section where uninitialized data is declared.
• Text section where code is placed.

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

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


b) To declare variables in data section, uninitilalized variables in bss section.
c) To copy data from a source memory to destination.
d) To load, set breakpoints, run program sep-by-step, examining CPU registers, memory areas in
the program.

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

Note for Apple computer equipped with Apple silicon processor:


This lab (and all the subsequent ones) only runs on computers with Intel processors. Students
who own the Apple-silicon processor (M1/M2) computers must use a cloud-based VM or
github codespaces (preferable).

4. Tasks
4.1. Compose an ASM program in nano text editor

Start nano with line number indicator by:

seed@ubuntu:~$ code second.asm


Compose an assembly program that fulfil the following requirements:

Initialized variables

Var name Data type Initialized values


bNum Byte 5
wNum Word -128
dNum Double-word 0x12345678
List Array of 16-bit words 0x1000, 0x2000, 0x3000, 0x4000, 0x5000
Hello String Netwide Assembly
line_of_a String String of 40 ‘a’

Unititialized variable

Var name Data type Number of elements


bArr Array of bytes 5
wArr Array of words 5
cArr Array of char 20

4.2. Compile source code and link to create executable file

Compile source code with nasm then load executable file in gdb

4.3. Examine the memory (*)

Answer the following questions:


a) List the location (address) of program’s variables?
b) Verify the value at those addresses?
4.4. Copy the memory and inspect the result (*)

To accomplish this task, the students need to refer to the internet resources for arithmetic assembly
instructions
Modify the source file, add code to compute Addition, Subtraction, Multiplication, Quotient, Remainder
of two variables, store the result in variables.
Compile source code, load executable file in gdb then inspect the memories to check the results.
4.5 Do the following (*)
To accomplish this task, the students need to refer to the internet resources for assembly instructions
like memory copying, string operations, …
Modify the source code to do the following:
a) Fill bArr with the value of bNum
b) Copy list to wArr
c) Copy hello string to cArr
d) Print out cArr to console
Compile source code, load executable file in gdb then inspect the memories to check the results.
4.6 Compute the sum of an array
Compute the sum of elements from list array, load the program in gdb to check the result.

You might also like