Help about piece of code

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

Help about piece of code

Post by BigAkuma » Sun Feb 02, 2020 10:26 am

Hi.

This piece of code:

Code: Select all

ScreenSize equ &4000
org &8200
ld a, %00001111
FillAgain:
 ld hl, &C000
 ld de, &C000+1
 ld bc, ScreenSize-1
 ld (hl), a
 ldir
 dec a
 cp 255
 jp nz, FillAgain
ret
Just two questions:

1) In this specific example, I don't understand well how "CP" works and why we need it.
According to what I read, JP C, ## jumps to ## if the condition of the last operation is met.
In this case, if the result of the last operation (DEC A) is NOT ZERO, then jump.

Why we need to compare A to 255?
Also, when A reaches 0, why it goes back to 255 in the instruction "CP 255"?

2) I don't quite understand how screen memory works. When I write bits to screen memory (i.e. &C000 on wards), the screen shows vertical parallel stripes, instead of solid horizontal sprites. For example, if I want to fill the screen with solid cyan colour, if I start writing byte by byte to screen mem, shouldn't it print pixel by pixel from left to right first, then moving to next "line", just under the previous one, creating a solid colour?

Anyway, if this last question is answered in a later lesson (right now I'm in lesson 3), I don't need an explanation now anyway.

Thanks beforehand.

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

Re: Help about piece of code

Post by akuyou » Mon Feb 03, 2020 8:59 am

Hey there! this is CPC code isn't it...

in 8 bit systems when a register is 0 and we subtract 1 it 'rolls around' back to 255

I can't actually remember writing this example - I did it about 3 years ago, and maybe I'd do it differently today.

The example is going to repeat while A>=0 ... 255 is effectively -1 in assembly - I know that sounds weird, but it'll become clearer in the later tutorials.

There's a long tutorial on Hex/Decimal numbers here which may make it clearer:
https://youtu.be/coKLkFmx3eI

Regarding your question about the screen memory - the CPC is not that straightforward unfortunately... bytes from left to right are concecutive, but going down the screen, they are more complex.

I cover working with the CPC screen in this tutorial.. but I'd suggest you complete the basic Z80 lessons first, as it assumes you have a good grasp of Z80
https://www.chibiakumas.com/z80/platform.php#LessonP3
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: Help about piece of code

Post by BigAkuma » Tue Feb 04, 2020 6:00 am

akuyou wrote:
Mon Feb 03, 2020 8:59 am
Hey there! this is CPC code isn't it...

in 8 bit systems when a register is 0 and we subtract 1 it 'rolls around' back to 255

I can't actually remember writing this example - I did it about 3 years ago, and maybe I'd do it differently today.

The example is going to repeat while A>=0 ... 255 is effectively -1 in assembly - I know that sounds weird, but it'll become clearer in the later tutorials.

There's a long tutorial on Hex/Decimal numbers here which may make it clearer:
https://youtu.be/coKLkFmx3eI

Regarding your question about the screen memory - the CPC is not that straightforward unfortunately... bytes from left to right are concecutive, but going down the screen, they are more complex.

I cover working with the CPC screen in this tutorial.. but I'd suggest you complete the basic Z80 lessons first, as it assumes you have a good grasp of Z80
https://www.chibiakumas.com/z80/platform.php#LessonP3
Thanks for your reply.

Yes, for now, the code will be CPC assembly.

Yes, I understand that cp 255 will rollaround back to 255 when it becomes 0.
What I mean with the cp 255 line is, why do we need it?

What is the difference between:

Code: Select all

dec a
jp nz, MyTag
and

Code: Select all

dec a
cp 255
jp nz, MyTag
Shouldn't both pieces of code result in the same thing? They will jump to MyTag as soon as the last operation is not zero.
The only difference that I can see is that the second example will do one additional operation (cp 255).

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

Re: Help about piece of code

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

Try it for yourself... remove the CP 255 from the first example and run it.

Code: Select all

ScreenSize equ &4000
org &8200
ld a, %00001111
FillAgain:
 ld hl, &C000
 ld de, &C000+1
 ld bc, ScreenSize-1
 ld (hl), a
 ldir
 dec a
 jp nz, FillAgain
ret
You'll see onscreen the code works differently depending on if the CP 255 is in the code or not.

try adding a breakpoint to the DEC A command, and step through the CP 255 command - watch what happens to the Zero flag depending on the value of A

You need to look more into the CP command and how it affects the flags - particularly the Zero flag, as we're using JP NZ
Test.jpg
Test.jpg (36.9 KiB) Viewed 338 times
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”