Jump to content
IGNORED

Why would a "+= 2" zero out the second byte in an integer?


Recommended Posts

Hey guys,

 

I've just found a weird little bug in my game caused by "SUZY.hoff += 2;". When I converted it to "SUZY.hoff = SUZY.hoff + 2;" it worked just fine.

Looking at the assembly output, I can't see anything off, but since I'm learning the 6502 assembly while doing this project, I'm probably missing something.

 

The assembly conversion of the additional assignment operator (... += 2) becomes:

; SUZY.hoff += 2
 
lda     #$02
clc
adc     $FC04
sta     $FC04
bcc     L00DE
inc     $FC04+1

L00DE: <..>
So the problem shows up when the value of FC04 is [00 01] (0x0100 / 256) and I start to increase it by another 2. What I end up with is 0x0002 and not the 0x0102.
From what I understand, the code is doing the following:
  • Loading up 'A' with 2
  • Clear the carry flag
  • Add the first byte of FC04 to 'A'
  • Store 'A' in FC04
  • Skip the increase of the second byte of FC04 if there was no carry
Can anyone explain to me why the second byte is set to zero in my case?
Edited by EvilActivity
Link to comment
Share on other sites

From :-

 

http://www.monlynx.de/lynx/hardware.html

 

"Any CPU write to an LSB will set the MSB to 0"

 

So as soon as the CPU writes to the low byte the upper byte is zeroed in hardware. The compiler is doing the right thing but it doesn't know that the MSB will be cleared when the CPU writes to the LSB. The correct way to update the register would be take a temporary/shadow copy of it, update that and then write that back to the hardware.

  • Like 2
Link to comment
Share on other sites

From :-

 

http://www.monlynx.de/lynx/hardware.html

 

"Any CPU write to an LSB will set the MSB to 0"

 

So as soon as the CPU writes to the low byte the upper byte is zeroed in hardware. The compiler is doing the right thing but it doesn't know that the MSB will be cleared when the CPU writes to the LSB. The correct way to update the register would be take a temporary/shadow copy of it, update that and then write that back to the hardware.

 

Ahhh nice, thanks for that!

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...