retagged by
291 views

1 Answer

0 votes
0 votes

@shima+abdullah

 

 In forth you could do it this way….

1.  5 3 7 * + 8 –

 

What is happening on the line :: 

 

  1.   5 is pushed onto the stack....

  2.   3 is pushed onto the stack….

  3.   7 is pushed onto the stack….

  4.   * multiplies the top two stack elements which removes them both from the stack and then pushes the result onto the stack……

  5.  + adds the top two stack elements which removes them both from the stack and then pushes the result onto the stack…..

  6.  8 is pushed onto the stack….

  7.  - the top element is subtracted from second element which removes them both from the stack and then pushes the result onto the stack….

 

So the stack looks like this at each stage.

Top of the stack is the rightmost element ::

#      5 ...

##    5 3 ...

###  5 3 7 ...

####  5 21 ...

#####  26 ...

######  26 8 ….

#######  18 …..

 

The . on the second line is a command that prints the top of the stack and then removes it ….

 

## assuming that the stack starts out empty?

Also, in the above code it does not matter if there is something in the stack before these operations as we only play with what we put there….

 

 

 

Related questions

0 votes
0 votes
0 answers
1
0 votes
0 votes
1 answer
2
Overflow04 asked Sep 5, 2022
817 views
Please Explain : “ sp content is incremented for stack going downward method and decrement for stack going upward method”
0 votes
0 votes
1 answer
3
0 votes
0 votes
0 answers
4
BHOJARAM asked Jan 11, 2019
416 views
A stack based CPU executes the instruction. Memory location 500 contain 0×88 and memory location 700 contain 0×37. Stack pointer is at 0×003F.I1: push 500I2: push 700I...