Why is edx changed during a call? This small snippet shows everything except the expected output 12345d.....

mov edx, 12345d
call ShowEDX
ret

ShowEDX:
invoke wsprintf, addr bff, S('%i'), edx
invoke MessageBox, 0h, addr bff, addr bff, 0h
ret

The other registers eax, ebx, ecx are working...can anyone explain...?
And please remind me: esi & edi never change during a simple call, do they?
Dominik
Posted on 2004-10-06 13:01:16 by Dom


.data
caption db 'wsprintf example',0
format1 db '%d',0
.data?
buffer db 6 dup(?)

.code
start:
mov edx, 12345
call ShowEDX
invoke ExitProcess,0

ShowEDX:
invoke wsprintf,ADDR buffer,ADDR format1,edx
invoke MessageBox,0,ADDR buffer,ADDR caption,MB_OK
ret
END start
Posted on 2004-10-06 13:35:02 by Vortex
The EAX, ECX and EDX registers are "scratch" for the stdcall calling convention. This means functions following this conv. (like the APIs, for example) don't need to preserve their values after on return.

I'm sure there's a whole thread on this in the FAQ section, just look it up (or do a board search). :)
Posted on 2004-10-06 13:40:55 by QvasiModo
thx for re-coding, vortex (->sense?)
ok QvasiModo, i will have a look....thx for the reference!
Dominik
Posted on 2004-10-06 14:01:43 by Dom
Sorry Dom, I misunderstood your question, you can ignore my previous post.
Posted on 2004-10-06 14:07:45 by Vortex
NoProb :wink:
Posted on 2004-10-06 14:57:01 by Dom
Dom,

Your main question was:
The other registers eax, ebx, ecx are working...can anyone explain...?

The 2nd parameter required with the wsprintf function must be a pointer to the format-control string. You were using S('%i') which seems to be a macro of some kind to generate a string and insert its address as the 2nd parameter. The macro instructions would be inserted in your code immediately before the call and may be responsible for trashing the EDX register. The call instruction itself does NOT trash any register.

You could experience something similar with the INVOKE "macro" if you use EAX as one of the parameters and also use the "ADDR xxx" of a local variable as another parameter. The EAX register gets trashed by the ADDR macro for local variables but not for global variables.

Raymond
Posted on 2004-10-06 21:36:01 by Raymond
Thx Raymond...you were right!
I tested the value in edx using cmp-jmp intructions and it still holds the initial value. The prob was I used several macros internally that did mess up edx, though not eax (cause my macro preserves it), ebx, ....


The EAX register gets trashed by the ADDR macro for local variables but not for global variables.

Great Info -> thanks alot...
Dominik
Posted on 2004-10-07 04:30:03 by Dom
At times like this try using this:
ml /Fllist.txt /Sa /Sn my_asm.asm

It'll generate a file called list.txt which contains the byte sequence, code, and expanded macros for your code.

Mirno
Posted on 2004-10-07 06:00:21 by Mirno