How is LDIR affecting memory?

Posts about Z80 programming that do not relate to a particular computer
Post Reply
Stroopwafel
Posts: 3
Joined: Tue Sep 15, 2020 8:48 am

How is LDIR affecting memory?

Post by Stroopwafel » Thu Sep 17, 2020 6:40 am

Hi,

I'm new here, thank you for giving me access to this forum. I'm following your lessons here (and watching the videos) but I'm already stuck at lesson two. Here LDIR is used to loop over memory addresses to set them to 0 to clear the screen. In the cheat sheet LDIR is explained as

Code: Select all

Load HL, DE
Until b=0:
  inc HL
  inc DE
  dec BC
HL and DE are pointed to two memory locations. So when they are both increased as long as BC has room to decrease, they are just pointing to the next memory location. How is this affecting that memory?

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

Re: How is LDIR affecting memory?

Post by akuyou » Sun Sep 20, 2020 7:22 am

Hey there! thanks for the question!

I think you've slightly misread the cheatsheet (unless there's a misprint version out there)

The first line should be:
LD (DE),(HL)

Notice the brackets ()?

HL means the value in HL
(HL) means the byte at address HL
so "LD (DE),(HL)" copies one byte from address HL to address DE

Note there's no such command as LD (DE),(HL) - it's just explaining what the command does... but the principal is the same as the real commands:
LD A,(HL)
LD (DE),A
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

Stroopwafel
Posts: 3
Joined: Tue Sep 15, 2020 8:48 am

Re: How is LDIR affecting memory?

Post by Stroopwafel » Tue Sep 22, 2020 8:34 am

I think you've slightly misread the cheatsheet (unless there's a misprint version out there)

The first line should be:
LD (DE),(HL)
Yes, that must be it. I already understood that (HL) and (DE) are pointers to an address in memory, but I thought that for instance INC HL would differ from INC (HL) in that the former would increase the pointer, while the latter would change the value of the memory address pointed to. So my confusion was that I thought only the pointers, not the values they pointed to, were changed by LDIR.

From the cheat sheet:

Code: Select all

LDIR Load(DE),(HL)... INC HL... ; no brackets around the argument for INC
INC r  increase r by one.
So can I conclude that when r is HL or DE, INC r actually increases the value in (HL)/(DE), not the value in HL/DE?
Last edited by Stroopwafel on Sun Sep 27, 2020 1:11 pm, edited 1 time in total.

Stroopwafel
Posts: 3
Joined: Tue Sep 15, 2020 8:48 am

Re: How is LDIR affecting memory?

Post by Stroopwafel » Tue Sep 22, 2020 9:08 am

So, I thought I could test this with this little piece of code:

Code: Select all

org &8000
LD DE,1
LD (&4000),DE	; load 1 into memory at &4000
LD HL,4000	; make HL a pointer to &4000
INC HL		; HL now holds &4001
RET
After this, HL has changed to &4001, DE holds 1, as does the memory address &4000.

When I change that line into INC (HL), HL holds &4000, DE holds &4001 and the memory address &4000 has increased to 2. Which is what I would expect. So, there is the expected difference between INC HL and (HL), which leads me back to my original question. :-S

Post Reply

Return to “General Z80 Programming”