Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need help passing and modifying c++ array from assembly library

Status
Not open for further replies.

soas

Technical User
Aug 3, 2001
49
0
0
US
Im trying to write a dll in assembly language that I can pass a pointer to an array from my c++ program to, to be modified.. I thought I had it figured out, but it crashes if the elements are more than 2, so I know Im doing it wrong

Here is what I have for my program so far...

in the c++ part I have

Code:
extern "C" void _cdecl Num(BYTE []);

int main()
{
  BYTE c[2];
  c[0] = 0;
  c[1] = 0;
  c[2] = 0;
  Num(c);
  printf("%d, %d, %d\n", c[0], c[1], c[2]);
  system("PAUSE");	
  return 0;
}

and on in the library part I have

Code:
Num proc public
	push ebp
	push eax
	push ebx
	mov ebp, esp

	mov eax, 10
	mov [ebx], eax
	mov eax, 12
	mov [ebx+1], eax
	mov eax, 22
	mov [ebx+2], eax
	
	pop ebx
	pop eax
	pop ebp
	ret
Num endp


When I run this progam it works fine, as long as c doesnt have more than c[2] elements.. I'd like it to be BYTE c[128], and to be able to modify those elements..

I dont know alot about using arrays in assembly, I've never tried passing them between languages especialy..

Im using masm32 just in case that makes a difference, if anyone knows what Im doing wrong could please give me some hints or point me in a direction where I could find more info about this, I'd really appreciate it
 
when I was pasting my assembly program in I skipped over a line, I only realized it after reading through it for the 3rd time... its supposed to say mov ebx, [ebp] below mov ebp, esp .. I hope that doesnt make things more confusing
 
You're not putting bytes. You're putting DWORDS. EAX is a DWORD. To use a byte, use al/ah,bl/bh,cl/ch,dl/dh "Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
Also, since you're putting constants, why bother going through the registers?

mov byte ptr [ebx],10

NOTE: for many assemblers, a 'byte ptr' or '* ptr' is often recommended when using register indices without any offsets:

mov word ptr [ebx],ax

"Information has a tendency to be free. Which means someone will always tell you something you don't want to know."
 
okay, well I dont intend it to be constants, Im just starting out like that to test it :)

And I made the necessary changes to change the registers to byte registers instead of DWORDS but it still crashes if the array I try to pass has more than 3 elements (c[2])..

any ideas about that part of it?
 
At the time you put:
mov ebp,esp

the stack was:
| pointer to passed array
| Return address (4 bytes)
| pushed ebp (4 bytes)
| pushed eax (4 bytes)
| pushed ebx (4 bytes) <-esp points here

so since you put mov ebp, esp, ebp now points to the pushed ebx. To get the pointer to array, you need
mov ebx,[ebp+16]
since the processor pushed 4 bytes (assuming 32-bit near or 16-bit far call), and you pushed a total of 12 bytes before putting esp into ebp.

Have fun! &quot;Information has a tendency to be free. Which means someone will always tell you something you don't want to know.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top