3
1

Compare commits

...

3 Commits

6 changed files with 230 additions and 0 deletions

View File

@ -0,0 +1,109 @@
# Assignment-A1
try:
source = open('source.txt','r')
data = source.read()
print('File read successfully\n\n')
source.seek(0)
except FileNotFoundError: #A little bit of exception handling
print('\n\n\nFile Not found. Create a source.txt first.\n\n\n ')
except IOError:
print('There was an IO error')
LT_index = 0 #index of LT table
ST_index = 0 #index of ST table
add = 0 # address in source code
MOT = {'STOP': '00','ADD': '01','SUB': '02','MULT': '03','MOVER': '04','MOVEM': '05','COMP': '06','BC': '07','DIV': '08','READ': '09','PRINT': '10','START': '01','END': '02','ORIGIN': '03','LTORG': '05','DS': '01','DC': '02','AREG,': '01','BREG,': '02','EQ':'01'}
ST = []
code=[]
LT=[]
MC = []
# LT, ST are lists of lists. code= intermediate code table
def classy(text):
'''This function will return the class of the word to be inputted in the Intermediate table'''
text = text.upper()
if text in ['STOP','ADD','SUB', 'MULT','MOVER','MOVEM','COMP','BC','DIV','READ', 'PRINT']:
return 'IS'
elif text in ['START','END','ORIGIN','LTORG']:
return 'AD'
elif text in ['DS','DC']:
return 'DL'
elif text in ['AREG,','BREG,']: return 'RG'
elif text in ['EQ']: return 'CC'
else: return 'None'
def handle_start():
'''This function gives you the starting address of the code'''
line= source.readline()
words=line.split()
if words[0].upper()=='START':
return int(words[1])
else:
print("No Start Statement! Abort!\n")
return 0
def pass1():
add=handle_start()
if not add:
print("Ending Pass 1 due to Above error.")
return
global ST_index, LT_index # to modify global variables, use global keyword
while True:
line=source.readline()# handlestart function reads line 1 and we start from the second line.
if not line:
break
words= line.split()
for w in words:
w=w.upper()
if w[0]=='=':
entry=[LT_index,w, add]
LT.append(entry)
LT_index +=1
elif classy(w)== 'None':
for term in ST:
if w== term[1]: break # I check if the label is already present in ST.
else:
entry=[ST_index,w, add]
ST.append(entry)
ST_index +=1
add+=1
print('LT:')
for a in LT:
print(a)
print('\n\n\nST:')
for a in ST:
print(a)
def intermediate():
source.seek(0)
while True:
entry=[]
ind = 0
line=source.readline()
if not line:
break
words=line.split()
for w in words:
w=w.upper()
if classy(w)!='None': #it is a directive
entry.append((classy(w),MOT[w]))
elif w[0]== '=': #it is a literal.
for a in LT:
if a[1]==w:
ind = a[0]
break
entry.append(('L',ind))
else: #it is a symbol
for a in ST:
if a[1]==w:
ind = a[0]
break
entry.append(('S',ind))
code.append(entry)
print("\nThe Intermediate code is:")
for entry in code:
print(entry)
pass1()
intermediate()

View File

@ -0,0 +1,4 @@
Start 100
Label MOVER AREG, =5
add areg, =999
sub breg, x

View File

@ -0,0 +1,32 @@
#include <jni.h>
#include <stdio.h>
#include "A3.h"
JNIEXPORT jint JNICALL Java_A3_add(JNIEnv *env, jobject obj, jint a, jint b) {
jint result = a + b;
printf("\n%d + %d = %d\n", a, b, result);
return result; // Return the result
}
JNIEXPORT jint JNICALL Java_A3_sub(JNIEnv *env, jobject obj, jint a, jint b) {
jint result = a - b;
printf("\n%d - %d = %d\n", a, b, result);
return result; // Return the result
}
JNIEXPORT jint JNICALL Java_A3_mult(JNIEnv *env, jobject obj, jint a, jint b) {
jint result = a * b;
printf("\n%d * %d = %d\n", a, b, result);
return result; // Return the result
}
JNIEXPORT jint JNICALL Java_A3_div(JNIEnv *env, jobject obj, jint a, jint b) {
if (b == 0) {
printf("Error: Division by zero.\n");
return 0; // Return 0 or handle error appropriately
}
jint result = a / b;
printf("\n%d / %d = %d\n", a, b, result);
return result; // Return the result
}

View File

@ -0,0 +1,45 @@
import java.io.*;
import java.util.*;
class A3 {
static {
System.loadLibrary("A3");
}
private native int add(int a, int b);
private native int sub(int a, int b);
private native int mult(int a, int b);
private native int div(int a, int b);
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a, b, ch;
System.out.println("\nEnter value of a : ");
a = sc.nextInt();
System.out.println("\nEnter value of b : ");
b = sc.nextInt();
do {
System.out.println("\nENTER YOUR CHOICE : ");
ch = sc.nextInt();
switch (ch) {
case 1:
new A3().add(a, b);
break;
case 2:
new A3().sub(a, b);
break;
case 3:
new A3().mult(a, b);
break;
case 4:
new A3().div(a, b);
break;
default:
System.out.println("Your choice is wrong.");
}
} while (ch < 5);
}
}

View File

@ -0,0 +1,36 @@
## Steps to run this code
These are the steps to run code for Assignment-A3.
---
> [!IMPORTANT]
> I've tested this on Linux, thus I've included instructions for the same.
### Prerequisites
1. open-jdk (version 11 or higher)
2. gcc
3. Common sense
### Steps
1. Compile `A3.java`:
```shell
javac A3.java
```
2. Generate header file:
```shell
javac -h . A3.java
```
3. Compile C code:
```shell
gcc -shared -o libA3.so -fPIC -I"$JAVA_HOME/include" -I"$JAVA_HOME/include/linux" A3.c
```
4. Run program:
```shell
java -Djava.library.path=. A3
```

View File

@ -8,7 +8,11 @@ This repository serves as a comprehensive resource for the Systems Programming a
### Codes ### Codes
##### Group A ##### Group A
1. [Code-A1 - Pass 1](https://git.kska.io/sppu-te-comp-content/SystemsProgrammingAndOperatingSystem/src/branch/main/Codes/Group%20A/Assignment-A1)
- [Code](https://git.kska.io/sppu-te-comp-content/SystemsProgrammingAndOperatingSystem/src/branch/main/Codes/Group%20A/Assignment-A1/Code-A1.py)
- [Source file](https://git.kska.io/sppu-te-comp-content/SystemsProgrammingAndOperatingSystem/src/branch/main/Codes/Group%20A/Assignment-A1/source.txt)
2. [Code-A2 - Pass 1 and Pass 2 of 2-Pass Macroprocessor](https://git.kska.io/sppu-te-comp-content/SystemsProgrammingAndOperatingSystem/src/branch/main/Codes/Group%20A/Code-A2.py) 2. [Code-A2 - Pass 1 and Pass 2 of 2-Pass Macroprocessor](https://git.kska.io/sppu-te-comp-content/SystemsProgrammingAndOperatingSystem/src/branch/main/Codes/Group%20A/Code-A2.py)
3. [Code-A3 - DLL](https://git.kska.io/sppu-te-comp-content/SystemsProgrammingAndOperatingSystem/src/branch/main/Codes/Group%20A/Assignment-A3/)
##### Group B ##### Group B
5. [CPU Scheduling Algorithms: FCFS, SJF (Preemptive), Priority (Non-Preemptive) and Round Robin (Preemptive)](https://git.kska.io/sppu-te-comp-content/SystemsProgrammingAndOperatingSystem/src/branch/main/Codes/Group%20B/Assignment%20-%205) 5. [CPU Scheduling Algorithms: FCFS, SJF (Preemptive), Priority (Non-Preemptive) and Round Robin (Preemptive)](https://git.kska.io/sppu-te-comp-content/SystemsProgrammingAndOperatingSystem/src/branch/main/Codes/Group%20B/Assignment%20-%205)