1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<stdio.h>intmain(){int burst_time[20], process[20], waiting_time[20], turnaround_time[20],priority[20];int i,j, limit, sum =0, position, temp;float avg_wait_time, avg_turnaround_time;// Input the number of processesprintf("Enter the number of processes");scanf("%d",&limit);// Input burst time and priority for each processprintf("Enter burst time and priority for %d processes\n",limit);for(i=0; i<limit;i++){printf("\n Process [%d]\n",i+1);printf("Process burst time");scanf("%d",&burst_time[i]);printf("Process priority");scanf("%d",&priority[i]);
process[i]= i+1;}// Sort the processes based on priority using bubble sortfor(i =0; i < limit -1; i++){for(j =0; j < limit - i -1; j++){if(priority[j]> priority[j +1]){// Swap if current element has higher priority// Swap priority
temp = priority[j];
priority[j]= priority[j +1];
priority[j +1]= temp;// Swap burst time
temp = burst_time[j];
burst_time[j]= burst_time[j +1];
burst_time[j +1]= temp;// Swap process ID
temp = process[j];
process[j]= process[j +1];
process[j +1]= temp;}}}// Calculate waiting time for each process
waiting_time[0]=0;for(i=1;i<limit;i++){
waiting_time[i]=0;for(j=0;j<i;j++){
waiting_time[i]+= burst_time[j];}
sum+= waiting_time[i];}// Calculate average waiting time
avg_wait_time =(float)sum/limit;
sum=0;// Print the process ID, burst time, waiting time, and turnaround time for each processprintf("Process ID\t Burst time\t Waiting Time\t Turnaround Time\n");for(i=0;i<limit;i++){
turnaround_time[i]= burst_time[i]+ waiting_time[i];
sum+=turnaround_time[i];printf("Process[%d]\t%d\t%d\t%d\n",process[i],burst_time[i],waiting_time[i],turnaround_time[i]);}// Calculate average turnaround time
avg_turnaround_time =(float)sum/limit;// Print the average waiting time and average turnaround timeprintf("Average waiting time %2f\n",avg_wait_time);printf("Average Turnaround Time %2f \n",avg_turnaround_time);return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<stdio.h>intmain(){// Number of processes (n) and resources (m)int n, m, i, j, k;// Input: Number of processesprintf("Enter the number of processes: \n");scanf("%d",&n);// Input: Number of resourcesprintf("Enter the number of resources:\n");scanf("%d",&m);// Allocation matrix, Max matrix, and Available resourcesint allocationMatrix[n][m], maxMatrix[n][m], availableResources[m];// Input: Allocation matrixprintf("Enter the Allocation matrix: \n");for(i =0; i < n; i++){for(j =0; j < m; j++){scanf("%d",&allocationMatrix[i][j]);}}// Input: Max matrixprintf("Enter the Max matrix: \n");for(i =0; i < n; i++){for(j =0; j < m; j++){scanf("%d",&maxMatrix[i][j]);}}// Input: Available resourcesprintf("Enter the Available resources: \n");for(i =0; i < m; i++){scanf("%d",&availableResources[i]);}// Arrays to keep track of processed and safe sequencesint processedProcesses[n], safeSequence[n], index =0;// Initialize processedProcesses arrayfor(k =0; k < n; k++){
processedProcesses[k]=0;}// Calculate the Need matrixint needMatrix[n][m];for(i =0; i < n; i++){for(j =0; j < m; j++)
needMatrix[i][j]= maxMatrix[i][j]- allocationMatrix[i][j];}// Check for safe sequencefor(k =0; k < n; k++){for(i =0; i < n; i++){if(processedProcesses[i]==0){int flag =0;for(j =0; j < m; j++){if(needMatrix[i][j]> availableResources[j]){
flag =1;break;}}if(flag ==0){
safeSequence[index++]= i;for(int y =0; y < m; y++)
availableResources[y]+= allocationMatrix[i][y];
processedProcesses[i]=1;}}}}// Check if the system is in a safe stateint isSafe =1;for(i =0; i < n; i++){if(processedProcesses[i]==0){
isSafe =0;printf("The system is not safe.\n");break;}}// Print the safe sequence if the system is safeif(isSafe){printf("Following is the SAFE Sequence\n");for(i =0; i < n -1; i++)printf(" P%d ->", safeSequence[i]);printf(" P%d\n", safeSequence[n -1]);}return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<stdio.h>#include<stdlib.h>intmain(){int ioq[20], i, n, j, ihead, temp, scan, tot;float seek =0, avgs;// Prompt user for the number of requestsprintf("Enter the number of requests: ");scanf("%d",&n);// Prompt user for the initial head positionprintf("Enter the initial head position: ");scanf("%d",&ihead);
ioq[0]= ihead;
ioq[1]=0;
n +=2;// Prompt user for I/O queue requestsprintf("Enter the I/O queue requests:\n");for(i =2; i < n; i++){scanf("%d",&ioq[i]);}// Sort the I/O queue requestsfor(i =0; i < n -1; i++){for(j =0; j < n - i -1; j++){if(ioq[j]> ioq[j +1]){
temp = ioq[j];
ioq[j]= ioq[j +1];
ioq[j +1]= temp;}}}
ioq[n]= ioq[n -1];// Find the initial head position in the sorted I/O queuefor(i =0; i < n; i++){if(ihead == ioq[i]){
scan = i;break;}}printf("\nOrder of request served:\n\n");
tot =0;// Serve requests in the lower directionfor(i = scan; i >=0; i--){if(i ==0)// For last element we will calculate the distance from 0 to scan +1
tot =abs(ioq[i]- ioq[scan +1]);else
tot =abs(ioq[i]- ioq[i -1]);// if (tot < 0)// tot = -tot;printf("%d\t%d\n", ioq[i], tot);}// Serve requests in the upper directionfor(i = scan +1; i < n; i++){
tot =abs(ioq[i +1]- ioq[i]);// if (tot < 0)// tot = -tot;printf("%d\t%d\n", ioq[i], tot);}// Calculate seek time and average seek time
seek = ihead + ioq[n -1];
avgs = seek /(n -2);printf("\n\nTotal seek time: %.2f\n", seek);printf("Average seek time: %.2f\n\n", avgs);return0;}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<stdio.h>voidmain(){int ioq[20], i, n, j, ihead, itail, temp, scan, tot =0;float seek =0, avgs;// Prompt user for the number of requestsprintf("Enter the number of requests: ");scanf("%d",&n);
ioq[0]=0;// Prompt user for the initial head positionprintf("Enter the initial head position: ");scanf("%d",&ihead);
ioq[1]= ihead;// Prompt user for the maximum track limitprintf("Enter the maximum track limit: ");scanf("%d",&itail);
ioq[2]= itail;
n +=3;// Prompt user for I/O queue requestsprintf("Enter the I/O queue requests:\n");for(i =3; i < n; i++){scanf("%d",&ioq[i]);}// Sort the I/O queue requestsfor(i =0; i < n -1; i++){for(j =0; j < n -1; j++){if(ioq[j]> ioq[j +1]){
temp = ioq[j];
ioq[j]= ioq[j +1];
ioq[j +1]= temp;}}}// Find the initial head position in the sorted I/O queuefor(i =0; i < n; i++){if(ihead == ioq[i]){
scan = i;break;}}
i = scan;
temp = n;printf("\nOrder of requests served:\n\n");printf("\n");while(i != temp){if(i < temp -1){
tot =abs(ioq[i +1]- ioq[i]);
seek += tot;}printf("%d-->", ioq[i]);
i++;if(i == n)// Checks if the end is reached, is so, then the seek goes to the beginning{
i =0;
temp = scan;
seek += itail;// The distance between start and end is added since, it goes back}}
avgs = seek /(n -3);printf("\n\nTotal seek time: %.2f", seek);printf("\nAverage seek time: %.2f\n\n", avgs);}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
DATA SEGMENT
N1 DW 1731H ; Define a 16-bit data word N1 with initial value 1731H
N2 DW 9212H ; Define a 16-bit data word N2 with initial value 9212H
N3 DW ?; Define a 16-bit data word N3 with an uninitialized value
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE;DS:DATA
; Set code and data segment registers
START:
MOV AX,DATA ; Load the address of the data segment into AX
MOV DS,AX ; Set the data segment register to the loaded address
XOR AX,AX ; Clear AX register(AX =0)
MOV BX,AX ; Clear BX register(BX =0)
MOV AX,N1 ; Load the value of N1 into AX
ADD AX,N2 ; Add the value of N2 to AX
MOV N3,AX ; Store the result in N3
JNC STOP ; Jump to STOP if there is no carry after the addition
INC BX ; Increment BX if there is a carry
STOP:
MOV CX,AX ; Move the result of the addition (AX) into CX
MOV AH,4CH ; Set the function code for program termination
INT 21H ; Invoke interrupt 21H to terminate the program
CODE ENDS
END START ; End of the program with START as the entry point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
DATA SEGMENT
N1 DW 8888H ; Define a 16-bit data word N1 with initial value 8888H
N2 DW 4444H ; Define a 16-bit data word N2 with initial value 4444H
N3 DW ?; Define a 16-bit data word N3 with an uninitialized value
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE;DS:DATA ; Set code and data segment registers
START:
MOV AX,DATA ; Load the address of the data segment into AX
MOV DS,AX ; Set the data segment register to the loaded address
XOR AX,AX ; Clear AX register(AX =0)
MOV BX,AX ; Clear BX register(BX =0)
MOV AX,N1 ; Load the value of N1 into AX
SUB AX,N2 ; Subtract the value of N2 from AX
MOV N3,AX ; Store the result in N3
JNC STOP ; Jump to STOP if there is no borrow after the subtraction
INC BX ; Increment BX if there is a borrow
STOP:
MOV CX,AX ; Move the result of the subtraction (AX) into CX
MOV AH,4CH ; Set the function code for program termination
INT 21H ; Invoke interrupt 21H to terminate the program
CODE ENDS
END START ; End of the program with START as the entry point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
DATA SEGMENT
a DW 5H ; Define a 16-bit data word 'a' with initial value 5H
b DW 20H ; Define a 16-bit data word 'b' with initial value 20H
c DD ?; Define a 32-bit data doubleword 'c' with an uninitialized value
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA,CS:CODE ; Set data and code segment registers
START:
MOV AX,DATA ; Load the address of the data segment into AX
MOV DS,AX ; Set the data segment register to the loaded address
MOV AX,a ; Load the value of 'a' into AX
MOV BX,b ; Load the value of 'b' into BX
MUL BX ; Multiply AX by BX, result in DX:AX
MOV WORD PTR c,AX ; Store the low word of the result in 'c'
MOV WORD PTR c+2,DX ; Store the high word of the result in 'c'
INT 3; Software breakpoint (debugger interrupt)
CODE ENDS
END START ; End of the program with START as the entry point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
DATA SEGMENT
a DW 6H ; Define a 16-bit data word 'a' with initial value 6H
b DW 0H ; Define a 16-bit data word 'b' with initial value 0H (Note: Avoid division by zero)
c DD ?; Define a 32-bit data doubleword 'c' with an uninitialized value
DATA ENDS
CODE SEGMENT
ASSUME DS:DATA,CS:CODE ; Set data and code segment registers
START:
MOV AX,DATA ; Load the address of the data segment into AX
MOV DS,AX ; Set the data segment register to the loaded address
MOV AX,a ; Load the value of 'a' into AX
MOV BX,b ; Load the value of 'b' into BX
DIV BX ; Divide AX by BX, quotient in AX, remainder in DX
MOV c,AX ; Store the quotient in the low word of 'c'
MOV c+2,DX ; Store the remainder in the high word of 'c'
INT 3; Software breakpoint (debugger interrupt)
CODE ENDS
END START ; End of the program with START as the entry point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
DATA SEGMENT
LIST DD 12121212H,12121212H ; Define a doubleword array 'LIST' with two 32-bit values
N3 DW ?; Define a 16-bit data word 'N3' with an uninitialized value
N4 DW ?; Define a 16-bit data word 'N4' with an uninitialized value
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE;DS:DATA ; Set code and data segment registers
START:
MOV AX,DATA ; Load the address of the data segment into AX
MOV DS,AX ; Set the data segment register to the loaded address
XOR AX,AX ; Clear AX register(AX =0)
MOV CL,AL ; Clear CL register(CL =0)
MOV AX,[SI]; Load the first 32-bit value from LIST into AX
ADD AX,[SI +4]; Add the second 32-bit value from LIST to AX
MOV BX, AX ; Copy the result to BX
MOV N3, BX ; Store the result in N3
MOV AX,[SI +2]; Load the third 32-bit value from LIST into AX
ADD AX,[SI +6]; Add the fourth 32-bit value from LIST to AX
MOV DX, AX ; Copy the result to DX
MOV N4, DX ; Store the result in N4
JNC STOP ; Jump to STOP if there is no carry after the addition
INC CL ; Increment CL if there is a carry
STOP:
MOV AX,4CH ; Set the function code for program termination
INT 21H ; Invoke interrupt 21H to terminate the program
CODE ENDS
END START ; End of the program with START as the entry point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
DATA SEGMENT
LIST DD 12121212H,12121212H ; Define a doubleword array 'LIST' with two 32-bit values
N3 DW ?; Define a 16-bit data word 'N3' with an uninitialized value
N4 DW ?; Define a 16-bit data word 'N4' with an uninitialized value
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE;DS:DATA ; Set code and data segment registers
START:
MOV AX,DATA ; Load the address of the data segment into AX
MOV DS,AX ; Set the data segment register to the loaded address
XOR AX,AX ; Clear AX register(AX =0)
MOV CL,AL ; Clear CL register(CL =0)
MOV AX,[SI]; Load the first 32-bit value from LIST into AX
SUB AX,[SI +4]; Subtract the second 32-bit value from LIST from AX
MOV BX, AX ; Copy the result to BX
MOV N3, BX ; Store the result in N3
MOV AX,[SI +2]; Load the third 32-bit value from LIST into AX
SUB AX,[SI +6]; Subtract the fourth 32-bit value from LIST from AX
MOV DX, AX ; Copy the result to DX
MOV N4, DX ; Store the result in N4
JNC STOP ; Jump to STOP if there is no borrow after the subtraction
INC CL ; Increment CL if there is a borrow
STOP:
MOV AX,4CH ; Set the function code for program termination
INT 21H ; Invoke interrupt 21H to terminate the program
CODE ENDS
END START ; End of the program with START as the entry point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
DATA SEGMENT
MSG1 DB "HELLO WORLD$"; Define a null-terminated string 'MSG1'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE;DS:DATA ; Set code and data segment registers
START:
MOV AX,DATA ; Load the address of the data segment into AX
MOV DS,AX ; Set the data segment register to the loaded address
MOV DX,OFFSET MSG1 ; Load the offset of the string MSG1 into DX
MOV AH,09H ; Set the function code for printing a string
INT 21H ; Invoke interrupt 21H to print the string
MOV AH,4CH ; Set the function code for program termination
INT 21H ; Invoke interrupt 21H to terminate the program
CODE ENDS
END START ; End of the program with START as the entry point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
DATA SEGMENT
MSG1 DB "HELLO$"; Define the first null-terminated string 'MSG1'
MSG2 DB "WORLD$"; Define the second null-terminated string 'MSG2'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE;DS:DATA;; Set code and data segment registers
START:
MOV AX,DATA ; Load the address of the data segment into AX
MOV DS,AX ; Set the data segment register to the loaded address
MOV DX,OFFSET MSG1 ; Load the offset of the first string MSG1 into DX
MOV AH,09H ; Set the function code for printing a string
INT 21H ; Invoke interrupt 21H to print the first string
MOV DX, OFFSET MSG2 ; Load the offset of the second string MSG2 into DX
MOV AH,09H ; Set the function code for printing a string
INT 21H ; Invoke interrupt 21H to print the second string
CODE ENDS
END START ; End of the program with START as the entry point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
DATA SEGMENT
STRING1 DB 11H,22H,33H,44H,55H ; Define an array of bytes 'STRING1'
MSG1 DB "FOUND$"; Define a null-terminated string 'MSG1'
MSG2 DB "NOT FOUND$"; Define a null-terminated string 'MSG2'
SE DB 33H ; Define a search element 'SE'
DATA ENDS
PRINT MACRO MSG
MOV AH,09H ; Set the function code for printing a string
LEA DX,MSG ; Load the offset of the message into DX
INT 21H ; Invoke interrupt 21H to print the message
INT 3; Software breakpoint for debugging
ENDM
CODE SEGMENT
ASSUME CS:CODE;DS:DATA ; Set code and data segment registers
START:
MOV AX,DATA ; Load the address of the data segment into AX
MOV DS,AX ; Set the data segment register to the loaded address
MOV AL,SE ; Load the search element into AL
LEA SI,STRING1 ; Load the offset of the array into SI
MOV CX,04H ; Set the loop counter to the size of the array
UP:
MOV BL,[SI]; Load the byte at the current position in the array into BL
CMP AL,BL ; Compare the search element with the current array element
JZ F0 ; Jump to F0 if they are equal
INC SI ; Move to the next byte in the array
DEC CX ; Decrement the loop counter
JNZ UP ; Jump to UP if there are more elements in the array
PRINT MSG2 ; Print "NOT FOUND"if the search element is not found
JMP END1 ; Jump to END1
F0:
PRINT MSG1 ; Print "FOUND"if the search element is found
END1:
INT 3; Software breakpoint for debugging
CODE ENDS
END START ; End of the program with START as the entry point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
DATA SEGMENT
STRING1 DB 99H,12H,56H,45H,36H ; Define an array of bytes 'STRING1'
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE;DS:DATA ; Set code and data segment registers
START:
MOV AX,DATA ; Load the address of the data segment into AX
MOV DS,AX ; Set the data segment register to the loaded address
MOV CH,04H ; Set the outer loop counter (number of passes)
UP2:
MOV CL,04H ; Set the inner loop counter (number of comparisons)
LEA SI,STRING1 ; Load the offset of the array into SI
UP1:
MOV AL,[SI]; Load the current byte at the SI position
MOV BL,[SI+1]; Load the next byte in the array
CMP AL,BL ; Compare the current and next bytes
JC DOWN ; Jump to DOWN if AL is less than BL
MOV DL,[SI+1]; Swap the bytes if AL is not less than BL
XCHG [SI],DL
MOV [SI+1],DL
DOWN:
INC SI ; Move to the next pair of bytes in the array
DEC CL ; Decrement the inner loop counter
JNZ UP1 ; Jump to UP1 if there are more comparisons
DEC CH ; Decrement the outer loop counter
JNZ UP2 ; Jump to UP2 if there are more passes
INT 3; Software breakpoint for debugging
CODE ENDS
END START ; End of the program with START as the entry point
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include<stdio.h>#include<string.h>#include<stdlib.h>voidmain(){
FILE *inputFile,*outputFile;int i, j, hexAddress;char ch, line[50], address[10];// Open the input file in read mode
inputFile =fopen("loader-input.txt","r");// Read lines from the input file until the end is reacheddo{fscanf(inputFile,"%s", line);// Check if the line is a text recordif(line[0]=='T'){// Extract the address from the text recordfor(i =1, j =0; i <=6; i++, j++)
address[j]= line[i];
address[j]='\0';// Open the output file in read and write mode
outputFile =fopen("output.txt","r+");// Write the extracted address to the output file and rewind for readingfprintf(outputFile,"%s", address);rewind(outputFile);// Read the address from the output file in hexadecimal formatfscanf(outputFile,"%x",&hexAddress);// Close the output filefclose(outputFile);// Process the data part of the text record
i =9;// data part starts at 9, 0 is the record type, 1-6 is the Starting address, 7-8 is the length of the datawhile(line[i]!='\0'){// Print the address and corresponding data, Data is of 2 bytes each, so line[i] and line[i+1]printf("%x\t %c%c\n", hexAddress, line[i], line[i +1]);// Increment the address and move to the next data pair
hexAddress = hexAddress +1;
i +=2;// 2 bytes are processed}}}while(!feof(inputFile));// Close the input filefclose(inputFile);}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Algorithm for Absolute Loader:1. Open the input file in read mode.2. Read lines from the input file until the end is reached:
a. Read a line from the input file.3. Check if the line is a text record:
a. If the first character is 'T':
i. Extract the address from the text record (characters from 1st to 6th index).
ii. Open the output file in read and write mode.
iii. Write the extracted address to the output file and rewind for reading.
iv. Read the address from the output file in hexadecimal format.
v. Close the output file.4. Process the data part of the text record:
a. Set the index i to 9.
b. While the character at position i is not'\0':
i. Print the address and corresponding data.
ii. Increment the address and move to the next data pair.
iii. Increment i by 2.5. Repeat steps 2-4 until the end of the input file.6. Close the input file.
End of Algorithm.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include<stdio.h>#include<string.h>#include<stdlib.h>voidmain(){// File pointers for input and output files
FILE *inputFile,*nametableFile,*deftableFile,*argtableFile,*outputFile;// Variables for storing different fields from the inputint length, i, position =1;char argument[20], mnemonic[20], operand[20], label[20], macroName[20], macroMnemonic[20], macroOperand[20], position1[10], position2[10];// Opening input and output files
inputFile =fopen("macro-input.txt","r");
nametableFile =fopen("namtab.txt","w+");
deftableFile =fopen("deftab.txt","w+");
argtableFile =fopen("argtab.txt","w+");
outputFile =fopen("op.txt","w+");// Reading the first line from the input filefscanf(inputFile,"%s%s%s", label, mnemonic, operand);// Loop until the end of the input file is reachedwhile(strcmp(mnemonic,"END")!=0){// Check if the current line is the start of a macroif(strcmp(mnemonic,"MACRO")==0){// Writing macro name to namtab.txtfprintf(nametableFile,"%s\n", label);fseek(nametableFile,SEEK_SET,0);// Writing macro name and its operands to deftab.txtfprintf(deftableFile,"%s\t%s\n", label, operand);// Reading the next line from the input filefscanf(inputFile,"%s%s%s", label, mnemonic, operand);// Loop until MEND is encounteredwhile(strcmp(mnemonic,"MEND")!=0){// Check if operand starts with '&'if(operand[0]=='&'){sprintf(position1,"%d", position);strcpy(position2,"?");strcpy(operand,strcat(position2, position1));
position = position +1;}// Writing macro name and its operands to deftab.txtfprintf(deftableFile,"%s\t%s\n", mnemonic, operand);// Reading the next line from the input filefscanf(inputFile,"%s%s%s", label, mnemonic, operand);}// Writing MEND to deftab.txtfprintf(deftableFile,"%s", mnemonic);}else{// Check if the current line is not the start of a macrofscanf(nametableFile,"%s", macroName);// Check if the current line's mnemonic is the same as a macro nameif(strcmp(mnemonic, macroName)==0){
length =strlen(operand);// Writing operands to argtab.txtfor(i =0; i < length; i++){if(operand[i]!=',')fprintf(argtableFile,"%c", operand[i]);elsefprintf(argtableFile,"\n");}// Reset file pointers to the beginning of the filesfseek(deftableFile,SEEK_SET,0);fseek(argtableFile,SEEK_SET,0);// Reading the first line from deftab.txtfscanf(deftableFile,"%s%s", macroMnemonic, macroOperand);// Writing macro name and its operands to op.txtfprintf(outputFile,".\t%s\t%s\n", macroMnemonic, operand);// Reading the next line from deftab.txtfscanf(deftableFile,"%s%s", macroMnemonic, macroOperand);// Loop until MEND is encounteredwhile(strcmp(macroMnemonic,"MEND")!=0){// Check if operand starts with '?'if((operand[0]=='?')){fscanf(argtableFile,"%s", argument);fprintf(outputFile,"-\t%s\t%s\n", macroMnemonic, argument);}else{fprintf(outputFile,"-\t%s\t%s\n", macroMnemonic, macroOperand);}// Reading the next line from deftab.txtfscanf(deftableFile,"%s%s", macroMnemonic, macroOperand);}}else{// Writing the current line to op.txtfprintf(outputFile,"%s\t%s\t%s\n", label, mnemonic, operand);}}// Reading the next line from the input filefscanf(inputFile,"%s%s%s", label, mnemonic, operand);}// Writing the last line to op.txtfprintf(outputFile,"%s\t%s\t%s", label, mnemonic, operand);// Closing all the file pointersfclose(inputFile);fclose(nametableFile);fclose(deftableFile);fclose(argtableFile);fclose(outputFile);// Printing a success messageprintf("Successfully processed the input file!\n");}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Algorithm for Macroprocessor:1. Open files for input and output (inputFile, nameTableFile, definitionTableFile, argumentTableFile, outputFile).2. Read the first line from the input file.3. Process input file until "END" is encountered:
a. Check if the current line is a macro definition:
i. Write macro name to namtab.txt.
ii. Write macro definition to deftab.txt.
iii. Read next line from input.
iv. Process the macro until "MEND" is encountered:- Handle macro arguments.- Write macro instructions to deftab.txt.- Read next line from input.
v. Write "MEND" to deftab.txt.
b. If it's not a macro definition:
i. Check if it's in namtab.txt:- If yes, write macro arguments to argtab.txt.- Write expanded macro to op.txt.- Process the expanded macro until "MEND" is encountered.
ii. If not, write non-macro lines to op.txt.4. Write the last line to op.txt.5. Close all files.6. Print success message.
End of Algorithm.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
1. Initialize Variables:
Open necessary input and output files (inputFile, optabFile, symtabFile, intermediateFile, lengthFile).
Initialize variables like locctr, start,and others.2. Read First Line:
Read the first line from the input file (label, opcode, operand).3. Check for START:
Check if the program starts with START.
If yes, set start and initialize locctr.
Write the first line to the intermediate file.4. Process Input Until END:
Loop until the opcode is END.
Write the current locctr to the intermediate file.
If label is not"**", write it to symtab.
Search for opcode in optab to update locctr.
Handle special cases for specific opcodes.
Write the current line to the intermediate file.
Read the next line from the input file.5. Write Last Line:
Write the last line to the intermediate file.6. Calculate Program Length:
Calculate the length of the program as length = locctr - start.
Write the length to the length file.7. Print Intermediate File:
Print the content of the intermediate file.8. Print Symtab:
Print the content of the symtab.9. Print Program Length:
Print the length of the program.10. Close Files:
Close all open files.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
1. Open necessary input and output files (inputFile, outputFile, optabFile, symtabFile).2. Read the first line from the input file.3. Check if the program starts with START:
a. If true, write the header record to the outputFile.
b. Display the header record.4. Process input until END is encountered:
a. Reset flag for opcode found (flag) to 0.
b. Read opcode and mnemonic from the optabFile.
c. Search for the opcode in the optab:
i. If found and the mnemonic is not"*", set the flag.
d. If flag is set:
i. Reset flag for symbol found in symtab (flagSymbol) to 0.
ii. Search for operand in symtab:- If found, set flagSymbol and create the object code using mnemonic and symbol address.
e. If opcode is BYTE or WORD:
i. If operand is a character or hexadecimal, create the object code accordingly.
f. Write the current line to the outputFile.
g. Display the current line.
h. Read the next line from the input file.5. Write the last line to the outputFile.
a. Display the last line.6. Close all files (inputFile, outputFile, optabFile, symtabFile).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include<stdio.h>#include<string.h>#include<stdlib.h>voidmain(){
FILE *inputFile,*outputFile;int i, j, hexAddress, startHexAddress;char line[50], address[10];// Open the input file in read mode
inputFile =fopen("input.txt","r");// Prompt the user to enter the actual starting addressprintf("Enter the actual starting address in hexadecimal: ");scanf("%x",&startHexAddress);// Read lines from the input file until the end is reacheddo{fscanf(inputFile,"%s", line);// Check if the line is a text recordif(line[0]=='T'){// Extract the address from the text recordfor(i =1, j =0; i <7; i++, j++)
address[j]= line[i];
address[j]='\0';// Open the output file in read and write mode
outputFile =fopen("output.txt","r+");// Write the extracted address to the output file and rewind for readingfprintf(outputFile,"%s", address);rewind(outputFile);// Read the address from the output file in hexadecimal formatfscanf(outputFile,"%x",&hexAddress);// Close the output filefclose(outputFile);// Process the data part of the text record
i =9;while(line[i]!='\0'){// Print the relocated address and corresponding dataprintf("%x \t %c%c\n", hexAddress + startHexAddress, line[i], line[i +1]);// Increment the address and move to the next data pair
hexAddress = hexAddress +1;
i +=2;}}}while(!feof(inputFile));// Close the input filefclose(inputFile);}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Algorithm for Relocating Loader:1. Open the input file in read mode.2. Prompt the user to enter the actual starting address in hexadecimal.3. Read lines from the input file until the end is reached:
a. Read a line from the input file.4. Check if the line is a text record:
a. If the first character is 'T':
i. Extract the address from the text record (characters 2 to 7).
ii. Open the output file in read and write mode.
iii. Write the extracted address to the output file and rewind for reading.
iv. Read the address from the output file in hexadecimal format.
v. Close the output file.5. Process the data part of the text record:
a. Set the index i to 9.
b. While the character at position i is not'\0':
i. Print the relocated address (sum of hexAddress and startHexAddress)and corresponding data.
ii. Increment the address and move to the next data pair.
iii. Increment i by 2.6. Repeat steps 3-5 until the end of the input file.7. Close the input file.
End of Algorithm.