The one-pass macro assembler processes source code in a single pass to directly generate the final object code. Key tasks include:
Reading Source Code: Processes assembly instructions and macro definitions line by line.
Expanding Macros: Replaces macro calls with their expanded bodies.
Handling Labels and Directives: Resolves labels and processes directives like START, END, and ORG.
Building Symbol Table: Stores labels with corresponding addresses or values.
Managing Macros: Uses tables (MNT and MDT) to store and expand macros.
Generating Object Code: Directly produces final machine-level code.
Symbol Table: Stores labels and addresses.
Macro Name Table (MNT): Stores macro names and pointers to their definitions.
Macro Definition Table (MDT): Stores macro bodies.
Argument List Array (ALA): Manages macro arguments during expansion.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main() {
FILE *fp1, *mdt, *mnt, *exp;
int MDTC = 0, MNTC = 0, i, l, k;
char c, st[20], ALA[5];
fp1 = fopen("INPUT_CODE.txt", "r");
if (fp1 == NULL) {
printf("Cannot open file Input_code\n");
exit(0);
}
mdt = fopen("MDT.txt", "w");
if (mdt == NULL) {
printf("Cannot open file MDT\n");
exit(0);
}
mnt = fopen("MNT.txt", "w");
if (mnt == NULL) {
printf("Cannot open file MNT\n");
exit(0);
}
exp = fopen("EXPAND.txt", "w"); // Intermediate file before macro expansion
if (exp == NULL) {
printf("Cannot open file EXPAND\n");
exit(0);
}
// Beginning of Pass 1
do {
fgets(st, 20, fp1); // Read one source card at a time
if (strcmp(st, "MACRO\n") == 0) { // Check if it's a MACRO pseudo-op
fgets(st, 20, fp1); // Read the macro name and arguments
MNTC++;
fprintf(mnt, "%d %s %d\n", MNTC, strtok(st, "\n"), ++MDTC); // Store in MNT
fprintf(mdt, "%d %s", MDTC, st); // Add macro name and arguments to MDT
i = 0, l = 0;
// Set up ALA
while (st[i] != '\0') {
if (st[i] == '&') {
ALA[l++] = st[i + 1];
}
i++;
}
do {
fgets(st, 20, fp1); // Read macro body
i = 0;
MDTC++;
while (st[i] != '\0') { // Replace formal arguments with index notation
if (st[i] == '&') {
st[i] = '#';
for (k = 0; k < l; k++) {
if (st[i + 1] == ALA[k]) {
st[i + 1] = k + '0';
}
}
}
i++;
}
fprintf(mdt, "%d %s", MDTC, st); // Add to MDT
} while (strcmp(st, "MEND\n") != 0); // Stop at MEND
} else {
fputs(st, exp); // Copy source code to EXPAND file
if (strcmp(st, "END") == 0) {
printf("Pass 1 complete!!\n");
fclose(fp1);
fclose(mdt);
fclose(mnt);
fclose(exp);
return 0;
}
}
} while (1);
}
START
L 1,X
ST 1,Y
MACRO
FUN &M,&N,&A
L 1,&M
A 3,&N
ST 3,&A
MEND
LINE 1
MACRO
HELLO &E,&F
L 2,&F
ST 2,&E
MEND
M 1,D5
N 2,D4
FUN P,R,P
L 1,K
HELLO S,T
FUN X,Y,Z
ST 1,K
END