One Pass Macro Processor Program
One Pass Macro Processor Program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
FILE *f1,*f2,*f3,*f4,*f5;
void main()
{
char lbl[20],opc[20],opr[20],mname[20],arg[20],check[20];char
ch,dlbl[20],dopc[20],dopr[20];
int c;
f1=fopen("input.txt","r");
rewind(f1);
f2=fopen("namtab.txt","r");
rewind(f2);
f3=fopen("deftab.txt","r");
f4=fopen("op.txt","w");
f5=fopen("argtab.txt","w");
while(!feof(f1))
{
l1:
fscanf(f1,"%s %s %s",lbl,opc,opr);
if(strcmp(opc,mname)==0)
c=1;
if(strcmp(opc,"MACRO")==0)
{
while(strcmp(opc,"MEND")!=0)
{
fscanf(f1,"%s%s%s",lbl,opc,opr);
continue;
}
goto l1;
}
rewind(f2);
rewind(f3);
fscanf(f2,"%s",mname);
if(strcmp(opc,mname)==0)
{
fprintf(f5," %s",opr);
rewind(f5);
while(!feof(f3))
{
fscanf(f3,"%s%s%s",dlbl,dopc,dopr);
if(strcmp(dopc,"MEND")!=0)
{
if(strcmp(dopc,"MACRO")==0)
{
continue;
}
if(strcmp(dopr,"=X'?1'")==0)
strcpy(dopr,"=X'F1'");
if(strcmp(dopr,"?2,X")==0)
strcpy(dopr,"BUFFER,X");
if(strcmp(dopr,"?3")==0)
strcpy(dopr,"LENGTH");
if(c==1)
{
fprintf(f4," %s\t%s\t%s\n",lbl,opc,opr);
c=0;
}
fprintf(f4," %s\t%s\t%s\n",dlbl,dopc,dopr);
}
}
goto l1;
}
fprintf(f4," %s\t%s\t%s\n",lbl,opc,opr);
}
fclose(f1);
fclose(f2);
fclose(f3);
fclose(f4);
fclose(f5);
printf("\n INPUT\n\n Macro Program before expanded \n");
printf(" ---------------------------------\n");
f1=fopen("input.txt","r");
ch=fgetc(f1);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(f1);
}
printf("\n Definition Table \n");
printf(" ---------------------------------\n");
f2=fopen("deftab.txt","r");
ch=fgetc(f2);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(f2);
}
printf("\n Name Table \n");
printf(" ---------------------------------\n");
f3=fopen("namtab.txt","r");
ch=fgetc(f3);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(f3);
}
printf("\n\n OUTPUT\n\n Macro Program after expanded \n");
printf(" ---------------------------------\n\n");
f4=fopen("op.txt","r");
ch=fgetc(f4);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(f4);
}
printf("\n Argument Table \n");
printf(" ---------------------------------\n\n");
f5=fopen("argtab.txt","r");
ch=fgetc(f5);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(f5);
}
fclose(f1);
fclose(f2);
fclose(f3);
fclose(f4);
fclose(f5);
}
OUTPUT:
INPUT
Macro Program before expanded
---------------------------------
COPY START NULL
RDBUFF MACRO INDEV,BUFADR,RECLTH
NULL CLEAR X
NULL CLEAR A
NULL CLEAR S
NULL +LDT #4096
NULL TD =X'&INDEV'
NULL JEQ *-3
NULL RD =X'&INDEV'
NULL COMPR A,S
NULL JEQ *+11
NULL STCH BUFADR,X
NULL TIXR T
NULL JLT *-19
NULL STX RECLTH
NULL MEND NULL
FIRST STL RETADR
CLOOP RDBUFF F1,BUFFER,LENGTH
NULL LDA LENGTH
NULL COMP #0
NULL JEQ ENDFIL
EOF BYTE C'EOF'
THREE WORD 3
RETADR RESW 1
LENGTH RESW 1
BUFFER RESB 4096
NULL END FIRST
Definition Table
---------------------------------
COPY START NULL
RDBUFF MACRO &INDEV,&BUFADR,&RECLTH
NULL CLEAR X
NULL CLEAR A
NULL CLEAR S
NULL +LDT #4096
NULL TD =X'?1'
NULL JEQ *-3
NULL RD =X'?1'
NULL COMPR A,S
NULL JEQ *+11
NULL STCH ?2,X
NULL TIXR T
NULL JLT *-19
NULL STX ?3
NULL MEND NULL
Name Table
---------------------------------
RDBUFF
OUTPUT
Argument Table
---------------------------------
F1,BUFFER,LENGTH
Absolute Loader Program
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char input[10],label[10],ch1,ch2;
int addr, w=0, start, ptaddr, l, length=0, end, count=0, k, taddr, address, i=0;
FILE *fp1,*fp2;
void check();
void main()
{
fp1=fopen("input1.txt","r");
fp2=fopen("output.txt","w");
fscanf(fp1,"%s",input);
printf("\n\n\t\t\t\tABSOLUTE LOADER\n");
fprintf(fp2,"\n-------------------------------------------------------\n");
fprintf(fp2,"MEMORY ADDRESS\t\t\tCONTENTS");
fprintf(fp2,"\n-------------------------------------------------------\n");
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s %x %x %s",label,&start,&end,input);
address=start;
}
else if(strcmp(input,"T")==0)
{
l=length;
ptaddr=addr;
fscanf(fp1,"%x %x %s",&taddr,&length,input);
addr=taddr;
if(w==0)
{
ptaddr=address;
w=1;
}
for(k=0;k<(taddr-(ptaddr+l));k++)
{
address=address+1;
fprintf(fp2,"xx");
count++;
if(count==4)
{
fprintf(fp2," ");
i++;
if(i==4)
{
fprintf(fp2,"\n\n%x\t\t",address);
i=0;
}
count=0;
}
}
if(taddr==start)
fprintf(fp2,"\n\n%x\t\t",taddr);
fprintf(fp2,"%c%c",input[0],input[1]);
check();
fprintf(fp2,"%c%c",input[2],input[3]);
check();
fprintf(fp2,"%c%c",input[4],input[5]);
check();
fscanf(fp1,"%s",input);
}
else
{
fprintf(fp2,"%c%c",input[0],input[1]);
check();
fprintf(fp2,"%c%c",input[2],input[3]);
check();
fprintf(fp2,"%c%c",input[4],input[5]);
check();
fscanf(fp1,"%s",input);
}
}
fprintf(fp2,"\n-------------------------------------------------------\n");
fclose(fp1);
fclose(fp2);
printf("\n\n The contents of output file:\n\n");
fp2=fopen("output.txt","r");
ch2=fgetc(fp2);
while(ch2!=EOF)
{
printf("%c",ch2);
ch2=fgetc(fp2);
}
fclose(fp1);
fclose(fp2);
}
void check()
{
count++;
address++;
taddr=taddr+1;
if(count==4)
{
fprintf(fp2," ");
i++;
if(i==4)
{
fprintf(fp2,"\n\n%x\t\t",taddr);
i=0;
}
count=0;
}
}
OUTPUT:
input.txt
H COPY 001000 00107A
T 001000 1E 141033 482039 001036 281030 301015 482061 3C1003 00102A 0C1039
00102D
T 00101E 15 0C1036 482061 081033 4C0000 454F46 000003 000000
T 001047 1E 041030 001030 E0205D 30203F D8205D 281030 302057 549039 2C205E
38203F
T 001077 1C 101036 4C0000 000000 001000 041030 E02079 302064 509039 DC2079
2C1036
E 001000
The contents of output file:
-------------------------------------------------------
MEMORY ADDRESS CONTENTS
-------------------------------------------------------
1000 14103348 20390010 36281030 30101548
1010 20613C10 0300102A 0C103900 102D0C10
1020 36482061 0810334C 0000454F 46000003
1030 000000xx xxxxxxxx xxxxxxxx xxxxxxxx
1040 xxxxxxxx xxxxxx04 10300010 30E0205D
1050 30203FD8 205D2810 30302057 5490392C
1060 205E3820 3Fxxxxxx xxxxxxxx xxxxxxxx
1070 xxxxxxxx xxxxxx10 10364C00 00000000
1080 00100004 1030E020 79302064 509039DC
1090 20792C10 36
Relocating loader Program:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void convert(char h[12]);
char bitmask[12];
char bit[12]={0};
void main()
{char add[6],length[10],input[10],binary[12],relocbit,ch,pn[5];
int start,inp,len,i,address,opcode,addr,actualadd,tlen;
FILE *fp1,*fp2;
printf("\n\n Enter the actual starting address : ");
scanf("%x",&start);
fp1=fopen("input1.txt","r");
fp2=fopen("output.txt","w");
fscanf(fp1,"%s",input);
fprintf(fp2," ----------------------------\n");
fprintf(fp2," ADDRESS\tCONTENT\n");
fprintf(fp2," ----------------------------\n");
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",pn);
fscanf(fp1,"%x",add);
fscanf(fp1,"%x",length);
fscanf(fp1,"%s",input);
}
if(strcmp(input,"T")==0)
{
fscanf(fp1,"%x",&address);
fscanf(fp1,"%x",&tlen);
fscanf(fp1,"%s",bitmask);
address+=start;
convert(bitmask);
len=strlen(bit);
if(len>=11)
len=10;
for(i=0;i<len;i++)
{
fscanf(fp1,"%x",&opcode);
fscanf(fp1,"%x",&addr);
relocbit=bit[i];
if(relocbit=='0')
actualadd=addr;
else
actualadd=addr+start;
fprintf(fp2,"\n %x\t\t%x%x\n",address,opcode,actualadd);
address+=3;
}
fscanf(fp1,"%s",input);
}
}
fprintf(fp2," ----------------------------\n");
fclose(fp1);
fclose(fp2);
printf("\n\n The contents of output file (output.txt):\n\n");
fp2=fopen("output.txt","r");
ch=fgetc(fp2);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp2);
}
fclose(fp2);
}
void convert(char h[12])
{
int i,l;
strcpy(bit,"");
l=strlen(h);
for(i=0;i<l;i++)
{
switch(h[i])
{
case '0':
strcat(bit,"0");
break;
case '1':
strcat(bit,"1");
break;
case '2':
strcat(bit,"10");
break;
case '3':
strcat(bit,"11");
break;
case '4':
strcat(bit,"100");
break;
case '5':
strcat(bit,"101");
break;
case '6':
strcat(bit,"110");
break;
case '7':
strcat(bit,"111");
break;
case '8':
strcat(bit,"1000");
break;
case '9':
strcat(bit,"1001");
break;
case 'A':
strcat(bit,"1010");
break;
case 'B':
strcat(bit,"1011");
break;
case 'C':
strcat(bit,"1100");
break;
case 'D':
strcat(bit,"1101");
break;
case 'E':
strcat(bit,"1110");
break;
case 'F':
strcat(bit,"1111");
break;
}
}
}
OUTPUT:
input1.txt
H COPY 001000 00107A
T 001000 1E FFC 14 0033 48 1039 10 0036 28 0030 30 0015 48 1061 3C 0003 20 002A 1C
0039 30 002D
T 002500 15 E00 1D 0036 48 1061 18 0033 4C 1000 80 1000 60 1003
E 000000
Enter the actual starting address : 5000
----------------------------
ADDRESS CONTENT
----------------------------
5000 144033
5003 485039
5006 104036
5009 284030
500c 304015
500f 485061
5012 3c4003
5015 20402a
5018 1c4039
501b 30402d
6500 1d4036
6503 485061
6506 184033
6509 4c1000
650c 801000
650f 601003
----------------------------
Addition Of 16 bit Numbers using Kit Program:
A
0400 MOV SI,3000
0403 MOV DI,4000
0406 MOV AX,[SI]
0408 INC SI
0409 INC SI
040A MOV BX,[SI]
040C ADD AX,BX
040E JNC 0415
0410 MOV [DI],0001
0413 JMP 0418
0415 MOV [DI],0000
0418 INC DI
0419 MOV [DI],AX
041B HLT
M
3000: 34
3001: 12
3002: 34
3003: 12
OUTPUT:
4000: 00
4001: 68
4002: 24
Subtraction Of 16 bit Numbers using Kit Program:
A
0400 MOV SI,3000
0403 MOV DI,4000
0406 MOV AX,[SI]
0408 INC SI
0409 INC SI
040A MOV BX,[SI]
040C SUB AX,BX
040E MOV [DI],AX
0410 HLT
M
3000: 34
3001: 12
3002: 22
3003: 11
OUTPUT:
4000: 12
4001: 01
Sorting Numbers in Ascending Order:
A
400 MOV SI,500
403 MOV CL,[SI]
405 DEC CL
407 MOV SI,500
40A MOV CH,[SI]
40C DEC CH
40E INC SI
40F MOV AL,[SI]
411 INC SI
412 CMP AL,[SI]
414 JC 41C
416 XCHG AL,[SI]
418 DEC SI
419 XCHG AL,[SI]
41B INC SI
41C DEC CH
41E JNZ 40F
420 DEC CL
422 JNZ 407
424 HLT
M
500: 04
501: F9
502: F2
503: 39
504: 05
OUTPUT:
501: 05
502: 39
503: F2
504: F9
Searching a Number:
DATA SEGMENT
STRING1 DB 11H,22H,33H,44H,55H
MSG1 DB "FOUND$"
MSG2 DB "NOT FOUND$"
SE DB 33H
DATA ENDS
CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START:
MOV AX, DATA
MOV DS, AX
MOV AL, SE
LEA SI, STRING1
MOV CX, 04H
UP:
MOV BL,[SI]
CMP AL, BL
JZ FO
INC SI
DEC CX
JNZ UP
PRINT MSG2
JMP END1
FO:
PRINT MSG1
END1:
INT 3
CODE ENDS
END START
OUTPUT:
FOUND
PALINDROME PROGRAM:
DATA SEGMENT
BLOCK1 DB 'MALAYALAM'
MSG1 DB "IT IS PALINDROME $"
MSG2 DB "IT IS NOT PALINDROME $"
PAL DB 00H
DATA ENDS
MOV AH,09H
LEA DX,MSG
INT 21H
INT 3H
ENDM
EXTRA SEGMENT
BLOCK2 DB 9 DUP(?)
EXTRA ENDS
CODE SEGMENT
ASSUME CS:CODE,DS:DATA,ES:EXTRA
START: MOV AX,DATA
MOV DS,AX
MOV AX,EXTRA
MOV ES,AX
LEA SI,BLOCK1
LEA DI,BLOCK2+8
MOV CX,00009H
BACK: CLD
LODSB
STD
STOSB
LOOP BACK
LEA SI,BLOCK1
LEA DI,BLOCK2
MOV CX,0009H
CLD
REPZ CMPSB
JNZ SKIP
PRINT MSG1
SKIP: PRINT MSG2
CODE ENDS
END START
OUTPUT:
IT IS PALINDROME