xtranophilist
xtranophilist

Reputation: 592

Small Help on stack based operation!

Stack Instructions:

PUSH A
PUSH B
SUB
POP X

Is X = A-B or X = B-A?

Thank you in advance! :)

Upvotes: 1

Views: 89

Answers (2)

xtranophilist
xtranophilist

Reputation: 592

So, if I have to demonstrate A*(B-C)/D-E with stack operation, I am doing this right:

PUSH A
PUSH B
PUSH C
SUB
MUL
PUSH D
DIV
PUSH E
SUB

Then, the top of stack would have what A*(B-C)/D-E evaluates to. Actually this needs to be done with zero-address format register and i believe using stack this way is how it is done.

Thank you very much for the help.

Highly appreciated.

Upvotes: 0

Matthew Slattery
Matthew Slattery

Reputation: 46998

In theory, SUB could be defined either way (as @delnan says).

In practice, most stack-based languages or instruction sets will follow the conventions of Reverse Polish notation: 5 2 - would be 3, and can be thought of in terms of stack operations as push 5; push 2; subtract. So, in your example, X = A-B would be the more typical expected result.

(A real example: isub in the JVM.)

Upvotes: 2

Related Questions