Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- // Function to perform the first pass of the assembler
- void passOne(char label[10], char opcode[10], char operand[15], char code[10], char mnemonic[3]){
- int locctr, start, length;
- // File pointers for input, opcode table, symbol table, and intermediate files
- FILE *f1, *f2, *f3, *f4;
- // Open the necessary files
- f1 = fopen("input.txt", "r"); // Input assembly program
- f2 = fopen("optab.txt", "r"); // Opcode table
- f3 = fopen("symtab.txt", "w"); // Symbol table output
- f4 = fopen("intermediate.txt", "w"); // Intermediate file output
- // Check if files are opened successfully
- if (f1 == NULL || f2 == NULL || f3 == NULL || f4 == NULL) {
- printf("Error opening one of the files.\n");
- exit(1); // Exit if any file failed to open
- }
- // Read the first line of the input file
- fscanf(f1, "%s\t%s\t%s", label, opcode, operand);
- // Check if the first opcode is "START"
- if(strcmp(opcode, "START") == 0){
- start = strtol(operand, NULL, 16); // Convert the starting address from string to integer
- locctr = start; // Initialize LOCCTR with the starting address
- fprintf(f4, "\t%s\t%s\t%s\n", label, opcode, operand); // Write the first line to the intermediate file
- fscanf(f1, "%s\t%s\t%s", label, opcode, operand); // Read the next line
- } else {
- locctr = 0; // If no "START" directive, start LOCCTR at 0
- }
- // Process the input file line by line until "END" is encountered
- while(strcmp(opcode, "END") != 0){
- // Write the current line to the intermediate file with the LOCCTR
- fprintf(f4, "%X\t%s\t%s\t%s\n", locctr, label, opcode, operand);
- // If the label is not "-", add it to the symbol table
- if(strcmp(label, "-") != 0){
- fprintf(f3, "%s\t%X\n", label, locctr);
- }
- // Reset the file pointer to the start of the opcode table file
- rewind(f2);
- int found = 0; // Flag to check if opcode is found
- // Search for the opcode in the opcode table
- while(fscanf(f2, "%s\t%s", code, mnemonic) != EOF){
- if(strcmp(opcode, code) == 0){
- locctr += 3; // Increment LOCCTR by 3 for standard instructions
- found = 1;
- break;
- }
- }
- // If the opcode is not found in the table, handle directives
- if (!found) {
- // Check if opcode is a directive and update LOCCTR accordingly
- if(strcmp(opcode, "WORD") == 0){
- locctr += 3; // WORD occupies 3 bytes
- } else if(strcmp(opcode, "RESW") == 0){
- locctr += (3 * atoi(operand)); // RESW reserves multiple words (3 bytes per word)
- } else if(strcmp(opcode, "RESB") == 0){
- locctr += atoi(operand); // RESB reserves bytes
- } else if(strcmp(opcode, "BYTE") == 0){
- locctr += (strlen(operand) - 3); // BYTE reserves space based on the length of the operand
- } else {
- // If the opcode is not recognized, print an error and break the loop
- printf("\nOPCODE ERROR: %s\n", opcode);
- break;
- }
- }
- // Read the next line from the input file
- fscanf(f1, "%s\t%s\t%s", label, opcode, operand);
- }
- // Write the last line to the intermediate file
- fprintf(f4, "%X\t%s\t%s\t%s\n", locctr, label, opcode, operand);
- // Close all the files
- fclose(f4);
- fclose(f3);
- fclose(f2);
- fclose(f1);
- // Calculate the length of the program
- length = locctr - start;
- // Print the length of the program
- printf("\nLength of program is %X\n", length);
- }
- // Main function
- int main(){
- // Declare arrays to hold label, opcode, operand, code, and mnemonic
- char label[10], opcode[10], operand[15];
- char code[10], mnemonic[3];
- // Call the passOne function to process the input file
- passOne(label, opcode, operand, code, mnemonic);
- return 0; // Return 0 to indicate successful execution
- }
- /*
- input.txt
- ------------------
- - START 2000
- - LDA FIVE
- - STA ALPHA
- - LDCH CHARZ
- - STCH C1
- ALPHA RESW 2
- FIVE WORD 5
- CHARZ BYTE C'Z'
- C1 RESB 1
- - END -
- ------------------
- optab.txt
- ------------------
- LDA 03
- STA 0f
- LDCH 53
- STCH 57
- ------------------
- intermediate.txt
- ------------------
- - START 2000
- 2000 - LDA FIVE
- 2003 - STA ALPHA
- 2006 - LDCH CHARZ
- 2009 - STCH C1
- 200C ALPHA RESW 2
- 2012 FIVE WORD 5
- 2015 CHARZ BYTE C'Z'
- 2016 C1 RESB 1
- 2017 - END -
- ------------------
- symtab.txt
- ------------------
- ALPHA 200C
- FIVE 2012
- CHARZ 2015
- C1 2016
- ------------------
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement