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