Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Is this correct and Safe (https://www.delphipraxis.net/153937-correct-safe.html)

sdean 20. Aug 2010 18:18

Delphi-Version: 2005

Is this correct and Safe
 
I would like to do 3 Loops and each loop gives a Message , but without using IF .. Then

i did it in this way * Is it correct and Safe ... ? *
// in ASM
Delphi-Quellcode:
var
  Str: string;
begin
  Str := 'Just to popup a message without using IF Then';
   try
  Asm
  PushAd
  Xor al,al
    @Loop:Cmp al,3//xLoop //---> 3 loops
          JE @ExitpRoc
         PushAd
            Push 0
            Push 0
            Push Str
            Push 0
            Call MessageBoxA
         PopAd
        Inc al
        Jmp @Loop
  @ExitpRoc:PopAd
  End;
  except
    Exit;
  end;
end;

rollstuhlfahrer 20. Aug 2010 18:51

AW: Is this correct and Safe
 
Zitat:

Zitat von sdean (Beitrag 1043843)
Is it correct and Safe ... ?

To check if it is correct you can just try out if it works. I haven't found any error in this code yet.

Bernhard

BTW: What do PushAd/PopAd do? - I just know "Push XXX"/"Pop XXX" (including space)

Namenloser 20. Aug 2010 19:21

AW: Is this correct and Safe
 
Should do fine imo, except for one thing: MessageBoxA is an API function, hence it uses PChar and not Delphi's internal string. You should pass a pointer to the first character instead of the string itself.
I'm not sure about the PushAd and PopAd stuff - I think it's for exception handling, isn't it? I can't tell if you're using those instructions correctly because I've never used them myself, but I think the easiest way to find that out is to just run the code and see if it works.

himitsu 20. Aug 2010 19:28

AW: Is this correct and Safe
 
@NamenLozer: Intern enthält ein Delphi-String alles, was einem PChar gleicht.


Delphi-Quellcode:
. except
    Exit;
  end;
end;
aka
Delphi-Quellcode:
. except
  end;
end;
Sowas macht man nicht :!:!

Ansonsten seh ich keinen Vorteil, weswegen man hier unbedingt ASM und nicht einfaches Pascal nutzt.

Namenloser 20. Aug 2010 19:30

AW: Is this correct and Safe
 
Zitat:

Zitat von himitsu (Beitrag 1043863)
@NamenLozer: Intern enthält ein Delphi-String alles, was einem PChar gleicht.

Aber ein String hat doch noch eine Längenangabe und einen Referenzzähler etc.

himitsu 20. Aug 2010 21:00

AW: Is this correct and Safe
 
Der interne Pointer eines Strings und der anderen dynamischen Arrays zeigt absichtlich auf das erste Array-Feld/Char.
Vor diesem Feld/Char liegen dann Referenzzählung, Längenangabe und Co. .
Beim String liegt dann hinter dem String auch noch eine zusätzliche #0, obwohl es dieses für einen String mit Längenangabe nicht nötig währe.
(Abgesehn vom ShortString)

Durch die Position des Pointers und die #0 entspricht es "absichtlich" (ja, man hat sich dabei was gedacht) einem PChar, denn daruch kann eine Konverierung in einen PChar ohne Probleme und großen Aufwand vorgenommen werden, bzw. man kann es einfach ohne Konvertierung nutzen.

Nur die andere Richtung, von PChar nach String geht natülich nicht.

Namenloser 20. Aug 2010 21:32

AW: Is this correct and Safe
 
Ah okay, wieder was gelernt. In dem Fall ist das natürlich dann kein Problem.

OldGrumpy 21. Aug 2010 10:28

AW: Is this correct and Safe
 
Zitat:

Zitat von rollstuhlfahrer (Beitrag 1043852)
BTW: What do PushAd/PopAd do? - I just know "Push XXX"/"Pop XXX" (including space)

PUSHAD/POPAD macht PUSH/POP für die meisten CPU-Register (genauer: EAX, ECX, EDX, EBX, originaler ESP, EBP, ESI, und EDI) mit einem einzelnen Befehl. Wenn man eh mehrere davon braucht, ist das bequemer als eine Litanei von PUSH/POP zu schreiben. Reduziert ausserdem die Gefahr von Vertippern in der Reihenfolge ;) Und performancemäßig fällt das überhaupt nicht mehr ins Gewicht heutzutage :)


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:21 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz