summaryrefslogtreecommitdiff
path: root/crenshaw
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2018-08-11 21:15:59 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2018-08-11 21:15:59 +0200
commit05102f66690054d9c545ee258224ef0297691095 (patch)
treef1b00af5e95c72e8e329d3739225caf45ce984e4 /crenshaw
parent80a3db3cde0cf59245e87e44164013143537212e (diff)
downloadcompilertests-05102f66690054d9c545ee258224ef0297691095.tar.gz
compilertests-05102f66690054d9c545ee258224ef0297691095.tar.bz2
trying to emulate a stack for add and sub
Diffstat (limited to 'crenshaw')
-rw-r--r--crenshaw/README3
-rw-r--r--crenshaw/main.pas20
-rw-r--r--crenshaw/test.prog2
3 files changed, 18 insertions, 7 deletions
diff --git a/crenshaw/README b/crenshaw/README
index 0d1e58d..7bad3b5 100644
--- a/crenshaw/README
+++ b/crenshaw/README
@@ -1,7 +1,8 @@
fpc main.pas
./main < test.prog > test.asm
nasm -f elf32 test.asm
-gcc -m32 -o test test.o
+gcc -m32 -march=i486 -ffreestanding -o test test.o
+objcopy -O binary --only-section=.text test test.bin
ndisasm -b32 test.bin
./test
diff --git a/crenshaw/main.pas b/crenshaw/main.pas
index eff412c..677c496 100644
--- a/crenshaw/main.pas
+++ b/crenshaw/main.pas
@@ -75,30 +75,40 @@ end;
procedure Term;
begin
EmitLn('mov eax, ' + GetNum);
+ EmitLn('push eax');
end;
procedure Add;
begin;
Match('+');
Term;
+ Emitln('pop eax');
+ EmitLn('pop ebx');
EmitLn('add eax, ebx');
+ EmitLn('push eax');
end;
procedure Subtract;
begin;
Match('-');
Term;
+ EmitLn('pop eax');
+ EmitLn('pop ebx');
EmitLn('sub eax, ebx');
+ EmitLn('neg eax');
+ EmitLn('push eax');
end;
procedure Expression;
begin
Term;
- EmitLn('mov ebx, eax');
- case Look of
- '+': Add;
- '-': Subtract;
- else Expected('+/-');
+ while Look in ['+', '-'] do
+ begin
+ case Look of
+ '+': Add;
+ '-': Subtract;
+ else Expected('+/-');
+ end;
end;
end;
diff --git a/crenshaw/test.prog b/crenshaw/test.prog
index d6c68c4..a490d4b 100644
--- a/crenshaw/test.prog
+++ b/crenshaw/test.prog
@@ -1,2 +1,2 @@
-4+2
+4+2+5-8