MPL A4
MPL A4
MPL A4
HADAPSAR, PUNE-28
ASSIGNMENT NO. 4
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
THEORY
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.
Instructions needed:
9. JBE-Jumps ifbelowofequal
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.