Jump to content
IGNORED

Dead Code???


Propane13

Recommended Posts

Hi there,

 

Was browsing through some legacy 6502 code, and came across this block:

 

LE4F9:

LDA $81

LDY #$12

CMP #$1F

BMI LE50B

CMP #$18

BMI LE50B

CMP #$27

BPL LE50B

LDY #$11 ;2

LE50B:

JMP $F026 ;3

 

Now, the code obviously sets Y to $11 or $12, and then jumps to address $F026.

The question, though, has to do with the input $81. Am I mistaken, or is there dead code in here (basically, one of the compares will always render that condition true or false based on a previous comparison's result)? Am I looking at this too late, or am I correct in this assumption? Input would be appreciated

 

 

Thank you,

-John K. Harvey

Link to comment
Share on other sites

maybe i just woke up... but as far as i can see...

 

depending on $81 you will have $11 or $12 in Y-register...then the code jumps to $f026

 

so i don't see a "dead code" here... it would be more interesting what is happening after $f026 and what is stored in $81???

Link to comment
Share on other sites

I agree...this part of the code reveals too little to make any conclusions.

The obvious things are that the Y register is held at a value equal to #$12 if one of the following three comparisons happens to be true...at which point the program branches to the JMP. You might be asking yourself "why not just place JMPs where the BMI's are?". First off, each branch acts as an IF-THEN statement...testing the value held at $81 (which is now in the accumulator) to see if it falls within acceptable parameters. CMP will NOT change the value held in the accumulator...so all the comparisons are checking the original value fetched from $81.

Secondly, it's easier to change the JMP memory location when it only appears in the code at one spot.

If none of the tests are positive, the value of Y changes to #$11 before the JMP is made.

 

The LDA $81 is definately not "dead code", as all of the following tests and the jump itself depend on the value held there to decide on what to do next.

 

[ 03-02-2002: Message edited by: Nukey Shay ]

Link to comment
Share on other sites

By the way, an easy way to see what the branches accomplish is to edit all of them. In this code, there is two ways to do this...

 

Each BMI statement uses 2 bytes, so just EA EA over all three of them and run it to see what changed when the tests always come up negative. Here's the edited code :

 

LE4F9:

LDA $81

LDY #$12

CMP #$1F

NOP

NOP

CMP #$18

NOP

NOP

CMP #$27

NOP

NOP

LDY #$11 ;2

LE50B:

JMP $F026 ;3

 

(NOP is like REM in Basic...it does nothing at all).

 

The second way is to stick your own value in for Y...like this :

LE4F9:

LDA $81

LDY #$11

CMP #$1F

BMI LE50B

CMP #$18

BMI LE50B

CMP #$27

BPL LE50B

LDY #$11 ;2

LE50B:

JMP $F026 ;3

 

...or this :

LE4F9:

LDA $81

LDY #$12

CMP #$1F

BMI LE50B

CMP #$18

BMI LE50B

CMP #$27

BPL LE50B

LDY #$12 ;2

LE50B:

JMP $F026 ;3

 

...try both and see what happens.

 

By far, the best method of hacking games to discover what each routine does is to look for a jump table. These usually show up as a number of JSR's in succession, and appear like this :

 

JSR $E02B

JSR $8450

JSR $846E

JSR $A0DF

JSR $B99C

 

To find out what each routine does, just put NOP's over one of them to see what changes when it is run...like this:

JSR $E02B

NOP

NOP

NOP

JSR $846E

JSR $A0DF

JSR $B99C

 

(this one is checking what the instructions at $8450 do...each JSR instruction uses three bytes, so you will need to use three NOP's.)

 

If it is something crucial to the game's engine...you may find that it won't run at all. If it is something exotic (like checking your inventory for a particular object, for example)...you may not notice a difference until you try to use that object.

Editing jump tables can mean the difference between tracing reams of data to just a page or two.

 

Just don't forget to write down the original values (or save a backup file).

 

[ 03-02-2002: Message edited by: Nukey Shay ]

Link to comment
Share on other sites

Hello,

 

I see I may have confused some people.

By "Dead code", I was referring to a condition check that will always be true or false, and therefore is a useless check on its own.

 

For example (per pseudocode):

 

Input X

If X < 5 Then GOTO Endline

If X < 3 Then GOTO Endline

Endline:

 

Obviously, the second conditional check is useless in this situation, since any number less than 3 is inherently less than 5.

 

So, does this happen in this snippet of code?

 

-John K. Harvey

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...