2 Replies - 2641 Views - Last Post: 05 September 2007 - 02:26 PM Rate Topic: -----

#1 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Compiler Stack Machine Code review

Posted 05 September 2007 - 12:45 PM

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:
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:

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:
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:
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: 05 September 2007 - 01:10 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Compiler Stack Machine Code review

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5227
  • View blog
  • Posts: 14,010
  • Joined: 18-April 07

Re: Compiler Stack Machine Code review

Posted 05 September 2007 - 02:11 PM

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

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! :)

This post has been edited by Martyr2: 05 September 2007 - 02:16 PM

Was This Post Helpful? 0
  • +
  • -

#3 PennyBoki   User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: Compiler Stack Machine Code review

Posted 05 September 2007 - 02:26 PM

Ha yes I totally overlooked those incrementing them by one by mistake. Thanks man.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1