Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
output ASM
#5
I think I see.

If asked in basic :

IF variable = value then

It does ((variable-value)-1)=overflow.

But surely the most logical thing to do is

variable=value ?

That is

ld a,(variable)
cp value
jp based on zero flag.

I'm aware that sub and cp are in flags terms the same thing, but it would make the code more readable - and you said the same thing, really. The bit I'm stuck on is that the compiler makes the call to do it in two parts in the first place - and you're thinking in terms of the optimizer at -O3 spotting it can shortcut this.

I'm pretty sure a multipass optimizer could do that, by the by - see that sub a follows by sub b could be sub (a+b) - in the same way as push X, pop X can be removed.

But why:

* use SUB at all here? Surely CP is the right instruction, and may well save us having to pull out the variable again (that is, the optimizer might see that we have (ix-9) in A already if we're about to do something to it, which would be common).
* Since the original basic code is testing for equality, why does it end up as a subtract, followed by a test for zero at all? Without referring to anything to do with the optimizer, surely a test-for-constant code block is the parse for if variable=constant ?
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)