With the sample.c program I tries to convert a pseudo-code (from mycode.txt file) in an assembly code (to a future file.asm)
Code:
root@ferran-F5GL:/home/ferran/Documents/C# cat mycode.txt
data:
db msg="Hello, World!\n";
equ len(msg);
start:
sys_write(len, msg, 1, 4);
kernel;
sys_exit(1);
kernel;
root@ferran-F5GL:/home/ferran/Documents/C# ./sample mycode.txt
section .data
msg db 'Hello, World',0xa
len equ $ - msg
Violació de segment (s'ha bolcat la memòria)
root@ferran-F5GL:/home/ferran/Documents/C#
"Violació de segment (s'ha bolcat la memòria)" in english means "Segment violation (overflow memory)
the sample.c code is
Code:
/*
This program will tries to convert a pseudo-code
from a text file to assembly code
which will -later- be saved in an .asm file.
(By the moment don't exists any real conversion.
The program currently makes a copy of a assembly file
meanwhile i fix this).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( int argc, char *argv[] ) {
char myfile[20];
char myline[100];
char *token;
const char sptr[10]=" ;()[]\t\n";
token = (char*) malloc (sizeof (50));
FILE *fp;
if ( argc == 2 ) {
strcpy(myfile, argv[1]);
}
else {
printf("Invalid number or arguments.\n");
}
fp = fopen(myfile,"r");
if ( fp == NULL ) {
fputs("Error in file ", stderr);
exit(1);
}
else {
while ( feof(fp) == 0 )
{
fgets(myline,100,fp);
token = strtok(myline, sptr);
if ( strcmp(token, "data:") == 0 ) {
printf("section\t.data\n\n");
}
else if ( strcmp(token, "bss:") == 0 ) {
printf("section\t.bss\n\n");
}
else if ( strcmp(token, "start:") == 0 ) {
printf("section\t.text\n");
printf("global\t_start\n\n");
printf("\t_start:\n\n");
}
else if ( strcmp(token, "db") == 0 ) {
printf("msg\tdb\t'Hello, World',0xa\n");
}
else if ( strcmp(token, "equ") == 0 ) {
printf("len\tequ $ - msg\n");
}
else {
printf(" \n");
}
}
}
fclose( fp );
return 0;
}
P.D. I tried to attach here the sample.c program, but is not possible.
if you could see where is the problem.
Thank you in advance.