EASy68K
http://easy68k.com/EASy68Kforum/

Some help needed with array adressing
http://easy68k.com/EASy68Kforum/viewtopic.php?f=6&t=1565
Page 1 of 1

Author:  der_richter [ Sun Dec 11, 2016 2:37 pm ]
Post subject:  Some help needed with array adressing

Hi.

So, long story short i am returning to 68k assembly after a long hiatus. Basically i have been contracted to do some demos on the subject for a local college. So since i worked a lot with it back in the 90s, i figured, should be a small gig to put together a few demos right? And well, it is, as i remember most of the stuff, but now i was writing a small sorter routine which is basically a right pivot quicksort, that i wanted to use as a demo comparison between C and asm code. Seeing as i packed away my manuals many years ago, i tried going from memory, which with age is going a bit fuzzy, but i made it up until this task fine.

What i am now seeing is that i have an array set in memory, that i wish to iterate and sort in-place, thus i want to establish two pointers in a-regs, one to the pivot value, and one to the new pivot position to swap into after partitioning (as per standard quicksorting). The array is defined like this:

Code:
arr  DC.W   12,16,1,5,14,3,9,2,4,6


As we can see a random selection of word-sized ints

So then i load up the labeled addr in a-reg:

Code:
lea arr,a1


Then i push offsets for begin and end indexes on the stack and call the sort.

There i will pop off the stack, the offsets, and going from memory, doing what i think is the way to index offset by having a 0 displacement:

Code:
lea  0(a1,d2.w),a2


So now a-reg A2 should hold pointer to the end element, then i do the same, using A3 for begin:

Code:
lea  0(a1,d1.w),a3


In initial run d1 contains #0

Now, this is ok, for all but one thing... Populating d1,d2 from the stack with the offsets (in this case index*2 for word) gives correct pointer.

But... and here it is strange... When i fetch from the start element pointer, i actually do not get the num 12 as i expected, i get 000C, i.e. 16 decimal. Meaning i get the element at 0+2 bytes from arr label.

Quick test:

Code:
move.w  (a3),d4 ;test <- WHY is this 16?


d4 has value 000C, second element, but i expected the 12 to go there, as d1 = 0, and displacement is 0.

Looking in the debugger shows that A3 indeed has the first addr of the "arr" label, but still reads as 000C.

Probably i am forgetting something on either declaring a sequence or indexing it... Please give me some hints, before i have to dig out my manuals (which i should do one of these days anyway). Also: Major thanks for the EASy68k program, and for supplying it to the community. Feels like i am a young, fresh engineer again, getting back into it, thanks to this new commision job, and the program makes it that much easier.

Author:  profkelly [ Tue Dec 13, 2016 8:25 pm ]
Post subject:  Re: Some help needed with array adressing

000C hex = 12 decimal.

Author:  der_richter [ Tue Dec 13, 2016 10:04 pm ]
Post subject:  Re: Some help needed with array adressing

profkelly wrote:
000C hex = 12 decimal.


Yes. My wording was a bit bad, i mean that the 000C part is the in memory offset of the "16" value of the array, while i am expecting to get an offset of 000A.

But, i since found the actual problem, and it was, needless to say an error of my own, where i simply added too much to the offsets in a prior piece of code. So all is well now and order is restored :)

Now that i have your attention anyhow, the "adressing with index offset" method i use based on my recollection, meaning doing a lea with a 0-displacement and a register, is there a more concise opcode for that, that i might have forgotten, seeing as i don't need the displacement at all, only the d-reg for indexing... But obviously omitting the "0" displacement is invalid?

Or is it more effective to do an "adda" with the val in d-reg?

Author:  profkelly [ Wed Dec 14, 2016 3:45 pm ]
Post subject:  Re: Some help needed with array adressing

I use LEA.
It is not necessary to include the 0 in the source code which might make it a little less confusing to read.
Code:
  lea     (a1,d2.w),a2

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/