SS_MPFinalNofront
SS_MPFinalNofront
1
8086 Kit
Ex.No.1
07-08-24
Aim
Write an assembly level program to implement
1. Addition of two 16 bit numbers
Algorithm
Addition
1. Start
2. Load 2 input numbers to locations [1500] and [1502]
3. Add both operands and store the result in location [1504]
4. If there is no carry then set value at location [1506] to 00H
5. Otherwise set value at location [1506] to 01H
6. Stop
Subtraction
1. Start
2. Load 2 input numbers to locations [1500] and [1502]
3. Subtract second input number from first and store the result at location [1504]
4. If there is no borrow then set value at location [3002] to 00H
5. Otherwise set value at location [3002] to 01H
6. Stop
Source Code
Addition
2000: MOV AX,[1500]
2004: MOV BX,[1502]
2008: ADD AX,BX
200A: MOV [1504],AX
200E: MOV [1506],00H
2013: JNC 201A
2015: MOV [1506],01H
201A: HLT
2
Subtraction
2000: MOV AX,[1500]
2004: MOV BX,[1502]
2008: SUB AX,BX
200A: MOV [3000],AX
200E: MOV [3002],00H
2013: JNC 201A
2015: MOV [3002],01H
201A: HLT
Input
Addition
[1500]: 2302
[1502]: 1020
Subtraction
[1500]: 2302H
[1502]: 1020H
Output
Addition
[1504]: 3322
[1506]: 0000
Subtraction
[3000]: 12E2H
[3002]: 0000H
Results
Program code has been executed and the output has been verified.
3
8086 Kit
Ex.No.2
07-08-24
Aim
Write an Assembly Level Program to implement
Algorithm
1. Addition
1. Start
2. Load 2 input numbers to locations [1000] and [1004]
3. Add lower 2 byte inputs from [1000] and [1004] and store the result at location
[3000]
4. Add higher 2 byte inputs from [1002] and [1006] and store the result at location
[3002]
5. Initialize BX to 0000H
6. If there is no carry then go to Step 8 otherwise go to Step 7
7. Increment BX and go to Step 9
8. Store BX to [3004]
9. Stop
2. Subtraction
1. Start
2. Load 2 input numbers to locations [1000] and [1004]
3. Subtract lower 2 bytes of second input number from first and store the result at
location [3000]
4. Subtract higher 2 bytes of second input from first and store the result at location
[3002]
5. If there is no borrow then set value at location [3002] to 00H
6. Otherwise set value at location [3002] to 01H
7. Stop
4
Source Code
1. Addition
2000: MOV AX,[1000]
2004: ADD AX,[1004]
2008: MOV [3000],AX
200C: MOV AX,[1002]
2010: ADC AX,[1006]
2014: MOV [3002],AX
2018: MOV BX,0000H
201C: JNC 201F
201E: INC BX
201F: MOV [3004],BX
2023: HLT
2. Subtraction
2000: MOV AX,[1000]
2004: MOV BX,[1004]
2008: SUB AX,BX
200A: MOV [3000],AX
200E: MOV AX,[1002]
2012: MOV AX,[1006]
2016: SBB AX,BX
2018: MOV [3002],AX
201C: MOV BX,0000H
2020: JNC 2024
2022: INC BX
2024: MOV [3004],BX
2028: HLT
Input
Addition
[1000]: 1440H
[1002]: 3118H
[1004]: 4111H
[1006]: 2643H
Subtraction
[1000]: 1FFFH
[1002]: FFFFH
[1004]: F111H
[1006]: 1111H
5
Output
Addition
[3000]: 5551H
[3002]: 575BH
Subtraction
[3000]: 2EEE
[3002]: EEED
Results
Program code has been executed and the output has been verified.
6
8086 Kit
Ex.No.3
07-08-24
Aim
Write an assembly level program to implement multiplication of signed and unsigned
numbers.
Algorithm
1. Start
2. Load 2 input numbers to locations [1000] and [1002]
3. Multiply both inputs and store it at location [1004]
4. Store contents of DX to location [1006]
5. Stop
Source Code
2000: MOV AX,[1000]
2004: MOV CX,[1002]
2008: MUL CX
200A: MOV [1004],AX
200E: MOV [1006],DX
2011: HLT
Input
[1000]: 1432H
[1002]: 0211H
Output
[1004]: BB52H
[1006]: 0029H
Results
Program code has been executed and the output has been verified.
7
8086 Kit
Ex.No.4
07-08-24
Aim
Write an assembly level program to implement division of two numbers
Algorithm
1. Start
2. Load contents of location [1000] to AX
3. Load contents of location [1002] to DX
4. Load contents of location [1004] to BX
5. Divide contents of DX:AX by BX and store it in AX, DX
6. Store the contents of DX at location [1006]
7. Store contents of AX at location [1008]
8. Stop
Source Code
2000: MOV AX,[1000]
2004: MOV DX,[1002]
2008: MOV BX,[1004]
200C: DIV BX
200E: MOV [1006],DX
2012: MOV [1008],AX
2016: HLT
Input
[1000]: FFFFH
[1002]: 0000H
[1004]: 00FFH
Output
[1006]: 0000H
[1008]: 0101H
8
Results
Program code has been executed and the output has been verified.
9
8086 Kit
Ex.No.5
14-08-24
Aim
Write an assembly level program to find LCM of two numbers.
Algorithm
1. Start
2. Load input numbers to AX, BX from location [1000] and [1002] respectively
3. Push AX and BX to stack
4. Compare BX to 0 and if equal to 0, then go to Step 10
5. Clear DX register
6. Divide AX by NX and store it in AX
7. Move contents of BX to AX
8. Move contents of DX to BX and go to Step 4
9. Move contents of AX to CX
10. Pop values from stack to BX and AX
11. Multiply AX and CX and store the result in AX
12. Divide AX with CX and store the result in AX
13. Store the contents of AX at location [1004]
14. Stop
Source Code
2000: MOV AX,[1000]
2004: MOV BX,[1002]
2008: PUSH AX
2009: PUSH BX
200A: CMP BX,0
200D: JE 2019
200F: XOR DX,DX
2011: DIV BX
2013: MOV AX,BX
2015: MOV BX,DX
2017: JMP 200A
2019: MOV CX,AX
201B: POP BX
201C: POP AX
201D: MUL BX
10
201F: DIV CX
2021: MOV [1004],AX
2025: HLT
Input
[1000]: 000CH
[1002]: 000FH
Output
[1004]: 003CH
Results
Program code has been executed and the output has been verified.
11
8086 Kit
Ex.No.6
14-08-24
Aim
Write an assembly level program to find factorial of two numbers
Algorithm
1. Start
2. Load contents of location [1500] to BX
3. Initialize AX to 0001H, and CX to 0001H
4. Compare CX and BX
5. If contents of CX is greater than contents of BX then go to Step 8 otherwise go to
Step 6
6. Multiply AX and CX and store the result in AX
7. Increment CX by 1 and go to Step 4
8. Store contents of AX to location [1504]
9. Stop
Source Code
2000: MOV BX,[1500]
2004: MOV AX,0001H
2008: MOV CX,0001H
200C: CMP CX,BX
200E: JG 2016
2010: MUL CX
2012: INC CX
2013: JMP 200C
2016: MOV [1504],AX
201A: HLT
Input
[1500]: 0005H
Output
[1504]: 0078H
12
Results
Program code has been executed and the output has been verified.
13
8086 Kit
Ex.No.7
14-08-24
Aim
Write an assembly level program to find square root of a number
Algorithm
1. Start
2. Initialize CX with 00H, BX with 01H, and AX with 00H
3. Add contents of BX to AX
4. Add 02H to BX
5. Increment CX by 1
6. If AX is less than or equal to the value at location [1000] then go to Step 3 otherwise
go to Step 7
7. Decrement CX by 1
8. Store contents of CX at location [1004]
9. Stop
Source Code
2000: MOV CX, 00H
2003: MOV BX, 01H
2006: MOV AX, 00H
2009: ADD AX, BX
200B: ADD BX, 02H
200D: INC CX
200F: CMP AX, [1000]
2011: JLE 2009
2013: DEC CX
2015: MOV [1004], CX
2018: HLT
Input
[1000]: 0040H
Output
[1004]: 0008H
14
Results
Program code has been executed and the output has been verified.
15
8086 Kit
Ex.No.8
14-08-24
Aim
Write an Assembly Level Program to convert a Binary number to its BCD equivalent
Algorithm
1. Start
2. Load AX with the input number from location [2000]
3. Initialize BX with 64H
4. Divide AX by BL and store the result in AX
5. Store the contents in AL at location [2500]
6. Move the contents of AH to AL
7. Load AH with 00H, and BL with 0AH
8. Divide AX by BL and store the result in AX
9. Store the contents of AL at location [2501] and that of AH at location [2502]
10. Stop
Source Code
1000: MOV AX.[2000]
1004: MOV BL, 64H
1007: DIV BL
1009: MOV [2500],AL
100D: MOV AL,AH
100F: MOV AH,00H
1012: MOV BL,0AH
1015: DIV BL
1017: MOV [2501],AL
101B: MOV [2502],AH
101F: HLT
Input
[2000]: 00FFH
16
Output
[2500]: 02H
[2501]: 05H
[2502]: 05H
Results
Program code has been executed and the output has been verified.
17
8086 Kit
Ex.No.9
22-08-24
Aim
Write an Assembly Level Program to convert a BCD number into its binary equivalent.
Algorithm
1. Start
2. Load AL with the leftmost byte of input number from location [2500]
3. Initialize CL with 64H
4. Multiply AX and CL, and store the results in AX
5. Move the contents of AX to DX
6. Load AL with the second byte of input number from location [2501]
7. Load CL with 0AH
8. Multiply AL and CL, and store the result in AX
9. Add the contents of AX and DX, and store the result in AX
10. Add the rightmost byte of input number at location [2502] to AX
11. Store the value in AX at location [2000]
12. Stop
Source Code
1000: MOV AL,[2500]
1004: MOV CL,64H
1007: MUL CL
1009: MOV DX,AX
100B: MOV AL,[2501]
100F: MOV CL,0AH
1012: MUL CL
1014: ADD AX,DX
1016: ADD AX,[2502]
101A: MOV [2700],AX
101E: HLT
Input
[2500]: 02H
[2501]: 05H
[2502]: 05H
18
Output
[2000]: 00FFH
Results
Program code has been executed and the output has been verified.
19
8086 Kit
Ex.No.10
22-08-24
Aim
Write an Assembly Level Program to convert a Hexadecimal number to its ASCII equiv-
alent.
Algorithm
1. Start
2. Load AL input byte from location [1001]
3. If the value in AL is greater than 09H then go to Step 5 otherwise go to Step 4
4. Add 30H to AL and go to Step 6
5. Add 5BH to AL
6. Store the value in AL at location [1002]
7. Stop
Source Code
2000: MOV AL,[1001]
2004: CMP AL,09H
2007: JG 200F
2009: ADD AL,30H
200C: JMP 2012
200F: ADD AL,5BH
2012: MOV [1002],AL
2016: HLT
Input
[1001]: 06H
Output
[1002]: 36H
Results
Program code has been executed and the output has been verified.
20
CYCLE 2
21
8086 Emulator
Ex.No.1
22-08-24
Aim
Write an Assembly Level Program to implement
1. Addition of two 16 bit numbers
Algorithm
1. Addition
1. Start
2. Load data segment into DS register
3. Load NUM1 into AX
4. ADD NUM2 to AX
5. Store the contents of AX in the RESULT variable
6. Stop
2. Subtraction
1. Start
2. Load data segment into DS register
3. Load NUM1 into AX
4. Subtract NUM2 from AX and store the result in AX
5. Store the contents of AX in the RESULT variable
6. Stop
Source Code
1. Addition
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DW 5678H
NUM2 DW 1234H
RESULT DW 1 DUP(?)
DATA ENDS
CODE SEGMENT
22
START:
MOV AX, DATA
MOV DS, AX
2. Subtraction
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1 DW 5678H
NUM2 DW 1234H
RESULT DW 1 DUP(?)
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
23
Output
Addition
Subtraction
Results
Program code has been executed and the output has been verified.
24
8086 Emulator
Ex.No.2
22-08-24
Aim
Write an Assembly Level Program to implement
Algorithm
1. Addition
1. Start
2. Initialize data segment with two 32-bit numbers with separate 16-bit parts.
3. Load data segment into DS register
4. Load AX with the lower byte of first number
5. Add lower byte of second number to AX
6. Store the contents of AX in the lower byte of result variable
7. Load AX with the higher byte of first number
8. Add higher byte of second number to AX with carry
9. Store the contents of AX in the higher byte of result variable
10. Stop
2. Subtraction
1. Start
2. Initialize data segment with two 32-bit numbers with separate 16-bit parts.
3. Load data segment into DS register
4. Load AX with lower byte of first number
5. Subtract lower byte of second number from AX
6. Store the contents of AX in the lower byte of result variable
7. Load AX with the higher byte of first number
8. Subtract higher byte of second number from AX with borrow
9. Store the contents of AX in the higher byte of result variable
10. Stop
25
Source Code
1. Addition
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1_LOW DW 1234H
NUM1_HIGH DW 5678H
NUM2_LOW DW 8765H
NUM2_HIGH DW 4321H
RES_LOW DW 1 DUP(?)
RES_HIGH DW 1 DUP(?)
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV AX, NUM1_LOW
ADD AX, NUM2_LOW
MOV RES_LOW, AX
END START
2. Subtraction
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM1_LOW DW 1234H
NUM1_HIGH DW 5678H
NUM2_LOW DW 8765H
NUM2_HIGH DW 4321H
RES_LOW DW 1 DUP(?)
RES_HIGH DW 1 DUP(?)
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
26
MOV AX, NUM1_LOW
SUB AX, NUM2_LOW
MOV RES_LOW, AX
END START
Output
Addition
27
Subtraction
Results
Program code has been executed and the output has been verified.
28
8086 Emulator
Ex.No.3
22-08-24
Aim
Write an Assembly Level Program to divide a 32-bit number by a 16-bit number.
Algorithm
1. Start
2. Initialize data segment with a 32-bit dividend with separate 16-bit parts and a
16-bit divisor
3. Load data segment into DS register
4. Load AX with lower byte of dividend
5. Load DX with higher byte of dividend
6. Load BX with divisor
7. Divide DX:AX by BX
8. Store the contents of AX and BX in quotient and remainder variables respectively
9. Stop
Source Code
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
NUM32_L DW 2345H
NUM32_H DW 0011H
NUM16 DW 00FFH
QUOTIENT DW 1 DUP(?)
REMAINDER DW 1 DUP(?)
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
29
MOV QUOTIENT, AX
MOV REMAINDER, DX
END START
Output
Results
Program code has been executed and the output has been verified.
30
8086 Emulator
Ex.No.4
22-08-24
Aim
Write an Assembly Level Program to convert a
Algorithm
Binary to BCD
1. Start
2. Initialize data segment with the input number
3. Load data segment into DS register
4. Load AX with the binary number
5. Load BL with 64H
6. Divide AX with BL
7. Store contents of AL in BCD1
8. Move contents of AH to AL
9. Load AH with 00H
10. Load BL with 0AH
11. Divide AX with BL
12. Store contents of AL, AH in BCD2, BCD3 respectively
13. Stop
BCD to Binary
1. Start
2. Initialize data segment with 3 1-byte BCD variables
3. Load data segment into DS register
4. Load AL with first BCD number
5. Load CL with 64H
6. Multiply AL and CL
7. Move contents of AX to DX
8. Load AL with second BCD number
9. Load CL with 0AH
10. Multiply AL and CL
31
11. Add DX to AX
12. Add third BCD number to AX and store the result in BIN variable
13. Stop
Source Code
Binary to BCD
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
BIN DW 0FFH
BCD1 DB ?
BCD2 DB ?
BCD3 DB ?
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
END START
BCD to Binary
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
BCD1 DB 03H
BCD2 DB 04H
BCD3 DB 04H
BIN DW ?
DATA ENDS
32
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
END START
Output
Binary to BCD
33
BCD to Binary
Results
Program code has been executed and the output has been verified.
34
8086 Emulator
Ex.No.5
04-09-24
Aim
Write an Assembly Level Program to compare two strings
Algorithm
1. Start
2. Initialize data segment with two strings
3. Load data segment into DS register
4. Load CX with length of string
5. Load address of first string in SI and that of second string in DI
6. Compare each character in both strings
7. If they are not equal go to step 9
8. Display ”Strings are equal” and go to step 10
9. Display ”Strings are not equal”
10. Stop
Source Code
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
STRING1 DB 'Hello'
STRING2 DB 'Hello'
MSG1 DB 'Strings are equal$'
MSG2 DB 'Strings are not equal$'
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV ES, AX
MOV CX, 5
LEA SI, STRING1
LEA DI, STRING2
REPE CMPSB
35
JNE NOT_EQUAL
DONE:
MOV AX, 4CH
INT 21H
CODE ENDS
END START
Output
Results
Program code has been executed and the output has been verified.
36
8086 Emulator
Ex.No.6
04-09-24
Aim
Write an Assembly Level Program to convert uppercase characters to lowercase in a string
Algorithm
1. Start
2. Initialize data segment with a string
3. Load data segment into DS register
4. Load address of string in SI
5. Load CX with length of string and clear direction flag
6. Load character at location SI to AL
7. If the character in AL is not in A-Z then go to step 9
8. Add 20H to AL
9. Store the contents of AL in SI and decrement CX by 1
10. Repeat steps 6 to 9 until CX becomes 0
11. Display the string
12. Stop
Source Code
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
STRING DB 'HELLO$', 0
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV ES, AX
CONVERT:
37
LODSB
CMP AL, 'A'
JB STORE
CMP AL, 'Z'
JA STORE
ADD AL, 20H
STORE:
STOSB
LOOP CONVERT
END START
Output
Results
Program code has been executed and the output has been verified.
38
8086 Emulator
Ex.No.7
04-09-24
Aim
Write an Assembly Level Program to find the number of vowels in a given string
Algorithm
1. Start
2. Initialize data segment with a string
3. Load data segment into DS register
4. Load address of string in SI
5. Clear direction flag
6. Load character at location SI to AL
7. If the character in AL is ’$’ then go to step 10
8. If the character in AL is one of ’a’, ’e’, ’i’, ’o’, ’u’, ’A’, ’E’, ’I’, ’O’, ’U’ then increment
count by 1
9. Go to step 7
10. Stop
Source Code
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
STRING DB 'Hello World$'
VOWEL_COUNT DB 0
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
COUNT_VOWELS:
LODSB
CMP AL, '$'
39
JE DONE
INCREMENT:
INC VOWEL_COUNT
JMP COUNT_VOWELS
DONE:
MOV AX, 4CH
INT 21H
CODE ENDS
END START
40
Output
Results
Program code has been executed and the output has been verified.
41
8086 Emulator
Ex.No.8
04-09-24
Aim
Write an Assembly Level Program to search for a substring in a given string
Algorithm
1. Start
2. Initialize data segment with two strings
3. Load data segment into DS and ES register
4. Load address of first string in SI and that of second string in DI
5. If the characters in SI and DI are not equal then go to step 9
6. Increment SI and DI by 1
7. If either SI or DI has reached the end of first and second string respectively then
go to step 12
8. Go to step 5
9. Increment SI by 1 and reset DI to starting of second string
10. If SI has not reached the end of first string then go to step 5
11. Display ”Substring not found” and go to step 13
12. Display ”Substring found”
13. Stop
Source Code
ASSUME CS:CODE, DS:DATA
PRINT MACRO ARG
LEA DX, ARG
MOV AH, 09H
INT 21H
ENDM
DATA SEGMENT
STR1 DB 'heil hitler$'
STR2 DB 'hi$'
MSG1 DB 'Substring found$'
MSG2 DB 'Substring not found$'
DATA ENDS
CODE SEGMENT
42
START:
MOV AX, DATA
MOV DS, AX
MOV ES, AX
LPCMP:
MOV AL, [SI]
MOV BL, [DI]
CMP AL, BL
JNE LPNE
INC SI
INC DI
CMP [SI], '$'
JE FOUND
CMP [DI], '$'
JE FOUND
JMP LPCMP
LPNE:
INC SI
LEA DI, STR2
CMP [SI], '$'
JNE LPCMP
PRINT MSG2
JMP DONE
FOUND:
PRINT MSG1
DONE:
MOV AH, 4CH
INT 21H
CODE ENDS
END START
43
Output
Results
Program code has been executed and the output has been verified.
44
8086 Emulator
Ex.No.9
04-09-24
Aim
Write an Assembly Level Program to implement bubble sort algorithm
Algorithm
1. Start
2. Initialize data segment with the array and its length
3. Load data segment into the DS register
4. Load the length of the array into CX register
5. Set SI to the offset of the first element of the array
6. Move contents of CX to BX and decrement BX by 1
7. Compare the elements at addresses SI and SI+2
8. If the first element is smaller or equal to the second, go to step 10
9. Swap the two elements by moving the second element to SI and the first to SI+2
10. Increment SI by 2 and decrement BX by 1
11. If BX is not zero, go back to step 7
12. Decrement CX and if CX is greater than 1, go back to step 5
13. Set SI to the first element of the sorted array
14. Convert the first element to ASCII, display it, and print a space
15. Increment SI by 2 and decrement CX by 1
16. If CX is not zero, go back to step 15
17. Stop
Source Code
ASSUME CS:CODE, DS:DATA
PRINT MACRO ARG
MOV DL, ARG
MOV AH, 02H
INT 21H
ENDM
DATA SEGMENT
ARRAY DW 53, 64, 22, 83, 4
LENGTH DW 5
MSG DB 'Sorted Array: $'
45
NEWLINE DB 0DH, 0AH, '$'
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV CX, LENGTH
OUTER_LOOP:
MOV SI, OFFSET ARRAY
MOV BX, CX
DEC BX
INNER_LOOP:
MOV AX, [SI]
MOV DX, [SI + 2]
CMP AX, DX
JBE NO_SWAP
MOV [SI], DX
MOV [SI + 2], AX
NO_SWAP:
ADD SI, 2
DEC BX
JNZ INNER_LOOP
DEC CX
CMP CX, 1
JNZ OUTER_LOOP
DISPLAY:
MOV SI, OFFSET ARRAY
MOV CX, LENGTH
DISPLAY_LOOP:
MOV AX, [SI]
AAM
ADD AX, 3030H
MOV BX, AX
PRINT BH
PRINT BL
PRINT ' '
46
ADD SI, 2
LOOP DISPLAY_LOOP
DONE:
MOV AH, 4CH
INT 21H
CODE ENDS
END START
Output
Results
Program code has been executed and the output has been verified.
47
8086 Emulator
Ex.No.10
04-09-24
Aim
Write an Assembly Level Program to find the second largest element in an array
Algorithm
1. Start
2. Initialize data segment with the array and its length
3. Load data segment into the DS register
4. Load the length of the array into CX register
5. Set SI to the offset of the first element of the array
6. Move contents of CX to BX and decrement BX by 1
7. Compare the elements at addresses SI and SI+2
8. If the first element is greater than the second, go to step 10
9. Swap the two elements by moving the second element to SI and the first to SI+2
10. Increment SI by 2 and decrement BX by 1
11. If BX is not zero, go back to step 7
12. Decrement CX and if CX is greater than 1, go back to step 5
13. Set SI to the second element of the sorted array
14. Convert the element to ASCII and display it
15. Stop
Source Code
ASSUME CS:CODE, DS:DATA
DATA SEGMENT
ARRAY DW 1, 8, 2, 5, 9
LENGTH DW 5
MSG DB 'Second Largest element: $'
DATA ENDS
CODE SEGMENT
START:
MOV AX, DATA
MOV DS, AX
MOV CX, LENGTH
48
OUTER_LOOP:
MOV SI, OFFSET ARRAY
MOV BX, CX
DEC BX
INNER_LOOP:
MOV AX, [SI]
MOV DX, [SI + 2]
CMP AX, DX
JG NO_SWAP
MOV [SI], DX
MOV [SI + 2], AX
NO_SWAP:
ADD SI, 2
DEC BX
JNZ INNER_LOOP
DEC CX
CMP CX, 1
JG OUTER_LOOP
DISPLAY:
MOV SI, OFFSET ARRAY
MOV DX, OFFSET MSG
MOV AH, 09H
INT 21H
DONE:
MOV AH, 4CH
INT 21H
CODE ENDS
END START
49
Output
Results
Program code has been executed and the output has been verified.
50
CYCLE 3
51
8051 Kit
Ex.No.1
26-09-24
Aim
Write a program to add two 8-bit numbers
Algorithm
1. Start
2. Load DPTR with address of first input number
3. Load register A with contents at location DPTR
4. Move contents of A to F0
5. Increment DPTR by 1
6. Load register A with contents at location DPTR
7. Add the contents of A and F0
8. Increment DPTR by 1
9. Store contents of A at location DPTR
10. Stop
Source Code
8000: MOV DPTR,#8500
8003: MOVX A,@DPTR
8004: MOV F0,A
8006: INC DPTR
8007: MOVX A,@DPTR
8008: ADD A,F0
8009: INC DPTR
800A: MOVX @DPTR,A
800B: SJMP 800B
Input
[8500]: 05
[8501]: 06
Output
[8502]: 0B
52
Results
Program code has been executed and the output has been verified.
53
8051 Kit
Ex.No.2
26-09-24
Aim
Write a program to subtract two 8-bit numbers
Algorithm
1. Start
2. Load DPTR with address of first input number
3. Load register A with contents at location DPTR
4. Move contents of A to F0
5. Increment DPTR by 1
6. Load register A with contents at location DPTR
7. Subtract the contents of F0 from A
8. Increment DPTR by 1
9. Store contents of A at location DPTR
10. Stop
Source Code
8000: MOV DPTR,#8500
8003: MOVX A,@DPTR
8004: MOV F0,A
8006: INC DPTR
8007: MOVX A,@DPTR
8008: SUBB A,F0
8009: INC DPTR
800A: MOVX @DPTR,A
800B: SJMP 800B
Input
[8500]: 05
[8501]: 06
Output
[8502]: 01
54
Results
Program code has been executed and the output has been verified.
55
8051 Kit
Ex.No.3
26-09-24
Aim
Write a program to divide two 8-bit numbers
Algorithm
1. Start
2. Load DPTR with address of the divisor
3. Load register A with contents at location DPTR
4. Move contents of A to F0
5. Increment DPTR by 1
6. Load register A with contents at location DPTR
7. Divide the contents of A with F0
8. Increment DPTR by 1
9. Store contents of A at location DPTR
10. Increment DPTR by 1
11. Move contents of F0 to A
12. Store contents of A at location DPTR
13. Stop
Source Code
8000: MOV DPTR,#8500
8003: MOVX A,@DPTR
8004: MOV F0,A
8006: INC DPTR
8007: MOVX A,@DPTR
8008: DIV AB
8009: INC DPTR
800A: MOVX @DPTR,A
800B: INC DPTR
800C: MOV A,F0
800E: MOVX @DPTR,A
800F: SJMP 800F
Input
[8500]: 03
[8501]: 09
56
Output
[8502]: 03
[8503]: 00
Results
Program code has been executed and the output has been verified.
57
8051 Kit
Ex.No.4
26-09-24
Aim
Write a program to multiply two 8-bit numbers
Algorithm
1. Start
2. Load DPTR with address of first input number
3. Load register A with contents at location DPTR
4. Move contents of A to F0
5. Increment DPTR by 1
6. Load register A with contents at location DPTR
7. Multiply the contents of A and F0
8. Increment DPTR by 1
9. Store contents of A at location DPTR
10. Increment DPTR by 1
11. Move contents of F0 to A
12. Store contents of A at location DPTR
13. Stop
Source Code
8000: MOV DPTR,#8500
8003: MOVX A,@DPTR
8004: MOV F0,A
8006: INC DPTR
8007: MOVX A,@DPTR
8008: MUL AB
8009: INC DPTR
800A: MOVX @DPTR,A
800B: INC DPTR
800C: MOV A,F0
800E: MOVX @DPTR,A
800F: SJMP 800F
Input
[8500]: 14
[8501]: 14
58
Output
[8502]: 90
[8503]: 01
Results
Program code has been executed and the output has been verified.
59
8051 Kit
Ex.No.5
26-09-24
Aim
Write a program to check whether a given number is positive or negative
Algorithm
1. Start
2. Load DPTR with address of input number
3. Load register A with contents at location DPTR
4. Rotate contents of A to the right through carry
5. If carry flag is not set then go to step 10
6. Increment DPTR by 1
7. Load F0 with the value 01H
8. Move contents of F0 to A
9. Store contents of A at location DPTR
10. Stop
Source Code
8000: MOV DPTR,#8500
8003: MOVX A,@DPTR
8004: RLC A
8005: JNC 800E
8007: INC DPTR
8008: MOV F0,#01
800A: MOV A,F0
800C: MOVX @DPTR,A
800E: SJMP 800E
Input
[8500]: 85
Output
[8501]: 01
60
Results
Program code has been executed and the output has been verified.
61
CYCLE 4
62
Pass 1 - Two Pass Assembler
Ex.No.1
16-10-24
Aim
Write a C program to implement the pass 1 algorithm of a two pass assembler
Algorithm
begin
read first input line
if OPCODE = 'START' then
begin
save #(OPERAND) as starting address
initialize LOCCTR to starting address
write line to intermediate file
read next input line
end {if START}
else
initialize LOCCTR to 0
while OPCODE != 'END' do
begin
if this is not a comment line then
begin
if there is a symbol in the LABEL field then
begin
search SYMTAB for LABEL
if found then
set error flag (duplicate symbol)
else
insert (LABEL, LOCCTR) into SYMTAB
end {if symbol}
search OPTAB for OPCODE
if found then
add 3 (instruction length) to LOCCTR
else if OPCODE = 'WORD' then
add 3 to LOCCTR
else if OPCODE = 'RESW' then
add 3 * #(OPERAND) to LOCCTR
else if OPCODE = 'RESB' then
add #(OPERAND) to LOCCTR
else if OPCODE = 'BYTE' then
begin
63
find length of constant in bytes
add length to LOCCTR
end {if BYTE}
else
set error flag (invalid operation code)
write line to intermediate file
end {if not a comment}
read next input line
end {while not END}
write last line to intermediate file
save (LOCCTR - starting address) as program length
end {Pass 1}
Source Code
1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < string .h >
4
5 void display ( char *) ;
6
7 int main ( void )
8 {
9 char * out = " intermediate . txt " , * src = " source . txt " , * optab = "
optab . txt " , * symtab = " symtab . txt " ;
10 char operand [20] , opcode [20] , label [20] , optabcode [20] , code
[20];
11 int locctr , length , start ;
12 FILE * f1 , * f2 , * f3 , * f4 , * f5 ;
13 // input
14 f1 = fopen ( src , " r " ) ;
15 f2 = fopen ( optab , " r " ) ;
16 // output
17 f3 = fopen ( symtab , " w " ) ;
18 f4 = fopen ( out , " w " ) ;
19
20 fscanf ( f1 , " % s \ t % s \ t % s " , label , opcode , operand ) ;
21
22 if ( strcmp ( opcode , " START " ) == 0)
23 {
24 locctr = atoi ( operand ) ;
25 start = locctr ;
26 fprintf ( f4 , " \ t % s \ t % s \ t % s \ n " , label , opcode , operand ) ;
27 }
28 else
29 locctr = 0;
30 fscanf ( f1 , " % s \ t % s \ t % s " , label , opcode , operand ) ;
31 while ( strcmp ( opcode , " END " ) != 0)
32 {
33 fprintf ( f4 , " % d \ t % s \ t % s \ t % s \ n " , locctr , label , opcode ,
operand ) ;
34 if ( strcmp ( label , " ** " ) != 0)
35 fprintf ( f3 , " % s \ t % d \ n " , label , locctr ) ;
36 rewind ( f2 ) ;
64
37 while (! feof ( f2 ) )
38 {
39 fscanf ( f2 , " % s \ t % s " , optabcode , code ) ;
40 if ( strcmp ( opcode , optabcode ) == 0)
41 {
42 locctr += 3;
43 break ;
44 }
45 }
46 if ( strcmp ( opcode , " BYTE " ) == 0)
47 locctr += 1;
48 else if ( strcmp ( opcode , " WORD " ) == 0)
49 locctr += 3;
50 else if ( strcmp ( opcode , " RESB " ) == 0)
51 locctr += atoi ( operand ) ;
52 else if ( strcmp ( opcode , " RESW " ) == 0)
53 locctr += 3 * atoi ( operand ) ;
54 fscanf ( f1 , " % s \ t % s \ t % s " , label , opcode , operand ) ;
55 }
56 fprintf ( f4 , " % d \ t % s \ t % s \ t % s " , locctr , label , opcode , operand ) ;
57 fclose ( f1 ) ;
58 fclose ( f2 ) ;
59 fclose ( f3 ) ;
60 fclose ( f4 ) ;
61 display ( out ) ;
62 printf ( " \ nLength of intermediate file is : % d \ n " , locctr - start
);
63 }
64
65 void display ( char * file )
66 {
67 FILE * fp = fopen ( file , " r " ) ;
68 char ch ;
69 while (( ch = fgetc ( fp ) ) != EOF )
70 printf ( " % c " , ch ) ;
71 fclose ( fp ) ;
72 }
Input
Source file
FIRST START 2000
** LDA FIVE
** STA ALPHA
** LDCH CHARZ
** STCH C1
ALPHA RESW 2
FIVE WORD 5
CHARZ BYTE C’Z’
C1 RESB 1
** END START
65
Optab
LDA 00
STA 01
LDCH 02
STCH 03
ADD 10
Output
Length of intermediate file is: 23
Intermediate file
FIRST START 2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 2
2018 FIVE WORD 5
2021 CHARZ BYTE C’Z’
2022 C1 RESB 1
2023 ** END START
Symtab
ALPHA 2012
FIVE 2018
CHARZ 2021
C1 2022
Results
Program code has been executed and the output has been verified.
66
Pass 2 - Two Pass Assembler
Ex.No.2
16-10-24
Aim
Write a C program to implement the pass 2 algorithm of a two pass assembler
Algorithm
begin
read first input line {from intermediate file}
if OPCODE = 'START' then
begin
write listing line
read next input line
end {if START}
write Header record to object program
initialize first Text record
while OPCODE != 'END' do
begin
if this is not a comment line then
begin
search OPTAB for OPCODE
if found then
begin
if there is a symbol in OPERAND field then
begin
search SYMTAB for OPERAND
if found then
store symbol value as operand address
else
begin
store 0 as operand address
set error flag (undefined symbol)
end {if not found}
end {if symbol}
else
store 0 as operand address
assemble the object code instruction
end {if opcode found}
else if OPCODE = 'BYTE' or 'WORD' then
convert constant to object code
if object code will not fit into the current Text record then
67
begin
write Text record to object program
initialize new Text record
end {if object code}
add object code to Text record
end {if not comment}
write listing line
read next input line
end {while not END}
write last Text record to object program
write End record to object program
write last listing line
end {Pass 2}
Source Code
1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < string .h >
4
5 void display () ;
6 char * intToHex ( int , char * , int ) ;
7
8 int main ()
9 {
10 char a [10] , ad [10] , label [10] , opcode [10] , operand [10] , symbol [10] ,
hexAddress [10];
11 int start , diff , i , address , add , len , actual_len , finaddr ,
prevaddr , j = 0;
12 char mnemonic [15][15] = { " LDA " , " STA " , " LDCH " , " STCH " };
13 char code [15][15] = { " 33 " , " 44 " , " 53 " , " 57 " };
14 char * src = " intermediate . txt " , * symtab = " symtab . txt " , * out = "
output . txt " , * objc = " objcode . txt " ;
15
68
36 {
37 fprintf ( fp1 , " \ t % s \ t % s \ t % s \ n " , label , opcode , operand ) ;
38 fprintf ( fp4 , " H ˆ% s ˆ%06 X ˆ%06 X \ n " , label , strtol ( operand , NULL ,
16) , finaddr ) ;
39 fscanf ( fp3 , " % X % s % s % s " , & address , label , opcode , operand ) ;
40 start = address ;
41 diff = prevaddr - start ;
42 fprintf ( fp4 , " T ˆ%06 X ˆ%02 X " , address , diff ) ;
43 }
44
45 while ( strcmp ( opcode , " END " ) != 0)
46 {
47 intToHex ( address , hexAddress , 4) ;
48 if ( strcmp ( opcode , " BYTE " ) == 0)
49 {
50 fprintf ( fp1 , " % s \ t % s \ t % s \ t % s \ t " , hexAddress , label , opcode ,
operand ) ;
51 len = strlen ( operand ) ;
52 actual_len = len - 3;
53 fprintf ( fp4 , " ˆ " ) ;
54 for ( i = 2; i < ( actual_len + 2) ; i ++)
55 {
56 fprintf ( fp1 , " %02 X " , operand [ i ]) ;
57 fprintf ( fp4 , " %02 X " , operand [ i ]) ;
58 }
59 fprintf ( fp1 , " \ n " ) ;
60 }
61 else if ( strcmp ( opcode , " WORD " ) == 0)
62 {
63 len = strlen ( operand ) ;
64 fprintf ( fp1 , " % s \ t % s \ t % s \ t % s \ t %06 X \ n " , hexAddress , label ,
opcode , operand , atoi ( operand ) ) ;
65 fprintf ( fp4 , " ˆ%06 X " , atoi ( operand ) ) ;
66 }
67 else if (( strcmp ( opcode , " RESB " ) == 0) || ( strcmp ( opcode , " RESW
" ) == 0) )
68 fprintf ( fp1 , " % s \ t % s \ t % s \ t % s \ n " , hexAddress , label , opcode ,
operand ) ;
69 else
70 {
71 while ( strcmp ( opcode , mnemonic [ j ]) != 0)
72 j ++;
73 if ( strcmp ( operand , " COPY " ) == 0)
74 fprintf ( fp1 , " % s \ t % s \ t % s \ t % s \ t % s0000 \ n " , hexAddress ,
label , opcode , operand , code [ j ]) ;
75 else
76 {
77 rewind ( fp2 ) ;
78 fscanf ( fp2 , " % s % X " , symbol , & add ) ;
79 while ( strcmp ( operand , symbol ) != 0)
80 fscanf ( fp2 , " % s % X " , symbol , & add ) ;
81 fprintf ( fp1 , " % s \ t % s \ t % s \ t % s \ t % s %04 X \ n " , hexAddress ,
label , opcode , operand , code [ j ] , add ) ;
82 fprintf ( fp4 , " ˆ% s %04 X " , code [ j ] , add ) ;
83 }
84 }
85 fscanf ( fp3 , " % X % s % s % s " , & address , label , opcode , operand ) ;
86 }
69
87 intToHex ( address , hexAddress , 4) ;
88 fprintf ( fp1 , " % s \ t % s \ t % s \ t % s \ n " , hexAddress , label , opcode , operand
);
89 fprintf ( fp4 , " \ nE ˆ%06 X " , start ) ;
90 fclose ( fp4 ) ;
91 fclose ( fp3 ) ;
92 fclose ( fp2 ) ;
93 fclose ( fp1 ) ;
94 printf ( " Output : \ n " ) ;
95 display ( out ) ;
96 printf ( " Object Program : \ n " ) ;
97 display ( objc ) ;
98
99 return 0;
100 }
101
102 char * intToHex ( int num , char * str , int width ) {
103 sprintf ( str , " %0* X " , width , num ) ;
104 return str ;
105 }
106
107 void display ( char * file )
108 {
109 FILE * fp = fopen ( file , " r " ) ;
110 char ch ;
111 while (( ch = fgetc ( fp ) ) != EOF )
112 printf ( " % c " , ch ) ;
113 fclose ( fp ) ;
114 printf ( " \ n " ) ;
115 }
Input
Intermediate file
FIRST START 2000
2000 ** LDA FIVE
2003 ** STA ALPHA
2006 ** LDCH CHARZ
2009 ** STCH C1
2012 ALPHA RESW 2
2018 FIVE WORD 5
2021 CHARZ BYTE C’Z’
2022 C1 RESB 1
2023 ** END START
Symtab
ALPHA 2012
FIVE 2018
CHARZ 2021
C1 2022
70
Output
Output file
FIRST START 2000
2000 ** LDA FIVE 332018
2003 ** STA ALPHA 442012
2006 ** LDCH CHARZ 532021
2009 ** STCH C1 572022
2012 ALPHA RESW 2
2018 FIVE WORD 5 000005
2021 CHARZ BYTE C‘Z’ 5A
2022 C1 RESB 1
2023 ** END START
Object Program
HˆFIRSTˆ002000ˆ002023
Tˆ002000ˆ22ˆ332018ˆ442012ˆ532021ˆ572022ˆ000005ˆ5A
Eˆ002000
Results
Program code has been executed and the output has been verified.
71
Single Pass Assembler
Ex.No.3
23-10-24
Aim
Write a C program to implement a single pass assembler
Algorithm
begin
Read first input line
if OPCODE = 'START' then
begin
Save #(OPERAND) as starting address
Initialize LOCCTR to starting address
Read next input line
end {if START}
else
Initialize LOCCTR to 0
while OPCODE != 'END' do
begin
if this is not a comment line then
begin
if there is a symbol in the LABEL field then
begin
Search SYMTAB for LABEL
if found then
begin
if symbol value is null then
begin
Set symbol value as LOCCTR
Search the linked list with the operand addresses
as corresponding symbol value
end {if null}
Store symbol value as LOCCTR in SYMTAB
Delete the linked list
end {if found}
else
Insert (LABEL, LOCCTR) into SYMTAB
end {if symbol}
Search OPTAB for OPCODE
if found then
begin
72
Search SYMTAB for operand address
if found then
begin
if symbol value is not equal to null then
Store symbol value as operand address
else
Insert node at the end of the linked list with address
as LOCCTR
end {if found SYMTAB}
else
Insert (symbol, name, null)
add 3 to LOCCTR
end {if found OPTAB}
else if OPCODE = 'WORD' then
Add 3 to LOCCTR and convert constant to object code
else if OPCODE = 'RESW' then
Add 3 * #(OPERAND) to LOCCTR
else if OPCODE = 'RESB' then
Add #(OPERAND) to LOCCTR
else if OPCODE = 'BYTE' then
begin
Find length of constant in bytes
Add length to LOCCTR
Convert constant to object code
end {if BYTE}
if object code will not fit in current text record then
begin
Write text record to object program
Initialize new text record
end
Add object code to text record
end {if not a comment}
Write listing line
Read next input line
end {while not END}
Write last text record to object program
Write end record to object program
Write last listing line
end {Pass}
Source Code
1 # include < stdio .h >
2 # include < stdlib .h >
3 # include < string .h >
4
5 void main () {
6 FILE * f1 , * f2 , * f3 , * f4 , * f5 ;
73
7 int lc , sa , i = 0 , j = 0 , m [10] , pgmlen , len , k , len1 , l = 0;
8 char name [10] , opnd [10] , la [10] , mne [10] , s1 [10] , mne1 [10] , opnd1
[10];
9 char lcs [10] , ms [10];
10 char sym [10] , symaddr [10] , obj1 [10] , obj2 [10] , s2 [10] , q [10] , s3
[10];
11 f1 = fopen ( " input . txt " , " r " ) ;
12 f2 = fopen ( " optab . txt " , " r " ) ;
13 f3 = fopen ( " symtab . txt " , " w + " ) ;
14 f4 = fopen ( " symtab1 . txt " , " w + " ) ;
15 f5 = fopen ( " output . txt " , " w + " ) ;
16 fscanf ( f1 , " % s % s % s " , la , mne , opnd ) ;
17 if ( strcmp ( mne , " START " ) == 0) {
18 sa = atoi ( opnd ) ;
19 strcpy ( name , la ) ;
20 lc = sa ;
21 }
22 strcpy ( s1 , " * " ) ;
23 fscanf ( f1 , " % s % s % s " , la , mne , opnd ) ;
24 while ( strcmp ( mne , " END " ) != 0)
25 {
26 if ( strcmp ( la , " -" ) == 0)
27 {
28 fscanf ( f2 , " % s % s " , mne1 , opnd1 ) ;
29 while (! feof ( f2 ) )
30 {
31 if ( strcmp ( mne1 , mne ) == 0)
32 {
33 m [ i ] = lc + 1;
34 fprintf ( f3 , " % s \ t % s \ n " , opnd , s1 ) ;
35 fprintf ( f5 , " % s \ t0000 \ n " , opnd1 ) ;
36 lc = lc + 3;
37 i = i + 1;
38 break ;
39 }
40 else
41 fscanf ( f2 , " % s % s " , mne1 , opnd1 ) ;
42 }
43 }
44 else
45 {
46 fseek ( f3 , SEEK_SET , 0) ;
47 fscanf ( f3 , " % s % s " , sym , symaddr ) ;
48 while (! feof ( f3 ) )
49 {
50 if ( strcmp ( sym , la ) == 0)
51 {
52 sprintf ( lcs , " % d " , lc ) ;
53 fprintf ( f4 , " % s \ t % s \ n " , la , lcs ) ;
54 sprintf ( ms , " % d " , m [ j ]) ;
55 j = j + 1;
56 fprintf ( f5 , " % s \ t % s \ n " , ms , lcs ) ;
57 i = i + 1;
58 break ;
59 }
60 else
61 fscanf ( f3 , " % s % s " , sym , symaddr ) ;
62 }
74
63 if ( strcmp ( mne , " RESW " ) == 0)
64 lc = lc + 3 * atoi ( opnd ) ;
65 else if ( strcmp ( mne , " BYTE " ) == 0)
66 {
67 strcpy ( s2 , " -" ) ;
68 len = strlen ( opnd ) ;
69 lc = lc + len - 2;
70 for ( k = 2; k < len ; k ++)
71 {
72 q [ l ] = opnd [ k ];
73 l = l + 1;
74 }
75 fprintf ( f5 , " % s \ t % s \ n " , q , s2 ) ;
76 break ;
77 }
78 else if ( strcmp ( mne , " RESB " ) == 0)
79 lc = lc + atoi ( opnd ) ;
80 else if ( strcmp ( mne , " WORD " ) == 0)
81 {
82 strcpy ( s3 , " # " ) ;
83 lc = lc + 3;
84 fprintf ( f5 , " % s \ t % s \ n " , opnd , s3 ) ;
85 break ;
86 }
87 }
88 fseek ( f2 , SEEK_SET , 0) ;
89 fscanf ( f1 , " % s % s % s " , la , mne , opnd ) ;
90 }
91 fseek ( f5 , SEEK_SET , 0) ;
92 pgmlen = lc - sa ;
93 printf ( " H ˆ% s ˆ% d ˆ0% x \ n " , name , sa , pgmlen ) ;
94 printf ( " T ˆ " ) ;
95 printf ( " 00% d ˆ0% x " , sa , pgmlen ) ;
96 fscanf ( f5 , " % s % s " , obj1 , obj2 ) ;
97 while (! feof ( f5 ) )
98 {
99 if ( strcmp ( obj2 , " 0000 " ) == 0)
100 printf ( " ˆ% s % s " , obj1 , obj2 ) ;
101 else if ( strcmp ( obj2 , " -" ) == 0)
102 {
103 printf ( " ˆ " ) ;
104 len1 = strlen ( obj1 ) ;
105 for ( k = 0; k < len1 ; k ++)
106 printf ( " % d " , obj1 [ k ]) ;
107 }
108 else if ( strcmp ( obj2 , " # " ) == 0)
109 {
110 printf ( " ˆ " ) ;
111 printf ( " % s " , obj1 ) ;
112 }
113 fscanf ( f5 , " % s % s " , obj1 , obj2 ) ;
114 }
115 fseek ( f5 , SEEK_SET , 0) ;
116 fscanf ( f5 , " % s % s " , obj1 , obj2 ) ;
117 while (! feof ( f5 ) )
118 {
119 if ( strcmp ( obj2 , " 0000 " ) != 0)
120 {
75
121 if ( strcmp ( obj2 , " -" ) != 0)
122 {
123 if ( strcmp ( obj2 , " # " ) != 0)
124 {
125 printf ( " \ n " ) ;
126 printf ( " T ˆ% s ˆ02ˆ% s " , obj1 , obj2 ) ;
127 }
128 }
129 }
130 fscanf ( f5 , " % s % s " , obj1 , obj2 ) ;
131 }
132 printf ( " \ nE ˆ00% d \ n " , sa ) ;
133 }
Input
Source
COPY START 1000
- LDA ALPHA
- STA BETA
ALPHA RESW 1
BETA RESW 1
- END -
Optab
LDA 00
STA 23
LDCH 15
STCH 18
Output
Object Program
HˆCOPYˆ1000ˆ0C
Tˆ001000ˆ0Cˆ000000ˆ230000
Tˆ1001ˆ02ˆ1006
Tˆ1004ˆ02ˆ1009
Eˆ001000
Output file
00 0000
23 0000
1001 1006
1004 1009
76
Symtab
ALPHA 1006
BETA 1009
Results
Program code has been executed and the output has been verified.
77
Relocating Loader
Ex.No.4
23-10-24
Aim
Write a C program to implement a relocating loader
Algorithm
begin
Read Header record
Verify program name and length
Input the address where the program is to be relocated
Calculate the effective address as (relocation address - start address)
Read first text record
while record type != `E' do
begin
if record type = `T' then
begin
if object code is in character form then
Convert it into internal representation
Move the object code from text record to specified location in memory
end {if `T'}
else if record type = `M'
begin
Read the location to be modified and length in half bytes
Add effective address to the value at location specified
end {if `M'}
Read next record from object program
end {while `E'}
Jump to address specified in end record
end {Pass}
Source Code
1 # include < stdio .h >
2 # include < string .h >
3 # include < stdlib .h >
4
5 void main () {
6 char add [6] , length [10] , input [10] , binary [12] , bitmask [12] ,
relocbit ;
78
7 int start , inp , len , i , address , opcode , addr , actualadd ;
8 FILE * fp1 , * fp2 ;
9 printf ( " Enter the actual starting address : " ) ;
10 scanf ( " % d " , & start ) ;
11 fp1 = fopen ( " input . txt " , " r " ) ;
12 fp2 = fopen ( " reloutput . dat " , " w " ) ;
13 fscanf ( fp1 , " % s " , input ) ;
14 while ( strcmp ( input , " E " ) != 0)
15 {
16 if ( strcmp ( input , " H " ) == 0)
17 {
18 fscanf ( fp1 , " % s " , add ) ;
19 fscanf ( fp1 , " % s " , length ) ;
20 fscanf ( fp1 , " % s " , input ) ;
21 }
22 if ( strcmp ( input , " T " ) == 0)
23 {
24 fscanf ( fp1 , " % d " , & address ) ;
25 fscanf ( fp1 , " % s " , bitmask ) ;
26 address += start ;
27 len = strlen ( bitmask ) ;
28 for ( i = 0; i < len ; i ++)
29 {
30 fscanf ( fp1 , " % d " , & opcode ) ;
31 fscanf ( fp1 , " % d " , & addr ) ;
32 relocbit = bitmask [ i ];
33 if ( relocbit == '0 ')
34 actualadd = addr ;
35 else
36 actualadd = addr + start ;
37 fprintf ( fp2 , " % d \ t % d % d \ n " , address , opcode , actualadd ) ;
38 address += 3;
39 }
40 fscanf ( fp1 , " % s " , input ) ;
41 }
42 }
43 fclose ( fp1 ) ;
44 fclose ( fp2 ) ;
45 printf ( " FINISHED " ) ;
46 }
Input
Enter the actual starting address: 2000
Source file
H 1000 200
T 1000 11001 14 1033 48 1039 90 1776 92 1765 57 1765
T 2011 11110 23 1838 43 1979 89 1060 66 1849 99 1477
E 1000
79
Output
FINISHED
Output file
3000 143033
3003 483039
3006 901776
3009 921765
3012 573765
4011 233838
4014 433979
4017 893060
4020 663849
4023 991477
Results
Program code has been executed and the output has been verified.
80
CYCLE 5
81
CPU Scheduling Algorithms
Ex.No.1
30-10-24
Aim
To write a menu driven program for implementing the following CPU scheduling algo-
rithms.
(a) FCFS (b) Round Robin (c) SJF (d) Priority
Structure ‘process’ is used to hold each process and it consists of process ID (pid), arrival
time, burst time, priority, turnaround time, waiting time, and completion time.
Structure ’pstat’ is used to store each execution instance of process and it consists of
process ID, arrival time, and burst time.
Algorithm
MAIN
1. Start
2. Read number of processes (n) and set c ← 1
3. Repeat Step 4 ‘n’ times
4. Read process information (id, arrival, and burst) and store it in the process array
5. Sort the array in ascending of process arrival time
6. Repeat Steps 7 to 18 while c = 1
7. Read choice (FCFS, Round Robin, SJF, Priority)
8. If choice = 0 then go to Step 9 otherwise go to Step 10
9. Set c ← 0
10. if choice = 1 then go to Step 11 otherwise go to Step 12
11. Call FCFS() function
12. if choice = 2 then go to Step 13 otherwise go to Step 15
13. Read time quantum ’tq’
14. Call RoundRobin() function
15. if choice = 3 then go to Step 16 otherwise go to Step 17
16. Call SJF() function
17. if choice = 4 then go to Step 18 otherwise go to Step 19
18. Call Priority() function
19. Display ”Invalid choice” and go to Step 6
[End of Repeat (Step 6)]
20. Stop
82
FCFS
1. Start
2. Set t ← 0
3. Repeat Steps 4 to 9 for each process in process array
4. If t ≥ arrival time of process then go to Step 5 otherwise go to Step 9
5. Copy pid, start, burst time of process to pstat array
6. Add burst time of process to t
7. Set completion time of process to t
8. Increment count by 1 and go to Step 3
9. Increment t by 1
[End of Repeat (Step 3)]
10. Display Gantt chart by calling Gantt() function
11. Call Display() function to display process information
12. Stop
Round Robin
1. Start
2. Set t ← minimum arrival time among the processes, count ← 0, gc ← 0
3. For each process in process array repeat Steps 4 and 5
4. Set remaining time of each process to its burst time
5. If t = arrival time of process then enqueue process into ready queue
6. Repeat Steps 7 to 26 while count ̸= n
7. Dequeue a process from the ready queue into curr process
8. If curr process is empty then go to Step 9 otherwise go to Step 23
9. Copy pid of curr process to pstat array at index ‘gc’
10. Set start time of process in pstat array to ‘t’
11. If remaining time of curr process > time quantum then go to Step 12 otherwise go
to Step 19
12. Set burst time of process in pstat array to time quantum
13. Add time quantum to ‘t’
14. Reduce time quantum from remaining time of curr process
15. Repeat Steps 16 to 17 for the ‘n’ processes in process array
16. If arrival time of process ≥ t, remaining time of process > 0, and this process is not
in curr process and is not already in the ready queue then go to Step 17 otherwise
go to Step 15
17. Enqueue the process and go to Step 15
[End of Repeat (Step 15)]
18. Enqueue curr process into the ready queue and go to Step 22
19. Set burst time of process at index gc in pstat array to remaining time of curr process
20. Add remaining time of curr process to t, set completion time of curr process to t,
and its remaining time to 0
21. Increment Count by 1
22. Increment gc by 1 and go to Step 6
83
23. Repeat Steps 24 to 26 while ready queue is empty
24. For each process in process array repeat Step 25
25. If arrival time of process ≤ t and remaining time > 0 then enqueue process to the
ready queue
26. If queue is empty, then increment ’t‘ by 1 and go to Step 6
[End of Repeat (Step 23)]
[End of Repeat (Step 6)]
27. Display Gantt chart using Gantt() function
28. Call Display() function to display process scheduling information
29. Stop
SJF
1. Start
2. Set count ← 0, t ← 0
3. Set remaining time of each process in process array to its burst time
4. Repeat Steps 5 to 13 while count = n
5. Set curr process as empty, and minBurstTime as 9999
6. For each process in process array repeat Steps 7 and 8
7. If arrival time of process ≤ t, remaining time > 0, and remaining time < minBurst-
Time then go to Step 8 otherwise go to Step 6
8. Set minBurstTime as remaining time of process, the process as curr process and go
to Step 6
[End of Repeat Step 6]
9. If curr process is not empty then go to Step 10 otherwise go to Step 13
10. Copy pid and burst time of curr process to pstat array at index ‘count’
11. Add burst time of curr process to ‘t’
12. Set remaining time of process to 0. completion time to ‘t’, increment count by 1,
and go to Step 4
13. Increment ‘t’ by 1 and go to Step 4
[End of Repeat (Step 4)]
14. Display gantt chart using Gantt() function
15. Call Display() function
16. Stop
Priority
1. Start
2. Set count ← 0, t ← 0
3. Read priority of each process and set remaining time of each process in process
array to its burst time
4. Repeat Steps 5 to 14 while count = n
5. Set curr process as empty, and minPriority as 9999
6. Repeat Steps 7 and 8 for each process in process array
84
7. If arrival time of process ≤ t, remaining time > 0, and priority < minPriority then
go to Step 8 otherwise go to Step 6
8. Set the process as curr process, minPriority as priority of the process, and go to
Step 6
[End of Repeat Step 6]
9. If curr process is not empty then go to Step 10 otherwise go to Step 14
10. Copy pid and burst time of curr process to pstat array at index ‘count’
11. Set start time of process at index ‘count’ in pstat array to ‘t’
12. Add burst time of curr process to ‘t’
13. Set remaining time of process to 0. completion time to ‘t’, increment count by 1,
and go to Step 4
14. Increment ‘t’ by 1 and go to Step 4
[End of Repeat (Step 4)]
15. Display gantt chart using Gantt() function
16. Call Display() function
17. Stop
Gantt Chart
1. Start
2. Display the top part of the box with length as twice the burst time for each process
combined including potential IDLE cases
3. Set t ← 0
4. Repeat Steps 5 to 10 for each process instance in pstat array
5. If start time of process ̸= t then go to Step 7 otherwise go to Step 9
6. Display ”IDLE”
7. Set t as start time of process
8. Display vertical line
9. Leave spaces equal to the burst time of process, and display pid of the process
10. Add burst time of process to ‘t’
[End of Repeat (Step 4)]
11. Display lower part of the box with length as twice the burst time for each process
combined including potential IDLE cases
12. Set t ← 0
13. Repeat Steps 14 to 19 for each process instance in pstat array
14. If start time of process ̸= t then go to Step 16 otherwise go to Step 19
15. Display ‘t’ and corresponding spaces
16. Display start time of the process
17. Set ‘t’ as start time of the process
18. Display ‘t’
19. Add burst time of process to ‘t’ and go to step 14
[End of Repeat (Step 14)]
20. Display ‘t’
21. Stop
85
Display
1. Start
2. Sort processes based on pid
3. Set turnaround time as completion - arrival time for each process in process array
4. Set waiting time as turnaround - burst time for each process in process array
5. For each process in process array, display it’s pid, arrival, burst, completion, turnaround,
and waiting time.
6. Add waiting times of all processes, divide it by ‘n’ and store it in avg tt
7. Add turnaround time of all processes, divide it by ‘n’ and store it in avg wt
8. Display avg tt and avg wt
9. Stop
Source Code
1 # include < stdio .h >
2
3 typedef struct {
4 int pid ;
5 int arrival ;
6 int burst ;
7 int completion ;
8 int turnaround ;
9 int waiting ;
10 int remaining ;
11 int priority ;
12 }
13 process ;
14
15 typedef struct {
16 int pid ;
17 int start ;
18 int burst ;
19 }
20 pstat ;
21
22 int front = -1 , rear = -1 , queue [100];
23
30 int dequeue () {
31 int item = queue [ front ];
32 if ( front == -1) return -1;
33 if ( front == rear ) front = rear = -1;
34 else front ++;
35 return item ;
36 }
37
38 void sortprocess ( process p [] , int n , int ch ) {
86
39 int i , j , swap = 0;
40 process temp ;
41 for ( i = 0; i < n ; i ++) {
42 for ( j = 0; j < n - i - 1; j ++) {
43 if ( ch == 0 && p [ j + 1]. arrival < p [ j ]. arrival ) {
44 temp = p [ j ];
45 p [ j ] = p [ j + 1];
46 p [ j + 1] = temp ;
47 swap = 1;
48 } else if ( ch == 1 && p [ j + 1]. pid < p [ j ]. pid ) {
49 temp = p [ j ];
50 p [ j ] = p [ j + 1];
51 p [ j + 1] = temp ;
52 swap = 1;
53 }
54 }
55 if ( swap == 0) break ;
56 }
57 }
58
59 void gantt ( pstat [] , int ) ;
60 void fcfs ( process [] , int ) ;
61 void roundrobin ( process [] , int , int ) ;
62 void sjf ( process [] , int ) ;
63 void priority ( process [] , int ) ;
64 void display ( process [] , int ) ;
65
66 int main () {
67 int n , c = 1 , ch , t , i , j , swap = 0;
68 process p [25];
69 printf ( " Enter the number of processes : " ) ;
70 scanf ( " % d " , & n ) ;
71 printf ( " Enter the process id , arrival time and burst time : \ n " ) ;
72 for ( i = 0; i < n ; i ++)
73 scanf ( " % d % d % d " , & p [ i ]. pid , & p [ i ]. arrival , & p [ i ]. burst ) ;
74 sortprocess (p , n , 1) ;
75 sortprocess (p , n , 0) ;
76 printf ( " [0]\ tExit \ n [1]\ tFCFS \ n [2]\ tRound Robin \ n [3]\ tSJF \ n [4]\
tPriority \ n " ) ;
77 while ( c ) {
78 printf ( " Enter your choice : " ) ;
79 scanf ( " % d " , & ch ) ;
80 switch ( ch ) {
81 case 0:
82 c = 0;
83 break ;
84 case 1:
85 fcfs (p , n ) ;
86 break ;
87 case 2:
88 printf ( " Enter the time quantum : " ) ;
89 scanf ( " % d " , & t ) ;
90 roundrobin (p , n , t ) ;
91 break ;
92 case 3:
93 sjf (p , n ) ;
94 break ;
95 case 4:
87
96 priority (p , n ) ;
97 break ;
98 default :
99 printf ( " Invalid input \ n " ) ;
100 }
101 }
102 return 0;
103 }
104
105 void gantt ( pstat p [] , int n ) {
106 int i , j , t = 0;
107 char hl [] = " \ u2500 " , vl [] = " \ u2502 " ;
108 char dr [] = " \ u250C " , dl [] = " \ u2510 " , ur [] = " \ u2514 " , ul [] = " \
u2518 " ;
109 char dh [] = " \ u252C " , uh [] = " \ u2534 " ;
110 printf ( " \ nGANTT CHART \ n " ) ;
111 for ( i = 0; i < n ; i ++) {
112 if ( i == 0) printf ( " % s " , dr ) ;
113 if ( p [ i ]. start != t ) {
114 for ( j = 0; j < 4; j ++) printf ( " % s " , hl ) ;
115 t += ( p [ i ]. start - t ) ;
116 printf ( " % s " , dh ) ;
117 }
118 for ( j = 0; j < 2 * p [ i ]. burst ; j ++) printf ( " % s " , hl ) ;
119 t += p [ i ]. burst ;
120 if ( i != n - 1) printf ( " % s " , dh ) ;
121 else printf ( " % s " , dl ) ;
122 }
123 printf ( " \ n " ) ;
124 t = 0;
125 for ( i = 0; i < n ; i ++) {
126 printf ( " % s " , vl ) ;
127 if ( p [ i ]. start != t ) {
128 printf ( " IDLE " ) ;
129 t += ( p [ i ]. start - t ) ;
130 printf ( " % s " , vl ) ;
131 }
132 for ( j = 0; j < p [ i ]. burst - 1; j ++) printf ( " " ) ;
133 printf ( " P % d " , p [ i ]. pid ) ;
134 for ( j = 0; j < p [ i ]. burst - 1; j ++) printf ( " " ) ;
135 t += p [ i ]. burst ;
136 }
137 printf ( " % s \ n " , vl ) ;
138 t = 0;
139 for ( i = 0; i < n ; i ++) {
140 if ( i == 0) printf ( " % s " , ur ) ;
141 if ( p [ i ]. start != t ) {
142 for ( j = 0; j < 4; j ++) printf ( " % s " , hl ) ;
143 t += ( p [ i ]. start - t ) ;
144 printf ( " % s " , uh ) ;
145 }
146 for ( j = 0; j < 2 * p [ i ]. burst ; j ++) printf ( " % s " , hl ) ;
147 t += p [ i ]. burst ;
148 if ( i != n - 1) printf ( " % s " , uh ) ;
149 else printf ( " % s " , ul ) ;
150 }
151 printf ( " \ n " ) ;
152 t = 0;
88
153 for ( i = 0; i < n ; i ++) {
154 if ( t != p [ i ]. start ) {
155 printf ( " % d " , t ) ;
156 for ( j = 0; j < 4; j ++) printf ( " " ) ;
157 printf ( " % d " , p [ i ]. start ) ;
158 t += ( p [ i ]. start - t ) ;
159 } else printf ( " % d " , t ) ;
160 if ( t % 100 < 10)
161 for ( j = 0; j < 2 * p [ i ]. burst ; j ++) printf ( " " ) ;
162 else
163 for ( j = 0; j < 2 * p [ i ]. burst - 1; j ++) printf ( " " ) ;
164 t += p [ i ]. burst ;
165 }
166 printf ( " % d \ n \ n " , t ) ;
167 }
168
169 void display ( process p [] , int n ) {
170 int i ;
171 double avgwt = 0 , avgtt = 0;
172 sortprocess (p , n , 1) ;
173 printf ( "
===============================================================\ n");
174 printf ( " ProcessId \ tArrival \ tBurst \ tCompletion \ tTurnAround \ tWaiting \
n");
175 printf ( "
===============================================================\ n");
176 for ( i = 0; i < n ; i ++)
177 printf ( " % -12 d \ t % -12 d \ t % -10 d \ t % -15 d \ t % -15 d \ t % -12 d \ n " , p [ i ]. pid ,
p [ i ]. arrival , p [ i ]. burst , p [ i ]. completion , p [ i ]. turnaround = p [ i ].
completion - p [ i ]. arrival , p [ i ]. waiting = p [ i ]. completion - p [ i ].
arrival - p [ i ]. burst ) ;
178 printf ( "
===============================================================\ n");
179 for ( i = 0; i < n ; i ++) {
180 avgtt += p [ i ]. turnaround ;
181 avgwt += p [ i ]. waiting ;
182 }
183 avgtt = avgtt / n ;
184 avgwt = avgwt / n ;
185 printf ( " Average TurnAround Time : % f \ n " , avgtt ) ;
186 printf ( " Average Waiting Time : % f \ n " , avgwt ) ;
187 }
188
89
204 }
205
206 void roundrobin ( process p [] , int n , int qt ) {
207 int i = 0 , j , t = p [0]. arrival , flag , cp , count = 0 , gc = 0;
208 pstat ps [50];
209 front = rear = -1;
210 for ( i = 0; i < n ; i ++) {
211 p [ i ]. remaining = p [ i ]. burst ;
212 if ( t == p [ i ]. arrival )
213 enqueue ( i ) ;
214 }
215 while ( count < n ) {
216 cp = dequeue () ;
217 if ( cp != -1) {
218 ps [ gc ]. pid = p [ cp ]. pid ;
219 ps [ gc ]. start = t ;
220 if ( p [ cp ]. remaining > qt ) {
221 ps [ gc ]. burst = qt ;
222 t += qt ;
223 p [ cp ]. remaining -= qt ;
224 for ( i = 0; i < n ; i ++) {
225 flag = 0;
226 if ( t >= p [ i ]. arrival && p [ i ]. remaining > 0 && i !=
cp ) {
227 for ( j = front ; j <= rear ; j ++)
228 if ( queue [ j ] == i )
229 flag = 1;
230 if ( flag == 0)
231 enqueue ( i ) ;
232 }
233 }
234 enqueue ( cp ) ;
235 } else {
236 ps [ gc ]. burst = p [ cp ]. remaining ;
237 t += p [ cp ]. remaining ;
238 p [ cp ]. completion = t ;
239 p [ cp ]. remaining = 0;
240 count ++;
241 }
242 gc ++;
243 } else {
244 while ( front == -1) {
245 for ( i = 0; i < n ; i ++) {
246 if ( t >= p [ i ]. arrival && p [ i ]. remaining > 0) {
247 enqueue ( i ) ;
248 }
249 }
250 if ( front == -1) t ++;
251 }
252 }
253 }
254 gantt ( ps , gc ) ;
255 display (p , n ) ;
256 }
257
90
261 for ( i = 0; i < n ; i ++)
262 p [ i ]. remaining = p [ i ]. burst ;
263 while ( count != n ) {
264 cp = -1;
265 minBurstTime = 9999;
266 for ( i = 0; i < n ; i ++) {
267 if (( p [ i ]. arrival <= t ) && ( p [ i ]. remaining > 0) && ( p [ i ].
remaining < minBurstTime ) ) {
268 minBurstTime = p [ i ]. remaining ;
269 cp = i ;
270 }
271 }
272 if ( cp != -1) {
273 ps [ count ]. pid = p [ cp ]. pid ;
274 ps [ count ]. start = t ;
275 ps [ count ]. burst = p [ cp ]. burst ;
276 t += p [ cp ]. burst ;
277 p [ cp ]. remaining = 0;
278 p [ cp ]. completion = t ;
279 count ++;
280 } else t ++;
281 }
282 gantt ( ps , count ) ;
283 display (p , n ) ;
284 }
285
286 void priority ( process p [] , int n ) {
287 int t = 0 , count = 0 , cp , i , prior ;
288 pstat ps [50];
289 printf ( " Enter the priorities of all the processes : \ n " ) ;
290 for ( i = 0; i < n ; i ++) {
291 printf ( " P % d : " , p [ i ]. pid ) ;
292 scanf ( " % d " , & p [ i ]. priority ) ;
293 p [ i ]. remaining = p [ i ]. burst ;
294 }
295 while ( count != n ) {
296 cp = -1;
297 prior = 9999;
298 for ( i = 0; i < n ; i ++) {
299 if (( p [ i ]. arrival <= t ) && ( p [ i ]. remaining > 0) && p [ i ].
priority < prior ) {
300 prior = p [ i ]. priority ;
301 cp = i ;
302 }
303 }
304 if ( cp != -1) {
305 ps [ count ]. pid = p [ cp ]. pid ;
306 ps [ count ]. start = t ;
307 ps [ count ]. burst = p [ cp ]. burst ;
308 t += p [ cp ]. burst ;
309 p [ cp ]. remaining = 0;
310 p [ cp ]. completion = t ;
311 count ++;
312 } else t ++;
313 }
314 gantt ( ps , count ) ;
315 display (p , n ) ;
316 }
91
Sample Output
Enter the number of processes: 5
Enter the process id, arrival time and burst time:
104
223
395
492
5 10 6
[0] Exit
[1] FCFS
[2] Round Robin
[3] SJF
[4] Priority
Enter your choice: 1
GANTT CHART
GANTT CHART
GANTT CHART
92
ProcessId Arrival Burst Completion TurnAround Waiting
1 0 4 4 4 0
2 2 3 7 5 2
3 9 5 16 7 2
4 9 2 11 2 0
5 10 6 22 12 6
Average TurnAround Time: 6.000000
Average Waiting Time: 2.000000
Enter your choice: 4
Enter the priorities of all the processes:
P1: 5
P2: 3
P3: 8
P4: 2
P5: 1
GANTT CHART
Results
Program code has been executed and the output has been verified.
93
Disk Scheduling Algorithms
Ex.No.2
30-10-24
Aim
To write a menu driven program for simulating the following Disk Scheduling algorithms
: (a) FCFS (b) SCAN (c) C-SCAN
Algorithm
MAIN
1. Start
2. Set c ← 1
3. Read total disk size (size), number of Requests (n), Request sequence (temp array),
and initial head position (init pos)
4. Repeat Steps 5 to 15 while c = 1
5. Initialize request array with values in temp array
6. Read choice
7. If choice = 0 then go to Step 8 otherwise go to Step 9
8. Set c ← 0
9. If choice = 1 then go to Step 10 otherwise go to Step 11
10. Call FCFS() function
11. If choice = 2 then go to Step 12 otherwise go to Step 13
12. Call SCAN() function
13. If choice = 3 then go to Step 14 otherwise go to Step 15
14. Call C SCAN() function
15. Display ”Invalid choice” and go to Step 4
[End of Repeat (Step 4)]
16. Stop
FCFS
1. Start
2. Initialize total head movement with 0
3. Repeat Steps 4 and 5 for each request in request array
4. Find displacement from initial position to the request and add that to the total
head movement
5. Copy request value to order array and to initial position
[End of Repeat (Step 3)]
6. Display seek sequence (elements of order array) and total head movement
7. Stop
94
SCAN
1. Start
2. Set k ← 0
3. Read direction to move (move)
4. Sort and display request array
5. For each request in request array, if initial position is less than the request then set
index as the location of request and go to Step 6 otherwise check next request until
all requests are checked.
6. If move = 1 then go to Step 7 otherwise go to Step 15
7. Repeat Steps 8 and 9 for each request from location index in request array
8. Find displacement from initial position to the request and add that to the total
head movement
9. Copy request value to order array and to initial position and go to Step 7
[End of Repeat (Step 7)]
10. Add the result from subtracting (final request + 1) from disk size to the total head
movement
11. Set initial position as disk size - 1
12. Repeat Steps 13 and 14 for each request from location index - 1 to 0 in request
array and then go to Step 23
13. Find displacement from initial position to the request and add that to the total
head movement
14. Copy request value to order array and to initial position and go to Step 12
[End of Repeat (Step 12)]
15. Repeat Steps 16 and 17 for each request from location index - 1 to 0 in request
array
16. Find displacement from initial position to the request and add that to the total
head movement
17. Copy request value to order array and to initial position
[End of Repeat (Step 15)]
18. Add the next request to the total head movement
19. Set initial position as 0
20. Repeat Steps 21 and 22 for each request from location index till the last request in
request array
21. Find displacement from initial position to the request and add that to the total
head movement
22. Copy request value to order array and to initial position
[End of Repeat (Step 20)]
23. Display seek sequence and total head movement
24. Stop
95
C SCAN
1. Start
2. Set k ← 0
3. Read direction to move (move)
4. Sort and display request array
5. For each request in request array, if initial position is less than the request then set
index ← location of request and go to Step 6 otherwise check next request
6. If move = 1 then go to Step 7 otherwise go to Step 15
7. Repeat Steps 8 and 9 for each request from location index in request array
8. Find displacement from initial position to the request and add that to the total
head movement
9. Copy request value to order array and to initial position and go to Step 7
[End of Repeat (Step 7)]
10. Add the result from subtracting (last request + 2) from twice the disk size to the
total head movement
11. Set initial position as 0
12. Repeat Steps 13 and 14 for each request from location index - 1 to 0 in request
array and then go to Step 23
13. Find displacement from initial position to the request and add that to the total
head movement
14. Copy request value to order array and to initial position and go to Step 12
[End of Repeat (Step 12)]
15. Repeat Steps 16 and 17 for each request from location index - 1 to 0 in request
array
16. Find displacement from initial position to the request and add that to the total
head movement
17. Copy request value to order array and to initial position
[End of Repeat (Step 15)]
18. Add the sum of next request and disk size - 1 to the total head movement
19. Set initial position as disk size - 1
20. Repeat Steps 21 and 22 for each request from the last to location index in request
array
21. Find displacement from initial position to the request and add that to the total
head movement
22. Copy request value to order array and to initial position
[End of Repeat (Step 20)]
23. Display seek sequence and total head movement
24. Stop
96
Source Code
1 # include < stdio .h >
2 # include < stdlib .h >
3
4 void display ( int [] , int ) ;
5 void fcfs ( int [] , int , int , int []) ;
6 void scan ( int [] , int , int , int []) ;
7 void c_scan ( int [] , int , int , int []) ;
8 int size , T_Head_Mv = 0;
9
10 void main () {
11 int rq [100] , temp [100] , c = 1 , i , j , n , init_pos , ch , order [100];
12 printf ( " Enter total disk size : " ) ;
13 scanf ( " % d " , & size ) ;
14 printf ( " Enter the number of Requests : " ) ;
15 scanf ( " % d " , & n ) ;
16 printf ( " Enter Request sequence : \ n " ) ;
17 for ( i = 0; i < n ; i ++)
18 scanf ( " % d " , & temp [ i ]) ;
19 printf ( " Enter initial head position : " ) ;
20 scanf ( " % d " , & init_pos ) ;
21 printf ( " \ n [0]\ tExit \ n [1]\ tFCFS \ n [2]\ tSCAN \ n [3]\ tC SCAN " ) ;
22 while ( c ) {
23 for ( i = 0; i < n ; i ++)
24 rq [ i ] = temp [ i ];
25 printf ( " \ n \ nEnter your choice : " ) ;
26 scanf ( " % d " , & ch ) ;
27 switch ( ch ) {
28 case 0:
29 c = 0;
30 break ;
31 case 1:
32 printf ( " FCFS \ n " ) ;
33 fcfs ( rq , n , init_pos , order ) ;
34 break ;
35 case 2:
36 printf ( " SCAN \ n " ) ;
37 scan ( rq , n , init_pos , order ) ;
38 break ;
39 case 3:
40 printf ( "C - SCAN \ n " ) ;
41 c_scan ( rq , n , init_pos , order ) ;
42 break ;
43 default :
44 printf ( " Invalid choice \ n " ) ;
45 }
46 }
47 }
48
49 void fcfs ( int rq [] , int n , int init_pos , int order []) {
50 for ( int i = 0; i < n ; i ++) {
51 T_Head_Mv += abs ( rq [ i ] - init_pos ) ;
52 order [ i ] = rq [ i ];
53 init_pos = rq [ i ];
54 }
55 display ( order , n ) ;
56 }
97
57
98
115 T_Head_Mv += abs ( rq [ i + 1]) ;
116 init_pos = 0;
117 for ( i = index ; i < n ; i ++) {
118 T_Head_Mv += abs ( rq [ i ] - init_pos ) ;
119 init_pos = rq [ i ];
120 order [ k ++] = rq [ i ];
121 }
122 }
123 display ( order , k ) ;
124 }
125
126 void c_scan ( int rq [] , int n , int init_pos , int order []) {
127 int move , i , j , k = 0 , index ;
128 printf ( " Enter direction to move (0 - low / 1 = high ) : ");
129 scanf ( " % d " , & move ) ;
130 for ( i = 0; i < n ; i ++) {
131 for ( j = 0; j < n - i - 1; j ++) {
132 if ( rq [ j ] > rq [ j + 1]) {
133 int temp = rq [ j ];
134 rq [ j ] = rq [ j + 1];
135 rq [ j + 1] = temp ;
136 }
137 }
138 }
139 // display sorted rq
140 printf ( " Sorted requests : " ) ;
141 for ( i = 0; i < n ; i ++)
142 printf ( " % d " , rq [ i ]) ;
143 printf ( " \ n " ) ;
144 // to find the starting position in the sorted rq
145 for ( i = 0; i < n ; i ++) {
146 if ( init_pos < rq [ i ]) {
147 index = i ;
148 break ;
149 }
150 }
151 // if movement is towards right side
152 if ( move == 1) {
153 for ( i = index ; i < n ; i ++) {
154 T_Head_Mv += abs ( rq [ i ] - init_pos ) ;
155 init_pos = rq [ i ];
156 order [ k ++] = rq [ i ];
157 }
158 // last movement for max size
159 T_Head_Mv += abs ( size - rq [ i - 1] - 1) ;
160 // head movement to starting position
161 T_Head_Mv += abs ( size - 1 - 0) ;
162 init_pos = 0;
163 for ( i = 0; i <= index - 1; i ++) {
164 T_Head_Mv += abs ( rq [ i ] - init_pos ) ;
165 init_pos = rq [ i ];
166 order [ k ++] = rq [ i ];
167 }
168 }
169 // if the movement is towards left side
170 else {
171 for ( i = index - 1; i >= 0; i - -) {
172 T_Head_Mv += abs ( rq [ i ] - init_pos ) ;
99
173 init_pos = rq [ i ];
174 order [ k ++] = rq [ i ];
175 }
176 // last movement for min size
177 T_Head_Mv += abs ( rq [ i + 1]) ;
178 T_Head_Mv += abs ( size - 1 - 0) ;
179 init_pos = size - 1;
180 for ( i = n - 1; i >= index ; i - -) {
181 T_Head_Mv += abs ( rq [ i ] - init_pos ) ;
182 init_pos = rq [ i ];
183 order [ k ++] = rq [ i ];
184 }
185 }
186 display ( order , k ) ;
187 }
Output
Enter total disk size: 200
Enter the number of Requests: 7
Enter Request sequence:
82 170 43 140 24 16 190
Enter initial head position: 50
[0] Exit
[1] FCFS
[2] SCAN
[3] C SCAN
100
Enter your choice: 2
SCAN
Enter direction to move (0 - low / 1 - high): 1
Sorted requests: 16 24 43 82 140 170 190
Seek sequence: 82 140 170 190 43 24 16
Total Head Movement = 1214
Results
Program code has been executed and the output has been verified.
101
Page Replacement Algorithms
Ex.No.3
30-10-24
Aim
To write a menu driven program for the implementation of the following Page Replace-
ment Algorithms: (a) FIFO (b) LRU (c) LFU
Algorithm
MAIN
1. Start
2. Read number of pages (n) and set c ← 1
3. Read page numbers and store them in an array (rs)
4. Read number of frames (nf)
5. Repeat Steps 6 to 15 while c = 1
6. Initialize frames array with -1
7. Read choice
8. If choice = 1 then go to Step 9 otherwise go to Step 10
9. Call FIFO() function
10. If choice = 2 then go to Step 11 otherwise go to Step 12
11. Call LRU() function
12. If choice = 3 then go to Step 13 otherwise go to Step 14
13. Call LFU() function
14. If choice = 0 then go to Step 15 otherwise go to Step 5
15. Set c ← 0
[End of Repeat (Step 5)]
16. Stop
FIFO
1. Start
2. Set j ← 0, k and fcount ← 0
3. Display table headers
4. Repeat Steps 5 to 12 for each page in rs array
5. Print page number
6. Set avail ← 0
7. Repeat Steps 8 to 10 for each frame in frames array
8. If frame matches page then go to Step 9 otherwise go to Step 7
102
9. Set avail ← 1
10. Print each frame in the frames array and then print ”H” and go to Step 7
[End of Repeat (Step 7)]
11. If avail is 0 then set frame at index j in frames array to page, increment j by 1
within the number of frames and increment fcount by 1
12. Print each frame in frames array and print ”F” and go to Step 4
[End of Repeat (Step 4)]
13. Print number of page faults (fcount)
14. Stop
LRU
1. Start
2. Set fcount ← 0 and k ← 0
3. Display table headers
4. Set frame at location k to page at location k
5. Display first page in rs array and each frame in frames array
6. Print ”F”
7. Increment fcount and k by 1
8. Repeat Steps 9 to 27 for each page in rs array
9. Print page number
10. Set c1 ← 0
11. Repeat Step 12 for each frame in frames array
12. If frame does not match page then increment c1 and go to Step 11
13. If c1 equals nf then go to Step 14 otherwise go to Step 27
14. Increment fcount by 1
15. If k is less than nf then add page to frames[k], increment k, print each frame, and
print ”F” otherwise go to Step 16
16. Repeat Steps 17 to 19 for each frame in frames array
17. Initialize c2 array with 0
18. Repeat Step 19 from j = location of current page in rs - 1 to 0
19. If the frame does not match the page at location j then increment value at location
corresponding to current frame in c2 array by 1 otherwise go to Step 16
[End of Repeat (Step 18)]
[End of Repeat (Step 16)]
20. Initialize nf locations of array b with values in c2
21. Repeat Steps 22 to 23 from r = 0 to nf - 1
22. Repeat Step 23 from j = r to nf - 1
23. If value at location r of b is less than value at location j of b then swap these values
otherwise go to Step 22
[End of Repeat (Step 22)]
[End of Repeat (Step 21)]
24. Repeat Step 25 from r = 0 to nf - 1
103
25. If value at location r of c2 = first element in b then set frame number at location r
in frames array to the current page number in rs, display the frame and go to Step
24
[End of Repeat (Step 24)]
26. Print ”F” and go to Step 8
27. Display each frame in frames array and print ”H” and go to Step 8
[End of Repeat (Step 8)]
28. Print number of page faults (fcount)
29. Stop
LFU
1. Start
2. Set front ← 0 and fcount ← 0
3. Print table headers
4. Repeat Steps 5 to 8 for each page in rs array
5. Print page number
6. If page is not in frames and front ≥ nf then find page with minimum frequency,
replace it in frames array, increment fcount by 1, display all frames, and print ”F”
otherwise go to Step 7
7. If page is not in frames then add page to frames at location front and increment
front, fcount by 1, display all frames and print ”F” otherwise go to Step 8
8. If page is in frames, display all frames and print ”H” otherwise go to Step 4
[End of Repeat (Step 4)]
9. Print number of page faults (fcount)
10. Stop
Source Code
1 # include < stdio .h >
2
3 typedef struct {
4 int page ;
5 int freq ;
6 } Freq ;
7
8 void FIFO ( int rs [] , int n , int frames [] , int nf ) ;
9 void LRU ( int rs [] , int n , int frames [] , int nf ) ;
10 void LFU ( int rs [] , int n , int frames [] , int nf ) ;
11 int check_fault ( int page , int frames [] , int nf ) ;
12 int find_min_freq ( int frames [] , int current , int nf , int rs []) ;
13 void sort ( Freq table [] , int len ) ;
14
15 void main () {
16 int n , frames [50] , rs [50] , nf , i , c = 1;
17 printf ( " Enter the number of pages : " ) ;
18 scanf ( " % d " , & n ) ;
19 printf ( " Enter the page reference numbers : \ n " ) ;
20 for ( i = 0; i < n ; i ++)
104
21 scanf ( " % d " , & rs [ i ]) ;
22 printf ( " Enter the number of frames : " ) ;
23 scanf ( " % d " , & nf ) ;
24 printf ( " [0]\ tExit \ n [1]\ tFIFO \ n [2]\ tLRU \ n [3]\ tLFU \ n " ) ;
25 int choice ;
26 while ( c ) {
27 for ( i = 0; i < nf ; i ++)
28 frames [ i ] = -1;
29 printf ( " Enter your choice : " ) ;
30 scanf ( " % d " , & choice ) ;
31 switch ( choice ) {
32 case 1:
33 FIFO ( rs , n , frames , nf ) ;
34 break ;
35 case 2:
36 LRU ( rs , n , frames , nf ) ;
37 break ;
38 case 3:
39 LFU ( rs , n , frames , nf ) ;
40 break ;
41 case 0:
42 c = 0;
43 break ;
44 }
45 }
46 }
47
48 void FIFO ( int rs [] , int n , int frames [] , int nf ) {
49 int j = 0 , k , fcount = 0 , avail ;
50 printf ( " \ nFIFO " ) ;
51 printf ( " \ n
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ nPage Ref
");
52 for ( int i = 0; i < nf ; i ++) printf ( " \ tF % d " , i ) ;
53 printf ( " \ tHit / Fault \ n
==========================================================\ n");
54 for ( int i = 0; i < n ; i ++) {
55 printf ( " % d \ t \ t " , rs [ i ]) ;
56 avail = 0;
57 for ( k = 0; k < nf ; k ++) {
58 if ( frames [ k ] == rs [ i ]) {
59 avail = 1;
60 for ( k = 0; k < nf ; k ++)
61 printf ( " % d \ t " , frames [ k ]) ;
62 printf ( " H " ) ;
63 }
64 }
65 if ( avail == 0) {
66 frames [ j ] = rs [ i ];
67 j = ( j + 1) % nf ;
68 fcount ++;
69 for ( k = 0; k < nf ; k ++)
70 printf ( " % d \ t " , frames [ k ]) ;
71 printf ( " F " ) ;
72 }
73 printf ( " \ n " ) ;
74 }
75 printf ( " = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \
105
n");
76 printf ( " Number of page faults : % d \ n " , fcount ) ;
77 }
78
79 void LRU ( int rs [] , int n , int frames [] , int nf ) {
80 int fcount = 0 , k = 0 , c1 , i , j , c2 [100] , b [100] , t , r ;
81 printf ( " \ nLRU " ) ;
82 printf ( " \ n
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ nPage Ref
");
83 for ( int i = 0; i < nf ; i ++) printf ( " \ tF % d " , i ) ;
84 printf ( " \ tHit / Fault \ n
==========================================================\ n");
85 frames [ k ] = rs [ k ];
86 printf ( " % d \ t \ t " , rs [0]) ;
87 for ( i = 0; i < nf ; i ++)
88 printf ( " % d \ t " , frames [ i ]) ;
89 printf ( " F \ n " ) ;
90 fcount ++;
91 k ++;
92 for ( i = 1; i < n ; i ++) {
93 printf ( " % d \ t \ t " , rs [ i ]) ;
94 c1 = 0;
95 for ( j = 0; j < nf ; j ++)
96 if ( rs [ i ] != frames [ j ])
97 c1 ++;
98 if ( c1 == nf ) {
99 fcount ++;
100 if ( k < nf ) {
101 frames [ k ] = rs [ i ];
102 k ++;
103 for ( j = 0; j < nf ; j ++)
104 printf ( " % d \ t " , frames [ j ]) ;
105 printf ( " F \ n " ) ;
106 } else {
107 for ( r = 0; r < nf ; r ++) {
108 c2 [ r ] = 0;
109 for ( j = i - 1; j >= 0; j - -) {
110 if ( frames [ r ] != rs [ j ])
111 c2 [ r ]++;
112 else
113 break ;
114 }
115 }
116 for ( r = 0; r < nf ; r ++)
117 b [ r ] = c2 [ r ];
118 for ( r = 0; r < nf ; r ++) {
119 for ( j = r ; j < nf ; j ++) {
120 if ( b [ r ] < b [ j ]) {
121 t = b [ r ];
122 b [ r ] = b [ j ];
123 b[j] = t;
124 }
125 }
126 }
127 for ( r = 0; r < nf ; r ++) {
128 if ( c2 [ r ] == b [0])
129 frames [ r ] = rs [ i ];
106
130 printf ( " % d \ t " , frames [ r ]) ;
131 }
132 printf ( " F \ n " ) ;
133 }
134 } else {
135 for ( j = 0; j < nf ; j ++)
136 printf ( " % d \ t " , frames [ j ]) ;
137 printf ( " H \ n " ) ;
138 }
139 }
140 printf ( " = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \
n");
141 printf ( " Number of page faults : % d \ n " , fcount ) ;
142 }
143
144 void sort ( Freq table [] , int len ) {
145 for ( int i = 0; i < len ; i ++) {
146 for ( int j = i ; j < len - i - 1; j ++) {
147 if ( table [ j ]. freq > table [ j + 1]. freq ) {
148 Freq temp ;
149 temp = table [ j ];
150 table [ j ] = table [ j + 1];
151 table [ j + 1] = temp ;
152 }
153 }
154 }
155 }
156
157 int check_fault ( int page , int frames [] , int nf ) {
158 for ( int i = 0; i < nf ; i ++)
159 if ( frames [ i ] == page )
160 return 0;
161 return 1;
162 }
163
164 int find_min_freq ( int frames [] , int current , int nf , int rs []) {
165 Freq table [30];
166 int count ;
167 for ( int i = 0; i < nf ; i ++)
168 table [ i ]. page = frames [ i ];
169 for ( int i = 0; i < nf ; i ++) {
170 count = 0;
171 // consider table [ i ]
172 for ( int j = 0; j < current ; j ++)
173 if ( rs [ j ] == table [ i ]. page )
174 count ++;
175 table [ i ]. freq = count ;
176 }
177 sort ( table , nf ) ;
178 for ( int i = 0; i < nf ; i ++)
179 if ( frames [ i ] == table [0]. page )
180 return i ;
181 return -1;
182 }
183
107
187 printf ( " \ n
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \ nPage Ref
");
188 for ( int i = 0; i < nf ; i ++) printf ( " \ tF % d " , i ) ;
189 printf ( " \ tHit / Fault \ n
==========================================================\ n");
190 for ( int i = 0; i < n ; i ++) {
191 printf ( " % d \ t \ t " , rs [ i ]) ;
192 if ( check_fault ( rs [ i ] , frames , nf ) && front >= nf ) {
193 int index = find_min_freq ( frames , i , nf , rs ) ;
194 frames [ index ] = rs [ i ];
195 fcount ++;
196 for ( j = 0; j < nf ; j ++)
197 printf ( " % d \ t " , frames [ j ]) ;
198 printf ( " F \ n " ) ;
199
200 } else if ( check_fault ( rs [ i ] , frames , nf ) ) {
201 frames [ front ] = rs [ i ];
202 front ++;
203 fcount ++;
204 for ( j = 0; j < nf ; j ++)
205 printf ( " % d \ t " , frames [ j ]) ;
206 printf ( " F \ n " ) ;
207 } else if (! check_fault ( rs [ i ] , frames , nf ) ) {
208 for ( j = 0; j < nf ; j ++)
209 printf ( " % d \ t " , frames [ j ]) ;
210 printf ( " H \ n " ) ;
211 }
212 }
213 printf ( " = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = \
n");
214 printf ( " Number of page faults : % d \ n " , fcount ) ;
215 }
Output
Enter the number of pages: 7
Enter the page reference numbers :
1303653
Enter the number of frames: 3
[0] Exit
[1] FIFO
[2] LRU
[3] LFU
Enter your choice: 1
FIFO
108
Number of page faults: 6
Enter your choice: 2
LRU
LFU
Results
Program code has been executed and the output has been verified.
109
Banker’s Algorithm
Ex.No.4
30-10-24
Aim
To implement the banker’s algorithm for Deadlock Avoidance.
Structure process info is used to store the resource information of each process which
consists of max, allocated, and the resource need for each process as elements.
Algorithm
MAIN
1. Start
2. Read number of processes (np) and resource types (nr)
3. Read details of processes using input() function
4. Display details of processes using showTheInfo() function
5. Check if system is in safe state using isSafeState() function
6. If in safe state, print safe sequence
7. If not in safe state, print system is not in safe state
8. Stop
Input
1. Start
2. Repeat Steps 3 to 5 for each process in process info array
3. Read maximum need of process for each resource type and store in max array in
process info array
4. Read allocated resources of process and store in allocated array in process info array
5. Calculate the need array by subtracting allocated from max for each resource type
and go to Step 2
[End of Repeat (Step 2)]
6. Read available resources into available array
7. Stop
110
ShowTheInfo
1. Start
2. Print header with PID, Maximum, Allocated, and Need columns
3. Repeat Step 4 for each process in process array
4. Print process ID, max array, allocated array, and need array in tabular form
5. Stop
IsSafeState
1. Start
2. Set proceed ← 1 and k ← 0 for safe sequence index
3. Copy available resources into work array
4. Initialize finish array to 0
5. Repeat Steps 6 to 10 until proceed is 0
6. Set proceed ← 0
7. Repeat Steps 8 to 10 for each process in process array
8. If process is not finished then go to Step 9 otherwise go to Step 7
9. If process can be executed with available resources (check if need for each process
≤ work) then go to Step 10 otherwise go to Step 7
10. Add allocated resources of the process to work array, set the process as finished
(value 1) in finish array, add process to safe sequence, and set proceed to 1
[End of Repeat (Step 7)]
[End of Repeat (Step 5)]
11. If all processes are finished, then return 1 (safe state), otherwise return 0 (not safe
state)
12. Stop
Source Code
1 # include < stdio .h >
2
3 typedef struct {
4 int max [10];
5 int allocated [10];
6 int need [10];
7 } process_info ;
8 int np , nr ;
9
10 void input ( process_info [] , int []) ;
11 void showTheInfo ( process_info []) ;
12 int isSafeState ( process_info [] , int [] , int []) ;
13
14 int main () {
15 printf ( " Enter No of Processes and Resource Instances in system : " ) ;
16 scanf ( " % d % d " , & np , & nr ) ;
17 int available [ nr ] , safeSequence [ np ];
18 process_info process [ np ];
19 printf ( " **************** Enter details of processes
* *** ** ** ** ** ** ** *\ n " ) ;
111
20 input ( process , available ) ;
21 showTheInfo ( process ) ;
22 if ( isSafeState ( process , available , safeSequence ) ) {
23 printf ( " System is in safe state \ n " ) ;
24 printf ( " Safe sequence is : " ) ;
25 for ( int i = 0; i < np ; i ++)
26 printf ( " P [% d ] " , safeSequence [ i ]) ;
27 printf ( " \ n " ) ;
28 } else
29 printf ( " System is not in Safe State \ n " ) ;
30 return 0;
31 }
32
112
76 for ( i = 0; i < np ; i ++) {
77 flag = 1;
78 if ( finish [ i ] == 0) {
79 for ( j = 0; j < nr ; j ++) {
80 if ( process [ i ]. need [ j ] <= work [ j ])
81 continue ;
82 else {
83 flag = 0;
84 break ;
85 }
86 }
87 if ( flag == 0) continue ;
88 for ( j = 0; j < nr ; j ++)
89 work [ j ] = work [ j ] + process [ i ]. allocated [ j ];
90 finish [ i ] = 1;
91 safeSequence [ k ++] = i ;
92 proceed = 1;
93 }
94 }
95 }
96 for ( i = 0; i < np && finish [ i ] == 1; i ++)
97 continue ;
98 if ( i == np ) return 1;
99 else return 0;
100 }
113
Output
Enter No of Processes and Resource Instances in system: 5 3
****************Enter details of processes*****************
Enter process[0] info
Enter Maximum need: 7 5 3
Enter No. of Allocated Resources: 0 1 0
Enter process[1] info
Enter Maximum need: 3 2 2
Enter No. of Allocated Resources: 2 0 0
Enter process[2] info
Enter Maximum need: 9 0 2
Enter No. of Allocated Resources: 3 0 2
Enter process[3] info
Enter Maximum need: 2 2 2
Enter No. of Allocated Resources: 2 1 1
Enter process[4] info
Enter Maximum need: 4 3 3
Enter No. of Allocated Resources: 0 0 2
Enter Available Resources: 3 3 2
PID Maximum Allocated Need
P[0] 7 5 3 0 1 0 7 4 3
P[1] 3 2 2 2 0 0 1 2 2
P[2] 9 0 2 3 0 2 6 0 0
P[3] 2 2 2 2 1 1 0 1 1
P[4] 4 3 3 0 0 2 4 3 1
System is in safe state
Safe sequence is: P[1] P[3] P[4] P[0] P[2]
Results
Program code has been executed and the output has been verified.
114