KEMBAR78
Microprocessor Lab Manual 22 23 Sem1 | PDF | Assembly Language | Software Engineering
0% found this document useful (0 votes)
27 views29 pages

Microprocessor Lab Manual 22 23 Sem1

The document is a lab manual for a Microprocessor Laboratory course, detailing assignments that involve writing assembly language programs (ALP) for various tasks such as accepting hexadecimal numbers, counting positive and negative numbers, and converting between hexadecimal and BCD. Each assignment includes objectives, required apparatus, theoretical background, algorithms, and conclusions. The manual emphasizes the use of X86/64 architecture and assembly language programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views29 pages

Microprocessor Lab Manual 22 23 Sem1

The document is a lab manual for a Microprocessor Laboratory course, detailing assignments that involve writing assembly language programs (ALP) for various tasks such as accepting hexadecimal numbers, counting positive and negative numbers, and converting between hexadecimal and BCD. Each assignment includes objectives, required apparatus, theoretical background, algorithms, and conclusions. The manual emphasizes the use of X86/64 architecture and assembly language programming techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

Microprocessor Laboratory

LAB MANUAL
MICROPROCESSOR ARCHITECTURE

LABORATORY

Subject Code: BCE4302

2022 - 23

4
Microprocessor Laboratory

Page Date of Date of Signature


Sr. Title
No No Conduction Submission of
Staff

1 Write X86/64 ALP to accept five 64-bit


Hexadecimal numbers from the user and
store them in an array and display the
accepted numbers.

2 Write X86/64 ALP to accept a string and


to display its length.

3 Write X86/64 ALP to count the number of


positive and negative numbers from the
array.

4
Write 64 bit ALP to convert 4-digit Hex
number into its equivalent BCD number and
5-digit BCD number into its equivalent HEX
number. Make your program user friendly to
accept the choice from user for:
1. HEX to BCD
2. BCD to HEX
3. EXIT

Display proper strings to prompt the user


while accepting the input and displaying the
result. (Use of 64-bit registers is expected)

5 Write ALP to switch from real mode to


protected mode and display the values of
GDTR, LDTR, IDTR, TR and MSW
Registers.

6 Write X86/64 ALP to perform non-


overlapped block transfer (without string
specific instructions). Block containing data
can be defined in the data segment

5
Microprocessor Laboratory

7 Write X86/64 ALP to perform


overlapped block transfer (with string
specific instructions). Block containing
data can be defined in the data segment

8 Write X86/64 ALP to perform multiplication


of two 8-bit hexadecimal numbers. Use
successive addition and add and shift method.
(use of 64-bit registers is expected)

9 Write X86 ALP to find, a) Number of Blank


spaces b) Number of lines c) Occurrence of a
particular character. Accept the data from the
text file. The text file has to be accessed
during Program_1 execution and write FAR
PROCEDURES in Program_2 for the rest of
the processing. Use of PUBLIC and
EXTERN directives is mandatory.

10 Write x86 ALP to find the factorial of a


given integer number on a command line by
using recursion. Explicit stack manipulation
is expected in the code.

11 Study assignment - Assembling and


disassembling of computer systems and
Identifying Internal Components such as
motherboard, RAM, Expansion Card, Power
Supply, Internal Memory, Serial Port, Parallel
Ports, Peripherals etc.

6
Microprocessor Laboratory

Assignment No. 1
ASSIGNMENT NO. 1

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

APPARATUS :
● Core 2 duo/i3/i5/i7 - 64bit processor
● OS – ubuntu 32bit/64bit OS
● Assembler used –nasm
● Editor Used – gedit

Theory:

Introduction to Assembly Language Programming:

Each personal computer has a microprocessor that manages the computer's arithmetical,
logical and control activities. Each family of processors has its own set of instructions for
handling various operations like getting input from the keyboard, displaying information on
screen and performing various other jobs. These set of instructions are called 'machine
language instruction'. Processor understands only machine language instructions which are
strings of 1s and 0s.

However machine language is too obscure and complex for use in software development. So
the low level assembly language is designed for a specific family of processors that represents
various instructions in symbolic code and a more understandable form. Assembly language is
a low-level programming language for a computer, or other programmable device specific to
particular computer architecture in contrast to most high-level programming languages,
which are generally portable across multiple systems. Assembly language isconverted into
executable machine code by a utility program referred to as an assembler like NASM,MASM
etc.

Assembly Basic Syntax:


An assembly program can be divided into three sections:
The data section
The bss section
The text section

The order in which these sections fall in your program really isn’t important, but by
convention the.data section comes first, followed by the .bss section, and then the .text
section.

The .data Section


The .data section contains data definitions of initialized data items. Initialized data is data that
has value before the program begins running. These values are part of the executable file.
They are loaded into memory when the executable file is loaded into memory for execution.
7
Microprocessor Laboratory

You don’t have to load them with their values, and no machine cycles are used in their
creation beyond what it takes to load the program as a whole into memory. The important
thing to remember about the .data section is that the more initialized data items you define,
the larger the executable file will be, and the longer it will take to load it from disk into
memory when you run it.

The .bss Section


Not all data items need to have values before the program begins running. When you’re
reading data from a disk file, for example, you need to have a place for the data to go after it
comes in from disk.Data buffers like that are defined in the .bss section of your program. You
set aside some number of bytes for a buffer and give the buffer a name, but you don’t say what
values are to be present in the buffer. There’s a crucial difference between data items defined
in the .data section and data items defined in the .bss section: data items in the .data section
add to the size of your executable file. Data Items in the .bss section do not.

The .text Section


The actual machine instructions that make up your program go into the .text section.
Ordinarily, no data items are defined in .text. The .text section contains symbols called labels
that identify locations in the program code for jumps and calls, but beyond your instruction
mnemonics, that’s about it.All global labels must be declared in the .text section, or the labels
cannot be ‘‘seen’’ outside your program by the Linux linker or the Linux loader. Let’s look at
the labels issue a little more closely.

ALGORITHM:-

INPUT: ARRAY

OUTPUT: ARRAY

STEP 1: Start.

STEP 2: Initialize the data segment.

STEP 3: Display msg1 “Accept array from user. “

STEP 4: Initialize counter to 05 and rbx as 00

STEP 5: Store element in array.

STEP 6: Move rdx by 17.

STEP 7: Add 17 to rbx.

STEP 8: Decrement Counter.

STEP 9: Jump to step 5 until counter value is not zero.

STEP 10: Display msg2.

STEP 11: Initialize counter to 05 and rbx as 00

STEP 12: Display element of array.

8
Microprocessor Laboratory

STEP 13: Move rdx by 17.

STEP 14: Add 17 to rbx.

STEP 15: Decrement Counter.

STEP 16: Jump to step 11 until the counter value is not zero.

STEP 17: Stop

Conclusion:
In this assignment, we learnt how to write assembly language program and Accept and display
array in assembly language.

9
Microprocessor Laboratory

ASSIGNMENT NO. 2

AIM :- Write X86/64 ALP to accept a string and to display its length.

APPARATUS :
● Core 2 duo/i3/i5/i7 - 64bit processor
● OS – ubuntu 32bit/64bit OS
● Assembler used –nasm
● Editor Used – gedit

THEORY :

What is Macro and Procedure?

1. Macro :
Macro is a set of instructions and the programmer can use it anywhere in the program by using
its name. It is mainly used to achieve modular programming. So the same set of instructions
can be used multiple times whenever required by the help of a macro. Wherever macro’s
identifier is used, it is replaced by the actual defined instructions during compilation thereby
no calling and return occurs.

Syntax of macro :
%macro macro_name number_of_parameters
<macro body>
%endmacro

2. Procedure :
Procedures are also like macro, but they are used for a large set of instructions when macro is
useful for a small set of instructions. It contains a set of instructions which performs a specific
task. It contains three main parts i.e Procedure name to identify the procedure, procedure body
which contains set of instructions, and RET statement which denotes return statement. Unlike
macros, procedures follow a call-return method thereby achieving true modularity.

Syntax of Procedure :
procedure_name :
procedure body
….......................
RET

To call a procedure
CALL procedure_name
After execution of procedure control passes to the calling procedure using RET statement.
Write Syntax for Read macro, Print macro, Exit macro.
10
Microprocessor Laboratory

Print:
%macro Print 2
mov rax,1
mov rdi,1
mov rsi,%1
mov rdx,%2
syscall
%endmacro
Read:
%macro Read 2
mov rax,0
mov rdi,0
mov rsi, %1
mov rdx, %2
syscall
%endmacro
Exit:
%macro Exit 0
mov rax,60
mov rdi,0
syscall
%endmacro

ALGORITHM:-

Write the steps of the algorithm as per the program.

INPUT: String

OUTPUT: Length of String in hex

STEP 1: Start.

STEP 2: Initialize data section.

STEP 3: Display msg1 on monitor

STEP 4: accept string from user and store it in Rsi Register (Its length gets stored in Rax
register by default).

STEP 5: Display the result using “display” procedure. Load length of string in data register.

STEP 6. Take counter as 16 int cnt variable

STEP 7: move address of “result” variable into rdi.

STEP 8: Rotate left rbx register by 4 bit.

STEP 9: Move bl into al.

11
Microprocessor Laboratory

STEP 10: And al with 0fh

STEP 11: Compare al with 09h

STEP 12: If greater add 37h into al

STEP 13: else add 30h into al

STEP 14: Move al into memory location pointed by rdi

STEP 15: Increment rdi

STEP 16: Loop the statement till counter value becomes zero

STEP 17: Call macro dispmsg and pass result variable and length to it. It will print the length
of the string.

STEP 18: Return from procedure

STEP 19: Stop

CONCLUSION:
In this assignment, we learnt how to convert hex to ASCII in the ALP program to display any
number.

12
Microprocessor Laboratory

ASSIGNMENT NO. 3

AIM :- Write X86/64 ALP to count the number of positive and negative numbers from the array.

APPARATUS :
⦁ Core 2 duo/i3/i5/i7 - 64bit processor
⦁ OS – ubuntu 32bit/64bit OS
⦁ Assembler used –nasm
⦁ Editor Used – gedit

THEORY :

Rotate Instructions :-

In 8086 you have two choices capable to perform single rotation :


1. The number of bits, to be rotated, normally referred as COUNT, may be specified as the
constant 1
2. COUNT may be specified in register CL. Register CL is by default register used, for this
operation.
Table 1 summarizes all rotate instructions.

13
Microprocessor Laboratory

14
Microprocessor Laboratory

ALGORITHM:-

The algorithm for implementation of 64 bit program is as below :-

⦁ Start

⦁ Declare & initialize the variables in .data section.

⦁ Declare uninitialized variables in .bss section.

⦁ Declare Macros for print and exit operation.

⦁ Initialize pointer with source address of array.

⦁ Initialize count for number of elements.

⦁ Set RBX as Counter register for storing positive numbers count.

⦁ Set RCX as Counter register for storing negative numbers count.

⦁ Get the number in RAX register.

⦁ Rotate the contents of RAX register left 1 bit to check sign bit.

⦁ Check if MSB is 1. If yes, goto step 12,else goto step 13.

⦁ Increment count for counting negative numbers.

⦁ Increment count for counting positive numbers.

⦁ Increment pointer.

⦁ Decrement count

⦁ Check for count = 0. If yes, goto step 17 else goto step 9.

⦁ Store the positive numbers count and negative numbers count in buffer.

⦁ Display first message. Call the procedure to display the positive count.

15
Microprocessor Laboratory
⦁ Display a second message. Call the procedure to display the negative count.

⦁ Add a newline.

⦁ Terminate the process.

⦁ Declare the Procedure.

⦁ Stop

CONCLUSION:
Hence programmed X86/64 ALP to count the number of positive and negative numbers from the array.

16
Microprocessor Laboratory
ASSIGNMENT NO. 4
AIM :- Write 64 bit ALP to convert 4-digit Hex number into its equivalent BCD number and 5-digit BCD number
into its equivalent HEX number. Make your program user friendly to accept the choice from user for:

1. HEX to BCD

2. BCD to HEX

3. EXIT

Display proper strings to prompt the user while accepting the input and displaying the result. (Use of 64-bit registers
is expected)

APPARATUS :

Core 2 duo/i3/i5/i7 - 64bit processor

OS – ubuntu 32bit/64bit OS

Assembler used –nasm (the netwide assembler)

Editor Used – gedit

THEORY :

STACK Operations:

The stack follows the LIFO (Last In First Out) principle. The stack can be used, for example, to pass parameters to
functions. There are three instructions that can be used for interaction with the stack: Pop, Push and Exch.

PUSH:-

The PUSH instruction increments the stack pointer and stores the value of the specified byte operand at the internal
RAM address indirectly referenced by the stack pointer. No flags are affected by this instruction.

Operation

PUSH

SP = SP + 1

SP) = (direct)

POP:-

The POP instruction reads a byte from the address indirectly referenced by the SP register. The value read is stored at
the specified address and the stack pointer is decremented. No flags are affected by this instruction.

Operation

POP

(direct) = (SP)

SP = SP - 1

17
Microprocessor Laboratory

ALGORITHM:- Hexadecimal to BCD conversion

1. Declare

Section .data of proper string messages.

Section .bss of proper variables.

Declare various macros e.g. print, read & exit macros.

Section .text as starting point of code segment.

2. Prompt messages on screen & accept choice from user.

3. According to choice, accept 4 digit hex no. from user using accept_16 procedure.

4. Load 16-bit number in AX.

5. Load RBX as 10 (BCD i.e. base 10 conversion)

6. Clear contents of RDX (for division it is used. So clear previous remainder)

7. Divide number by 10.

i.e. RDX:RAX / RBX = RDX = Remainder, RAX = Quotient ( number / 10 )

8. Store the remainder on stack. So that it will be popped in reverse order as BCD no.

9. Increment digit count.

10. Repeat steps 6 to 8 till the quotient becomes zero. i.e. until RAX = 00

11. Print Hex to BCD Conversion message on screen.

12. Pop the content of the stack in DX. (Previously stored remainders).

13. Convert remainder from digit to character for display purpose. The remainders are in the range 0-9. So add 30h to
convert digit to character.

14. Display the digit on screen.

15. Decrement digit count.

16. Repeat steps 12-15 until digit count becomes zero.

17. Stop.

ALGORITHM:- BCD to Hexadecimal conversion

1. Declare

Section .data of proper string messages.

Section .bss of proper variables.

Declare various macros e.g. print, read & exit macros.

Section .text as starting point of code segment.

18
Microprocessor Laboratory
2. Prompt messages on screen & accept choice from user.

3. According to choice, accept 5 digit BCD no. from user using read macro. The number is

accepted in character format.

4. Point at the start of the number by RSI.

5. Clear contents of RAX. Final answer will be stored in RAX.

6. Load RBX as 10 (BCD number i.e. base 10, so multiply with 10)

7. Load counter CX as 5. (5 digit no.)

8. Clear contents of RDX (for multiplication it is used. So clear previous answer)

9. Multiply RAX by 10.

i.e. RAX * RBX = RDX:RAX

previous digit * 10 = ans

10. Clear contents of RDX (for multiplication it is used. So clear previous answer)

11. Load current digit in DL from RSI.

12. Convert the contents of DL from character to digit by subtracting 30h.

13. Add the current digit in final answer.

14. Increment source pointer.

15. Repeat steps 8 to 14 till CX becomes zero.

16. Store answer from AX to ans variable.

17. Print BCD to HEX Conversion message on screen.

18. Store the answer from ans variable to AX again.

19. Call display_16 procedure to display final 16-bit hex no. on screen.

20. Stop

CONCLUSION:

Hence we have written a user defined 64 bit ALP to perform HEX to BCD and BCD to HEX conversion.

19
Microprocessor Laboratory

ASSIGNMENT NO. 5
AIM :- Write ALP to switch from real mode to protected mode and display the values of GDTR, LDTR,
IDTR, TR and MSW Registers.

APPARATUS :
⦁ Core 2 duo/i3/i5/i7 - 64bit processor
⦁ OS – ubuntu 32bit/64bit OS
⦁ Assembler used –nasm (the netwide assembler)
⦁ Editor Used – gedit

THEORY :

Protected Mode Definition:

The 80286, 80386, 80486 and Pentium microprocessors are capable of operating in two

basic modes of operation:

⦁ Real mode

⦁ Protected mode.

In protected mode, the full power of the processor is available. Registers are 32 bits wide. DOS is an example
of a program that runs in real mode, because DOS was created for an 8086 based machine. An example of a
program that runs in protected mode is Windows TM /Linux.

Protected Mode Application:

The program presented here takes advantage of the capabilities to execute some privileged instructions such as
SGDT and STR, and display the results of the execution. A number of instructions are included in this program.

The SMSW instruction saves a copy of the lower 16 bit of CR 0 . These bits are known as the Machine Status
Word and can be loaded with new data using LMSW (Load Machine Status Word) instruction.

20
Microprocessor Laboratory

Protected mode Registers

1. Global Descriptor Table Register (GDTR):

⦁ GDTR is a 48 – bit register of the Pentium processor.

⦁ The lower 2 bytes [bit 0 to bit 15] are called LIMIT.

⦁ The upper 4 bytes of GDTR are called BASE. The BASE gives the beginning physical address of GDT in
memory.

⦁ This 32 –bit base address allows the GDT to be place anywhere in the Pentiums address space.

Eg.

If BASE = 00 10 00 00 H

then

GDT starts at 00 10 00 00 H in physical memory Space

2. Interrupt Descriptor Table Register [IDTR]:

⦁ IDTR is a 48 bit register of the Pentium processor

⦁ The lower 2 bytes of the register [LIMIT] gives the size of the IDT and upper four bytes [BASE] identifies the
starting address of the IDT in physical memory.

⦁ The size of the IDT is equal to LIMIT +1 and IDT can be up to 65,536 bytes long.

⦁ Similar to GDTR, the IDTR has to be loaded before switching the Pentium from the real mode operation to
the protected mode operation.

21
Microprocessor Laboratory

3. Local Descriptor Table Register (LDTR):

⦁ LDTR is a 16-bit register of the Pentium processor

⦁ The LDTR does not directly select the LDT, rather it gives a selector which points to an LDT descriptor in the
GDTR.

⦁ The 32-bit base value in local descriptor table cache of Pentium gives the starting point of the LDT table in
physical memory and the value of the 16-bit limit in it gives the size of the LDT.

4. Task Register

⦁ Whenever a task Switch occurs, Pentium automatically saves the complete context dof the old task in a TSS
and loads the context of a new task specified in another TSS.

⦁ The Task register consist of two parts a visible part and an invisible part. The visible part is accessible to the
user. The invisible part is automatically loaded with the information associated with the TSS descriptor.

⦁ It is a 16- bit selector for (TSS) Task State Segment Descriptor.

ALGORITHM :-

⦁ Start

⦁ Declare & initialize the variables in .data section.

⦁ Declare uninitialized variables in .bss section.

⦁ Declare Macros for print and exit operation.

⦁ Store MSW.

22
Microprocessor Laboratory
⦁ Rotate right MSW by 1 bit, so the last bit will be available at the carry flag.

⦁ If carry is set then, print the message “Processor is in protected mode” else print the message “Processor is
in Real mode”.

⦁ Save the contents of GDTR, IDTR, LDTR and TR.

⦁ Print the contents of GDTR (Global Descriptor Table Register).

⦁ Print the contents of IDTR (Interrupt Descriptor Table Register).

⦁ Print the contents of LDTR (Local Descriptor Table Register).

⦁ Print the contents of TR (Task Register).

⦁ Print the contents of MSW (Machine Status Word) (16 bits of CR0 [0-15]).

⦁ Using Macro terminates the process.

⦁ Define display procedure.

⦁ Stop

CONCLUSION:
Hence we have written ALP to switch from real mode to protected mode and display the values of
GDTR, LDTR, IDTR, TR and MSW registers.

23
Microprocessor Laboratory

ASSIGNMENT NO. 6

AIM :- Write X86/64 ALP to perform non-overlapped block transfer (without string specific instructions).
Block containing data can be defined in the data segment

APPARATUS :

Core 2 duo/i3/i5/i7 - 64bit processor

OS – ubuntu 32bit/64bit OS

Assembler used –nasm (the netwide assembler)

Editor Used – gedit

THEORY :

ALGORITHM:

Non overlapped block transfer without string instruction

0. Start

1. Declare & initialize the variables in .data section.

2. Declare uninitialized variables in .bss section.

3. Declare Macros for Read, print and exit operation.

4. Define source block in data section.

5. Point rsi to source block

6. call display_block_ procedure

7a. Initialize count

7b. Move content of rsi in accumulator.

7c. Push rsi

7d. Call display_procedure to display number

7e. Print space

7f. Pop rsi and increment rsi and decrement count.

7g. Repeat step b to f until count =0.

8. Repeat step 7 for destination_block

9. Call Block_transfer procedure

9a. Mov content of rsi into accumulator and increment rsi

24
Microprocessor Laboratory
9b. Mov content of accumulator into rdi and increment rdi.

9c. Repeat a to b until count is 0.

10. Repeat step 3 for displaying source block and destination_block.

11. Using Macro terminates the process.

12. Stop.

CONCLUSION:

Hence we have performed X86/64 ALP to perform non-overlapped block transfer (without string specific
instructions).

25
Microprocessor Laboratory

ASSIGNMENT NO. 7

AIM :- Write X86/64 ALP to perform overlapped block transfer (with string specific instructions). Block containing data can
be defined in the data segment

APPARATUS :

Core 2 duo/i3/i5/i7 - 64bit processor

OS – ubuntu 32bit/64bit OS

Assembler used –nasm (the netwide assembler)

Editor Used – gedit

THEORY :

STRING INSTRUCTION

1. MOVSB/MOVSW

The string instructions help operations on sequences of bytes or words. No one take an explicit operand; instead, they
all work implicitly on the source and/or destination strings. The current element (byte or word) of the source string is
at DS : SI, and the current element of the destination string is at ES:DI. Each instruction works on one element and
then automatically adjusts RSI and/or RDI; if the Direction flag is clear, then the index is incremented, otherwise it is
decremented (when working with overlapping strings it is sometimes necessary to work from back to front, but usually
you should leave the Direction flag clear and work on strings from front to back).

To work on an entire string at a time, each string instruction can be accompanied by a repeat prefix, either REP or one
of REPE and REPNE (or their synonyms REPZ and REPNZ). These cause the instruction to be repeated the number of
times in the count register, RCX; for REPE and REPNE, the Zero flag is tested at the end of each operation and the
loop is stopped if the condition (Equal or Not Equal to zero) fails.

The MOVSB and MOVSW instructions have the following forms:

MOVSB
or
REP MOVSB

MOVSW
or
REP MOVSW

2. STOSB/STOSW

STOSB stores the byte in AL at [ES:DI] or [ES:EDI], and sets the flags accordingly. It then increments or decrements
(depending on the direction flag: increments if the flag is clear, decrements if it is set) DI (or EDI).

26
Microprocessor Laboratory
STOSB - Store AL at address ES:(E)DI

STOSW - Store AX at address ES:(E)DI

3. STD/CLD

STD sets the direction flag. The STD instruction puts the DF flag in 1.

Syntax: STD

CLD clear the direction. This instruction turns off the corresponding bit to the address flag.

Syntax: CLD

ALGORITHM:

Overlapped block transfer with string instruction

Start

1. Declare and initialized the variable in .data section


2. Declare uninitialized variables in .bss section.
3. Declare Macros for Read, print and exit operation.
4. Define source block in data section.
5. Point rsi to source block
6. call display_block_ procedure

7a. Initialize count

7b. Move the content of rsi in the accumulator.

7c. Push rsi

7d. Call display_procedure to display number

7e. Print space

7f. Pop rsi and increment rsi and decrement count.

7g. Repeat step b to f until count =0.

Repeat step 7 for destination_block Call Block_transfer procedure

9a. Used MOVSB instruction to transfer the content and set the direction flag..

9b.Repeat b until count is 0.

Repeat step 3 for displaying source block and destination_block.

Using macro terminate procedure.

Stop

CONCLUSION:

Hence we have performed X86/64 ALP to perform overlapped block transfer (with string specific instructions).

27
Microprocessor Laboratory

Assignment no 8

AIM :- Write X86/64 ALP to perform multiplication of two 8-bit hexadecimal numbers. Use successive addition
and add and shift method. (use of 64-bit registers is expected)

APPARATUS :
● Core 2 duo/i3/i5/i7 - 64bit processor
● OS – ubuntu 32bit/64bit OS
● Assembler used –nasm (the netwide assembler)
● Editor Used – gedit

THEORY :

SHL / SAL – Shift Left / Shift Arithmetic Left

ALGORITHMS:-

1. Accept numbers from user.


1. Declare
Section .data of proper string messages.
Section .bss of proper variables.
28
Microprocessor Laboratory
Declare various macros e.g. print, read & exit macros.
Section .text as starting point of code segment.
2. Prompt message on screen & accept 2 numbers from user using accept_16 procedure.
3. Display menu on screen & accept choice from user.

Successive Addition Method


4. According to choice, display Result by Successive Addition Method message on screen.
5. Clear the contents of RAX & RDX. These are used to store final answer.
6. Load 1 st number in BX.
7. Load 2 nd number in CX.
8. (Perform RAX + RBX = RDX: RAX.))
Perform addition of RAX & 1 st no. i.e. RBX.
If carry then increment RDX
Decrement CX by 1.
Repeat these steps until CX becomes. Zero
9. Store result in ansL and ansH from AX and DX respectively.
10. Call display_16 procedure to display ansH on screen.
11. Call display_16 procedure to display ansL on screen.
12. Stop.

CONCLUSION:
In this assignment, we learnt how to perform multiplication of two 8-bit hexadecimal numbers using successive
addition and add and shift methods.

29
Microprocessor Laboratory

ASSIGNMENT NO. 9

AIM :- Write X86 ALP to find, a) Number of Blank spaces b) Number of lines c) Occurrence of a particular
character. Accept the data from the text file. The text file has to be accessed during Program_1 execution and write
FAR PROCEDURES in Program_2 for the rest of the processing. Use of PUBLIC and EXTERN directives is
mandatory.

APPARATUS :
● Core 2 duo/i3/i5/i7 - 64bit processor
● OS – ubuntu 32bit/64bit OS
● Assembler used –nasm (the netwide assembler)
● Editor Used – gedit

THEORY :

NEAR & FAR Procedures

The 80x86 supports near and far subroutines. Near calls and returns transfer control between procedures in the same
code segment. Far calls and returns pass control between different segments. The two calling and return mechanisms
push and pop different return addresses.
Call instruction uses the following syntax:
call ProcName
and the ret instruction
ret
Algorithm for program_1
● Start
● Initialize all the sections needed in programming
● Display “Enter file name” message using Print macro expansion
● Accept file name using Accept macro and store in filename buffer
● Display “Enter character to search” message with the expansion of Print macro
● Read character using Accept macro expansion
● Open file using fopen macro
● Compare RAX with -1H if equal then display error message “Error in Opening File” with Print macro
expansion else go to step ix
● Read content of opened file in buffer
● Store file length in abuf_len
● Call far_procedure
● Stop
Macros:
Macro 1
1. Name : Print
2. Purpose: to display the messages by replacing the whole code by simple macro
declaration
3. I/P: sys_write call Number i.e rax=1, File descriptor (for Standard output rdi=1), Buffer
Address in rsi, and length of Buffer in rdx. Then Call syscall.
Macro 2
1. Name : Accept
2. Purpose: to accept input from the user by replacing the whole code by simple macro
declaration
4. I/P: sys_read call Number i.e rax=0, File descriptor (for Standard input rdi=0), Buffer
Address in rsi, and length of Buffer in rdx. Then Call syscall.
Macro 3
1. Name : fopen
30
Microprocessor Laboratory
2. Purpose: to open a file in given mode
3. I/P: sys_write call Number i.e rax=2, File name in rdi, Mode of file in rsi
(R=0,W=1,RW=2), and file permission in rdx. Then Call syscall.
Macro 4
1. Name : fread
2. Purpose: to read the content of file
3. I/P: sys_read call Number i.e rax=0, File descriptor in rdi , Buffer Address in rsi, and
length of Buffer in rdx. Then Call syscall.
Macro 5
1. Name : fclose
2. Purpose: to close opened file
3. I/P: sys_read call Number i.e rax=3, File handler in rdi. Then Call syscall.
Algorithm for Far Procedure
1. Name : far_procedure
2. Purpose: to count 1. Number of Blank spaces 2. Number of lines 3. Occurrence of a particular character.
3. I/P : Content stored in buffer
4. Algorithm for Procedures
● Start
● Load effective address of buffer in RSI
● Load content of abuf_len in RCX
● Load content of char in BL
● Move value of RSI in AL
● Compare AL with 20H (ASCII value of space) if not equal then go to step vii else increment content of
scount
● Compare AL with 10H (ASCII value of line) if not equal then go to step viii else increment content of
ncount
● Compare AL with BL if not equal then go to step ix else increment content of ccount
● Increment RSI
● Repeat from step vi if RCX is not equal to zero
● Display “Number of space” message with the expansion of Print macro.
● Move content of scount in RBX
● Call display8num procedure
● Display “Number of lines” message with the expansion of Print macro.
● Move content of ncount in RBX
● Call display8num procedure
● Display “Number of Occurrence of Character” message with the expansion of Print macro.
● Move content of ccount in RBX

● Call display8num procedure


● Ret
● Stop

CONCLUSION:

31
Microprocessor Laboratory

ASSIGNMENT NO. 10
AIM :- Write x86 ALP to find the factorial of a given integer number on a command line by using recursion.
Explicit stack manipulation is expected in the code.

APPARATUS :
Core 2 duo/i3/i5/i7 - 64bit processor
OS – ubuntu 32bit/64bit OS
Assembler used –nasm (the netwide assembler)
Editor Used – gedit

THEORY :
Factorial of a number:
In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less
than or equal to n. For example,
factorial(3)
=> 3 * factorial(2)
=> 3 * 2 * factorial(1)
=> 3 * 2 * 1 * factorial(0) ..... { factorial(0) => 1 }
=> 3 * 2 * 1 * 1
=> 6
Recursion:
In computer programming, a recursion (noun, pronounced ree-KUHR-zhion) is programming that is recursive
(adjective), and recursive has two related meanings:
1) A recursive procedure or routine is one that has the ability to call itself. This usually means that it has the
capability to save the condition it was in or the particular process it is serving when it calls itself (otherwise, any
variable values that have been developed in executing the code are overlaid by the next iteration or go-through).
Typically, this is done by saving values in registers or data area stacks before calling itself or at the beginning of the
sequence where it has just been reentered.
2) A recursive expression is a function, algorithm, or sequence of instructions (typically, an IF, THEN, ELSE
sequence) that loops back to the beginning of itself until it detects that some condition has been satisfied.
Algorithm:
1. Accept Number from User
2. Call Factorial Procedure
3. Define Recursive Factorial Procedure
4. Disply Result.
Algorithm : Factorial using recursion
● Push rcx value onto stack
● Compare rcx value with 1
● if Not equal to one then
○ Decrement rcx by one
○ Call factorial procedure again (recursion)
● Ifequal to one then
○ Pop rcx value from stack
○ multiply that value with answer variable
○ Store result of multiplication again in answer variable
● Stop

CONCLUSION:
In this assignment, we learnt how to find the factorial of a given integer number on a command line by using
recursion.

32

You might also like