Welcome to Dream.In.Code
Become an Expert!

Join 149,608 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,892 people online right now. Registration is fast and FREE... Join Now!




Compiler Stack Machine Code review

 
Reply to this topicStart new topic

Compiler Stack Machine Code review, review

PennyBoki
5 Sep, 2007 - 11:45 AM
Post #1

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi I need a review on a STACK MACHINE CODE.
There is this language called Simple, small language, used for helping studetns on understanding compiler construction.

So I have an example of the language in this little code:
CODE
let
    integer    a, b, c, d.
in
    d:=10;
    c:=1;
    while c>0
        do c:=2*c;
    end;
    write c;
end


so the stack machine for this code IS:

CODE

0:  DATA          3
1:  LD_INT        10
2:  STORE         3
3:  LD_INT        1
4:  STORE         2
5:  LD_VAR        2
6:  LD_INT        0
7:  LT            0
8:  JUMP_FALSE    14
9:  LD_INT        2
10: LD_VAR        2
11: MULT          0
12: STORE         2
13: GOTO          5
14: LD_VAR        2
15: WRITE         0
16: HALT          0


I need a review about the stack machine code of this code:
CODE
let
    integer    x, y, n.
in    
    read n;
    y:=1;
    repeat
        y:=y*5;
        x:=x+1;
    until x>n
    write y;
end

bellow is the code I wrote and I need a review:
CODE
0:  DATA       2
1:  READ       0
2:  STORE      2
3:  LD_INT     1
4:  STORE      1
5:  LD_VAR     1
6:  LD_INT     5
7:  MULT       0
8:  STORE      2
9:  LD_VAR     0
10: LD_INT     1
11: ADD        0
12: STORE      1
13: LD_VAR     1
14: LD_VAR     3
15: LT         0
16: JUMP_FALSE 18
17: GOTO       5
18: WRITE      1
19: HALT       0

I hope I'm making any sense if not pls do let me know.
Anybody? Thanks in advance.

This post has been edited by PennyBoki: 5 Sep, 2007 - 12:10 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Compiler Stack Machine Code Review
5 Sep, 2007 - 01:11 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well from what I understand of what you are doing here I think there are a few errors... sorry if I am wrong here.

CODE

0:  DATA       2
1:  READ       0
2:  STORE      2
3:  LD_INT     1
4:  STORE      1
5:  LD_VAR     1
6:  LD_INT     5
7:  MULT       0
8:  STORE      2 <-- Shouldn't this be STORE 1?
9:  LD_VAR     0
10: LD_INT     1
11: ADD        0
12: STORE      1 <--- And this be STORE 0?
13: LD_VAR     1 <--- This be LD_VAR 0?
14: LD_VAR     3 <--- This be LD_VAR 2?
15: LT         0
16: JUMP_FALSE 18
17: GOTO       5
18: WRITE      1
19: HALT       0


Hopefully I am not far off base here. Hope this helps! smile.gif

This post has been edited by Martyr2: 5 Sep, 2007 - 01:16 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Compiler Stack Machine Code Review
5 Sep, 2007 - 01:26 PM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Ha yes I totally overlooked those incrementing them by one by mistake. Thanks man.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:05AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month