Saturday, 29 September 2018

Microprocessor 80386 Lab Experiment no 1



Experiment  No:01
Title: 
Write X86/64 ALP to count number of positive and negative numbers from the array.

Objectives:
1)      To study  the basic concepts of assembly language .
2)       To  Practically implement instruction set of 80386 microprocessor .
3)      To study How to accept and display the numbers by using assembly programming  theory.
Environment:
1.      OS: ubuntu 12.10
2.      Assembler: NASM
3.      Linker: ld

Theory:
          To count the number of positive number, negative number, and zero from the given set of numbers entered by the user in C++ programming, you have to ask to the user to enter some set of numbers (10 numbers here). Now to find occurrence of positive, negative and zero from the given set of numbers, just check all the numbers using for loop whether the number is 0, less than zero or greater than 0 to count the occurrence of positive, negative and zero as shown here in the following program.

What is ALP?
Assembly language Program is mnemonic representation of machine code.

Which assembler used in execution of ALP?

            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 variables. The syntax for declaring bss section is:
section .bss

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

global main
main:
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
 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 EDX, 4            ; message length
MOV EBX,1             ; file descriptor (stdout)
MOV ECX, MSG      ; message to write
MOV EAX,4             ; system call number (sys_write)
INT 0x80                    ; call kernel

       OPEN File
mov rax, 2            ; 'open' syscall
mov rdi, fname1   ; file name
mov rsi, 0             ;
mov rdx, 0777       ; permissions set
Syscall
mov [fd_in], rax

      OPEN File/Create file
mov rax, 2            ; 'open' syscall
mov rdi, fname1   ; file name
mov rsi, 0102o      ; read and write mode,                                          create if not
mov rdx, 0666o       ; permissions set
Syscall
mov [fd_in], rax

      READ File
mov rax, 0            ; ‘Read' syscall
mov rdi, [fd_in]     ; file Pointer
mov rsi, Buffer      ; Buffer for read
mov rdx, length     ; len of data want to read
Syscall

      WRITE File
mov rax, 01           ; ‘Write' syscall
mov rdi, [fd_in]     ; file Pointer
mov rsi, Buffer      ; Buffer for write
mov rdx, length     ; len of data want to read
Syscall

      DELETE File
mov rax,87
mov rdi,Fname
syscall
      CLOSE File
mov rax,3
mov rdi,[fd_in]
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:
DB - Define Byte (1byte)
DW- Define Word(2 byte)
DD- Define Doubleword(4 byte)
DQ- Define Quadword (8 byte)
DT- Define Ten byte (10 byte)

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:
RESB- Reserve a Byte
RESW- Reserve a Word
RESD -Reserve a Doubleword
RESQ -Reserve a Quadword
REST- Reserve a Ten Byte

Instructions needed:
1. MOV-Copies byte or word from specified source to specified destination
2. ROL-Rotates bits of byte or word left , LSB to MSB and to CF
3. AND-AND each bit in a byte or word with corresponding bit in another byte or word
4. INC-Increments specified byte/word by 1
5. DEC-Decrements specified byte/word by 1
6. JNZ-Jumps if not equal to Zero
7. JNC-Jumps if no carry is generated
8. CMP-Compares to specified bytes or words
9. JBE-Jumps if below of equal
10. ADD-Adds specified byte to byte or word to word
11. CALL-Transfers the control from calling program to procedure

Algorithm:-
1. Declare data segment.
2. Declare string variable “pmsg”.
3. Calculate length of “pmsg” in “pmsg_len”
4. Declare variable “nmsg”.
5. Calculate length of “nmsg” in “nmsg_len”.
6. Declare variable “nwline”.
7. Declare array named array of double word valued seven numbers.
8. Declare variable “arrcnt” and initialize it to 7.
9. Declare variable “pcnt” and initialize it to 0.
10. Declare variable “ncnt” and initialize it to 0.
11. Declare extra segment.
12. Declare variable “dispbuff” and initialize it to 2.
13. Declare macro “print” with 2 arguments.
14. Within the macro following steps.
14.a Move 04 into register “eax”.
14.b Move 01 into ebx.
14.c Move first argument of macro into register “ecx”.
14.d Move second argument of macro into “edx”.
14.e Call interrupt 80H.
15. End the macro.
16. Declare code segment.
17. Give the entry point label for assembly program.
18. Point “esi” to first element in “array”.
19. Move “arrcnt” to “ecx” register.
20. Mark label “up1”.
21. Test the 31st bit of memory location pointed by “esi”.
22. Check the value of carry flag and jump if carry flag is not set to label pointed by “pnxt”.
23. Increment the value of “ncnt” by 1.
24. Skip instruction pointed by “pnxt”
25. Increment “pcnt”
26. Increment esi.
27. Increment esi.
28. Check the value of ecx , if it is zero jump to label pointed by “pskip”.
29. Print “pmsg” having size “pmsg_len”.
30. Move “pcnt” to bl register.
31. Call “disp8num” procedure.
32. Print “nmline” variable.
33. Move 01 to eax.
34. Move 00 to ebx.
35. Call interrupt 80H.




                          
                                                                                       

No comments:

Post a Comment