Hello everyone, I have been trying to figure out the best way to work with arrays in assembly, and I have been running into some problems. I would like to add a column to this 2D array, but I am not sure of the best way to do it. Would I have to copy the original data to a separate area of memory, or is there an easier way, simply by counting the original amount of columns per row, and then adding a 0 onto the end of every line?


Code:
Data:
   .db   12,8  ;Columns,Rows
   .db   3,2,2,2,2,2,2,2,2,2,2,4
   .db   1,0,0,0,0,0,0,0,0,0,0,1
   .db   1,0,3,2,2,2,4,0,3,2,4,1
   .db   1,0,1,0,0,0,1,0,1,0,1,1
   .db   1,0,1,0,0,0,1,0,1,0,1,1
   .db   1,0,5,2,2,2,6,0,5,2,6,1
   .db   1,0,0,0,0,0,0,0,0,0,0,1
   .db   5,2,2,2,2,2,2,2,2,2,2,6


To something like this:


Code:
Data:
   .db   13,8  ;Columns,Rows
   .db   3,2,2,2,2,2,2,2,2,2,2,4,0
   .db   1,0,0,0,0,0,0,0,0,0,0,1,0
   .db   1,0,3,2,2,2,4,0,3,2,4,1,0
   .db   1,0,1,0,0,0,1,0,1,0,1,1,0
   .db   1,0,1,0,0,0,1,0,1,0,1,1,0
   .db   1,0,5,2,2,2,6,0,5,2,6,1,0
   .db   1,0,0,0,0,0,0,0,0,0,0,1,0
   .db   5,2,2,2,2,2,2,2,2,2,2,6,0


Any help is much appreciated!
(yes, this is really not how the bytes will appear in memory, it is just easier for me to think this way.)
Assuming that in the original data space you have enough extra free memory to store the extra data, you could do the following:

Start at the last row of the 12x8 array. Move the last row of 12 bytes forward 8-1 (7) bytes.
Go to the start of the second last row, and move this forward 8-2 (6) bytes.
Go to the start of the third last row, move this forward 8-3 (5) bytes.
Continue until you've moved the second from the top row forward 8-7 (1) byte.

These above loops could be accomplished using a B register counter, and then working with HL, DE, BC & the ldir instruction. In each loop, the DE destination would have to be calculated to be n bytes ahead of HL (the source), depending on the value of the B register counter (don't forget to push bc / pop bc at the start/end of your loop, as you'll be using BC for the ldir instruction).

You will then have 1 byte space between each row. These values won't necessarily be zero, so if you want this you will need to go through and zero these, or you could incorporate this into the above loop.

Sorry, that's probably a really bad explanation and just off the top of my head, but hopefully it's a starting point Smile
Actually, if you're moving data forward, you'll need to do a reverse copy with LDDR to make sure you don't overwrite part of the row before copying it. But that actually makes things a lot easier anyway, because then it goes like:


Code:
;Point HL at the last byte of the original array
;Point DE at the last byte of the expanded array
;Load old number of columns into BC
;Load number of rows into A

expansionLoop:
    dec de    ;Skip over the last column (put code before this to initialize it if needed)
    push bc
    lddr    ;Copy original row to expanded row
    pop bc
    dec a
    jr nz,expansionLoop
calc84maniac wrote:
Actually, if you're moving data forward, you'll need to do a reverse copy with LDDR to make sure you don't overwrite part of the row before copying it.
Yes, my bad. What calc84maniac said Smile
Great, thank you guys so much! I had tried before hand to do this with LDIR... It didn't come out to great. Smile I imagine that I could simply restructure this to take out a column using LDIR. Once again, thank you!
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement