Hi, I have a question about the logic in one of the blargg's tests in 01 - special. Test #5: POP AF has some logic that I don't quite understand.
First let's look at the source for that test:
Now, I think I understand what the operations of the test are, but I would imagine this to always fail for the second iteration (BC = $1301) because of this:
I feel like there's something I'm missing, but I'm not sure what it is. I've been pouring over any documents I can find, but haven't found anything that would suggest different behavior from what I expect. Can anyone shed some light on this?
First let's look at the source for that test:
Code:
LD BC,$1200 ;start with BC=$1200
- PUSH BC ; save bc on the stack
POP AF ; pop the stack into af (effectively AF = BC)
PUSH AF ; push AF onto the stack
POP DE ; pop the stack into DE (effectively DE = AF (= BC))
LD A,C ; load C into A
AND $FO ; only keep the high 4 bits of A
CP E ; compare E to A
JP NZ,test_failed ; if they're not equal, the test is failed
INC B ; otherwise, increment B
INC C ; and C
JR NZ,- ; and do it again until C = $00
- PUSH BC ; save bc on the stack
POP AF ; pop the stack into af (effectively AF = BC)
PUSH AF ; push AF onto the stack
POP DE ; pop the stack into DE (effectively DE = AF (= BC))
LD A,C ; load C into A
AND $FO ; only keep the high 4 bits of A
CP E ; compare E to A
JP NZ,test_failed ; if they're not equal, the test is failed
INC B ; otherwise, increment B
INC C ; and C
JR NZ,- ; and do it again until C = $00
Now, I think I understand what the operations of the test are, but I would imagine this to always fail for the second iteration (BC = $1301) because of this:
Code:
LD A,C ; load $01 to A
AND $F0 ; keep only high bits ($01 -> $00)
CP E ; compare A ($00) with E ($01) -> not same, test failed
AND $F0 ; keep only high bits ($01 -> $00)
CP E ; compare A ($00) with E ($01) -> not same, test failed
I feel like there's something I'm missing, but I'm not sure what it is. I've been pouring over any documents I can find, but haven't found anything that would suggest different behavior from what I expect. Can anyone shed some light on this?