Home > OS >  Pass one of a two pass assembler
Pass one of a two pass assembler

Time:04-25

The following is the code of pass one assembler in c. This code has a problem i want to get the address mentioned in "output.txt" in hex and not in decimal . How can i fix this ?


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

void main(){
    char opcode[10], operand[10], label[10], mnemonic[10], code[10];
    int locctr, start, length;

    FILE *input, *optab, *symbol, *output;

    input = fopen("input.txt", "r");
    optab = fopen("optab.txt", "r");
    symbol = fopen("symbol.txt", "w");
    output = fopen("output.txt", "w");

    fscanf(input,"%s\t%s\t%s",label,opcode,operand);

    if(strcmp(opcode,"START")==0){
        start = atoi(operand);
        locctr = start;
        fprintf(output, "\t%s\t%s\t%s\n",label,opcode,operand);
        fscanf(input,"%s\t%s\t%s",label,opcode,operand);
    } else {
        locctr = 0;
    }

    while(strcmp(opcode,"END")!=0){
        fprintf(output, "%d\t",locctr);
        if(strcmp(label,"-")!=0){
            fprintf(symbol, "%s\t%d\n",label,locctr);
        }
        fscanf(optab,"%s\t%s",code,mnemonic);
        while(strcmp(code,"END")!=0){
            if(strcmp(opcode,code)==0){
                locctr  = 3;
                break;
            }
            fscanf(optab,"%s\t%s",code,mnemonic);
        }
        if(strcmp(opcode,"WORD")==0){
            locctr  = 3;
        }
        else if(strcmp(opcode,"RESW")==0){
            locctr  = (3*(atoi(operand)));
        }
        else if(strcmp(opcode,"RESB")==0){
            locctr  = atoi(operand);
        }
        else if(strcmp(opcode,"BYTE")==0){
            locctr =strlen(operand)-2;
    
        }
        fprintf(output, "%s\t%s\t%s\t\n",label,opcode,operand);
        fscanf(input,"%s\t%s\t%s",label,opcode,operand);
    }
    fprintf(output, "\t%s\t%s\t%s\n",label,opcode,operand);
    length = locctr-start;
    printf("The length of code: %d\n",length);
    fclose(input);
    fclose(optab);

Output.txt

COPY    START   1000
1000    -   LDA ALPHA   
1003    -   ADD ONE 
1006    -   SUB TWO 
1009    -   STA BETA    
1012    ALPHA   BYTE    C'KLNCE 
1017    ONE RESB    2   
1019    TWO WORD    5   
1022    BETA    RESW    1   
    -   END -


As you can see on the output.txt file the address mentioned is not in hex . I want to make it a hex value. For instance the addresses mentioned here is

1000 1003 1006 1009 1012

instead it should be

1000 1003 1006 1009 1012

CodePudding user response:

Just replace %d to %X , the modified code is mentioned below

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
    char opcode[10], operand[10], label[10], mnemonic[10], code[10];
    FILE *input, *optab, *symbol, *output;
    int locctr, start, length;
    input = fopen("input.txt", "r");
    optab = fopen("optab.txt", "r");
    output = fopen("output.txt", "w");
    symbol = fopen("symbol.txt", "w");
    
    fscanf(input, "%s\t%s\t%s", label, opcode, operand);
    if(strcmp(opcode, "START") == 0){
        start = atoi(operand);
        locctr = start;
        fprintf(output, "\t%s\t%s\t%X\n", label, opcode, locctr   3096);
        fscanf(input, "%s\t%s\t%s", label, opcode, operand);
    }
    else locctr = 0;
    while(strcmp(opcode, "END") != 0){
        fprintf(output, "%X\t", locctr   3096);
        if(strcmp(label, "-") != 0) fprintf(symbol, "%s\t%X\n", label, locctr   3096);
        fseek(optab, SEEK_SET, 0);
        fscanf(optab, "%s\t%s", code, mnemonic);
        while(strcmp(code, "END") != 0){
            if(strcmp(opcode, code) == 0){
                locctr  = 3;
                break;
            }
            fscanf(optab, "%s\t%s", code, mnemonic);
        }
        if(strcmp(opcode, "WORD") == 0)
            locctr  = 3;
        else if(strcmp(opcode, "RESW") == 0)
            locctr  = 3*(atoi(operand));
        else if(strcmp(opcode, "RESB") == 0)
            locctr  = atoi(operand);
        else if(strcmp(opcode, "BYTE") == 0){
            char ch = operand[0];
            if(strcmp(&ch, "X") == 0)
                locctr  = 1;
            else
                locctr  = strlen(operand) - 3;
        }
        fprintf(output, "%s\t%s\t%s\t\n", label, opcode, operand);
        fscanf(input, "%s\t%s\t%s", label, opcode, operand);
    }
    fprintf(output, "\t%s\t%s\t%s\n", label, opcode, operand);
    length = locctr - start;
    printf("Code length = %x\n", length);
    fclose(input);
    fclose(output);
    fclose(optab);
    fclose(symbol);
}

  •  Tags:  
  • c
  • Related