Why value not stored into A register?

Subjects related to all or many CPU's... such as issues relating to multi-CPU Linkers and assemblers like VASM, Source editors like Notepad++ etc
If your post is relating to an emulator, post in that platforms post (eg CPC for Winape)
Post Reply
User avatar
BigAkuma
Posts: 6
Joined: Fri Jan 31, 2020 1:17 am

Why value not stored into A register?

Post by BigAkuma » Tue Feb 04, 2020 10:43 am

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.

User avatar
akuyou
Posts: 430
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: Why value not stored into A register?

Post by akuyou » Wed Feb 05, 2020 3:13 am

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
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

User avatar
BigAkuma
Posts: 6
Joined: Fri Jan 31, 2020 1:17 am

Re: Why value not stored into A register?

Post by BigAkuma » Wed Feb 05, 2020 8:33 am

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?

User avatar
akuyou
Posts: 430
Joined: Mon Apr 22, 2019 3:19 am
Contact:

Re: Why value not stored into A register?

Post by akuyou » Wed Feb 05, 2020 11:31 am

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.
Chibi Akuma(s) Comedy-Horror 8-bit Bullet Hell shooter! // 「チビ悪魔」可笑しいゴシックSTG ! // Work in Progress: ChibiAliens

Interested in CPU's :Z80,6502,68000,6809,ARM,8086,RISC-V
Learning: 65816,ARM,8086,6809

Post Reply

Return to “General Assembly Programming”