MPL Programs Related Theory
MPL Programs Related Theory
Experiment 1
Title Accept numbers , store & display
Problem Write an X86/64 ALP to accept five 64 bit Hexadecimal numbers
statement from user and store them in an array and display the accepted
numbers
Experiment 2
Title Length of a string
Problem statement Write an X86/64 ALP to accept a string and to display its length
Algorithm:
1. Reserve memory locations to store string given by user
2. Display message to the user to enter a string
3. Use system call to Accept string inputted by user
4. that same system call also gives the size( length )of string
5. convert the length to ASCII format
6. display the length
Experiment 3
Experiment No 4
A byte can also be used to represent a signed number . Then MSB bit indicates whether the
number is positive or negative. If it is 0 , then it is positive. If it is 1 then number is negative.
& remaining 7 bits used for the magnitude of the number.
The negative numbers are represented in 2’s complement
form.Eg
+ 1 is represented as
Experiment 5
TR6 is used to set conditions for testing TLB and to start testing.
TR7 returns the result ( status) of TLB testing
2)Control Registers
The 386 has three control registers of 32 bits, CR0, CR2 and CR3, These registers, hold
machine status for all tasks in the system.
The low-order 16 bits of CR0 are also known as the Machine Status Word, MSW.
PG ( Paging) : when set to 1, then paging unit is enabled
PE (Protection Enable)
When PE = 0 80386 works in real mode
When PE is set to 1 , 80386 works in protected mode.
Changing PE from 1 to 0 , to switch from protected mode to real mode requires longer
sequence of instructions.
Descriptor of a segment
includes its base address, its length, its type, its privilege level, and some miscellaneous
status information.
It is 8 byte ( 64 bit)
Descriptor tables
The descriptors of all segments are kept together at one place in memory called Descriptor
table.
1. LDT :
All segments of a single user tasks ( programs) are kept in single Local Descriptor table.(
LDT)
when there is multitasking, Each user task’s segments has its own LDT, Thus There can
be many LDT in 80386 system
The LDT may contain only code, data, stack, task gate, and call gate descriptors.
LDT can hold 1 to max 8192 descriptors ( & since each descriptor takes 8 bytes , the size
of LDT is between 8 bytes and 64K bytes.
2. GDT
GDT contains code and data segments used by the operating systems and task state
segments, and descriptors for the LDTs in a system.
There can be only one GDT in 80386 system
3. IDT
The segments of all ISRs ( interrupt service routines) are kept in interrupt Descriptor
table(IDT)
IDT contain descriptors of only task gates, interrupt gates, and trap gates
There can be only one IDT in 80386 system
These 3 tables can be stored anywhere in the memory. But the processor needs to know
their starting address and these addresses are in :
R means register.
The starting address of these 3 tables( GDT, IDT, LDT) has to be loaded in processor in
three registers namely GDTR, IDTR & LDTR, By using instructions LGDT, LIDT &
LLDT respectively.
Note :
GDTR & IDTR hold 32 bit base ( starting) address of GDT & 16 bit limit of GDT.
But LDTR holds descriptor of LDT, which is stored in GDT , from where its base address
& limit is copied in to invisible part of LDTR register. ( means it can’t be accessed by
programmer).
Algorithm
1. Read a protected mode register ( using instructions :SMSW, SGDT, SIDT, SLDT, STR,
CPUID)
2. convert each digit of value in register to ASCII ( by adding 30H if digit is less than or
equal to 9 or adding 37H if digit is more than 9)
3. display all digits of a register ( after converting them to ASCII in step 2)
4.repeat for all registers
Experiment 6
Title Non overlapped block transfer without using string instructions
Statement Write X86/64 ALP to perform non overlapped block transfer without using
string specific instructions. Block containing data can be defined in the data
segment.
Theory :
80386 supports 17 data types:
1.Bit:2.Bit Field: 3. Bit String:4. Signed Byte ( 8 bit ) 5. Unsigned Byte( 8 bit ) 6. Signed
Word ( 16 bit ) 7. Unsigned Word ( 16 bit 8. Signed Double Word ( 32 bit) 9. Unsigned
Double Word ( 32 bit) 10. Signed Quad Word (64 bit ) 11. Unsigned Quad Word (64 bit )
12. Unpacked BCD: 13. Packed BCD: 14. Offset: 15. Pointer: 16. Char:
17. String: String is Sequences of separate but related data items stored in consecutive
addresses
( can be thought as single dimensional array) A string size can be between 1 byte to 4
Gbytes.The 80386 supports byte strings, wordstrings, and dword strings.
Algorithm
( case 1: source block is at lower address & destination block is at higher address )
1. initialize two blocks (strings) with 5 bytes , such that both blocks non overlapping.
2. display both strings before transfer
3. set pointer1 to start of source block.
4. set pointer2 to start of destination block
5. set counter =5
6. Copy a byte from location pointed by pointer1 to location pointed by pointer2
7. Increment pointer1 & pointer2 by 1( because data is byte size)
8. Decrement counter
9. If counter is not zero then go to 5 , else go to 10
10. display both strings after transfer.
11. end
Note:
For data of word size, the pointers need to be incremented by two &
For data of double word size, the pointers need to be incremented by four.
Experiment 7
Title overlapped block transfer using string instructions
Statement Write X86/64 ALP to perform overlapped block transfer using string specific
instructions. Block containing data can be defined in the data segment.
Theory :
Algorithm
( case 1: source block is at lower address & destination block is at higher address )
1. initialize two blocks (strings) with 5 bytes , such that second block overlaps
the first
2. display both strings before transfer
3. set SI/ESI to end of source block & set DI/EDI to end of destination block
4. set counter =5
5. set direction flag i.e. set DF=1
6. move a byte from source block to destination block by Using string
instruction REP MOVSB
7. display both strings after transfer.
8. end
Note:
For data of word size, the pointers need to be incremented by two ( & use MOVSW)
For data of double word size, the pointers need to be incremented by four. ( & use
MOVSD)
Experiment 8
Title Multiplication
Problem Write 8086 ALP to perform multiplication of two 8-bit numbers. Use
statement Successive Addition & shift & add method
Theory:
80386 has two instructions for multiplication :
1. MUL
It is for Unsigned multiplication (i.e. when both numbers are positive numbers)
Eg MUL CL ; multiplies 8 bit AL register with 8 bit CL register & result is stored
in 16
;bit AX register
2. IMUL
It is for signed multiplication (multiplication of two numbers which can be positive or
negative )
Format : IMUL R/M/X ; multiplier can be register /memory/immediate
number
Eg
IMUL CL ; It multiplies AL* CL & result is stored in AX
8-bit AL AX
16-bit AX DX:AX
32-bit EAX EDX:EAX
Multiplication can be done by two methods without using MUL or IMUL instruction.
2. Shift/Add Method
• Work out partial product for each bit of multiplier ( if multiplier bit is 0 , the partial
product is 0000 & if multiplier bit is 1 , the partial product is same as multiplicand)
• We need to store all the Partial products, then shift each partial product as shown
above before adding all of them .
1. DL is loaded with 8 . DL is used as counter to repeat the shift& add 8 times as there
are 8 bits in multiplier.
2. RCX is used to store partial products as well as the final result.
3. RAX holds multiplicand= 0000 1001
4. BL holds multiplier = 0000 1101
5. BL is shifted to right by SHR instruction . so LSB bit goes into carry flag.
6. If carry flag is 1 then RAX is added to RCX & RAX is shifted to left
a. But if carry flag is 0 , then only RAX is shifted to left
7. Decrement DL
8. If DL is not zero , then go to 5 else go to 9
9. Display the result which is in RCX after converting it to ASCII
1. Successive Addition
2. Shift & add method
3. Exit
Enter your choice::
2. Accept the choice from user.
3. If (choice =1), then call procedure SUCCADD else go to 4
4 If (choice =2), then call procedure SHIFTADD else go to 5
5. If (choice =3), then exit else go to 1 to display the menu again.
Experiment 9
Extrn- is used to tell the assembler that labels or names following this directive are in
some other program.
Public – is used to make a label or name available to other modules ( in those other
modules it is declared as extern)
eg
in one module P1.asm we define count variable &declare it as public as shown below
public count
count db 0
in another module P2.asm ,we declare that count is defined in some other module by
extrn count
we assemble P1.asm & P2.asm separately to create P1.o & P2.o object files
These object files can be linked together by linker using command
This will link P1.o & P2.o to create an executable file by the name ‘program’
Algorithm
Assumption :
1. Maximum Size of file abc.txt is 200.
2. the count of spaces, lines & occurrence of a characters are all less than 9, to simplify the
program.
Procedure spaces
1. Set pointer to buffer
2. Read data( character ) pointed by pointer
3. Compare it with ASCII code of space ( i.e. 20h)
4. If match, then increment counter of spaces
5. Increment pointer
6. If all characters not over then go to 2 else go to 7
7. Display count of spaces after converting to ASCII
8. Return to the calling program P1
Procedure lines
1. Set pointer to buffer
2. Read data( character ) pointed by pointer
3. Compare it with ASCII code of line feed ( i.e. 0Ah)
4. If match, then increment counter of lines
5. Increment pointer
6. If all characters not over then go to 2 else go to 7
7. Display count of lines after converting to ASCII
8. Return to the calling program P1
Procedure occr_char
1. Set pointer to buffer
2. Read data( character ) pointed by pointer
3. Compare it with ASCII code of character whose occurrence is to be counted
4. If match, then increment counter of occurrence
5. Increment pointer
6. If all characters not over then go to 2 else go to 7
7. Display count of occurrence after converting to ASCII
8. Return to the calling program P1
Experiment 10
Title Password check
Problem Write an X86/64 ALP password program that operates as follows:
statement a. Do not display what is actually typed instead display asterisk (“*”).
If the password is correct display, “access is granted” else display
“Access not Granted”
Theory
Password is used for authentication ( i.e. only persons who have been assigned credentials
are allowed to use the system or application after entering correct password)
The password entered by user should not be displayed ( echoed ) on the screen.
But the normal syscall function 0 ( rax=0) echoes the typed in keys.
i.e.
mov rax,0
mov rdi,0
mov rsi,starting memory address to store entered keys
mov rdx,number of keys to read
syscall
The struct termios structure is a general terminal interface which provides an interface to
asynchronous communications devices such as keyboard.
The flag c_lflag of the struct termios structure, control higher-level aspects of input
processing such as echoing and the choice of canonical or non canonical input.
tcflag_t ECHO
If this bit is set, echoing of input characters back to the terminal is enabled.
If this bit is cleared, echoing is disabled.
tcflag_t ICANON
If this bit is set, enables canonical input processing mode. Otherwise, input is processed in
non canonical mode.
In canonical input processing mode, No input can be read until <enter> key is pressed.
In noncanonical input mode, the special editing characters such as ERASE ( backspace key
and KILL ( ctrl+C )are ignored and program has to check for <enter > key ( ASCII code
0AH).
Algorithm
Disable echo & canonical mode.
Accept password entered by user while displaying * on screen
Compare entered password with stored password
If match then print “access granted” else print “access denied”
Enable echo & canonical mode.