News: 11 March 2016 - Forum Rules

Author Topic: [SNES] Is CMP #$00 necessary in this situation?  (Read 5499 times)

Mauron

  • Submission Reviewer
  • Hero Member
  • *****
  • Posts: 551
    • View Profile
[SNES] Is CMP #$00 necessary in this situation?
« on: May 18, 2014, 07:29:36 pm »
Code: [Select]
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

  • Hero Member
  • *****
  • Posts: 1660
  • Welp
    • View Profile
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #1 on: May 18, 2014, 09:14:13 pm »
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

  • Hero Member
  • *****
  • Posts: 2763
    • View Profile
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #2 on: May 19, 2014, 06:26:38 am »
It is completely useless. Unless there is some kind of self-mod code going on, I see no reason why to do that.

furrykef

  • Full Member
  • ***
  • Posts: 132
    • View Profile
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #3 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.

magno

  • Full Member
  • ***
  • Posts: 157
    • View Profile
    • Traducciones Magno
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #4 on: May 20, 2014, 08:31:21 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

Code: [Select]
if ( var != 3 )
it converts it to

Code: [Select]
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:

Code: [Select]
LDA $CC0000,x
here_2
 CMP #$00
 BNE here_1
 <do whatever...>
here_1
 LSR A
 BRA here_2

Bregalad

  • Hero Member
  • *****
  • Posts: 2763
    • View Profile
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #5 on: May 20, 2014, 09:24:05 am »
Quote
If 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

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #6 on: May 20, 2014, 01:18:08 pm »
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

  • Submission Reviewer
  • Hero Member
  • *****
  • Posts: 551
    • View Profile
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #7 on: May 20, 2014, 04:49:55 pm »
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)

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

magno

  • Full Member
  • ***
  • Posts: 157
    • View Profile
    • Traducciones Magno
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #8 on: May 21, 2014, 01:59:59 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

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • pantalytron.com
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #9 on: May 21, 2014, 04:29:17 am »
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

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #10 on: May 21, 2014, 08:20:23 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

  • Full Member
  • ***
  • Posts: 151
    • View Profile
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #11 on: May 22, 2014, 10:08:26 am »
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

  • Hero Member
  • *****
  • Posts: 2763
    • View Profile
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #12 on: May 22, 2014, 10:45:28 am »
CMP #$00 always set the C flag, and SEC is more efficient in doing so.

Quote
AFAIK 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

  • Submission Reviewer
  • Hero Member
  • *****
  • Posts: 551
    • View Profile
Re: [SNES] Is CMP #$00 necessary in this situation?
« Reply #13 on: May 22, 2014, 06:02:16 pm »
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.