November 26th, 2006, 11:47 PM
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  , int, int);
You can probably ignore my code, but I figure I would put it anyways;
Sort (Strings, 10, 20);
cout << "Sorted Strings are" << endl;
for (i = 0; i < 10; i++)
cout << '\t' << Strings [i] << endl;
mov ebp, esp
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
mov ebx, found_length
inc ebx ;add 1 to found_length's sum
cmp ebx, eax ;jump if at last row
; mov found_length, ebx
mov eax, char_position
mov ebx, bytes_per_line
cmp eax, ebx
... ...and it becomes useless jargon here
February 15th, 2007, 11:27 PM
[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.
February 18th, 2007, 08:17 PM
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!