Question

This is a beginner question... but I am struggling on it for almost 2 full days now.

I have to implement a function in NASM cooperate with C, which from main function (done in C) will pass a pointer array pointing to constant short arrays to the function, I have to sort the pointer according to the requirement which requires me to read the items in each constant short array. Since it's a pointer there will be no return from my function.

pointer passed to my function will be like this: const short *v[], each *v[n] points to a different constant array, n size unknown, but terminates with null. Constant array size unknown as well, but terminates with a negative number (valid elements in the array guaranteed to be positive).

My question is, how do I access each individual element in those constant arrays?

I have successfully read the pointer array from the [EBP+8], and able to detect how many elements in the pointer array by shifting the address, but I could not figure out how do I access the constant array which pointer array is pointing to.

fragments of my code:

...
segment .text

global sort_vectors
sort_vectors:
enter 0,0
pusha

mov EBX, dword [EBP+8] ; const short *v[]

mov ECX, 0     
.forloop:                    
;I am able to count how many elements in v from this for loop

mov EAX, dword [EBX+4*ECX]
cmp EAX, 0
jz  .forloopj

...
;but I need to access each individual constant short array to do the sorting
...

inc ECX
jmp forloop
.forloopj:
...

Any hint on how to get to each constant short array? Thanks!

edit:

Ok, I have figured what I have done wrong. Just realize const short is 2 bytes, I was using extended general register (4 bytes) to read and write this whole time! Solved by only using lower part of the 32 bits register and it works now!

I may post the working codes later when the assignment is due. Thanks to those who replied!

=================================================================

Was it helpful?

Solution

Oh, thanks to someone who reminds me.

Solution is actually wrote in the edit, but I will post it again: const short is only 2 bytes, I was using extended general register (4 bytes) to read and write in my program which that causes the issue on jumping address and random values.

And for accessing each individual element inside an array is essentially like this: consider any array is a flat long chain of elements. And reading each element is just like moving alone the chain until something tells you to stop.

Solution codes will not be posted due to academic regulation. Sorry for that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top