News:

11 March 2016 - Forum Rules

Main Menu

[SNES] Is CMP #$00 necessary in this situation?

Started by Mauron, May 18, 2014, 07:29:36 PM

Previous topic - Next topic

Mauron

LDA $CC0000,x
CMP #$00
BNE $0E


From my understanding, LDA will set the z flag if the value loaded is 0, and bne will branch if the z flag is clear, so the CMP is unnecessary.

I just wanted to make sure I'm right on this.
Mauron wuz here.

justin3009

Nope, I'm pretty sure it's completely unnecessary to have it there.  If it's supposed to break when the value isn't 00, then just remove the CMP and you're all set.
'We have to find some way to incorporate the general civilians in the plot.'

'We'll kill off children in the Juuban district with an infection where they cough up blood and are found hanging themselves from cherry blossom trees.'

Bregalad

It is completely useless. Unless there is some kind of self-mod code going on, I see no reason why to do that.

furrykef

Probably the programmer was either new to 6502/65816 or was used to architectures like x86 and Z80 which don't set flags on register loads.

magno

Quote from: furrykef on May 19, 2014, 11:34:05 AM
Probably the programmer was either new to 6502/65816 or was used to architectures like x86 and Z80 which don't set flags on register loads.

Don't think so... It is far more likely that that was the result of some higher level language compiler. If the compiler finds

if ( var != 3 )

it converts it to

LDA $CC0000,x
CMP #$03
BNE $0E


And systematically does the same whatever the compared value.

Anyway, double-check that there isn't any branch or jump just before CMP #$00; it there is, removing that instruction will corrupt the game. This is an example:


LDA $CC0000,x
here_2
CMP #$00
BNE here_1
<do whatever...>
here_1
LSR A
BRA here_2

Bregalad

QuoteIf the compiler finds [....] systematically does the same whatever the compared value.
Nope, peepholes optimizers are supposed to remove this kind of things.

Although bad code produced by a (badly configured ?) compiler could be a valid explaination.

Revenant

Quote from: Bregalad on May 20, 2014, 09:24:05 AM
Nope, peepholes optimizers are supposed to remove this kind of things.

Although bad code produced by a (badly configured ?) compiler could be a valid explaination.

Most/all of EA's SNES sports games were written using a compiler, and it routinely did stupid things like explicitly allocating 0 bytes on the stack for functions without any arguments or local variables, so I wouldn't be surprised to see something like the OP either.

Then again, my first assumption was that this is a bit of code Mauron wrote themselves for feedback, rather than something found in an existing ROM (and if I had time, I'd let bgrep check for me  :D)

Mauron

Quote from: Revenant on May 20, 2014, 01:18:08 PMThen again, my first assumption was that this is a bit of code Mauron wrote themselves for feedback, rather than something found in an existing ROM (and if I had time, I'd let bgrep check for me  :D)

It was taken out of Chrono Trigger's AI routines. It would show up when checking to display a message.
Mauron wuz here.

magno

Quote from: Bregalad on May 20, 2014, 09:24:05 AM
Nope, peepholes optimizers are supposed to remove this kind of things.

I've checked several games' source code and you can trust me: the ASM code is barely optimized, maybe programmer's fault or maybe compiler's fault. My guess is that compilers weren't capable of getting the most optimized machine code, specially when using macros.

lytron

Quote from: Mauron on May 20, 2014, 04:49:55 PM
It was taken out of Chrono Trigger's AI routines. It would show up when checking to display a message.

AFAIK it is wrong to expect that Squaresoft's games are all masterly programmed (just because they are masterly concepted). I was told that FF6 was "more like a big hack" with many things programmed that they work somehow, not necessarily the fastest or most clearly arranged way. There were different programmers, and I expect them to be of different quality. Some did the tasks nobody else could do, and some did where they could damage the project the least...  ::) ;)

Revenant

Quote from: lytron on May 21, 2014, 04:29:17 AM
AFAIK it is wrong to expect that Squaresoft's games are all masterly programmed (just because they are masterly concepted).

I find this is true of big-name software in general, not just Square games :P

DougRPG

CMP sets the C flag, and LDA donĀ“t. So there is no way to tell if this CMP is useless unless you check the next intructions following the branches.

And, like was already said, you need to be sure there is no branch falling in this CMP (may be very difficult to find out).


Bregalad

CMP #$00 always set the C flag, and SEC is more efficient in doing so.

QuoteAFAIK it is wrong to expect that Squaresoft's games are all masterly programmed (just because they are masterly concepted)
Well, this is interesting, I never thought of it this way, but I think you're right. Some shitty games could have been masterfully programmed and vice-versa.

At least I know some early NES Square games have really stranges programming quicks and are programmed in a very non-standard way for the console (i.e. the games don't use "true" interrupts, sprites have hard-wired OAM positions and the engine relying on them, stuff like that).

Mauron

I double checked the branching, and there's nothing relevant going on - just a skip of the JSL if the value is 0.

The carry flag is not used for a while afterward, where it's used in a different CMP instruction.

Thanks for all the help.
Mauron wuz here.