Skip to content

Instantly share code, notes, and snippets.

@d33tah
Created April 13, 2013 19:40
Show Gist options
  • Save d33tah/5379750 to your computer and use it in GitHub Desktop.
Save d33tah/5379750 to your computer and use it in GitHub Desktop.
A quick-and-dirty tongue-in-cheek generator of a tiny assembler source code written in assembler ;)
//make asm ; python -c "open('asm.S','w').write('\n'.join(map(lambda x: 'db 0x%02x' % ord(x) ,open('asm','r').read())))"
#include "stdlib.h"
#include "assert.h"
#include "stdio.h"
int hex2int(char hex)
{
if(hex<'0'|| (hex<'A' && (hex>'Z' && hex<'a')) || hex>'z' )
{
fprintf(stderr,"FATAL: Invalid syntax: expected "
"\"db 0x??\", no multiple whitespace allowed.\n");
exit(1);
}
if(tolower(hex)=='a') return 10;
if(tolower(hex)=='b') return 11;
if(tolower(hex)=='c') return 12;
if(tolower(hex)=='d') return 13;
if(tolower(hex)=='e') return 14;
if(tolower(hex)=='f') return 15;
else
return hex-48;
}
int main()
{
int zero;
char* line;
char dec;
do
{
zero = 0;
line = NULL;
getline(&line,&zero,stdin);
if(feof(stdin)) break;
if(!(tolower(line[0])=='d' &&
tolower(line[1])=='b' &&
line[2]==' ' &&
line[3]=='0' &&
line[4]=='x' &&
line[7]=='\n')
)
{
fprintf(stderr,"FATAL: Invalid syntax: expected "
"\"db 0x??\", no multiple whitespace allowed.\n");
return 1;
}
dec = hex2int(line[6])+hex2int(line[5])*16;
printf("%c",dec);
fflush(stdout);
free(line);
}
while(!feof(stdin));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment