0% found this document useful (0 votes)
140 views9 pages

MPL A1

The document describes an assembly language programming assignment to accept 5 64-bit hexadecimal numbers from the user as input, store them in an array, and display the numbers from the array as output. It provides background on assembly language programming, the algorithm to solve the problem, test cases, and concludes that the problem of accepting, storing, and displaying the numbers was successfully solved using NASM assembly language.

Uploaded by

Rushikesh Gaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
140 views9 pages

MPL A1

The document describes an assembly language programming assignment to accept 5 64-bit hexadecimal numbers from the user as input, store them in an array, and display the numbers from the array as output. It provides background on assembly language programming, the algorithm to solve the problem, test cases, and concludes that the problem of accepting, storing, and displaying the numbers was successfully solved using NASM assembly language.

Uploaded by

Rushikesh Gaware
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

JSPM’S JAYAWANTRAO SAWANT COLLEGE OF ENGINEERING,

HADAPSAR, PUNE-28

DEPARTMENT OF COMPUTER ENGINEERING


210257 : MICROPROCESSOR LAB
S.E. (2019 Pattern)

ASSIGNMENT NO. 1
TITLE: Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers from
the user and store them in an array and display the accepted numbers.

OBJECTIVES:
1. To be familiar with the format of assembly language program structure and
instructions.
2. To learn the procedure of how to accept N numbers from the user through an input
device like keyboard and store them in an array using Assembly Language
programming in NASM.
3. To learn the procedure of how to display N numbers stored in an array using
Assembly Language programming in NASM.

PROBLEM DEFINITION:
Write an Assembly language program (ALP) to accept five 64 bit Hexadecimal numbers
from the user, store them in an array and display the accepted numbers

WORKING ENVIRONMENT:

1) CPU: Intel I5 Processor


2) OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
3) Editor: gedit, GNU Editor
4) Assembler: NASM (Netwide Assembler)
5) Linker:-LD, GNU Linker

S/W AND H/W REQUIREMENT:

Software Requirements
1. CPU: Intel I5 Processor
2. OS:- Windows XP (16 bit Execution ), Fedora 18 (32 & 64 bit Execution)
3. Editor: gedit, GNU Editor
4. Assembler: NASM (Netwide Assembler)

INPUT: Five 64 bit Hexadecimal numbers accepted from the user


OUTPUT: Five 64 bit Hexadecimal numbers which had been given as input

THEORY
Assembly language Program is mnemonic representation of machine code.
Three assemblers available for assembling the programs for IBM-PC are:

1. Microsoft Macro Assembler(MASM)

2. Borland Turbo Assembler(TASM)

3. Net wide Assembler(NASM)

Assembly Basic Syntax


An assembly program can be divided into three sections:
1. The data section
2. The bss section
3. The text section

1. The data Section


The data section is used for declaring initialized data or constants. This data
does not change at runtime. Youcan declare various constant values, file names
or buffer size etc. in this section.
The syntax for declaring data section is:
section.data

2. The bss Section


The bss section is used for declaring uninitialized data or variables. The syntax
for declaring bss section is:
section .bss

3. The text section


The text section is used for writing the actual code. This section must begin
with the declaration global _start, which tells the kernel where the program
execution begins.
The syntax for declaring text section is:
section .text
global _start
_start:

Assembly Language Statements


Assembly language programs consist of three types of statements:
1. Executable instructions or instructions
The executable instructions or simply instructions tell the
processor what to do. Each instruction consists of an operation code
(opcode). Each executable instruction generates one machine language
instruction.

2. Assembler directives or pseudo-ops


The assembler directives or pseudo-ops tell the assembler about the
various aspects of the assembly process. These are non-executable and do
not generate machine language instructions.

Assembly System Calls


System calls are APIs for the interface between user space and kernel space. We
are using the system calls sys_write and sys_exit for writing into the screen and
exiting from the program respectively.

Linux System Calls (32 bit)


You can make use of Linux system calls in your assembly programs. You need
to take the following steps for using Linux system calls in your program:
 Put the system call number in the EAX register.
 Store the arguments to the system call in the registers EBX, ECX, etc.
 Call the relevant interrupt (80h)
 The result is usually returned in the EAX register
There are six registers that stores the arguments of the system call used. These
are the EBX, ECX, EDX, ESI, EDI, and EBP. These registers take the
consecutive arguments, starting with the EBX register. If there are more than six
arguments then the memory location of the first argument is stored in the EBX
register.
The following code snippet shows the use of the system call sys_exit:
MOV EAX, 1 ; system call number (sys_exit)
INT 0x80 ; call kernel
The following code snippet shows the use of the system call sys_write:
MOV EAX,4; system call number (sys_write)
MOV EBX,1; file descriptor (stdout)
MOV ECX, MSG ; message to write
MOV EDX, 4; message length
INT0x80; call kernel
Linux System Calls (64 bit)
Sys_write:
MOV RAX,1
MOV RDI,1
MOV RSI,message
MOV RDX,message_length
SYSCALL

Sys_read:
MOV RAX,0
MOV RDI,0
MOV RSI,array_name
MOV RDX,array_size
SYSCALL

Sys_exit:
MOV RAX,60
MOV RDI,0
SYSCALL
Assembly Variables
NASM provides various define directives for reserving storage space for
variables. The define
Assembler directive is used for allocation of storage space. It can be used to
reserve as well as initialize one or more bytes.

Allocating Storage Space for Initialized Data


There are five basic forms of the define directive:

Allocating Storage Space for Uninitialized Data


The reserve directives are used for reserving space for uninitialized data. The
reserve directives take a single operand that specifies the number of units of
space to be reserved. Each define directive has a related reserve directive.
There are five basic forms of the reserve directive:

Commands required in Linux:


• To assemble : nasm –f elf 64 hello.nasm
It converts the assembly language program file (.nasm file) to an
object file (.o file)

• To link : ld –o hello hello.o


It combines the generated object files and links them to obtain the
executable file (.exe file)

• To execute : ./hello
It runs the .exe file to execute the program

Instructions needed:
1. MOV- Transfers a byte, word or double word from the source operand to
the destination operand
2. ADD- Adds specified byte to byte or word to word and stores the result
of addition in the first operand or source register
3. DEC-Decrements specified byte/word by1
4. JNZ- unconditional jump instruction which transfers the control to the
specified label in the program to enable the repetitive execution of the
instructions within the loop formed by this instruction

ALGORITHM:
1. Start
2. Declare an array to store five 64 bit numbers under the BSS
section by reserving memory for it using an appropriate
reserve directive
3. Set the counter to 05 to accept these five numbers one by
one, set a pointer to the array.
4. Accept the number from the user through an input device
like keyboard using read system call
5. Store this number in the array
6. Increment the pointer to access its next location to store the
next number in the array
7. Decrement the counter
8. If counter is not zero go to step 4
9. Reset the pointer to the array such that it points to its first
location
10. Reset the counter to 05 to access these five numbers stored
in the array and display them one by one
11. Display the number from the array using the write system
call
12. Increment the pointer to this array to access and display the
next number
13. Decrement the counter
14. If counter is not zero go to step 11
15. Write the exit system call to exit from the program
16. Stop

Flowchart:
TEST CASES:

1. Take any three examples which explains how the user can give the input
of five 64 bit numbers and store them in the array.
2. The same five numbers getting displayed at the output when the array is
displayed indicates that the numbers have been successfully stored in the
array and can be accessed.

CONCLUSION

Hence we conclude that we have successfully –


1. accepted the numbers from the user as input
2. stored them in an array
3. displayed them from the array as the output
in Assembly Language Programming using NASM

Oral Question Bank

1) What is assembler & Linker. Which file is converted into which file by
assembler & Linker ?
2) Steps to execute a NASM Program ?
3) What are different Assembler Directives ?
4) What is dd, dw, db, dq, dt, resb, resw etc ?
5) What is .data section, .bss section, .text section?
6) Explain System calls for Exit, Read, Write for a 64 bit program.
7) How can one access the numbers stored in an array?
8) What is the use of a counter In the program?

You might also like