From 0cc325193bb0593dfa95301186ddef253555e9e5 Mon Sep 17 00:00:00 2001 From: Karim Hamidou Date: Fri, 1 Jan 2016 11:11:43 +0100 Subject: [PATCH] basic assembly --- hello.asm | 29 ++++++++++ test.asm | 4 +- tests/1_empty.pas | 1 - tests/2_basic_assignment.pas | 6 ++ tiny.pas | 109 +++++++++++++++++++++++++++-------- 5 files changed, 122 insertions(+), 27 deletions(-) create mode 100644 hello.asm create mode 100644 tests/2_basic_assignment.pas diff --git a/hello.asm b/hello.asm new file mode 100644 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" diff --git a/test.asm b/test.asm index 9ea2710..44b7ae9 100644 --- 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 diff --git a/tests/1_empty.pas b/tests/1_empty.pas index 87064bf..2e268b8 100644 --- a/tests/1_empty.pas +++ b/tests/1_empty.pas @@ -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 index 0000000..66ba9e6 --- /dev/null +++ b/tests/2_basic_assignment.pas @@ -0,0 +1,6 @@ +{ Test program } +program test; + +begin + var1 := 2 + 3 * 5 +end. diff --git a/tiny.pas b/tiny.pas index f3bcac1..41d4a9a 100644 --- 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(''); Write('Compilation error: '); Write(s); @@ -55,22 +58,14 @@ begin Write(s2) end; - Writeln(''); + Writeln(''); 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. -- 2.43.0