]> khamidou — GitHub mirrors - tiny.pas.git/commitdiff
basic assembly
authorKarim Hamidou <karim@macbook.home>
Fri, 1 Jan 2016 10:11:43 +0000 (11:11 +0100)
committerKarim Hamidou <karim@macbook.home>
Fri, 1 Jan 2016 10:11:43 +0000 (11:11 +0100)
hello.asm [new file with mode: 0644]
test.asm
tests/1_empty.pas
tests/2_basic_assignment.pas [new file with mode: 0644]
tiny.pas

diff --git a/hello.asm b/hello.asm
new file mode 100644 (file)
index 0000000..f1c3298
--- /dev/null
+++ b/hello.asm
@@ -0,0 +1,29 @@
+; /usr/local/bin/nasm -f macho64 64.asm && ld -macosx_version_min 10.7.0 -lSystem -o 64 64.o && ./64
+global start
+
+section .text
+
+start:
+    mov     rax, 0x2000004 ; write
+    mov     rdi, 1 ; stdout
+    mov     rsi, msg
+    mov     rdx, msg.len
+    syscall
+
+    mov     rax, 0x2000001 ; exit
+    mov     rdi, 0
+    syscall
+
+
+section .data
+
+msg:    db      "Hello, world!", 10
+.len:   equ     $ - msg
+
+section .text
+
+reset:
+    xor rax, rax
+
+section .data
+ttest: db "Hey there"
index 9ea2710abc9421006e1720e1825f51f7b041647d..44b7ae9f63285ef8841580bd31accbdaff7a7249 100644 (file)
--- a/test.asm
+++ b/test.asm
@@ -4,5 +4,7 @@
 .globl start
 start:
   movl $0x2000001, %eax # system call $1 with $0x2000000 offset
-  movl $0, %ebx         # set the exit code to be $0
+  movl $3, %eax
+  add %eax, 2
+  # movl $0, %ebx         # set the exit code to be $0
   syscall
index 87064bf5efb9d75bfc1f645a3dd9307bd9fa6093..2e268b8f6365215d85359e64e1fe4f7403b969c5 100644 (file)
@@ -2,5 +2,4 @@
 program test;
 
 begin
-
 end.
diff --git a/tests/2_basic_assignment.pas b/tests/2_basic_assignment.pas
new file mode 100644 (file)
index 0000000..66ba9e6
--- /dev/null
@@ -0,0 +1,6 @@
+{ Test program }
+program test;
+
+begin
+    var1 := 2 + 3 * 5
+end.
index f3bcac146981ee1a97ffa0b12f43dc6b1caf3732..41d4a9a97a183326830a0d7cb7438f0acd2cf111 100644 (file)
--- a/tiny.pas
+++ b/tiny.pas
@@ -32,6 +32,8 @@ const
     _PROCEDURE = 22;
     _EQUAL = 23;
     _ASSIGNMENT = 24;
+    _LESS_THAN = 25;
+    _MORE_THAN = 26;
 
 { Lexing-related values. }
 var
@@ -47,6 +49,7 @@ var
 { Display a "user-friendly" error message. }
 procedure error(s: String; s2: String);
 begin
+    Write('\e[0;31;40m');
     Write('Compilation error: ');
     Write(s);
 
@@ -55,22 +58,14 @@ begin
         Write(s2)
     end;
 
-    Writeln('');
+    Writeln('\e[0;37;40m');
     Halt(-1);
 end;
 
-{ Emit a line of assembly. }
-procedure emit(s: String; s2: String);
+procedure yysetup;
 begin
-    Write(yyoutput, s);
-
-    if s2 <> '' then
-    begin
-            Write(yyoutput, ', ');
-            Write(yyoutput, s2);
-    end;
-
-    Writeln(yyoutput, '');
+    yyunreadch := #0;
+    yytoken := 0;
 end;
 
 { Get a character from the file. Returns yyunreadch if it's defined. }
@@ -94,12 +89,6 @@ begin
     yyunreadch := c;
 end;
 
-procedure yysetup;
-begin
-    yyunreadch := #0;
-    yytoken := 0;
-end;
-
 {
   As far as lexers go, this one is pretty slow because
   it reads everything byte-by-byte.
@@ -108,6 +97,7 @@ function NextToken: integer;
 var
     c: Char;
     fpos: Integer;
+    s: String; { Temporary string }
 begin
     yytext := '';
     c := Getch();
@@ -142,16 +132,40 @@ begin
         exit(_SEMICOLON)
     else if c = '.' then
         exit(_DOT)
+    else if c = '+' then
+        exit(_PLUS)
+    else if c = '-' then
+        exit(_MINUS)
+    else if c = '*' then
+        exit(_MULT)
+    else if c = '/' then
+        exit(_DIV)
     else if c = ':' then
         begin
             c := Getch();
 
             if c <> '=' then
                 error('expected :=, got ', yytext);
-            exit(_ASSIGNMENT);
-        end;
-    if ((c >= 'a') and (c <= 'z')) or ((c >= 'A') and (c <= 'Z'))  then
+
+            exit(_ASSIGNMENT)
+        end
+    else if (c >= '0') and (c <= '9') then
         begin
+            yytext := '';
+
+            repeat
+                yytext := yytext + c;
+                c := Getch()
+            until (c < '0') or (c > '9');
+
+            { We've read one character too many. Put it back. }
+            Ungetc(c);
+            exit(_NUMBER);
+
+        end
+    else if ((c >= 'a') and (c <= 'z')) or ((c >= 'A') and (c <= 'Z'))  then
+        begin
+
             repeat
                 yytext := yytext + c;
                 c := Getch();
@@ -183,13 +197,14 @@ var
     token: Integer;
 begin
     if yytoken = 0 then
-        exit(nextToken())
+        token := nextToken()
     else
         begin
             token := yytoken;
             yytoken := 0;
-            exit(token)
         end;
+
+    exit(token)
 end;
 
 procedure yyunread(token: Integer);
@@ -204,6 +219,7 @@ procedure pascal_program; Forward;
 procedure code_block; Forward;
 procedure statement_list; Forward;
 procedure expression; Forward;
+procedure emit(s: String; s2: String); Forward;
 
 procedure pascal_program;
 var
@@ -280,14 +296,57 @@ begin
     if token <> _NUMBER then
         error('expecting number, got: ', yytext);
 
+    Emit('mov rax', yytext);
+
     token := yylex();
+
+    { The user typed a single number. Return. }
+    if (token = _SEMICOLON) or (token = _END) then
+        begin
+            yyunread(token);
+            exit()
+        end;
+
+    if (token = _PLUS) or (token = _MINUS) or (token = _MULT) or (token = _DIV) then
+        begin
+            expression()
+        end;
+end;
+
+procedure emit_assembly_header;
+begin
+    Writeln('; On MacOS X use something like /usr/local/bin/nasm -f macho64 file.asm && ld -macosx_version_min 10.7.0 -lSystem -o file file.o && ./file');
+    Writeln('; to compile. You may need to get nasm from homebrew.')
+end;
+
+procedure emit_assembly_footer;
+begin
+    Writeln('; over and out')
 end;
 
+{ Emit a line of assembly. }
+procedure emit(s: String; s2: String);
 begin
-    assign(yyinput, 'test.pas');
+    Write(s);
+
+    if s2 <> '' then
+    begin
+            Write(', ');
+            Write(s2);
+    end;
+
+    Writeln('');
+end;
+
+
+begin
+    assign(yyinput, ParamStr(1));
     reset(yyinput);
     yysetup();
 
+    emit_assembly_header;
+
     pascal_program();
-    Writeln('Program compiled.')
+
+    emit_assembly_footer;
 end.