CPU Scheduling

001. fcfscpuscheduling.c

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 // Formula to study // TAT = CT - AT // WT = TAT - BT #include <stdio.h> void main() { // Declare variables int i = 0, j = 0, burst[20], arrival[20], completion[20], process[20], waitingtime[20], tat[20], n = 0, m; float avgw = 0, avgt = 0; // Prompt user for the number of processes printf("Enter the number of processes: "); scanf("%d", &n); // Input process details for (i = 0; i < n; i++) { printf("Process ID: "); scanf("%d", &process[i]); printf("Arrival time: "); scanf("%d", &arrival[i]); printf("Burst time: "); scanf("%d", &burst[i]); } // Sort processes based on arrival time using bubble sort int temp = 0; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1; j++) { if (arrival[j] > arrival[j + 1]) { // Swap arrival time temp = arrival[j]; arrival[j] = arrival[j + 1]; arrival[j + 1] = temp; // Swap burst time temp = burst[j]; burst[j] = burst[j + 1]; burst[j + 1] = temp; // Swap process ID temp = process[j]; process[j] = process[j + 1]; process[j + 1] = temp; } } } // Calculate completion time for each process completion[0] = 0; for (i = 0; i < n; i++) { completion[i + 1] = completion[i] + burst[i]; } // Calculate turnaround time and waiting time for each process for (i = 0; i < n; i++) { tat[i] = completion[i + 1] - arrival[i]; waitingtime[i] = tat[i] - burst[i]; avgw += waitingtime[i]; avgt += tat[i]; } // Calculate average waiting time and average turnaround time avgw = avgw / n; avgt = avgt / n; // Print process details and performance metrics printf("PID\tAT\tBT\tCT\tWT\tTAT\n"); for (i = 0; i < n; i++) { printf("%d\t%d\t%d\t%d\t%d\t%d\n", process[i], arrival[i], burst[i], completion[i + 1], waitingtime[i], tat[i]); } printf("Average waiting time: %f\n", avgw); printf("Average turnaround time: %f\n", avgt); }

002. sjfcpuscheduling.c

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 #include<stdio.h> void main(){ // Variable declarations int i=0,j=0,k=1,btime=0,burst[20],arrival[20],completion[20],process[20],waitingtime[20],tat[20],n=0,m,min=0; float avgw = 0, avgt = 0; // Input number of processes printf("Enter the number of processes"); scanf("%d",&n); // Input process details for(i=0;i<n;i++){ printf("Process ID\t"); scanf("%d",&process[i]); printf("Arrival time\t"); scanf("%d",&arrival[i]); printf("Burst time\t"); scanf("%d",&burst[i]); } int temp = 0; // Sort processes based on arrival time using bubble sort for(i=0;i<n-1;i++){ for(j=0;j<n-1;j++){ if(arrival[j]>arrival[j+1]){ // Swap arrival time temp = arrival[j]; arrival[j] = arrival[j+1]; arrival[j+1] = temp; // Swap burst time temp = burst[j]; burst[j] = burst[j+1]; burst[j+1] = temp; // Swap process ID temp = process[j]; process[j] = process[j+1]; process[j+1] = temp; } } } // Schedule the processes for(i=0;i<n;i++){ btime = btime+burst[i]; // Updating burst time min = burst[k]; // Initializing the min variable // It compares the burst time of each process // with the current minimum burst time (min) // to find the process with the shortest remaining burst time. for(j=k;j<n;j++){ if(btime>=arrival[j] && burst[j]<min){ // Swap arrival time temp=arrival[j]; arrival[j]=arrival[j-1]; arrival[j-1]=temp; // Swap burst time temp=burst[j]; burst[j]=burst[j-1]; burst[j-1]=temp; // Swap process ID temp=process[j]; process[j]=process[j-1]; process[j-1]=temp; } } k++; } // Calculate completion time, waiting time, and turnaround time completion[0] = arrival[0]; for(i=0;i<n;i++){ completion[i+1] = completion[i] + burst[i]; if(completion[i]<arrival[i]) completion[i] = arrival[i]; } for(i=0;i<n;i++){ tat[i]=completion[i+1]-arrival[i]; waitingtime[i]=tat[i]-burst[i]; avgw+=waitingtime[i]; avgt+=tat[i]; } avgw=avgw/n; avgt=avgt/n; // Print the results printf("pid\tAT\tBT\tCT \tWT \t TAT\n "); for(i=0;i<n;i++){ printf("%d\t%d\t%d\t%d\t%d\t%d\n",process[i],arrival[i],burst[i],completion[i+1],waitingtime[i],tat[i]); } printf("average waiting time = %f \n",avgw); printf("average turn around time = %f \n",avgt); }

003. roundrobincpuscheduling.c

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 #include <stdio.h> int main() { int i, limit, total = 0, procnum, counter = 0, time_quantum; int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], remainingTime[10]; float avg_wait_time, avg_turnaround_time; printf("\nEnter the total number of processes\n"); scanf("%d", &limit); procnum = limit; for (i = 0; i < limit; i++) { // Entering process details printf("\nEnter details of process[%d]", i + 1); printf("Arrival time:\t"); scanf("%d", &arrival_time[i]); printf("Burst time:\t"); scanf("%d", &burst_time[i]); remainingTime[i] = burst_time[i]; } printf("Enter Time Quantum\t"); scanf("%d", &time_quantum); printf("\nProcess ID\tBurst time\tTurnaround time\tWaiting time"); for (i = 0; procnum != 0;) { if (remainingTime[i] <= time_quantum && remainingTime[i] > 0) { total = total + remainingTime[i]; remainingTime[i] = 0; counter = 1; } else if (remainingTime[i] > 0) { remainingTime[i] = remainingTime[i] - time_quantum; total = total + time_quantum; } if (remainingTime[i] == 0 && counter == 1) { procnum--; printf("\nProcess[%d]\t\t%d\t\t%d\t\t%d\n", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]); wait_time = wait_time + total - arrival_time[i] - burst_time[i]; turnaround_time = turnaround_time + total - arrival_time[i]; counter = 0; } if (i == limit - 1) { i = 0; } else if (arrival_time[i + 1] <= total) { i++; } else { i = 0; } } avg_wait_time = wait_time * 1.0 / limit; avg_turnaround_time = turnaround_time * 1.0 / limit; printf("Average waiting time:\t%f", avg_wait_time); printf("Average turnaround time:\t%f", avg_turnaround_time); return 0; }

004. prioritycpuscheduling.c

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> int main(){ 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 processes printf("Enter the number of processes"); scanf("%d",&limit); // Input burst time and priority for each process printf("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 sort for (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 process printf("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 time printf("Average waiting time %2f\n",avg_wait_time); printf("Average Turnaround Time %2f \n",avg_turnaround_time); return 0; }

Bankers Algorithm

bankersalgorithm.c

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> int main() { // Number of processes (n) and resources (m) int n, m, i, j, k; // Input: Number of processes printf("Enter the number of processes: \n"); scanf("%d", &n); // Input: Number of resources printf("Enter the number of resources:\n"); scanf("%d", &m); // Allocation matrix, Max matrix, and Available resources int allocationMatrix[n][m], maxMatrix[n][m], availableResources[m]; // Input: Allocation matrix printf("Enter the Allocation matrix: \n"); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf("%d", &allocationMatrix[i][j]); } } // Input: Max matrix printf("Enter the Max matrix: \n"); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf("%d", &maxMatrix[i][j]); } } // Input: Available resources printf("Enter the Available resources: \n"); for (i = 0; i < m; i++) { scanf("%d", &availableResources[i]); } // Arrays to keep track of processed and safe sequences int processedProcesses[n], safeSequence[n], index = 0; // Initialize processedProcesses array for (k = 0; k < n; k++) { processedProcesses[k] = 0; } // Calculate the Need matrix int 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 sequence for (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 state int 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 safe if (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]); } return 0; }

Disk Schedling

001. fcfsdiskscheduling.c

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #include <stdio.h> #include <stdlib.h> void fcfsDiskScheduling(int requests[], int numRequests, int head) { int totalSeekTime = 0; int current = head; printf("Sequence: "); printf("%d", current); for (int i = 0; i < numRequests; i++) { int distance = abs(current - requests[i]); totalSeekTime += distance; current = requests[i]; printf(" -> %d", current); } printf("\nTotal Seek Time: %d\n", totalSeekTime); } int main() { int requests[] = {98, 183, 37, 122, 14, 124, 65, 67}; int numRequests = sizeof(requests) / sizeof(requests[0]); int head = 53; fcfsDiskScheduling(requests, numRequests, head); return 0; }

002. scandiskscheduling.c

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> int main() { int ioq[20], i, n, j, ihead, temp, scan, tot; float seek = 0, avgs; // Prompt user for the number of requests printf("Enter the number of requests: "); scanf("%d", &n); // Prompt user for the initial head position printf("Enter the initial head position: "); scanf("%d", &ihead); ioq[0] = ihead; ioq[1] = 0; n += 2; // Prompt user for I/O queue requests printf("Enter the I/O queue requests:\n"); for (i = 2; i < n; i++) { scanf("%d", &ioq[i]); } // Sort the I/O queue requests for (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 queue for (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 direction for (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 direction for (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); return 0; }

003. cscandiskscheduling.c

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> void main() { int ioq[20], i, n, j, ihead, itail, temp, scan, tot = 0; float seek = 0, avgs; // Prompt user for the number of requests printf("Enter the number of requests: "); scanf("%d", &n); ioq[0] = 0; // Prompt user for the initial head position printf("Enter the initial head position: "); scanf("%d", &ihead); ioq[1] = ihead; // Prompt user for the maximum track limit printf("Enter the maximum track limit: "); scanf("%d", &itail); ioq[2] = itail; n += 3; // Prompt user for I/O queue requests printf("Enter the I/O queue requests:\n"); for (i = 3; i < n; i++) { scanf("%d", &ioq[i]); } // Sort the I/O queue requests for (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 queue for (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); }

MASM Programs

How to use DosBox to execute masm programs?

  1. Press D: (To change the drive)
  2. cd yourfolder
  3. Edit test.asm
  4. ALT+F+X quit
  5. masm test.asm
  6. link test.obj
  7. debug test.exe
  8. Visit this link for debugging commands in MASM

001.16ADDN.asm

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

002.16SUB.asm

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

003.16MULT.asm

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

004.16DIV.asm

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

005.32ADD.asm

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

006.32SUB.asm

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

007.DISPLAY.ASM

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

008.CONCAT.asm

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

009.SEARCH.ASM

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

010.SORT.asm

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

System Software

absoluteloader.c

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> void main() { 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 reached do { fscanf(inputFile, "%s", line); // Check if the line is a text record if (line[0] == 'T') { // Extract the address from the text record for (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 reading fprintf(outputFile, "%s", address); rewind(outputFile); // Read the address from the output file in hexadecimal format fscanf(outputFile, "%x", &hexAddress); // Close the output file fclose(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 data while (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 file fclose(inputFile); }

absoluteloader.md

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.

macroprocessor.c

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> void main() { // File pointers for input and output files FILE *inputFile, *nametableFile, *deftableFile, *argtableFile, *outputFile; // Variables for storing different fields from the input int 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 file fscanf(inputFile, "%s%s%s", label, mnemonic, operand); // Loop until the end of the input file is reached while (strcmp(mnemonic, "END") != 0) { // Check if the current line is the start of a macro if (strcmp(mnemonic, "MACRO") == 0) { // Writing macro name to namtab.txt fprintf(nametableFile, "%s\n", label); fseek(nametableFile, SEEK_SET, 0); // Writing macro name and its operands to deftab.txt fprintf(deftableFile, "%s\t%s\n", label, operand); // Reading the next line from the input file fscanf(inputFile, "%s%s%s", label, mnemonic, operand); // Loop until MEND is encountered while (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.txt fprintf(deftableFile, "%s\t%s\n", mnemonic, operand); // Reading the next line from the input file fscanf(inputFile, "%s%s%s", label, mnemonic, operand); } // Writing MEND to deftab.txt fprintf(deftableFile, "%s", mnemonic); } else { // Check if the current line is not the start of a macro fscanf(nametableFile, "%s", macroName); // Check if the current line's mnemonic is the same as a macro name if (strcmp(mnemonic, macroName) == 0) { length = strlen(operand); // Writing operands to argtab.txt for (i = 0; i < length; i++) { if (operand[i] != ',') fprintf(argtableFile, "%c", operand[i]); else fprintf(argtableFile, "\n"); } // Reset file pointers to the beginning of the files fseek(deftableFile, SEEK_SET, 0); fseek(argtableFile, SEEK_SET, 0); // Reading the first line from deftab.txt fscanf(deftableFile, "%s%s", macroMnemonic, macroOperand); // Writing macro name and its operands to op.txt fprintf(outputFile, ".\t%s\t%s\n", macroMnemonic, operand); // Reading the next line from deftab.txt fscanf(deftableFile, "%s%s", macroMnemonic, macroOperand); // Loop until MEND is encountered while (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.txt fscanf(deftableFile, "%s%s", macroMnemonic, macroOperand); } } else { // Writing the current line to op.txt fprintf(outputFile, "%s\t%s\t%s\n", label, mnemonic, operand); } } // Reading the next line from the input file fscanf(inputFile, "%s%s%s", label, mnemonic, operand); } // Writing the last line to op.txt fprintf(outputFile, "%s\t%s\t%s", label, mnemonic, operand); // Closing all the file pointers fclose(inputFile); fclose(nametableFile); fclose(deftableFile); fclose(argtableFile); fclose(outputFile); // Printing a success message printf("Successfully processed the input file!\n"); }

macroprocessor.md

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.

pass1assembler.c

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 #include <stdio.h> #include <string.h> #include <stdlib.h> int main() { // Input buffer char line[50]; // File pointers FILE *inputFile = fopen("pass1-input.txt", "r"); FILE *opTableFile = fopen("pass1-optab.txt", "r"); FILE *symbolTableFile = fopen("symtab.txt", "w"); FILE *intermediateFile = fopen("infile.txt", "w"); FILE *lengthFile = fopen("length.txt", "w"); // Program attributes int startingAddress; // address specified in START directive int locationCounter = 0; // current location counter int programLength = 0; // length of the program // Instruction components char label[10]; // label char opcode[10]; // mnemonic char operand[10]; // operand // Opcode table entries char mnemonicCode[10]; // mnemonic from opcode table char machineCode[10]; // machine code for the mnemonic // Read the first line fgets(line, sizeof(line), inputFile); // Extract label, opcode, and operand sscanf(line, "%s %s %s", label, opcode, operand); printf("Label opcode and operand are %s %s %s\n", label, opcode, operand); // Check for START directive if (strcmp(opcode, "START") == 0) { // Get starting address from operand startingAddress = atoi(operand); locationCounter = startingAddress; // Write the START line to the intermediate file fprintf(intermediateFile, "**\t%s\t%s\t%s\n", label, opcode, operand); // Read the next line fgets(line, sizeof(line), inputFile); // Extract label, opcode, and operand sscanf(line, "%s %s %s", label, opcode, operand); } // Main loop while (strcmp(opcode, "END") != 0) { printf("%s\n", opcode); // Write the location counter to the intermediate file fprintf(intermediateFile, "%d\t", locationCounter); // Check if label exists if (strcmp(label, "**") != 0) { // Add label and location counter to the symbol table fprintf(symbolTableFile, "%s\t%d\n", label, locationCounter); } // Search for the opcode in the optable, so we can get the address fscanf(opTableFile, "%s %s", mnemonicCode, machineCode); while (strcmp(mnemonicCode, "END") != 0) { if (strcmp(opcode, mnemonicCode) == 0) { // Update location counter for instruction locationCounter += 3; break; } // Reads the next line fscanf(opTableFile, "%s %s", mnemonicCode, machineCode); } // Handle special instructions if (strcmp(opcode, "WORD") == 0) { locationCounter += 3; } else if (strcmp(opcode, "RESW") == 0) { locationCounter += (3 * atoi(operand)); } else if (strcmp(opcode, "RESB") == 0) { locationCounter += atoi(operand); } else if (strcmp(opcode, "BYTE") == 0) { locationCounter += 1; } // Write the line to the intermediate file fprintf(intermediateFile, "%s\t%s\t%s\n", label, opcode, operand); // Read the next line fgets(line, sizeof(line), inputFile); // Extract label, opcode, and operand sscanf(line, "%s %s %s", label, opcode, operand); } // Write the last line to the intermediate file fprintf(intermediateFile, "%d\t%s\t%s\t%s\n", locationCounter, label, opcode, operand); // Calculate program length programLength = locationCounter - startingAddress; // Write program length to file fprintf(lengthFile, "%d", programLength); // Close all files fclose(inputFile); fclose(opTableFile); fclose(symbolTableFile); }

pass1assembler.md

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.

pass2assembler.c

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 #include <stdio.h> #include <string.h> #include <stdlib.h> void main() { char currentChar, label[10], opcode[10], operand[10], code[10], mnemonic[10], read[50], address[20], opcodeWithAddress[20], symbol[20]; int locctr, flag, flag1, location, loc; FILE *inputFile, *outputFile, *opcodeFile, *symtabFile; // Open files for reading and writing inputFile = fopen("pass2-input.txt", "r"); outputFile = fopen("twoout.txt", "w"); opcodeFile = fopen("opcode.txt", "r"); symtabFile = fopen("pass1-symtab.txt", "r"); // Read the first line of input file fscanf(inputFile, "%s\t%s\t%s\n", label, opcode, operand); // Check if the program starts with START if (strcmp(opcode, "START") == 0) { // Write to output and print on console fprintf(outputFile, "%s\t%s\t%s\n", label, opcode, operand); printf("%s\t%s\t%s\n", label, opcode, operand); // Read the next line fscanf(inputFile, "%d\t%s\t%s\t%s\n", &locctr, label, opcode, operand); } // Continue reading until END opcode is encountered while (strcmp(opcode, "END") != 0) { flag = 0; // Read opcode and mnemonic from opcode file fscanf(opcodeFile, "%s%s", code, mnemonic); // Check if the opcode exists in the opcode file while (strcmp(code, "END") != 0) { if ((strcmp(opcode, code) == 0) && (strcmp(mnemonic, "*") != 0)) { flag = 1; break; } fscanf(opcodeFile, "%s%s", code, mnemonic); } if (flag == 1) { flag1 = 0; // Search for the operand in the symbol table rewind(symtabFile); while (!feof(symtabFile)) { fscanf(symtabFile, "%s%d", symbol, &loc); if (strcmp(symbol, operand) == 0) { flag1 = 1; break; } } if (flag1 == 1) { // Create the address with symbol's location sprintf(address, "%d", loc); strcpy(opcodeWithAddress, strcat(mnemonic, address)); } } else if (strcmp(opcode, "BYTE") == 0 || strcmp(opcode, "WORD") == 0) { if ((operand[0] == 'C') || (operand[0] == 'x')) { currentChar = operand[2]; sprintf(address, "%d", currentChar); strcpy(opcodeWithAddress, address); } else { strcpy(opcodeWithAddress, address); } } else { strcpy(opcodeWithAddress, "\0"); } // Write to output and print on console fprintf(outputFile, "%s\t%s\t%s\t%d\t%s\n", label, opcode, operand, locctr, opcodeWithAddress); printf("%s\t%s\t%s\t%d\t%s\n", label, opcode, operand, locctr, opcodeWithAddress); // Read the next line fscanf(inputFile, "%d\t%s\t%s\t%s\n", &locctr, label, opcode, operand); } // Write the last line to output and print on console fprintf(outputFile, "%s\t%s\t%s\t%d\n", label, opcode, operand, locctr); printf("%s\t%s\t%s\t%d\n", label, opcode, operand, locctr); // Close all opened files fclose(inputFile); fclose(outputFile); fclose(opcodeFile); fclose(symtabFile); }

pass2assembler.md

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).

relocatingloader.c

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> void main() { 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 address printf("Enter the actual starting address in hexadecimal: "); scanf("%x", &startHexAddress); // Read lines from the input file until the end is reached do { fscanf(inputFile, "%s", line); // Check if the line is a text record if (line[0] == 'T') { // Extract the address from the text record for (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 reading fprintf(outputFile, "%s", address); rewind(outputFile); // Read the address from the output file in hexadecimal format fscanf(outputFile, "%x", &hexAddress); // Close the output file fclose(outputFile); // Process the data part of the text record i = 9; while (line[i] != '\0') { // Print the relocated address and corresponding data printf("%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 file fclose(inputFile); }

relocatingloader.md

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.

8086 Programs

16bitaddn.asm

1 2 3 4 5 6 7 8 MOV AX,[2000] MOV BX,[2002] MOV [2004],0000 ADD AX,BX JNC L1 MOV [2004], 0001 L1: MOV[2006],AX HLT

16bitdiv.asm

1 2 3 4 5 6 7 MOV AX,[3000] MOV BX,[3002] DIV BX MOV[3004],AX MOV AX,DX MOV[3006],AX HLT

16bitmul.asm

1 2 3 4 5 6 7 8 9 MOV AX, [3000] MOV BX, [3002] MUL BX MOV [3004], AX MOV AX,DX MOV [3006],AX HLT

16bitsub.asm

1 2 3 4 5 6 7 8 9 10 11 MOV AX,[2000] MOV BX,[2002] MOV [2004],0000 SUB AX,BX JNB L1 MOV [2004],0001 L1:MOV[2006],AX HLT

8bitaddn.asm

1 2 3 4 5 MOV AL,[2000] MOV BL,[2002] ADD AL,BL MOV[2004],AL HLT

8bitdiv.asm

1 2 3 4 5 MOV AL, [2000] MOV BL, [2002] DIV BL MOV [2004],AL HLT

8bitmul.asm

1 2 3 4 5 MOV AL,[2000] MOV BL,[2002] MUL BL MOV [2004],AL HLT

8bitsub.asm

1 2 3 4 5 MOV AL [2000] MOV BL [2002] SUB AL,BL MOV [2004],AL HLT

AND.asm

1 2 3 4 MOV AX,[3000] AND AX,0F0F MOV [1400],AX HLT

NOT.asm

1 2 3 4 MOV AX,[3000] NOT AX MOV [1400],AX HLY

OR.asm

1 2 3 4 MOV AX,[3000] OR AX,0F0F MOV [1400],AX HLT

ROL.asm

1 2 3 4 5 MOV CL,1 MOV AL,[3000] ROL AL,CL MOV [2500],AL HLT

ROR.asm

1 2 3 4 5 MOV CL,1 MOV AL,[3000] ROR AL,CL MOV [2500],AL HLT

SAR.asm

1 2 3 4 5 MOV CL,3 MOV AL,[3000] SAR AL,CL MOV [2500],AL HLT

SHL.asm

1 2 3 4 5 MOV CL,1 MOV AL,[3000] SHL AL,CL MOV [2500],AL HLT

SHR.asm

1 2 3 4 5 MOV CL,2 MOV AL,CL SHR AL,CL MOV [2500],AL HLT

linearsearch.asm

1 2 3 4 5 6 7 8 9 10 11 12 13 14 MOV SI,3000 MOV AX,[4000] MOV CX,000A L2: CMP AX,[SI] JE L1 INC SI DEC CX CMP CX,0000 JG L2 MOV BX,0000 JMP L3 L1:MOV BX,0001 MOV [2000],BX HLT

8051 Programs

8bitadd.asm

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 MOV R1,#00 MOV DPTR,#8200 MOVX A,@DPTR MOV R0,A INC DPTR MOVX A,@DPTR ADD A,R0 JNC 800D INC R1 MOV DPTR,#8300 MOVX @DPTR,A INC DPTR MOV A,R1 MOVX @DPTR,A SJMP 8014

8bitmul.asm

1 2 3 4 5 6 7 8 9 10 11 12 13 MOV DPTR, #8200 MOVX A,@DPTR MOV R0,ADD INC DPTR MOVX A,@DPTR MUL AB MOV DPTR,#8300 MOVX @DPTR,A MOV A,0F0F INC DPTR MOVX @DPTR,A SJMP 8011

8bitsub.asm

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 MOV R1,#00 MOV DPTR,#8200 MOV X A,@DPTR MOV R0,A INC DPTR MOVX A,@DPTR CLR C SUBB A,R0 JNC 800E INC R1 MOV DPTR,#8300 MOVX @DPTR,A INC DPTR MOV A,R1 MOVX @DPTR,A SJMP 8015