Understanding a bit of code

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
User avatar
electro
Posts: 132
Joined: Tue Jan 29, 2008 11:12 am
Location: New York

Understanding a bit of code

Post by electro »

Code: Select all


reset:
    sei        ; ignore IRQs
    cld        ; disable decimal mode
    ldx #$40
    stx $4017  ; disable APU frame IRQ
    ldx #$ff
    txs        ; Set up stack
    inx        ; now X = 0
    stx $2000  ; disable NMI
    stx $2001  ; disable rendering
    stx $4010  ; disable DMC IRQs 

I was wondering how x became zero in this bit of code.

First x contains #$40

After the stx $4017 ; #$40 still in the x reg?

Then ldx #$ff ; x now contains 255?

Then txs ; transfering the contents (#$ff) of x reg to stack?

x still contains 255?

Then inx ; does x reset back to 0 (255 + 1 =0) here?


Thanks,
T
Last edited by electro on Wed Mar 05, 2008 7:30 pm, edited 1 time in total.
User avatar
Disch
Posts: 1848
Joined: Wed Nov 10, 2004 6:47 pm

Re: Understanding a bit of code

Post by Disch »

electro wrote: Then inx ; does x reset back to 0 (255 + 1 =0) here?
Yes. X is only 8 bits wide, so it gets clipped at 8-bits.

$FF + 1 = $100

but you need to truncate $100 to 8 bits, so you're left with $00
User avatar
electro
Posts: 132
Joined: Tue Jan 29, 2008 11:12 am
Location: New York

Post by electro »

I see. Thanks.

T
User avatar
Laserbeak43
Posts: 188
Joined: Fri Sep 21, 2007 4:31 pm
Contact:

Post by Laserbeak43 »

triiiiiicky tricky tricky tricky.
btw i'm still taking in the last post in the controllers thead. that's why i haven't replied.
Noob sticky!!
Please document this part of the NESdevWiki!! XD
YOU NEED A RETROMACHINESHOP!!
Post Reply