News:

Printed Amstrad Addict magazine announced, check it out here!

Main Menu
avatar_Dabz

Reading 1D arrays in nested loops

Started by Dabz, 17:28, 24 September 22

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Dabz

Hi all, I've got a bit of code, which I've been playing around with, and... Well, I'll totally admit, I'm not totally assembler proficient [Anything but]. Basically, I want to retrieve values in a 1D array within nested loops, think of a tile map, but, plucking the tileID out of a 1D array... Now I've seen I could used tables, but, the incoming data will be from an external source.

Anyway, that is the general setup, but my issue is, I've created a little array in the asm as a placeholder type setup, and for the life of me, I cannot get my little program to read the values in it, here's a stripped down version:-

org &1000

printChar     equ &BB5A

ld a,(GridSizeXY)
ld (GridXCount),a
ld (GridYCount),a
ld a,(totalGridSize)
ld (totalGridSize),a

ld a,0
ld (count),a


.loopy
;Do stuff in here
.loopx:
ld a,(GridXCount)
dec a
ld (GridXCount),a


;;THIS IS THE BIT I'M NOT GETTING! :)
;;Because it should look like it works, but,
;;It isnt, and my head has become a shed! :P

ld hl,0 ;Reset HL just in case
ld de,0 ;Reset DE just in case
ld hl,GridData ;Supposedly get address of 'GridData'
ld de,count ;Load current count
add hl,de ; Move up one address
ld a,(hl) ; Store value at new address in A

;Do stuff in here that uses the result, but for example
;Implement crude debug to show above not working, e.g.
;Not reading values in "GridData"
add a,&21
call PrintChar

;Increment counter and store
ld a,(count)
inc a
ld (count),a

;Compare and jump back to loox if needed
ld a,(GridXCount)
ld (GridXCount),a
cp 0
jr nz,loopx

;reset SpriteX
ld a,(GridSizeXY)
ld (GridXCount),a

;Decrement y loop and jump if needed.
ld a,(GridYCount)
dec a
ld (GridYCount),a
cp 0
jr nz,loopy

;Finish
ret

GridXCount: byte 0
GridYCount: byte 0
count: byte 0
GridSizeXY: byte 6

GridData:db 5,1,1,1,1,1 ,4,1,1,1,1,1 ,3,1,1,1,1,1 ,2,1,1,1,1,1 ,1,1,1,1,1,1 ,0,1,1,1,1,1
totalGridSize:byte 30

I've commented the best I can on it, the bit I need a bit of help on is in the middle somewhere.

Now, I've either missed something, which I probably have, or I'm simply not picturing it correctly, which I'm probably not, I've tried loads of ways "I think" will work, and... Well... ???!!!

Any help would be appreciated, because it's getting on my nerves now! :D

Dabz
 

andycadley

When you do:

ld hl,GridData
ld de,count ;Load current count
 add hl,de

You are, in fact, adding the address of your count variable to GridData, rather than the value of your count variable. Which means you're always ending with DE holding the same value and one that's probably not inside your array.

You probably mean to do something more like:

LD hl, GridData
LD a,(count) ; get the value of count
LD E,a
LD d,0; DE now holds the value of count
Add hl, de; HL is now GridData + Count

Dabz

Ahhhhhh, I see, looking at it, yep, makes sense!!! :) 

Thank you Andy, much appreciated!

Your a star! ;) 

Dabz

Powered by SMFPacks Menu Editor Mod