🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.language.basm
Article #25

Re: ASM conversion from Delphi to C++Builder

#25
From: Ed Mulroy [TeamB]
Date: Mon, 23 Jul 2007 13:21
124 lines
4139 bytes
> I would also be tempted to turn off stack frames for this function.

In that case you would be jumping out of a plane without your parachute. 
Better to use
__declspec(naked)
and write every byte of the assembly code yourself.

>>void __fastcall GetCPUID_1(unsigned int cmd, TRegisters & regs)
>>  {
>>  _EBX = _ECX = _EDX = 0;
>
> Premature clearing of passed variables.
> _fastcall Order( EAX, EDX, ECX, SP[4], SP[8].... )
> ...
> EDI and EBX not preserved as required

Not so.

Compile the source from the command line with the -S option to see the 
generated code in a *.asm file.

SOP for since at least TC 2 is to push registers that are explicitly used. 
Between the possiblity of fancy code masking its detection and paranoia, it 
is an implicit requirement for a programmer to check the generated code for 
any source using inline assembly or pseudo-registers to insure that what is 
required has been saved and restored.

---------------------
 ; void __fastcall GetCPUID_1(unsigned int cmd, TRegisters & regs)
 ;
 push ebp
 mov ebp,esp
 push ecx
 push ebx
 push esi
 push edi
 mov esi,edx
 mov dword ptr [ebp-4],eax
 ;
 ;   {
 ;   _EBX = _ECX = _EDX = 0;
 ;

---------------------

>>    mov edi,esi /* _EDI = (unsigned long) & regs; */
>
> Same issues, plus new one:
> Where did esi come from?

GetCPUID_1's generated code shows esi contains &regs

---------------------
 ;   _EDI = reinterpret_cast<unsigned long>(& regs);
 ;
 ;
 mov edi,esi

---------------------

> Does the compiler "know" that you trounced edx (regs)?

It does not use the value so does not need to know.

.  Ed

> Bob Gonder wrote in message
> news:37l6a3dnmrn3m9blstu4dqvmmjah97nbif@4ax.com...
>
>>Either of these should be close.  I say 'close' because I have not tested
>
> Definately untested <G>
>
> I would also be tempted to turn off stack frames for this function.
>
>>void __fastcall GetCPUID_1(unsigned int cmd, TRegisters & regs)
>>  {
>>  _EBX = _ECX = _EDX = 0;
>
> Premature clearing of passed variables.
> _fastcall Order( EAX, EDX, ECX, SP[4], SP[8].... )
>
>>  _EDI = reinterpret_cast<unsigned long>(& regs);
>
> _EDI = _EDX
> EDI and EBX not preserved as required
> (unless compiler is doing hidden magic)
>
>>  _EAX = cmd;
>
> NOP, cmd is passed in eax
>
>>  __emit__(0x0F, 0xA2); // insert opcode for GetCpuId
>>  regs.EDX = _EDX;
>>  regs.ECX = _ECX;
>>  regs.EBX = _EBX;
>>  regs.EAX = _EAX;
>>  }
>>
>>void __fastcall GetCPUID_2(unsigned int cmd, TRegisters & regs)
>>  {
>>  __asm {
>>    xor edx,edx
>>    mov ebx,edx
>>    mov ecx,edx
>>    mov eax,[cmd]
>>    mov edi,esi /* _EDI = (unsigned long) & regs; */
>
> Same issues, plus new one:
> Where did esi come from?
>
>>    db 0FH, 0A2H /* opcode for GetCpuId */
>>    }
>>
>>  regs.EDX = _EDX;
>
> Does the compiler "know" that you trounced edx (regs)?
>
>>  regs.ECX = _ECX;
>>  regs.EBX = _EBX;
>>  regs.EAX = _EAX;
>>  }
>
> 
Technical Details
Message-ID: <46a4ab6c$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!news.clara.net!wagner.news.clara.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <46a047d3$1@newsgroups.borland.com> <46a3542c$1@newsgroups.borland.com> <37l6a3dnmrn3m9blstu4dqvmmjah97nbif@4ax.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 107b11,b95ef5f9173e844a
X-Google-Attributes: gid107b11,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Ed Mulroy [TeamB]" <dont_email_me@bitbuc.ket>
Newsgroups: borland.public.cppbuilder.language.basm
References: <46a047d3$1@newsgroups.borland.com> <46a3542c$1@newsgroups.borland.com> <37l6a3dnmrn3m9blstu4dqvmmjah97nbif@4ax.com>
Subject: Re: ASM conversion from Delphi to C++Builder
Date: Mon, 23 Jul 2007 09:21:46 -0400
Lines: 126
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 69.142.254.168
Message-ID: <46a4ab6c$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1185196908 69.142.254.168 (23 Jul 2007 06:21:48 -0700)
X-Authenticated-User: edmulroy
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder1-2.proxad.net!proxad.net!feeder2-2.proxad.net!news.clara.net!wagner.news.clara.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail