www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - asm trouble

reply Vladimir A. Reznichenko <vladimir.a.reznichenko gmail.com> writes:
Asm trouble, again. There is a function in a programm.

void test (inout uint a, uint sh, uint b)
{
	version (X86)
	{asm {
		mov EAX, a;
		mov EBX, sh;
		mov ECX, b;
		
		push EAX; //a adress saved
		
		//EAX = rotateLeft (a, sh) + b 
		push [EAX];
		push EBX;
		call rotateLeft;
		add EAX, ECX;

		pop EDX; //a adress restored
		
		mov [EDX], EAX;
	}}
}

void main ()
{
	uint a = 1;
	test (a, 5, 25);
	writeln (a);
}

When launched the executable,got this: Error: Access Violation

What's intresting, if i change asm code to
(save a address in the stack removed, forcing register values after the call
instraction), it works just fine

	...
	call rotateLeft;
		
	//force registers values
	mov EDX, a;
	mov ECX, b;
	..

So the question is why the call instruction drops register values?
It have to save registers state and restore it after the rotateLeft finished
(EAX of course will store a function call result).
May 15 2009
parent reply Walter Bright <newshound1 digitalmars.com> writes:
Vladimir A. Reznichenko wrote:
 So the question is why the call instruction drops register values?

The call instruction doesn't drop anything, it just does a call. What is happening is the rotateLeft function is using ECX for something else. The EAX, ECX, and EDX registers are (by convention) not preserved across function calls.
May 15 2009
parent =?ISO-8859-1?Q?=22J=E9r=F4me_M=2E_Berger=22?= <jeberger free.fr> writes:
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable

Walter Bright wrote:
 Vladimir A. Reznichenko wrote:
 So the question is why the call instruction drops register values?

The call instruction doesn't drop anything, it just does a call. What i=

 happening is the rotateLeft function is using ECX for something else.=20
 The EAX, ECX, and EDX registers are (by convention) not preserved acros=

 function calls.

AIUI, that would not cause an access violation, it would only cause=20 the returned value to be wrong. My question would be: does the=20 rotateLeft function pop its arguments from the stack? If it doesn't,=20 then "pop EDX" will actually put "sh" into EDX instead of the=20 address of "a"... Jerome --=20 mailto:jeberger free.fr http://jeberger.free.fr Jabber: jeberger jabber.fr
May 16 2009