Microprocessor Lab Manual 22 23 Sem1
Microprocessor Lab Manual 22 23 Sem1
LAB MANUAL
MICROPROCESSOR ARCHITECTURE
LABORATORY
2022 - 23
4
Microprocessor Laboratory
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
5
Microprocessor Laboratory
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:
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.
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.
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.
ALGORITHM:-
INPUT: ARRAY
OUTPUT: ARRAY
STEP 1: Start.
8
Microprocessor Laboratory
STEP 16: Jump to step 11 until the counter value is not zero.
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 :
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:-
INPUT: String
STEP 1: Start.
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.
11
Microprocessor Laboratory
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.
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 :-
13
Microprocessor Laboratory
14
Microprocessor Laboratory
ALGORITHM:-
⦁ Start
⦁ Rotate the contents of RAX register left 1 bit to check sign bit.
⦁ Increment pointer.
⦁ Decrement count
⦁ 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.
⦁ 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 :
OS – ubuntu 32bit/64bit OS
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
1. Declare
3. According to choice, accept 4 digit hex no. from user using accept_16 procedure.
8. Store the remainder on stack. So that it will be popped in reverse order as BCD no.
10. Repeat steps 6 to 8 till the quotient becomes zero. i.e. until RAX = 00
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.
17. Stop.
1. Declare
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
6. Load RBX as 10 (BCD number i.e. base 10, so multiply with 10)
10. Clear contents of RDX (for multiplication it is used. So clear previous answer)
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 :
The 80286, 80386, 80486 and Pentium microprocessors are capable of operating in two
⦁ 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.
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
⦁ 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
⦁ 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
⦁ 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.
ALGORITHM :-
⦁ Start
⦁ 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”.
⦁ Print the contents of MSW (Machine Status Word) (16 bits of CR0 [0-15]).
⦁ 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 :
OS – ubuntu 32bit/64bit OS
THEORY :
ALGORITHM:
0. Start
24
Microprocessor Laboratory
9b. Mov content of accumulator into rdi and increment rdi.
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 :
OS – ubuntu 32bit/64bit OS
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.
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
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:
Start
9a. Used MOVSB instruction to transfer the content and set the direction flag..
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 :
ALGORITHMS:-
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 :
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
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