BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

using registers in methods

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Language BASM
View previous topic :: View next topic  
Author Message
Jens Gruschel
Guest





PostPosted: Wed Mar 16, 2005 12:31 pm    Post subject: using registers in methods Reply with quote



I know how registers are used in assembler procedures or functions. But
when I have a method with a small asm block in it, can I make use of any
registers without having to save them to the stack? I fact I only need
one register.

Example:

procedure TMyClass.MyProcedure(LotsOfPArameters);
var
LotsOfVariables;
begin
DoSomething;
asm
push eax // is this line neccessary?
xor eax, eax
// work with one register + MMX registers
emms
pop eax // is this line neccessary?
end;
DoMore;
end;

eax is only an example, could be another register as well.

Jens
Back to top
Avatar Zondertau
Guest





PostPosted: Wed Mar 16, 2005 12:55 pm    Post subject: Re: using registers in methods Reply with quote



Quote:
I know how registers are used in assembler procedures or functions.
But when I have a method with a small asm block in it, can I make use
of any registers without having to save them to the stack? I fact I
only need one register.

eax is only an example, could be another register as well.

If you look at the code generated by Delphi you can see that it avoids
having to rely on the values stored EAX, ECX and EDX after the asm
block, if they are modified there. It will store the var in some other
register, like EDX, ECX or EBX instead. It will not do this for EBX,
ESP, EBP, ESI and EDI.

Suppose Delphi wants to store a var in EAX, but it encounters this asm
block:

asm
xor eax, eax
end;

I think that the floating point stack isn't used between different code
lines anyway, so using them shouldn't be a problem. It starts out empty
and should be empty at the end of the block. Lik you're doing, you
should use EMMS to cleanup the FPU after MMX registers. The SSE
registers shouldn't be a problem anyway, because they aren't used by
Delphi.

Back to top
Jens Gruschel
Guest





PostPosted: Wed Mar 16, 2005 4:11 pm    Post subject: Re: using registers in methods Reply with quote



Quote:
If you look at the code generated by Delphi you can see that it avoids
having to rely on the values stored EAX, ECX and EDX after the asm
block, if they are modified there. It will store the var in some other
register, like EDX, ECX or EBX instead. It will not do this for EBX,
ESP, EBP, ESI and EDI.

Thanks. That's what I thought, too. Looking at my code it seems to be
ok, but can I rely on it (maybe I change my code, and I don't want to
check it again and again)? And what happens if I modify EAX, ECX and
EDX (irrelevant for what I'm doning now)? In this case, there is no
register left Delphi can use instead. Is there any documentation about
this or is it just a matter of experience (Delphi has done it like that
100 times, so it will do so the 101st time, too)?

Jens

Back to top
Avatar Zondertau
Guest





PostPosted: Wed Mar 16, 2005 4:32 pm    Post subject: Re: using registers in methods Reply with quote

Quote:
If you look at the code generated by Delphi you can see that it
avoids having to rely on the values stored EAX, ECX and EDX after
the asm block, if they are modified there. It will store the var in
some other register, like EDX, ECX or EBX instead. It will not do
this for EBX, ESP, EBP, ESI and EDI.

Thanks. That's what I thought, too. Looking at my code it seems to be
ok, but can I rely on it (maybe I change my code, and I don't want to
check it again and again)? And what happens if I modify EAX, ECX and
EDX (irrelevant for what I'm doning now)? In this case, there is no
register left Delphi can use instead.

Delphi still has EBX, ESP, EBP, ESI, EDI and the stack. Except for ESP
all of these can potentially hold data (EBP cannot if there is a stack
frame).

You're allowed to modify those regs, as long as you restore them.
Delphi does the same thing.

Quote:
Is there any documentation
about this or is it just a matter of experience (Delphi has done it
like that 100 times, so it will do so the 101st time, too)?

I haven't seen this documented, so i'm afraid it'll have to come down
to experience.

Back to top
Peter Dudley
Guest





PostPosted: Wed Mar 16, 2005 8:04 pm    Post subject: Re: using registers in methods Reply with quote

"Jens Gruschel" <nospam (AT) thisurldoesnotexist (DOT) com> wrote

Quote:
I know how registers are used in assembler procedures or functions. But
when I have a method with a small asm block in it, can I make use of any
registers without having to save them to the stack?

See http://www.guidogybels.net/asmch1.html under 1.4

It says: "When using registers inside your function or procedure, please
note that only the registers eax, ecx and edx can be modified without having
to be restored to their previous content upon exit."

P



Back to top
Peter Dudley
Guest





PostPosted: Wed Mar 16, 2005 8:06 pm    Post subject: Re: using registers in methods Reply with quote

"Jens Gruschel" <nospam (AT) thisurldoesnotexist (DOT) com> wrote

Quote:
Is there any documentation about
this or is it just a matter of experience (Delphi has done it like that
100 times, so it will do so the 101st time, too)?

It's documented behaviour, this is what online help says:

"Register saving conventions

Procedures and functions must preserve the EBX, ESI, EDI, and EBP registers,
but can modify the EAX, EDX, and ECX registers. When implementing a
constructor or destructor in assembler, be sure to preserve the DL register.
Procedures and functions are invoked with the assumption that the CPU's
direction flag is cleared (corresponding to a CLD instruction) and must
return with the direction flag cleared."

P



Back to top
Avatar Zondertau
Guest





PostPosted: Wed Mar 16, 2005 8:15 pm    Post subject: Re: using registers in methods Reply with quote

Quote:
Is there any documentation about
this or is it just a matter of experience (Delphi has done it like
that 100 times, so it will do so the 101st time, too)?

It's documented behaviour, this is what online help says:

"Register saving conventions

Procedures and functions must preserve the EBX, ESI, EDI, and EBP
registers, but can modify the EAX, EDX, and ECX registers. When
implementing a constructor or destructor in assembler, be sure to
preserve the DL register. Procedures and functions are invoked with
the assumption that the CPU's direction flag is cleared
(corresponding to a CLD instruction) and must return with the
direction flag cleared."

Actually you're right, but that's not the part of the documentation
indicating this. What you're quoting is only about assembler functions
and procedures, not about inline asm blocks.

However the "The asm statement" help topic says this:

<quote>
In general, the rules of register use in an asm statement are the same
as those of an external procedure or function. An asm statement must
preserve the EDI, ESI, ESP, EBP, and EBX registers, but can freely
modify the EAX, ECX, and EDX registers. On entry to an asm statement,
EBP points to the current stack frame and ESP points to the top of the
stack. Except for ESP and EBP, an asm statement can assume nothing
about register contents on entry to the statement.
</quote>

This is the guarantee Jens needs.

Back to top
Jens Gruschel
Guest





PostPosted: Wed Mar 16, 2005 10:05 pm    Post subject: Re: using registers in methods Reply with quote

Quote:
However the "The asm statement" help topic says this:

quote
In general, the rules of register use in an asm statement are the same
as those of an external procedure or function. An asm statement must
preserve the EDI, ESI, ESP, EBP, and EBX registers, but can freely
modify the EAX, ECX, and EDX registers. On entry to an asm statement,
EBP points to the current stack frame and ESP points to the top of the
stack. Except for ESP and EBP, an asm statement can assume nothing
about register contents on entry to the statement.
/quote

This is the guarantee Jens needs.

Right. Thanks a lot!

Jens

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Language BASM All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.