LDIR, dynamic DE

Por BlueCrystal

Expert (113)

Imagen del BlueCrystal

14-01-2023, 00:22

Lately I picked up some interest in msx z80 coding. Never done it on a serious level, neither have background in computer science.

I am use this piece of code:

	ld	de,(.Pointer)	; Tile pointer
	ld 	hl,World.Active	; Load complete world
	add	hl,de			; set pointer in world

	ld	de,World.Page1
	ld	bc,32			; 32 characters per line
	ldir				; DE<-HL until BC=0

I am trying to get World.Page1 dynamic, alike how one can set the pointer for hl. Using ld de,World.Page+32 works, which is static and not desired. How do you guys do this?

Login sesión o register para postear comentarios

Por Grauw

Ascended (10821)

Imagen del Grauw

14-01-2023, 00:39

Using add as well. Like for example:

	; a = page
	ld	h,0
	ld	l,a
	add	hl,hl			; x 32
	add	hl,hl
	add	hl,hl
	add	hl,hl
	add	hl,hl
	ld	bc,World.Page
	add	hl,bc
	ex	de,hl

	ld	bc,(.Pointer)	; Tile pointer
	ld 	hl,World.Active	; Load complete world
	add	hl,bc			; set pointer in world

	ld	bc,32			; 32 characters per line
	ldir				; DE<-HL until BC=0

By not modifying de but using bc as addend instead, one can calculate de before hl is calculated. Shifting things around like this can improve your register availability. Otherwise you have to push and pop registers temporarily while you use them for other calculations, which I mean it’s fine and a common thing to do as well.

Obviously using the x32 multiplication can be avoided if you pass in the multiple of 32 in hl directly, but that all depends on your use case. Anyway now you know how a multiplication by a power of two is done, if you didn’t already Smile. Just double the number 5 times and it multiplies by 32.

Por BlueCrystal

Expert (113)

Imagen del BlueCrystal

14-01-2023, 15:07

Using bc as shown in the example above does what I wanted! I might (should) post some of my work soon, but do try to not overload myself and restrict my time on such works. Just hope that msxDEV makes an announcement soon for 2023 and includes msx2. That is a good motivation to actually finish it while it also provides an exposure platform.