D ASM. Program fails
Adam D. Ruppe via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jan 22 06:06:42 PST 2016
On Friday, 22 January 2016 at 12:18:53 UTC, anonymous wrote:
> I don't know much about these things, but it seems to be the
> `ret;`.
Right. This is an ordinary D function so the compiler generates
code to set up a stack for local variables. It looks like:
push ebp;
mov ebp, esp;
sub EBP, some_size;
/* sometimes a few other register saves */
/*
your code here
*/
/* sometimes a few other register restores */
leave;
ret;
`leave` btw is the same as `mov esp,ebp; pop ebp;` - it undoes
the result of those first three instructions.
All this setup stuff is about creating a stack frame for the
function's local variables. If you ret without restoring the
frame, all local variables (and return addresses!) from there on
are going to be out of sync and will lead to memory access
violations. That's what happened to you.
If you want to write a whole function in assembly without the
compiler inserting any additional code, start it off with `asm {
naked; }` inside so dmd knows what you are trying to do. Then you
are in complete control.
Otherwise, remember to clear the frame correctly, or better yet,
just return using the ordinary D statement instead of the asm
instruction.
More information about the Digitalmars-d-learn
mailing list