Page 1 of 1

Why value not stored into A register?

Posted: Tue Feb 04, 2020 10:43 am
by BigAkuma
Hi. Sorry for this next topic, but it is a bit unrelated to the other previous message.


Let's consider this piece of code, that appears in Lesson 3:

Code: Select all

org &8000

ld a, (&9000)
ld bc, (&9001)

cp 0
jr z, MathAdd
cp 1
jr z, MathSub
ld a, 0

SaveResult:
ld a, (&9003)
ret

MathSub:
ld a, c
sub b
jr SaveResult

MathAdd:
ld a,c
add b
jr SaveResult 
The part that I am having problems is the next:

Code: Select all

SaveResult:
ld a, (&9003)
ret

MathSub:
ld a, c
sub b
jr SaveResult
If I am not wrong, this part:
1) Loads C into A registry.
2) Substracts B from A and stores the result in A.
3) Jumps to SAVERESULT
4) Loads A into &9003

We have:
A = Value of A - B
B = Value of B
C = Value of C
&9003=Value of A

Essentially, A and &9003 have the same value.


Now, lets consider this other code, with a small change:

Code: Select all

SaveResult:
ret

MathSub:
ld a, c
sub b
jr SaveResult
The only difference is that I removed LD (&9003), A in SAVERESULT

According to my understanding, this should:
1) load C into A
2) Substract B from A and store the result in A.
3) Jump to SAVERESULT
4) Return to BASIC.

In this case we have:
A=Value of B - A
B=Value of B
C=Value of C

If I PEEK &9003 using the first code, I get the addition of A+B

But if I peek &9000 (address of A) I do not get the value of the result B - A.

What I am missing here?

Thanks beforehand.

But in the second case, if I PEEK the address of A, I don't get the addition of A + B, as I was expecting.

Re: Why value not stored into A register?

Posted: Wed Feb 05, 2020 3:13 am
by akuyou
You have an error in your code

Code: Select all

SaveResult:
ld a, (&9003)
ret
Should be

Code: Select all

SaveResult:
ld (&9003),a
ret
You can download the sourcecode from the lesson page:
https://www.chibiakumas.com/z80/#lesson3

Re: Why value not stored into A register?

Posted: Wed Feb 05, 2020 8:33 am
by BigAkuma
Hi.

Ok I finally understand what was the problem.

I thought that when we do "ld a, (&9000)", we assign the address &9000 to A, and any time we change the value of A, it is changed to the address &9000, as if A was a pointer.

So, this is already solved.

But now, I have one question: is it possible to access the accumulator register from BASIC?

Re: Why value not stored into A register?

Posted: Wed Feb 05, 2020 11:31 am
by akuyou
There's no way to access A from basic that I know, you have to do it via RAM addresses as shown in this tutorial.