]> khamidou — GitHub mirrors - miniforth.git/commitdiff
implemented an db...times directive similar to the one in nasm
authorkhamidou <khamidou@gmail.com>
Wed, 23 Feb 2011 08:16:28 +0000 (09:16 +0100)
committerkhamidou <khamidou@gmail.com>
Wed, 23 Feb 2011 08:16:28 +0000 (09:16 +0100)
as.c
aslexer.l
tokens.h
vm.h

diff --git a/as.c b/as.c
index 5ace3cacb89a2105bed739e0f0da1bb6d99a9197..59a1fce273ff9b6a188748487b3507f4ed4aca90 100644 (file)
--- 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;
index 2761acc3a8672a06fdc0650151bc73f3fc0ce80d..e744ce96df8698b440bd727426c21eea8259ef59 100644 (file)
--- a/aslexer.l
+++ b/aslexer.l
@@ -10,7 +10,6 @@
 ;              BEGIN(in_comment);
 <in_comment>\n   BEGIN(INITIAL);
 <in_comment>[^\n]+    // eat comment
-<in_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;        }
index 7ba151619924f5191350fd9b252ed592f789aca8..928a0ba8f49e6dd6f09ad81a45f5ddd37ff62d81 100644 (file)
--- 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 6e55d41172a4547bb86725fc8f2177cab67c2cf9..0cd253700dfd9dc23c8f45cc2639702bd4859ede 100644 (file)
--- 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