Page 1 of 1

Understanding a bit of code

Posted: Wed Mar 05, 2008 7:16 pm
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

Re: Understanding a bit of code

Posted: Wed Mar 05, 2008 7:25 pm
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

Posted: Wed Mar 05, 2008 7:28 pm
by electro
I see. Thanks.

T

Posted: Thu Mar 06, 2008 7:53 pm
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.