You are on page 1of 4

Assignment #2

Subject : System programming


Course Code: CS-4103
Submitted To : Ma’am Sumaira Shafiq
Submitted By:

Name Roll No.


Usman Aziz 37

Semester: BS(CS) 7th


Session: 2019-2023
Submission Date: 18-04-2023

University of Poonch Rawalakot


Department Of Computer Science & IT

1
Q1

Implement single pass Assembler

Step 1: Start the program execution.

Step 2: Assembler simply generate object code as it scans the source code.

Step 3: If the instruction operand is a symbol text, has not yet been defined, the operands address is

omitted.

Step 4: When nearly half the program translation is over, some of the forward reference problem are

existed.

Step 5: Combine the process to the end of the program to fill forward reference property.

Step 6: At the end of the program, the symbol table entries with 'x' are undefined.

Step 7: Stop the program execution.

Ans:

#include<stdio.h> #include<conio.h> #include<string.h>

#include<stdlib.h> #include<iostream>

using namespace std;

void clrscr() {

system("cls"); }

int main()

{FILE *f1,*f2;

char ch,str[30],str1[10],str2[30],cstr[15];

int i,j,num,q,r;

clrscr();

cout<<"Enter your assembly instructions"<<endl;

f1=fopen("asin","w");

while(1) { ch=getchar();
2
if(ch=='*')

break;

fputc(ch,f1);}

fclose(f1);

f1=fopen("asin","r");

f2=fopen("asout","w");

while(1){

fgets(str,25,f1);

strncpy(str1,str,3);

str1[3]='\0';

j=0;

for(i=3;i<strlen(str);i++)

{str2[j]=str[i];

j++; } str2[j]='\0';

if((strcmp(str1,"lda"))==0)

{fputs("3a \t",f2);

fputs(str2,f2);

} else if((strcmp(str1,"mov"))==0)

{fputs("47\n",f2);

} else if((strcmp(str1,"add"))==0)

{fputs("80\n",f2);

} else if((strcmp(str1,"sub"))==0)

{fputs("90\n",f2);

} else if((strcmp(str1,"hlt"))==0)

3
{f7puts("76\n",f2);

break;

} else if((strcmp(str1,"sta"))==0)

{fputs("32\t",f2);

num=atoi(str2);

q=num/100; r=num%100;

if(r==0)

fputs("00\t",f2);

else

fputs(itoa(r,cstr,10),f2);

fputs("\t",f2);

fputs(itoa(q,cstr,10),f2); fputs("\n",f2);

} else

{fputs("error\n",f2);}}

fclose(f1); fclose(f2);

f2=fopen("asout","r");

cout<<endl<<"THE OBJECT CODE CONTENTS"<<endl;

ch=fgetc(f2);

while(ch!=EOF) {putchar(ch); Output

ch=fgetc(f2);}

fclose(f2);

getch();

You might also like