Experiment 1
Title Accept numbers , store & display
Problem statement Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from
user and store them in an array and display the accepted numbers
Theory :
Introduction to Assembly Language Programming:
What is Assembly Language?
A processor understands only machine language instructions, which are strings of 1's
and 0's. However, machine language is too complex for using in software development. So, the
low-level assembly language is designed for a specific family of processors that represents
various instructions in symbolic code ( called mnemonic) and a more understandable form.
Assembly language is a low-level computer language. It is processor dependent,
since it represents machine codes of instructions of a particular processor with relevant words
( called mnemonic),which makes it easier for a programmer to write a program instead of writing
in machine code format consisting of only 0s & 1s .
Assembler is a software which converts program written in assembly language into the machine
code ( instructions ) of a particular CPU .
High level language Low level language
More close to English & so easy to learn & Less close to English & so difficult to learn &
understand understand
Require more memory consume less memory
do not depend on machines. : These are machine-dependent These are not portable from
portable from any one device to another. any one device to another with different
processor or design.
very easy to debug Difficult to debug
allow a higher abstraction. allow very little abstraction or no abstraction at
all.
One does not require knowledge of hardware Having knowledge of hardware is a prerequisite
for writing programs. to writing programs.
Every single statement in it, may execute a One statement is converted to one machine
bunch of instructions. instruction
Examples : C C++, JAVA, Basic, Python etc Examples : separate assembly language for each
processor
Advantages of Assembly Language
Having an understanding of assembly language makes one aware of -
∙ How programs interface with OS, processor, and BIOS;
∙ How data is represented in memory and other external devices;
∙ How the processor accesses and executes instruction;
∙ How instructions access and process data;
∙ How a program accesses external devices.
Other advantages of using assembly language are -
∙ It requires less memory and execution time;
∙ It allows hardware-specific complex jobs in an easier way;
∙ It is suitable for time-critical jobs;
∙ It is most suitable for writing interrupt service routines and other
memory resident programs & for system programming
There are many assembler programs, such as -
∙ Microsoft Assembler (MASM)
∙ Borland Turbo Assembler (TASM)
∙ The GNU assembler (GAS)
We will use the NASM (Netwide) assembler, as it is -
∙ Free. You can download it from various web sources.
∙ Well documented and you will get lots of information on net.
∙ Could be used on both Linux and Windows.
Installing NASM
If you select "Development Tools" while installing Linux, you may get
NASM installed along with the Linux operating system and you do not
need to download and install it separately. For checking whether you
already have NASM installed, take the following steps -
∙ Open a Linux terminal.
∙ Type whereis nasm and press ENTER.
∙ If it is already installed, then a line like, nasm: /usr/bin/nasm appears.
Otherwise, you will see just nasm:, then you need to install NASM.
To install NASM, take the following steps -
∙ Check The netwide assembler (NASM) website for the latest version.
∙ Download the Linux source archive nasm-X.XX.ta.gz, where X.XX is the
NASM version number in the archive.
∙ Unpack the archive into a directory which creates a subdirectory
nasm-X. XX.
∙ cd to nasm-X.XX and type ./configure. This shell script will find the best C
compiler to use and set up Makefiles accordingly.
∙ Type make to build the nasm and ndisasm binaries.
∙ Type make install to install nasm and ndisasm in /usr/local/bin and to install the
man pages.
This should install NASM on your system.
Steps to write & execute ASM program
Make sure you have set the path of nasm and ld binaries in your PATH
environment variable
1. Open a terminal window showing command prompt.
2. Give the following command at the prompt to invoke the editor
gedit hello.asm
3. Make sure that you are in the same directory as where you saved hello.asm.
4. Type in the program in gedit window, save and exit
5. To assemble the program write the command at the prompt as follows and
press enter key
nasm –f elf32 hello.asm –o hello.o (for 32 bit)
nasm –f elf64 hello.asm –o hello.o (for 64 bit)
6. If the execution is error free, it implies hello.o object file has been created.
7. To link and create the executable give the command as
ld –o hello hello.o
Or
ld -m elf_i386 -s -o hello hello.o
8. To execute the program write at the prompt
./hello
9. “hello world” will be displayed at the prompt
The assembly program structure –
Assembly language programs consist of three types of statements:
1. Instructions written in mnemonic form
2. Assembler directives or pseudo-ops
3. Macros
The instructions tell the processor what to do. Each instruction consists of an operation
code(opcode). Each instruction is converted to one machine language instruction by the
assembler.
The assembler directives or pseudo-ops are not executed by the processor but are the
commands to assembler software i.e. additional information to help the assembler for
assembling and so these directives are not converted to machine language instructions.
Macros are used to replace a block of repetitive instructions by single statement. So macros
are used to save time in typing the same block of instructions again & again.
An assembly program can be divided into three sections:
The data section
The bss section
The text section
∙ The .data section
The data section is used for declaring initialized data or constants. This data does not change at
runtime. You can declare various constant values, file names or buffer size etc
you can also define constants using the EQU directive.
Here you can use the DB, DW, DD, DQ and DT directives.
For example:
section .data
message db 'Hello world!' ;
msglength equ $ - msg ;
buffersize dw 1024 ;
∙ The .bss section (block starting symbol)
This section is where you declare your variables.
You use the RESB, RESW, RESD, RESQ and REST directives to reserve uninitialized space in
memory for your variables, like this:
section .bss
filename resb 255 ; Reserve 255 bytes
number resb 1 ; Reserve 1 byte
bignum resw 1 ; Reserve 1 word (1 word = 2 bytes)
realarray resq 10 ; Reserve an array of 10 quad words
∙ The .text section
This is where the actual assembly code is written. The .text section
must begin with the declaration global _start, which just tells the
kernel where the program execution begins.
Eg.:
section .text
global _start
_start:
Here is the where the program actually begins
...
The Linux System Calls (int 80h for 32bit execution & syscall for 64 bit )
System calls are APIs for the interface between user program and kernel program.
Eg:
1. system call for exiting the program
mov rax, 60 ;function to exit or terminate program
mov rdi, 1/0 ; with error status either 0 or 1 ( if no error then set to 0)
syscall ;System call
2. system call for writing on the screen
mov rax,1 ;function to Display
mov rdi, 1 ; Display on monitor
mov rsi, Name of the Variable
mov rdx, length of the variable in bytes
syscall ;System call
Algorithm
1. Reserve 5 memory locations each of 64 bits ( array of 5 * 64 bits)
2. set a counter of 5
3. set a pointer to the start of the array
4. Accept a 64 bit number & store in array
5. Increment pointer
6. decrement counter
7. if counter is not 0 go to 4
8. set the pointer to the start of the array again
9. Set counter to 5
10. Display a 64 bit number
11. Increment pointer
12. decrement counter
13. if counter is not 0 go to 10
14. exit the program
( Assumption : user has to enter only 0 to 9 & A to F characters. Program is not checking the
validity of the digits in the entered numbers)