MP Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Microprocessor Lab Manual

1 Write X86/64 ALP to count number of positive and negative numbers from the array
2 Write X86/64 ALP to perform non-overlapped and overlapped block transfer (with and without string
specific instructions). Block containing data can be defined in the data segment.
3 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:
(a) HEX to BCD b) BCD to HEX (c) EXIT.
Display proper strings to prompt the user while accepting the input and displaying the result. (use of 64-
bit registers is expected)
4 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)
5 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.
6 Write X86/64 ALP to switch from real mode to protected mode and display the values of GDTR,
LDTR, IDTR, TR and MSW Registers.
7 Write X86 program to sort the list of integers in ascending/descending order. Read the input from the
text file and write the sorted data back to the same text file using bubble sort
8 Write X86menu driven Assembly Language Program (ALP) to implement OS (DOS) commands TYPE,
COPY and DELETE using file operations. User is supposed to provide command line arguments in all
cases.
9 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.
10 Write 80387 ALP to find the roots of the quadratic equation. All the possible cases must be considered
in calculating the roots.
11 Write 80387 ALP to plot Sine Wave, Cosine Wave and Sinc function. Access video memory directly
for plotting.
12 Write a Terminate but Stay Resident (TSR) program for a key-logger. The key-presses during the
stipulated time need to be displayed at the center of the screen. OR
Write a TSR to generate the pattern of the frequency tones by reading the Real Time Clock (RTC). The
duration of the each tone is solely decided by the programmer.

Computer Department, NBNSSOE, Pune.


Assignment No: 1

Aim Write an ALP to count no. of positive & negative numbers from the array.

Theory:

Positive & negative numbers

If the MSB bit of the number is 0, then it is a positive number else if it is 1, then it is negative
number.

int 80h : System Call 4:System Write.

EAX = 4 indicates system write operation.

EBX represent file descriptor: 0- Standard Input device. 1- Standard Output device. 2- Standard
Error.

ECX represents the variable/parameter whose value one want to display.

EDX represents the length of the variable/parameter.

BT: BIT TEST: BT saves the value of the bit indicated by the base (first operand) and the bit
offset (second operand) into the carry flag.

Operation: CF <- BT [LeftSRC, RightSRC];

JNC: Jump on not carry

Syntax: JNC LABEL

JNC checks the contents of a result of BT instruction. If carry is generated then execute the next
instruction otherwise make a jump on a label mentioned with JNC instruction.

Algorithm:

1) In initialized data segment, declare the variables to display the messages on the standard
output device. Declare the array of 7 elements, Declare arrcnt& equate it with no. of
elements in array. Declare the variable pcnt to hold the count of positive numbers &ncnt

Computer Department, NBNSSOE, Pune.


to hold the count of negative numbers.

2) In uninitialized data segment declare the variable dispbuff& reserve 2 bytes.

3) Define the macro print with 2 parameters. Call interrupt 80h with function EAX = 4 &
EBX as file descriptor 1for standard output device.

4) In Code Segment display the message “Welcome to count +ve& -ve numbers in the array”

5) Move the effective address of array into ESI register.

6) Move the arrcnt value into ECX.

7) Use BT instruction to check the word pointed by ESI with bit 15 to check whether carry
generated.

8) If carry generated, Increment the value of ncnt.

9) Else Increment the value of pcnt.

10) Increment the ESI twice so that ESI can point to the next number in the array.

11) Repeat the steps 7,8,9,10 till all the elements are identified as either positive or negative
number of the array.

12) Display the message “Count of +ve numbers::”by macro expansion

13) Move the value of pcnt into bl register & call the procedure disp8num.

14) Display the message “Count of -ve numbers::”by macro expansion

15) Move the value of ncnt into bl register & call the procedure disp8num.

16) Display the new line by macro expansion.

17) Call interrupt 80h with EAX = 1 to take the exit.

Computer Department, NBNSSOE, Pune.


Conclusion:-

FAQ:

1. Explain the instruction: JNC, ADD, DEC, DIV

2. Explain in detail: Bit Test

3. Discuss int 80h with function AH=04.

Computer Department, NBNSSOE, Pune.


Assignment No -2

Title: Block Transfer with string and without string

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

Objectives: a. To Study String related instruction used in program


b. To learn the logic to transfer a block

Theory:
All members of the 80x86family support five different string instructions: movs,
cmps, scas, lods, and stos. They are the string primitives since you can build most other
string operations from these five instructions. How you use these five instructions is the
topic of the next several sections This sequence of instructions treats CharArray1 and
CharArray2 as a pair of 384 byte strings. However, the last 383 bytes in the CharArray1
array overlap the first 383 bytes in theCharArray2 array. Let's trace the operation of this
code byte by byte. When the CPU executes the MOVSB instruction, it copies the byte at
ESI (CharArray1) to the byte pointed at by EDI (CharArray2). Then it increments ESI
and EDI, decrements ECX by one, and repeats this process. Now the ESI register points
at CharArray1+1 (which is the address of CharArray2) and the EDI register points at
CharArray2+1. The MOVSB instruction copies the byte pointed at by ESI to the byte
pointed at by EDI. However, this is the byte originally copied from location CharArray1.
So the MOVSB instruction copies the value originally in location CharArray1 to both
locations CharArray2 and CharArray2+1. Again, the CPU increments ESI and EDI,
decrements ECX, and repeats this operation. Now the
movsb instruction copies the byte from location CharArray1+2 (CharArray2+1) to
location CharArray2+2. But once again, this is the value that originally appeared in
location CharArray1. Each repetition of the loop copies the next element in CharArray1
[0] to the next available location in the C charArray2 array. Pictorially, it looks
something like that shown in figure.
The end result is that the MOVSB instruction replicates X throughout the string.
The MOVSB instruction copies the source operand into the memory location which will

Computer Department, NBNSSOE, Pune.


Become the source operand for the very next move operation, which causes the
replication. If you really want to move one array into another when they overlap, you
should move each element of the source string to the destination string.

Computer Department, NBNSSOE, Pune.


Setting the direction flag and pointing ESI and EDI at the end of the strings will allow you to
(correctly) move one string to another when the two strings overlap and the source string begins
at a lower address than the destination string. If the two strings overlap and the source string
begins at a higher address than the destination string, then clear the direction flag and point
ESI and EDI at the beginning of the two strings.

If the two strings do not overlap, then you can use either technique to move the strings around in
memory. Generally, operating with the direction flag clear is the easiest, so that makes the most
sense in this case.

You shouldn't use the MOVSx instruction to fill an array with a single byte, word, or double
word value. Another string instruction, STOS, is much better for this purpose. However, for
arrays whose elements are larger than four bytes, you can use the MOVS instruction to initialize
the entire array to the content of the first element.

Computer Department, NBNSSOE, Pune.


The MOVS instruction is generally more efficient when copying double words than it is copying
bytes or words. In fact, it typically takes the same amount of time to copy a byte using MOVSB
as it does to copy a double word using MOVSD3. Therefore, if you are moving a large number of
bytes from one array to another, the copy operation will be faster if you can use the MOVSD
instruction rather than the MOVSB instruction. Of course, if the number of bytes you wish to
move is an even multiple of four, this is a trivial change; just divide the number of bytes to copy
by four, load this value into ECX, and then use the MOVSB instruction. If the number of bytes is
not evenly divisible by four, then you can use the MOVSD instruction to copy all but the last
one, two, or three bytes of the array (that is, the remainder after you divide the byte count by
four). For example, if you want to efficiently move 4099 bytes, you can do so with the following
instruction sequence:

Algorithm For Overlapping Blocks Transfer:-

(TYPE A : Latter half of source overlapped)

1. Physical initialization of data segment.

2. Initialization of source memory pointer to last element in source array.

3. Initialization of destination memory pointer to last element in destination


array.

4. Initialize counter to no. of elements in source array.

5. Copy element in a source array pointed by source memory pointer to a


location in a destination array pointed by destination memory pointer.

6. Decrement destination memory pointer, decrement source memory pointer


and decrement counter by 1.

7. If (counter 0), goto step 5.

8. Terminate program and exit to DOS.

Algorithm For Overlapping Block Transfer:-

(TYPE B: Prior half of source overlapped)

Computer Department, NBNSSOE, Pune.


1. Physical initialization of data segment.

2. Initialization of memory pointer to first element of source.

3. Initialization of memory pointer to first element of destination.

4. Initialization of counter to no. of elements in source array.

5. Copy element in a source array pointed by source memory pointer to a


location in a destination array pointed by destination memory pointer.

6. Increment destination memory pointer, Increment source memory pointer and


decrement counter.

7. If (counter 0), goto step 5.

8. Terminate program and exit to DOS.

Conclusion:

Questions:

1) What are the interrupts used in above program. Explain in detail.


2) What is the assembler directives used in above program?
3) Explain the flag register of 8086 and 80386 microprocessor?

Computer Department, NBNSSOE, Pune.


Assignment No: 3

Title: HEX to BCD & BCD to HEX Conversion

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: (a) HEX to BCD b) BCD to HEX (c) EXIT. Display proper strings to
prompt the user while accepting the input and displaying the result. (use of 64-bit registers is
expected)

Objectives: To learn the number conversion logic.

Theory:

1. BCD TO HEX CONVERSION

We are given a five digit BCD number whose HEX equivalent is to be found i.e. 65535 whose
HEX equivalent is to be found. First we will find the Hex equivalent of 60,000. We will compare
60,000H with 10,000H. Each time we compare a counter is decremented by 10,000 and we add
10,000 decimal (2710 Hex). Then we will find the equivalent of 5000. Now we will compare
5000H with 1000H. Each time we compare the counter decrements by 1000 and we add 1000
decimal (3E8 H). Then we find the equivalent of 500H by comparing it with 100H. Each time
counter decrements by 100 and we add decimal 100 (64 H). Then we find equivalent of 30H by
Comparing it with 10H. Each time counter decrements by 10 and we add 10 decimal (0A H).
Then, equivalent of 5 is 5H. Finally, all the equivalents obtained are added to get the equivalent
of 65535

2. HEX TO BCD CONVERSION

We have a 4 digit Hex number whose equivalent binary number is to be found i.e. FFFF H.
Initially we compare FFFF H with decimal 10000 ( 2710 H in Hex ). If number is greater than
10,000 we add it to DH register. Also, we subtract decimal 10,000 from FFFF H, each time
comparison is made. Then we compare the number obtained in AX by 1000 decimal. Each time
we subtract 1000 decimal from AX and add 1000 decimal to BX. Then we compare number
obtained in AX by 100 decimals. Each time we subtract 100 decimal from AX and add 100
decimal to BX to obtain BCD equivalent. Then we compare number obtained in AX with 10
decimal. Each time we subtract 10 decimal from AX and we add 10 decimal to BX. Finally we
add the result in BX with remainder in AX. The final result is present in register DH with
contains the 5th bit if present and register AX.

Computer Department, NBNSSOE, Pune.


Algorithm for BCD to Hex Conversion

Step I : Initialize the data segment.


Step II : Load the MSB of word in register AX.
Step III : Compare it with 0, if zero goto step VII else go to step IV.
Step IV : decrement AX and initialize BX = 0000.
Step V : add 10000 decimal to BX.
Step VI : Jump to step III.
Step VII : Load LSB of word in register AX.
Step VIII : Compare it with 1000, if below go to step XII else go to step IX.
Step IX : subtract 1000 H from AX.
Step X : Add 1000 decimal to BX.
Step XI : Jump to step VIII
Step XII : Compare number in AX now with 100 H, if below go to step XVI, else go to step
XIII.
Step XIII : Subtract 100 H from AX.
Step XIV : Add 100 decimal to BX.
Step XV : Jump to step XII.
Step XVI : Compare number in AX with 10H, if below go to step XX, else go to step XVII.
Step XVII : Subtract 10 H from AX
Step XVIII : Add 10 decimal to BX
Step XIX : Jump to step XVI
Step XX : Add contents of AX and BX.
Step XXI : Display the result.
Step XXII : Stop.

Algorithm for Hex to BCD Conversion

Step I : Initialize the data segment.


Step II : Initialize BX = 0000 H and DH = 00H.
Step III : Load the number in AX.
Step IV : Compare number with 10000 decimal. If below go to step VII else go to step V.
Step V : Subtract 10,000 decimal from AX and add 1decimal to DH
Step VI : Jump to step IV.
Step VII : Compare number in AX with 1000, if below go to step X else go to step VIII.
Step VIII : Subtract 1000 decimal from AX and add 1000 decimal to BX.
Step IX : Jump to step VII.
Step X : Compare the number in AX with 100 decimal if below goto step XIII

Computer Department, NBNSSOE, Pune.


Step XI : Subtract 100 decimal from AX and add 100 decimal to BX.
Step XII : Jump to step X
Step XIII : Compare number in AX with 10. If below goto step XVI
Step XIV : Subtract 10 decimal from AX and add 10decimal to BX..
Step XV: Jump to step XIII.
Step XVI : Add remainder in AX with result in BX.
Step XVII : Display the result in DH and BX.
Step XVIII : Stop.

Conclusion:

FAQ:

1) What are the interrupts used in above program. Explain in detail.


2) What is the assembler directives used in above program?
3) What is logic used to convert BCD TO HEX number?
4) What is logic used to convert HEX TO BCD number

Computer Department, NBNSSOE, Pune.


Assignment No 4

Title:- Multiplication of Hexadecimal numbers.

Aim:-Write an 8086 assembly language program to perform the multiplication of two 8 bit Hex
numbers by
1) Successive Addition
2) Shift & Add Method
Objectives:
1. To study basics of assembly language programming i.e. Software commands to use them,
format of alp, assembler directives.
2. To study step in assembly language programming.
3. To study DOS-DEBUG to execute program and to check the results.
4. To study basic 8086 instructions & an interrupt instruction

Assember directives used:-

1. segment
2. ends
3. macro & endm
4. proc & endp

ALGORITHMS FOR PROCEDURE MAIN:-

1. Start
2. Physical initialization of data segment
3. Display the following menu for user :-

**** MULTIPLICATION ****


1. Accept the numbers
2. Successive Addition
3. Shift & add method
4. Exit
Enter your choice::
4. Accept the choice from user.
5. If (choice =1), then call procedure „ACCEPT‟.
6. If (choice =2), then call procedure „SUCC‟.
7. If (choice =3), then call procedure „SHIFT‟.
8. STOP / Exit to DOS.

Computer Department, NBNSSOE, Pune.


ALGORITHMS FOR PROCEDURE ‘SUCC’:-
1. Start
2. Copy the multiplicand in count register & copy the
multiplier in base register.
3. Add the content of base register with itself.
4. Decrement the contents in count register.
5. If (choice !=0), then goto step (3)
6. Display the result in base register.
7. STOP / Exit to DOS.

ALGORITHMS FOR PROCEDURE ‘SHIFT’:-


1. Start
2. Get the LSB of multiplier.
3. Do the multiplication of LSB of multiplier with
multiplicand by Successive Addition Method.
4. Store the result in accumulator.
5. Get the MSB of multiplier.
6. Do the multiplication of MSB of multiplier with
multiplicand by successive addition method.
7. Store the result in base register. Shift the contents of
base register towards left by 4 bits.
8. Add the contents of accumulator & base register.
9. Display the result.
10.RETURN.

Conclusion:

Questions:
1. Explain Logic for the program
2. Explain Successive addition process
3. Explain Shift & rotate method.

Computer Department, NBNSSOE, Pune.


Assignment No – 5

Title:- Near and FAR Procedure


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.

Prerequisites:
1. Basic knowledge about String operations
2. Knowledge about procedure in ALP
Objectives:
1. To study basics of assembly language programming i.e. Software development alp, assembler
directives.
2. To study step in assembly language programming.
3. To study dos-debug to execute program and to check the results.
4. To study basic 8086 instructions & interrupt instruction

Theory:
Far CALL and RET Operation
When executing a far call, the processor performs these actions
1. Pushes current value of the CS register on the stack.
2. Pushes the current value of the EIP register on the stack.
3. Loads the segment selector of the segment that contains the called procedure in the CS
register.
4. Loads the offset of the called procedure in the EIP register.
5. Begins execution of the called procedure.
When executing a far return, the processor does the following:
1. Pops the top-of-stack value (the return instruction pointer) into the EIP register.
2. Pops the top-of-stack value (the segment selector for the code segment being returned to) into
the CS register.
3. (If the RET instruction has an optional n argument.) Increments pointer by the number of
bytes specified with the n operand to release parameters from the stack.
4. Resumes execution of the calling procedure.

ASSEMBER DIRECTIVES USED:-


1. MACRO & ENDM
2. PROC & ENDP
3. EXTRN
4. PUBLIC

Computer Department, NBNSSOE, Pune.


LIST OF PROCESDURES USED:-
1. ACCEPT PROC
2. CONCAT PROC
3. COMPARE PROC
4. SUBSTR PROC
5. NO_WORD PROC
6. NO_CHAR PROC
LIST OF MACROS USED:-
MACRO IS USED TO DISPLAY A STRING:-
DISP MACRO MESSAGE
MOV AH, 09H
LEA DX, MESSAGE
INT 21H
ENDM
ALGORITHMS:-
ALGORITHMS FOR PROCEDURE MAIN:-
1. Start
2. Physical initialization of data segment
3. Display the following menu using macro :-
**** STRING OPERATIONS ****
1. Accept the string
2. Concatenation
3. Check for substring
4. Compare the strings
5. Number of words
6. Number of characters
7. Number of digits
8. Number of Capital characters
9. Exit
Select your option ::
4. Accept the choice from user.
5. If (choice =1), then call FAR procedure „ACCEPT‟.
6. If (choice =2), then call FAR procedure „CONCAT‟.
7. If (choice =3), then call FAR procedure „SUBSTR‟.
8. If (choice =4), then call FAR procedure „COMPARE‟.
9. If (choice =5), then call FAR procedure „NO_WORDS‟.
10. If (choice =6), then call FAR procedure „NO_CHAR‟.
11. If (choice =7), then call FAR procedure „NO_DIGIT‟.
12. If (choice =8), then call FAR procedure „NO_CAP‟.
13. If (choice =9), then Exit to DOS, terminating the program.

Computer Department, NBNSSOE, Pune.


14. If (choice!=9), repeat the steps (3), (4) & (5).
15. Stop.
ALGORITHMS FOR PROCEDURE ‘CONCAT’:-
1. Start
2. Initialization of pointer1 to first string & pointer2 to second string.
3. Initialize the count1 & count2 to length of first & second string respectively.
4. Display the character pointed by pointer1.
5. Increment the pointer1 & decrement the count1.
6. If count1 is not zero, goto step (4).
7. Display the character pointed by pointer2.
8. Increment the pointer2 & decrement the count2.
9. If count2 is not zero, gotostep(7).
10. Stop.
ALGORITHMS FOR PROCEDURE ‘SUBSTR’:-
1. Start
2. Initialize the source pointer to main string & destination pointer to substring.
3. Initialize the count1 & count2 to length of main string & substring respectively.
4. Compare the characters pointed by source & destination pointer.
5. If they are equal, goto step (6), else goto ( ).
6. Increment the source pointer & destination pointer. Decrement the count1 & count2.
7. If (count2!=0), then goto step (4) else goto step (9).
8. Increment the source pointer & reinitialize the destination pointer. Decrement the count1 &
reinitialize the count2 &goto step (4).
9. Increment the count for number of occurrences.
10. If (count1!=0), then goto step (10) else goto step (12).
11. Reinitialize the destination pointer & count2 &goto step (4).
12. If count for no. of occurrences of substring is „zero‟, then print “NOT SUBSTRING”, else
print “SUBSTRING” & print the number of occurrences of string.
13. Stop.
ALGORITHMS FOR PROCEDURE COMPARE:-
1. Start
2. Initialize the source pointer the source pointer to string1 & destination pointer to string2.
Initialize the count1 & count2 to the length of string1 & string2 respectively.
3. If length of string1 & string2 are not same, goto step (9).
4. Compare the characters pointed by source & destination pointer.
5. If they are equal, goto step (6), else goto(9).
6. Increment the source pointer & destination pointer.
Decrement count1.
7. If (count1!=0), goto step (4), else goto (8).
8. Print “STRING ARE EQUAL……!” &goto (10).

Computer Department, NBNSSOE, Pune.


9. Print “STRING IS NOT EQUAL……!”
10. STOP.
ALGORITHMS FOR PROCEDURE ‘NO_WORD’:-
1. Start
2. Initialize the source pointer to the given string & the count to length of string.
3. Compare the character pointed by source pointer to “ “ (space).
4. If equal, increment the count for no. of words & increment source pointer.
Else increment the source pointer, goto step (3) till count!=0.
5. Print the no. of words.
6. STOP.

ALGORITHMS FOR PROCEDURE ‘NO_CHAR’:-


1. Start
2. Initialize the source pointer to the given string & the count to length of string.
3. Compare the character with „30H‟. If below, goto step (8).
If greater, goto step (4).
4. Compare the character with „39H‟. If below or equal, „increment the count for no. of digits‟. If
greater, goto step (5).
5. Compare the character with „5AH‟. If below or equal, „increment the count for no. of capital
letters‟ & also increment the count for no. of characters. If greater, goto step (6).
6. Compare the character with „60H‟, If below or equal, goto step (8).
If greater, goto step (7).
7. Compare the character with „7AH‟. If below or equal, „increment the count for no. of
characters‟.
8. Decrement the count and increment the source pointer &goto step (9).
9. If count is not zero, goto step (3). Else goto step (10).
10. Display the result i.e. no. of words, no. of characters, no. of capital letters.
11. STOP.
Conclusion:

FAQ:
Q. 1 Explain which instructions are used for string operations in
ALP
Q.2 Explain logic for the program.
Q.3 Explain What is the Procedure in ALP
Q.4 Explain FAR procedure.
Q.5 Which interrupts are used in the program.

Computer Department, NBNSSOE, Pune.


Assignment No -6

Aim:- Write X86/64 ALP to switch from real mode to protected mode and display the values of
GDTR, LDTR, IDTR, TR and MSW Registers.
Theory:-

Global Descriptor Table Register (GDTR):

Interrupt Descriptor Table Register (IDTR):

Computer Department, NBNSSOE, Pune.


Local Descriptor Table Register (LDTR):

Computer Department, NBNSSOE, Pune.


Control Registers:

Task Register (TR):

Computer
Department,
NBNSSOE, Pune.
Instruction Description Mode

LGDT S:- Load the global descriptor table register. S specifies both the memory location that
contains the first byte of the 6 bytes to be loaded into the GDTR.

SGDT D:- Store the global descriptor table register. D specifies both the memory location that
gets the first of the six bytes to be stored from the GDTR.

LIDT S: - Load the interrupt descriptor table register. S specifies both the memory location that
contains the first byte of the 6 bytes to be loaded into the IDTR.

SIDT D:- Store the interrupt descriptor table register. D specifies both the memory location that
gets the first of the six bytes to be stored from the IDTR.

Algorithm:

1) Start
2) Variable declaration in data section with initialization
3) Variable bss. section without initialization
4) Macro definition for display msg on screen
5) Read CRo
6) If PE beat =1
7) Store contains of GDT
8) Store contains of LDT
9) Store contains of IDT
10) Store contains of TR
11) Call display processor to display control of GDT
12) Call display processor to display contain of LDT
13) Call display processor to display contain of IDT
14) Call display processor to display control of TR
15) Call display processor to display control of MSW

16)Point to esi buffer 17)Load no. of digit to display

18) Rotate no. left by 4 bit


19) Move lower byte in DL
20) Mask upper digit of byte in DL
21) Add 30h to calculate ASCCI code
22) If DL < 39 , no add 7, yes Skip adding 07 more

Computer Department, NBNSSOE, Pune.


23) Store ASCCI code in buffer

24) Point to next byte


25) 25)Display the no. from buffer
26) END

Conclusion:

FAQ:

1. What is LDT, GDT, IDT, TR, MSW?


2. Explain CR0 register.
3. What is the size of LDTR,GDTR,IDTR,TR & MSW?

4. Which instructions are used to load & store LDTR,GDTR,IDTR,TR, MSW?

Computer Department, NBNSSOE, Pune.


Assignment No-7

Aim: - Write X86 programs to sort the list of integers in ascending/descending order. Read the
input from the text file and write the sorted data back to the same text file using bubble sort.
Theory:-
Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly
steps through the list to be sorted, compares each pair of adjacent items and swaps them if they
are in the wrong order. The pass through the list is repeated until no swaps are needed, which
indicates that the list is sorted. The algorithm, which is a comparison sort, is named for the way
smaller or larger elements "bubble" to the top of the list. Although the algorithm is simple, it is
too slow and impractical for most problems even when compared to insertion sort.[1] It can be
practical if the input is usually in sorted order but may occasionally have some out-of-order
elements nearly in position.

Algorithm

1. Compare each pair of adjacent elements from the beginning of an array and, if they are in
reversed order, swap them.
2. If at least one swap has been done, repeat step 1.

you can imagine that on every step big bubbles float to the surface and stay there. At
the step, when no bubble moves, sorting stops

Conclusion:

FAQ:-

1. What do you mean by bubble sort?


2. Which instructions can be used in bubble sort?

Computer Department, NBNSSOE, Pune.


Assignment No – 8

Aim:- Write X86menu driven Assembly Language Program (ALP) to implement OS (DOS)
commands TYPE, COPY and DELETE using file operations. User is supposed to provide
command line arguments in all cases.

Theory:-
• 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

Computer Department, NBNSSOE, Pune.


mov rdi,Fname
syscall

• CLOSE File
mov rax,3
mov rdi,[fd_in]
syscall

TYPE Command:-
• Open file in read mode using open interrupt.
• Read contents of file using read interrupt.
• Display contents of file using write interrupt.
• Close file using close interrupt

COPY Command

• Open file in read mode using open interrupt.


• Read contents of file using read interrupt.
• Create another file using read interrupt change only attributes.
• Open another file using open interrupt.
• Write contents of buffer into opened file.
• Close both files using close interrupt.

DELETE Command
1. DELETE file using delete interrupt

Algorithm

1.Accept Filenames from Command line.


2.Display MENU:-
1.TYPE
2.COPY
3.DEL
3.Procedure for TYPE command
4.Procedure for COPE command

Computer Department, NBNSSOE, Pune.


5.Procedure for DELETE command
6.EXIT

Conclusion:

FAQ:-
1. Why we use TYPE command?
2. Why we use COPY command?
3. Why we use DEL command?

Computer Department, NBNSSOE, Pune.


Assignment No – 9

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.

Theory:-

Algorithm
1.Accept Number from User
2.Call Factorial Procedure
3.Define Recursive Factorial Procedure
4.Disply Result.

Conclusion:

FAQ:-
1. What are the applications of factorial?
2. Why to use factorial?
Computer Department, NBNSSOE, Pune.
Assignment No – 10

Aim:- Write 80387 ALP to find the roots of the quadratic equation. All the possible cases
must be considered in calculating the roots.

Theory:-

Prerequisites:
 Knowledge about coprocessor

Objectives:
1. To study basics of assembly language programming i.e. Software development
tools required commands to use them, format of alp, assembler directives.
2. To study step in assembly language programming.
3. To study DOS-DEBUG to execute program and to check the results.
4. To study basic 8086 instructions & an interrupt instruction & addressing modes

Theory:
1.a) Features of 80387:
 Fabricated using HMOS III technology and packaged in a 40-pin cerdip package,
 68- instructions,
 Instruction set includes both simple and complex floating point instructions,
 By interfacing 8087 NDP with 80386 processor, programmer can perform various
powerful arithmetic floating point operations. e.g. add, sub, div, square root, logarithm
etc.,
 80387 support 7 data types: 16-, 32-, 64-bit integers, 32-, 64-, 80-bit floating point and
18-Digit BCD operands,
 Having 8 X 80 bit individually addressable register stack
 Available in
 5MHz (80387),
 8MHz (80387-2) and
 10MHz (80387-1),
 7- built-in exception handling functions
 Compatible with IEEE floating point standard 754,
 The data information inside 8087 is always stored in temporary real format (80-bit
floating point representation)
 Use of 80387 in 80386 system increases processing speed to perform different
mathematical operations required in CAM, numeric controllers, CAD or Graphics,

Computer Department, NBNSSOE, Pune.


f) Instruction of co-processor used in the assignment:
FINIT: Initialise Co-processor
FLDZ: Load zero on stack top
FILD: Load Integer on stack
FIDIV: Divide stack top by an integer value
FIMUL: Multiply stack top by an integer value
FST: Store stack top
FADD: Add in stack top
FBSTP: Store integer part of stack top in 10 byte packed BCD format
FMUL: Multiply stack top
FSQRT: Square Root of Stack Top

d) Concept of Mean: It is the average of numbers, Variance: It is the average of


squared difference from mean and Standard Deviation: It is square root of variance.

2. Algorithms for program


A1: Algorithm for program to calculate mean
i. Start
ii. Initialise the co-processor
iii. Load zero on stack top
iv. Add data elements from data segment on top of stack
v. Divide the result by count of elements added to get mean
vi. Display result message
vii. Display result
viii. Stop

A2: Algorithm for program to find variance


i. Start
ii. Initialise the co-processor
ix. Load zero on stack top
x. Add data elements from data segment on top of stack
xi. Divide the result by count of elements added to get mean
xii. Store mean in the memory
xiii. Load zero on stack top
xiv. Load element on stack top and subtract the mean from it and square
the difference
xv. Repeat step xiii to xiv for all elements
xvi. Add all squares
xvii. Divide the addition by count of elements

Computer Department, NBNSSOE, Pune.


xviii. Display result message
iii. Display result
iv. Stop

A2: Algorithm for program to find standard deviation


v. Start
vi. Initialise the co-processor
xix. Load zero on stack top
xx. Add data elements from data segment on top of stack
xxi. Divide the result by count of elements added to get mean
xxii. Store mean in the memory
xxiii. Load zero on stack top
xxiv. Load element on stack top and subtract the mean from it and square
the difference
xxv. Repeat step xiii to xiv for all elements
xxvi. Add all squares
xxvii. Divide the addition by count of elements
xxviii. Calculate square root
xxix. Display result message
vii. Display result
viii. Stop

Conclusion:

FAQ:-

1. Which instructions we need to use for quadratic equations?

Computer Department, NBNSSOE, Pune.


Assignment No – 11

Aim:- Write 80387 ALP to plot Sine Wave, Cosine Wave and Sinc function. Access video
memory directly for plotting.

• Theory:- The overall display characteristics, such as vertical and horizontal resolution,
background color, and palette, are controlled by values written to I/O ports whose
addresses are hardwired on the adapter,

• whereas the appearance of each individual character or graphics pixel on the display is
controlled by a specific location within an area of memory called the regen buffer or
refresh buffer.

• Both the CPU and the video controller access this memory;

• The software updates the display by simply writing character codes or bit patterns
directly into the regen buffer. (This is called memory-mapped I/O.)

• E.g. MDA, CGA, EGA, MCGA, VGA etc.

Text Mode

• Sometimes also called as alphanumeric display mode

• Use 16KB of memory starting at segment B800H

• 2 Bytes per character (ASCII Code & Attribute)

• For 80-by-25 text mode

offset = (y*80 + x) *2

Graphics Mode

• More complicated

• Each bit or group of bits in regen buffer corresponds to an addressable point or pixel on
the screen

• E.g. 640-by-200, 2-color graphics display mode of the CGA

• Each pixel is represented by a single bit

• (x,y) range (0,0) through (639,199)

Computer Department, NBNSSOE, Pune.


• The memory map is set up so that all the even y coordinates are scanned as a set and all
the odd y coordinates are scanned as a set; this mapping is referred to as the memory
interlace.

• Offset = ((y AND 1)*2000H) + (y/2*50H)+ (x/8)

• Bit position = 7 – (x MOD 8)

( 8 byte table or array of bit masks and operation „x AND 7‟)

Sine Wave

• Y = 100 – 60 sin ({pi/180}*x)

Cosine Wave

• Y = 100 – 60 cos ({pi/180}*x)

Sinc function

• Sinc (x) = sin (x)/x

Conclusion:

FAQ:-

1. Which instructions we need to use for Sine wave?


2. Which instructions we need to use for Cosine wave?
3. Which instructions we need to use for Sinc function?

Computer Department, NBNSSOE, Pune.


Assignment No – 12

Aim:- Write a Terminate but Stay Resident (TSR) program for a key-logger. The key-
presses during the stipulated time need to be displayed at the center of the screen

Theory:-
Most MS-DOS applications are transient. They load into memory,execute, terminate, and
DOS uses the memory allocated to the application for the next program the user executes.
Resident programs follow these same rules, except for the last. A resident program, upon
termination, does not return all memory back to DOS. Instead, a portion of the program remains
resident, ready to be reactivated by some other program at a future time. Resident programs, also
known as terminate and stay resident programs or TSRs, provide a tiny amount of multitasking
to an otherwise single tasking operating system. Until Microsoft Windows became
popular, resident programs were the most popular way to allow multiple applications to coexist
in memory at one time. Although Windows has diminished the need for TSRs for background
processing, TSRs are still valuable for writing device drivers, antiviral tools, and program
patches
INSTALLING A TSR
Although we‟ve already discussed how to make a program go resident, there are a few aspects to
installing a TSR that we need to address. First, what happens if a user installs a TSR and then
tries to install it a second time without first removing the one that is already resident? Second,
how can we assign a TSR identification number that won‟t conflict with a TSR that is already
installed? This section will address these issues. The first problem to address is an attempt to
reinstall a TSR program. Although one could imagine a type of TSR that allows multiple copies
of itself in memory at one time, such TSRs are few and far in-between. In most cases, having
multiple copies of a TSR in memory will, at best, waste memory and, at worst, crash the system.
Therefore, unless you are specifically written a TSR that allows multiple copies of itself in
memory at one time, you should check to see if the TSR is installed before actually installing it.
This code is identical to the code an application would use to see if the TSR is installed, the only
difference is that the TSR should print a nasty message and refuse to go TSR if it finds a copy of
itself already installed in memory
REMOVING A TSR
Removing a TSR is quite a bit more difficult that installing one. There are three things the
removal code must do in order to properly remove a TSR from memory: first, it needs to stop
any pending activities (e.g., the TSR may have some flags set to start some activity at a future
time); second it needs to restore all interrupt vectors to their former values; third, it needs to
return all reserved memory back to DOS so other applications can make use of it. The primary
difficulty with these three activities is that it is not always possible to properly restore the
interrupt vectors. If your TSR removal code simply restores the old interrupt vector values, you
may

Computer Department, NBNSSOE, Pune.


create a really big problem. What happens if the user runs some other TSRs after running yours
and they patch into the same interrupt vectors as your TSR? This would produce interrupt chains
that look something like the following:
Interrupt Vector ‡ TSR #1‡ TSR #1‡ Your TSR Original TSR
If you restore the interrupt vector with your original value, you will createthe following:
Interrupt Vector TSR #1‡ TSR #1‡? Original TSR
This effectively disables the TSRs that chain into your code. Worse yet,this only disable the
interrupts that those TSRs have in common with your TSR. the other interrupts those TSRs patch
into are still active. Who knows how those interrupts will behave under such circumstances? One
solution is to simply print an error message informing the user that they cannot remove this TSR
until they remove all TSRs installed prior to this one. This is a common problem with TSRs and
most DOS users who install and remove TSRs should be comfortable with the fact that they must
remove TSRs in the reverse order that they install them. It would be tempting to suggest a new
convention that TSRs should obey; perhaps if the function number is 0FFh, a TSR should store
the value in es:bx away in the interrupt vector specified in cl. This would allow a TSR that
would like to remove itself to pass the address of its original interrupt handler to the previous
TSR in the chain. There are only three problems with this approach: first, almost no TSRs in
existence currently support this feature, so it would be of little value; second, some TSRs might
use function 0FFh for something else, calling them with this value, even if you knew their ID
number, could create problem; finally, just because you‟ve removed the TSR from the interrupt
chain doesn‟t mean you can (truly) free up the memory the TSR uses. DOS‟ memory
management scheme (the free pointer business) works like a stack. If there are other TSRs
installed above yours in memory, most applications wouldn‟t be able to use the memory freed up
by removing your TSR anyway. Therefore, we‟ll also adopt the strategy of simply informing the
user that they cannot remove a TSR if there are others installed in shared interrupt chains. Of
course, that
does bring up a good question, how can we determine if there are other TSRs chained in to our
interrupts? Well, this isn‟t so hard. We know that the 80x86‟s interrupt vectors should still be
pointing at our routines if we‟re the last TSR run. So all we‟ve got to do is compare the patched
interrupt vectors against the addresses of our interrupt service routines. If they all match, then we
can safely remove our TSR from memory. If only one of them does not match, then we cannot
remove the TSR from memory

1. The timer interrupt TYPE 8 is initiated by the system 18.2 times per
second. Using this information, For 5 Seconds Can Be Calculated as:
18.2*5 =91
2. The video RAM is distributed in 4 pages of size 2K bytes. The
base address of each is
Page0 – B800H
Page1 – b900h

Computer Department, NBNSSOE, Pune.


Page 2- ba00h
Page3 – bb00h
By default page 0 is activated. This means the data written to this page will be seen on monitor.
3. To display the data in any other page, the respective page has to be
activated first using BIOS interrupt.
Interrupt 21h function used:
Σ Initializes a CPU interrupt vector to point to an interrupt
handling routine.
Call with:
AH = 25H
AL = interrupt number
DS:DX = segment:offset of interrupt handling routine
Σ Terminate and stay resident
Call with:
AH = 31H
AL = return code
DX = amount of memory to reserve (in paragraphs)
Returns:
Nothing
Σ Get interrupt vector
Call with:
AH = 35H
AL = interrupt number
Returns:
ES:BX = segment:offset of interrupt handler

Algorithm
1. INIT ROUTINE
a. Read vector for interrupt (Type 9) from IVT.
b. Save the vector (CS:IP) in memory variable OLDINT9
c. Write BASE:OFFSET of “keyboard ISR” for “TYPE 9”
entry in IVT.
d. Read vector for interrupt (Type 8) from IVT.
e. Save the vector (CS:IP) in memory variable OLDINT8
f. Write BASE:OFFSET of “TIMER ISR” for “TYPE 8”
entry in IVT.
g. Instruct DOS to make the ISR part of the resident in
memory.
2. Keyboard ISR:
a. Push all registers

Computer Department, NBNSSOE, Pune.


b. Reset the counter
c. Activate page 0(video RAM)
d. Pop all registers
e. Transfer the control to original system ISR using OLDINT9
3. TIMER ISR
a. Push all registers
b. Increment the counter
c. Check the counter .If <=91 then go to next step else go to
step b
d. Activate page1 of video RAM
e. Write string str to video RAM page1
f. Pop all registers
g. Transfer control to original system ISR using OLDINT8

Conclusion:

FAQ:-

1. What are the interrupts used in above program. Explain in detail.


2. What is the assembler directives used in above program?
3. Explain the flag register of 8086 and 80386 microprocessor?

Computer Department, NBNSSOE, Pune.

You might also like