1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Rep Power

    Accessing Array from ASM passed from C++?

    I've been studying assemby for about a semester now. I am having a horrible time figuring out how to access the data I pass from C++ to ASM. The data is a array of strings which I will have to sort in ASM and return a sorted array back to C++. The biggest problem I am having is accessing the data itself. The array is being passed at [ebp+8]... Any help with this is appreciated.

    This is the C++ code I'm working with;
    extern "C" int Sort (char [] [20], int, int);
    	Sort (Strings, 10, 20);
    	cout << "Sorted Strings are" << endl;
    	for (i = 0; i < 10; i++)
    		cout << '\t' << Strings [i] << endl;
    You can probably ignore my code, but I figure I would put it anyways;
    _Sort proc
    		push		ebp
    		mov			ebp, esp 
    		push		edi
    		mov			eax, [ebp+08]
    		mov			array_ptr, eax
    		mov			eax, [ebp+12]
    		mov			array_length, eax
    		mov			eax, [ebp+16]
    		mov			bytes_per_line, eax
    		mov			eax, array_length
    		cmp			eax, 0			;jump if array has length of 0
    		jnae		EndTRow
    		mov			ebx, found_length
    		inc			ebx			;add 1 to found_length's sum
    		cmp			ebx, eax	;jump if at last row
    		ja			EndTrow
    ;		mov			found_length, ebx
    		mov			eax, char_position
    		mov			ebx, bytes_per_line
    		cmp			eax, ebx
    		jnb			TRows
    ... ...and it becomes useless jargon here
    		pop			edi		
    		pop			ebp
    _Sort endp
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2006
    Rep Power
    [ebp+08] point to your array of 20 byte elements right?

    Put that in a reg where you can reference it without needing to slosh it back and forth to memory. In this case EDX or EBX will be a good choice.

    ESI and EDI you'll be using for REP CMPSB's. ECX will be used for the string compare byte counts.

    so you can access the first array element with say [EDX+0], the next with [EDX+20], then [EDX+40]etc, etc.

    The LEA instruction can advance you through the array. If EBX held a pointer to some array element do a LEA EBX,[EBX+20] would load EBX with the address of the next element. Alternatively, you could just ADD 20 to EBX. LEA makes it clearer that you're doing address arithmetic though.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2006
    Rep Power
    Thanks so much for your reply! I have already passed the class with a 'B', I ended up figring out my problem....Thanks again though!

IMN logo majestic logo threadwatch logo seochat tools logo