MPL A4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

JSPM’S JAYAWANTRAO SAWANT COLLEGE OF ENGINEERING,

HADAPSAR, PUNE-28

DEPARTMENT OF COMPUTER ENGINEERING


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

ASSIGNMENT NO. 4

TITLE:Write an ALP to count numbers of positive and negative numbers


from the array.

OBJECTIVES:
1. To be familiar with the format of assembly language program structure
and instructions.
2. To study the format of assembly language program along with different
assembler directives and different functions of the NASM.
3. To learn the procedure how to store N hexadecimal number in memory.

PROBLEM DEFINITION:
Write X86/64 Assembly language program (ALP) to count number of positive
and negative numbers from array.

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: Hexadecimal numbers
OUTPUT: Number of Negative numbers, Number of Positive numbers

THEORY

Assembly language Program is mnemonic representation ofmachine


code.Three assemblers available for assembling the programs for IBM-PC are:
1. Microsoft Micro 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
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

The bss Section


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

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
2. Assembler directives or pseudo-ops
3. Macros
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.
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.
Macros are basically a text substitution mechanism.
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:

Instructions needed:

1. MOV-Copies byte or word from specified source to specified destination

2. ROR-Rotates bits of byte or word right, LSB to MSB and to CF

3. AND-AND each bit in abyteorword with correspondingbit in


anotherbyteorword

4. INC-Increments specified byte/word by1

5. DEC-Decrements specified byte/word by1

6. JNZ-Jumps ifnotequal to Zero

7. JNC-Jumps ifno carryisgenerated

8. CMP-Compares to specified bytes orwords

9. JBE-Jumps ifbelowofequal

10. ADD-Adds specified bytetobyteorword to word

11. CALL-Transfers the control from callingprogramto procedure.

12. RET-Return from where call is made

ALGORITHM:

1 Start
2. Declare & initialize the variables in .data section.
3. Declare uninitialized variables in .bss section.
4. Declare Macros for display.
5. Initialize pointer with source address of array.
6. Initialize count for number of elements.
7. Set pcnt for storing positive numbers count.
8. Set ncnt for storing negative numbers count.
9. Get the number in BL register.
10. Rotate the contents of BLregister left 1 bit to check sign bit.
11. Check if MSB is 1. If yes, goto step 12,elsegoto step 13.
12. Increment count for counting negative numbers.
13. Increment count for counting positive numbers.
14. Increment pointer.
15. Decrement count
16. Check for count = 0. If yes, goto step 17 else goto step 9.
17. Store the positive numbers count and negative numbers count in buffer.
18. Display first message. Call the procedure to display the positive count.
19. Display second message. Call the procedure to display the negative count.
20. Add newline.
21. Terminate the process.
22. Declare the Procedure.
23. Stop.

Flowchart:
TEST CASES:
Take any three examples and explain how to decide whether it is
negative or positive.

CONCLUSION
Here we count the 64-bit numbers of positive and negative numbers in
the array in the assembly language.

Oral Question Bank

1) What is logic of program?


2) What is meaning of BT instruction.?
3) What is ascii for newline?
4) What are the different sections present in programme.
5) Write other instructions used to check positive and
negative numbers.
6) Explain instruction of ROL BX,1
7) What is the logic to identify whether a number is positive
or negative?

You might also like