2

I am programming an assembler to my class (basically translates an assembly code to hexadecimal code). It takes the code from a File, split lines (strtok) and search the mnemonics.

But when I use some mnemonics like RET and HLT, that use no argument (like MOV A, [X], for example), the strtok keeps the '\n' in the end and simply doesn't find the mnemonic. If I use strtok(string, "\n") it gives and an error and doesnt work.

So, how can I use strtok() to remove this '\n' from the end of the string? Here is the first function of the programm, used to count the bytes of each assembly function:

void contabyte (char arquivo[50], char arquivo_byte[50])
{
    FILE *arq, *arq_dest;
    int byte = 0;
    char linhatok[80], linha[80];
    char *mnemonico;

    arq = fopen(arquivo, "r");
    if (!arq)
        printf("Erro na abertura do arquivo\n");

    arq_dest = fopen(arquivo_byte, "w+");
    if (!arq_dest)
        printf("Erro na criação do arquivo destino\n");


    while (fgets(linhatok, 80, arq) != NULL) {
        if (feof(arq))   break;

        strcpy(linha, linhatok);
        strlwr(linhatok);

        mnemonico = strtok(linhatok, "      [];");

        if (strcmp(mnemonico, "add") == 0) {
            mnemonico = strtok(NULL, "  [];");
            mnemonico = strtok(NULL, "  [];");

            if (strcmp(mnemonico, "b") == 0)     byte++;
            else    byte += 2;

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "sub") == 0) {
            mnemonico = strtok(NULL, "  [];");
            mnemonico = strtok(NULL, "  [];");

            if (strcmp(mnemonico, "b") == 0)     byte++;
            else    byte += 2;

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "cmp") == 0) {
            mnemonico = strtok(NULL, "  [];");
            mnemonico = strtok(NULL, "  [];");

            if (strcmp(mnemonico, "b") == 0)     byte++;
            else    byte += 2;

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "inc") == 0) {
            byte++;

            mnemonico = strtok(NULL, "  [];");

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "dec") == 0) {
            byte++;

            mnemonico = strtok(NULL, "  [];");

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "jc") == 0) {
            byte += 2;

            mnemonico = strtok(NULL, "  [];");

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "jnc") == 0) {
            byte += 2;

            mnemonico = strtok(NULL, "  [];");

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "jz") == 0) {
            byte += 2;

            mnemonico = strtok(NULL, "  [];");

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "jnz") == 0) {
            byte += 2;

            mnemonico = strtok(NULL, "  [];");

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "jbe") == 0) {
            byte += 2;

            mnemonico = strtok(NULL, "   [];");

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "ja") == 0) {
            byte += 2;

            mnemonico = strtok(NULL, "  [];");

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "mov") == 0) {
            mnemonico = strtok(NULL, "  [];");
            if (strcmp(mnemonico, "b") == 0)     byte++;

            mnemonico = strtok(NULL, "  [];");
            if (strcmp(mnemonico, "a") == 0)     byte++;

            else    byte += 2;

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "jmp") == 0) {
            byte += 2;

            mnemonico = strtok(NULL, "  [];");

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "call") == 0) {
            byte += 2;

            mnemonico = strtok(NULL, "  [];");

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "ret") == 0) {
            byte++;

            mnemonico = strtok(NULL, "  [];");

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else if (strcmp(mnemonico, "hlt") == 0) {
            byte++;

            mnemonico = strtok(NULL, "  [];");

            //  printf("%d\t%s\n", byte, linha);
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
        else {
            printf("%d\t%s\t%d\n", byte, mnemonico, strlen(mnemonico));
            fprintf(arq_dest, "%d\t%s\n", byte, linha);
        }
    }
    fclose(arq_dest);
    fclose(arq);
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43
  • You should an example of your input and describe the output you want and the output you get. – Support Ukraine Oct 14 '18 at 07:03
  • See [Removing trailing newline character from fgets() input](https://stackoverflow.com/q/2693776/2410359) for better alternatives than [strtok(Name, "\n")](https://stackoverflow.com/a/2693826/2410359) – chux - Reinstate Monica Oct 14 '18 at 07:09
  • `if(feof(arq)) break;` serves no purpose here. – chux - Reinstate Monica Oct 14 '18 at 07:15
  • Succinctly, don't use `strtok()` to zap newlines. Actually, a simpler rule still is "Don't use `strtok()`". Use either `strtok_r()` on Unix or `strtok_s()` on Windows if you think you must use something like `strtok()`. Or use `strspn()`/`strcspn()`/`strpbrk()` instead. Amongst others, see [Nested `strtok()` function problem in C](https://stackoverflow.com/questions/4693884/). – Jonathan Leffler Oct 14 '18 at 07:39

0 Answers0