简体   繁体   中英

How to write an expression in assembly with no operands?

I'm trying to write this expression A = (B × (C + D × (E × F))) ÷ (E × (C + D)) using an instruction set where each instruction has no operands (except for the two that must have one each).

This is an example from the book that I've followed: The expression is z = x * y + w * u

The code:

PUSH x
PUSH y
MULT
PUSH w
PUSH u
MULT
ADD
STORE z

What I have done:

PUSH E
PUSH F
MULT
PUSH D
MULT 
PUSH C
ADD
PUSH B
MULT
STORE Z (store the result)
PUSH C
PUSH D
ADD
PUSH E
MULT
PUSH Z
DIVIDE
STORE A

I'm not sure about the way that I stored the result in and the division seems incorrect. Is there a better way to write the expression?

Updated:

PUSH C
PUSH D
ADD
PUSH E
MULT
PUSH E
PUSH F
MULT
PUSH D
MULT
PUSH C
ADD
PUSH B
MULT
DIVIDE
STORE A

You don't need Z : calculate Ex(C+D) , then calculate (B×(C+D×(E×F))) ; now you have the values you need for the final division, which can then be stored in A . In fact, except for avoiding redundant calculations, you should never need to use STORE until the very end.

If your division is incorrect (you don't specify what assembly language this is, so I can't tell), then most likely you have the arguments backwards. In that case, you'd want to leave out the first STORE Z and the PUSH Z .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM