From: khamidou Date: Wed, 23 Feb 2011 08:16:28 +0000 (+0100) Subject: implemented an db...times directive similar to the one in nasm X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=15d793fa25d3b3c34040771f1a2912c18ba9e39c;p=miniforth.git implemented an db...times directive similar to the one in nasm --- diff --git a/as.c b/as.c index 5ace3ca..59a1fce 100644 --- a/as.c +++ b/as.c @@ -81,6 +81,23 @@ int first_pass() { l = alloc_label(yytext, current_offset); append_label(l, label_head); break; + + case T_DB: + yylex(); + t = yylex(); + + if (t != T_TIMES) + fail("Expected a times keyword after db directive"); + + t = yylex(); + + if (t != NUMBER) + fail("Expected a number after times"); + + current_offset += atoi(yytext); + break; + + default: current_offset++; break; @@ -91,6 +108,8 @@ int first_pass() { int parse() { int t; int current_offset = 0; /* the offset in the generated binary file */ + int val; + int i, max_iter; int s; struct label *l; @@ -154,6 +173,27 @@ int parse() { writeint(l->offset, outfp); break; + case T_DB: + yylex(); + val = atoi(yytext); + t = yylex(); + + if (t != T_TIMES) + fail("Expected a times keyword after db directive"); + + t = yylex(); + + if (t != NUMBER) + fail("Expected a number after times"); + + max_iter = atoi(yytext); + + for(i = 1; i <= max_iter; i++) + writeint(val, outfp); + break; + + + default: fail("unexpected %s in input at line %d", yytext, yylineno); break; diff --git a/aslexer.l b/aslexer.l index 2761acc..e744ce9 100644 --- a/aslexer.l +++ b/aslexer.l @@ -10,7 +10,6 @@ ; BEGIN(in_comment); \n BEGIN(INITIAL); [^\n]+ // eat comment -; // eat the lone ';' @@ -23,6 +22,8 @@ "call" { return T_CALL; } "ret" { return T_RET; } "jmp" { return T_JMP; } +"db" { return T_DB; } +"times" { return T_TIMES; } [ \t\n]+ /* ignore whitespace */ [0-9]+ { return NUMBER; } diff --git a/tokens.h b/tokens.h index 7ba1516..928a0ba 100644 --- a/tokens.h +++ b/tokens.h @@ -12,6 +12,10 @@ enum tokens { NUMBER, T_DEC, T_CALL, T_RET, - T_JMP}; + T_JMP, + T_DB, + T_TIMES, + + }; #endif diff --git a/vm.h b/vm.h index 6e55d41..0cd2537 100644 --- a/vm.h +++ b/vm.h @@ -18,5 +18,7 @@ struct vm { }; struct vm* alloc_vm(int memsize, int stacksize); +int load_file(struct vm *v, FILE *fd); +int run(struct vm *v); #endif