问题
I have a rather long code written in assembly language and I want to use it inside a C program using asm()
function, but every line in the code must be quoted and a new line character ('\n') must be inserted at the end of each line in order to make it usable. Is there any text editing tool that can do that or I have to do it manually?
回答1:
I wrote a program that gets the assembly code from file a.s
and saves the formatted text into file b.s
.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE *pFileA=fopen("a.s","r"),
*pFileB=fopen("b.s","w");
if(pFileA==NULL || pFileB==NULL)
{
printf("ERROR!\n");
system("pause");
return 1;
}
char a;
START:
a=getc(pFileA);
while(a==' ' || a=='\t' || a=='\n')
{
putc(a,pFileB);
a=getc(pFileA);
}
if(a==EOF)
goto END;
putc('"',pFileB);
MID:
while(a!='\n' && a!='"')
{
putc(a,pFileB);
a=getc(pFileA);
}
if(a=='"')
{
putc(92,pFileB);
putc(a,pFileB);
a=getc(pFileA);
goto MID;
}
putc(92,pFileB);
putc('n',pFileB);
putc('"',pFileB);
putc(a,pFileB);
goto START;
END:
fclose(pFileA);
fclose(pFileB);
printf("SUCCESS!\n");
system("pause");
return 0;
}
来源:https://stackoverflow.com/questions/48836769/how-to-format-an-assembly-code