Spos Practical Codes
Spos Practical Codes
GROUP A
Practical- 1
Design suitable Data structures and implement Pass-I and Pass-Il of a two-pass
assembler for pseudo-machine. Implementation should consist of a few instructions from
each category and few assembler directives. The output of Pass-1 (intermediate code le
and symbol table) should be input for Pass-II.
Ans.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>
// Data structures for symbol table, literal table, and intermediate code
unordered_map<string, int> symbolTable; // Symbol Table
vector<string> literalTable; // Literal Table
vector<string> intermediateCode; // Intermediate Code
int main() {
const string sourceFilename = "source.asm";
// Pass I
passI(sourceFilename);
// Pass II
generateMachineCode(); // Note: Using a separate function for Pass II
// Generate output les
generateFiles();
return 0;
}
Telegram Channel
https://t.me/SPPU_TE_BE_COMP
(for all engineering Resources)
WhatsApp Channel
(for all tech updates)
https://whatsapp.com/channel/0029ValjFriICVfpcV9HFc3b
Insta Page
(for all engg & tech updates)
https://www.instagram.com/sppu_engineering_update
string line;
int locationCounter = 0;
if (!sourceFile.is_open()) {
cerr << "Error opening source le!" << endl;
return;
}
// Identify literals
if (!operand.empty() && operand[0] == '=') {
string literal = operand.substr(1); // Remove '='
if ( nd(literalTable.begin(), literalTable.end(), literal) == literalTable.end()) {
literalTable.push_back(literal);
}
}
}
sourceFile.close();
}
void generateMachineCode() {
cout << "Pass II Output (Machine Code):" << endl;
cout << "Address\tMachine Code" << endl;
void generateFiles() {
// Write symbol table to a le
ofstream symbolFile("symbol_table.txt");
symbolFile << "Symbol Table:\n";
for (const auto& entry : symbolTable) {
symbolFile << entry. rst << "\t" << entry.second << endl;
}
symbolFile.close();
Practical NO- 2
Design suitable data structures and implement Pass-1 and Pass-ll of a two-pass macro-
processor. The output of Pass-I (MNT, MDT and intermediate code le without any macro
de nitions) should be input for Pass-II.
Ans.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
if (parts[0].equalsIgnoreCase("MACRO")) {
ag = 1; // Indicate we're in a macro de nition
line = br.readLine();
parts = line.split("\\s+");
macroname = parts[0];
if (parts.length <= 1) {
fi
fl
fi
fi
fl
fi
mnt.write(parts[0] + "\t" + pp + "\t" + kp + "\t" + mdtp + "\t" + (kp == 0 ? kpdtp :
(kpdtp + 1)) + "\n");
continue;
}
} else if (parts[0].equalsIgnoreCase("MEND")) {
mdt.write(line + "\n");
ag = kp = pp = 0;
mdtp++;
paramNo = 1;
pnt.write(macroname + ":\t");
Iterator<String> itr = pntab.keySet().iterator();
while (itr.hasNext()) {
pnt.write(itr.next() + "\t");
}
pnt.write("\n");
pntab.clear();
} else if ( ag == 1) {
for (int i = 0; i < parts.length; i++) {
if (parts[i].contains("&")) {
parts[i] = parts[i].replaceAll("[&,]", "");
mdt.write("(P," + pntab.get(parts[i]) + ")\t");
} else {
mdt.write(parts[i] + "\t");
}
}
mdt.write("\n");
mdtp++;
} else {
ir.write(line + "\n");
fl
fl
}
}
br.close();
mdt.close();
mnt.close();
ir.close();
pnt.close();
kpdt.close();
GROUP B-
Practical no 3-
Ans.
import java.util.*;
class Process {
String name;
int arrivalTime;
int burstTime;
int priority; // Only used for Priority Scheduling
int startTime;
int completionTime;
int waitingTime;
int turnaroundTime;
int remainingTime; // Only used for SJF and Round Robin
if (p.remainingTime == 0) {
p.completionTime = time;
p.turnaroundTime = p.completionTime - p.arrivalTime;
p.waitingTime = p.turnaroundTime - p.burstTime;
} else {
queue.add(p); // Re-add process if not completed
fi
fi
}
} else {
time++; // Idle time
}
}
printGanttChart(new ArrayList<>(processMap.values()), "Round Robin Scheduling");
}
scanner.close();
}
}
Practical No -4
Ans.
import java.util.*;
class Block {
int id;
int size;
int start; // Start of allocation
int end; // End of allocation
class Process {
String name;
int size;
int allocatedBlockId;
scanner.close();
}
}
fi