Advertisement
Bewin

Pass1

Aug 26th, 2024 (edited)
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. // Function to perform the first pass of the assembler
  6. void passOne(char label[10], char opcode[10], char operand[15], char code[10], char mnemonic[3]){
  7.     int locctr, start, length;
  8.  
  9.     // File pointers for input, opcode table, symbol table, and intermediate files
  10.     FILE *f1, *f2, *f3, *f4;
  11.  
  12.     // Open the necessary files
  13.     f1 = fopen("input.txt", "r");           // Input assembly program
  14.     f2 = fopen("optab.txt", "r");           // Opcode table
  15.     f3 = fopen("symtab.txt", "w");          // Symbol table output
  16.     f4 = fopen("intermediate.txt", "w");    // Intermediate file output
  17.  
  18.     // Check if files are opened successfully
  19.     if (f1 == NULL || f2 == NULL || f3 == NULL || f4 == NULL) {
  20.         printf("Error opening one of the files.\n");
  21.         exit(1); // Exit if any file failed to open
  22.     }
  23.  
  24.     // Read the first line of the input file
  25.     fscanf(f1, "%s\t%s\t%s", label, opcode, operand);
  26.  
  27.     // Check if the first opcode is "START"
  28.     if(strcmp(opcode, "START") == 0){
  29.         start = strtol(operand, NULL, 16);  // Convert the starting address from string to integer
  30.         locctr = start;         // Initialize LOCCTR with the starting address
  31.         fprintf(f4, "\t%s\t%s\t%s\n", label, opcode, operand); // Write the first line to the intermediate file
  32.         fscanf(f1, "%s\t%s\t%s", label, opcode, operand);  // Read the next line
  33.     } else {
  34.         locctr = 0;  // If no "START" directive, start LOCCTR at 0
  35.     }
  36.  
  37.     // Process the input file line by line until "END" is encountered
  38.     while(strcmp(opcode, "END") != 0){
  39.         // Write the current line to the intermediate file with the LOCCTR
  40.         fprintf(f4, "%X\t%s\t%s\t%s\n", locctr, label, opcode, operand);
  41.  
  42.         // If the label is not "-", add it to the symbol table
  43.         if(strcmp(label, "-") != 0){
  44.             fprintf(f3, "%s\t%X\n", label, locctr);
  45.         }
  46.  
  47.         // Reset the file pointer to the start of the opcode table file
  48.         rewind(f2);
  49.         int found = 0;  // Flag to check if opcode is found
  50.  
  51.         // Search for the opcode in the opcode table
  52.         while(fscanf(f2, "%s\t%s", code, mnemonic) != EOF){
  53.             if(strcmp(opcode, code) == 0){
  54.                 locctr += 3;  // Increment LOCCTR by 3 for standard instructions
  55.                 found = 1;
  56.                 break;
  57.             }
  58.         }
  59.  
  60.         // If the opcode is not found in the table, handle directives
  61.         if (!found) {
  62.             // Check if opcode is a directive and update LOCCTR accordingly
  63.             if(strcmp(opcode, "WORD") == 0){
  64.                 locctr += 3;  // WORD occupies 3 bytes
  65.             } else if(strcmp(opcode, "RESW") == 0){
  66.                 locctr += (3 * atoi(operand));  // RESW reserves multiple words (3 bytes per word)
  67.             } else if(strcmp(opcode, "RESB") == 0){
  68.                 locctr += atoi(operand);  // RESB reserves bytes
  69.             } else if(strcmp(opcode, "BYTE") == 0){
  70.                 locctr += (strlen(operand) - 3);  // BYTE reserves space based on the length of the operand
  71.             } else {
  72.                 // If the opcode is not recognized, print an error and break the loop
  73.                 printf("\nOPCODE ERROR: %s\n", opcode);
  74.                 break;
  75.             }
  76.         }
  77.  
  78.         // Read the next line from the input file
  79.         fscanf(f1, "%s\t%s\t%s", label, opcode, operand);
  80.     }
  81.  
  82.     // Write the last line to the intermediate file
  83.     fprintf(f4, "%X\t%s\t%s\t%s\n", locctr, label, opcode, operand);
  84.  
  85.     // Close all the files
  86.     fclose(f4);
  87.     fclose(f3);
  88.     fclose(f2);
  89.     fclose(f1);
  90.  
  91.     // Calculate the length of the program
  92.     length = locctr - start;
  93.     // Print the length of the program
  94.     printf("\nLength of program is %X\n", length);
  95. }
  96.  
  97. // Main function
  98. int main(){
  99.     // Declare arrays to hold label, opcode, operand, code, and mnemonic
  100.     char label[10], opcode[10], operand[15];
  101.     char code[10], mnemonic[3];
  102.  
  103.     // Call the passOne function to process the input file
  104.     passOne(label, opcode, operand, code, mnemonic);
  105.  
  106.     return 0;  // Return 0 to indicate successful execution
  107. }
  108.  
  109.  
  110. /*
  111.  
  112.     input.txt
  113. ------------------
  114. -   START   2000
  115. -   LDA FIVE
  116. -   STA ALPHA
  117. -   LDCH    CHARZ
  118. -   STCH    C1
  119. ALPHA   RESW    2
  120. FIVE    WORD    5
  121. CHARZ   BYTE    C'Z'
  122. C1  RESB    1
  123. -   END -
  124. ------------------
  125.  
  126.  
  127.  
  128.     optab.txt
  129. ------------------
  130. LDA 03
  131. STA 0f
  132. LDCH    53
  133. STCH    57
  134. ------------------
  135.  
  136.  
  137.  
  138. intermediate.txt
  139. ------------------
  140.     -   START   2000
  141. 2000    -   LDA FIVE
  142. 2003    -   STA ALPHA
  143. 2006    -   LDCH    CHARZ
  144. 2009    -   STCH    C1
  145. 200C    ALPHA   RESW    2
  146. 2012    FIVE    WORD    5
  147. 2015    CHARZ   BYTE    C'Z'
  148. 2016    C1  RESB    1
  149. 2017    -   END -
  150. ------------------
  151.  
  152.  
  153.  
  154.     symtab.txt
  155. ------------------
  156. ALPHA   200C
  157. FIVE    2012
  158. CHARZ   2015
  159. C1  2016
  160. ------------------
  161.  
  162. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement