Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pass one of a two pass assembler

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1000
1003
1006
1009
1012

instead it should be

1000
1003
1006
1009
1012

>Solution :

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);
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading