Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Assembly

  1. #11

    Assembly

    hehe type packing is something I also very often forget when I port stuff

    Why do you handle it as raw strings? What if you send a zero integer? It would make a string like this: #0#0#0#0. If at some point the string is seen as nullterminated the whole datapacket would be lost.

    I haven't figured out when or not strings are defined as nullterminated. But I've had quite a few problems with it in delphi
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  2. #12

    Assembly

    hmm... i have had no trubble with that...

    i use Indy 10 for sockets (just figured out how to get it to listen properly, as you can see of my previous thread), and have made my own End-Of-Line, because the original one is just two bytes, that can easily be stumbled uppon in a packet. hterefor, my EOL is now: "'|NDPKG|'+EOL". To use it when sending, just add '|NDPKG|' to whatever you are sending, and it will work.

    i am happy to give you my "ConvertUtils.pas", if it can help!
    besides: you all have helped me fix some major bugs in it!

    Hint: Look at my "StrToRaw" and "GetStr" functions

    [pascal]
    unit ConvertUtils;

    interface

    uses
    Dialogs;

    type
    TStrArray4 = Array[1..4] of Char;
    TStrArray8 = Array[1..8] of Char;

    function ToRawString(P: PChar; Length: Integer) : string;
    function StrToHex (Str: String) : String;
    function HexToInt(s: string): Longword;
    function HexToStr(HexStr: String): String;
    function InvertHex(HexStr: String): String;
    function InvertStr(Str: String): String;

    function StrToRaw(S: String): String;
    function BoolToRaw(B: Boolean): String;
    function ByteToRaw(B: Byte): String;
    function IntToRaw(I: Integer): String;
    function Int64ToRaw(I: Int64): String;
    function SingleToRaw(S: Single): String;

    function GetInt(var RawStr: String): Integer;
    function GetInt64(var RawStr: String): Int64;
    function GetSingle(var RawStr: String): Single;
    function GetByte(var RawStr: String): Byte;
    function GetBool(var RawStr: String): Boolean;
    function GetStr(var RawStr: String): String;

    implementation

    Uses StrUtils, SysUtils;

    function StrToRaw(S: String): String;
    begin
    if (Length(S) <255>= 4) then
    begin
    IntStr:= TStrArray4(RawStr);
    Delete(RawStr,1,4);
    PI:= PInteger(IntStr);
    Result:= PI^;
    end
    else
    ShowMessage('GetInt: Raw String Too Short!');
    end;

    function GetInt64(var RawStr: String): Int64;
    var
    IntStr: TStrArray8;
    PI: PInt64;
    begin
    // IntStr:= TStrArray8(RawStr);
    Delete(RawStr,1,;
    // PI:= PInt64(IntStr);
    Result:= PI^;
    end;

    function GetSingle(var RawStr: String): Single;
    var
    SingleStr: TStrArray4;
    PS: PSingle;
    begin
    if (Length(RawStr) >= 4) then
    begin
    SingleStr:= TStrArray4(RawStr);
    Delete(RawStr,1,4);
    PS:= PSingle(SingleStr);
    Result:= PS^;
    end
    else
    ShowMessage('GetSingle: Raw String Too Short!');
    end;

    function GetByte(var RawStr: String): Byte;
    var
    PB: PByte;
    ByteStr: String;
    begin
    if (Length(RawStr) >= 1) then
    begin
    //ByteStr:= LeftStr(RawStr,1);
    Result:= Ord(RawStr[1]);
    Delete(RawStr,1,1);
    //PB:= PByte(@ByteStr);
    //Result:= PB^;
    end
    else
    ShowMessage('GetByte: Raw String Too Short!');
    end;

    Function GetStr(var RawStr: String): String;
    var
    LenByte: Byte;
    IntStr: String;
    begin
    if (Length(RawStr) >= 1) then
    begin
    LenByte:= Ord(RawStr[1]);
    Delete(RawStr,1,1);
    if (Length(RawStr) >= LenByte) then
    begin
    Result:= LeftStr(RawStr,LenByte);
    Delete(RawStr,1,LenByte);
    end
    else
    ShowMessage('GetStr: Raw String Missing Data!');
    end
    else
    ShowMessage('GetStr: Raw String Too Short!');
    end;

    Function GetBool(var RawStr: String): Boolean;
    var
    BoolByte: Byte;
    begin
    if (Length(RawStr) >= 1) then
    begin
    BoolByte:= GetByte(RawStr);
    Result:= (BoolByte = 255)
    end
    else
    ShowMessage('GetBool: Raw String Too Short!');
    end;

    Function InvertHex(HexStr: String): String;
    var
    i,j: Integer;
    temp: String;
    begin
    i:= 0;
    j:= (length(HexStr) div 2);
    while i < j do
    begin
    temp:= temp+RightStr(HexStr,2);
    HexStr:= LeftStr(HexStr,Length(HexStr)-2);
    inc(i);
    end;

    Result:= temp;
    end;

    Function InvertStr(Str: String): String;
    var
    i,j: Integer;
    temp: String;
    begin
    i:= 0;
    j:= length(Str);
    while i < j do
    begin
    temp:= temp+RightStr(Str,1);
    Str:= LeftStr(Str,Length(Str)-1);
    inc(i);
    end;

    Result:= temp;
    end;

    Function HexToStr(HexStr: String): String;
    var
    Buffer: Array of Byte;
    i,j: Integer;
    begin
    i:= 0;
    SetLength(Buffer, Length(HexStr) div 2);
    j:= 1;
    while i < Length(Buffer) do begin
    Buffer[i]:= StrToInt('$'+ MidStr(HexStr,j,2));
    Inc(j); Inc(j);
    inc(i);
    end;
    Result:= ToRawString(PChar(Buffer),Length(Buffer));
    end;

    function HexToInt(s: string): Longword;
    var
    b: Byte;
    c: Char;
    begin
    Result := 0;
    s := UpperCase(s);
    for b := 1 to Length(s) do
    begin
    Result := Result * 16;
    c := s[b];
    case c of
    '0'..'9': Inc(Result, Ord(c) - Ord('0'));
    'A'..'F': Inc(Result, Ord(c) - Ord('A') + 10);
    else
    raise EConvertError.Create('No Hex-Number');
    end;
    end;
    end;

    function StrToHex (Str: String) : String;
    var
    i, len: integer;
    byte: string;

    begin
    len:= Length(Str);
    i:= 1;

    while i <len> 0) do begin
    Result := result + c;
    P := P + 1;
    c := P^;
    Length := Length - 1;
    end;
    end;

    end.
    [/pascal]

  3. #13

    Assembly

    Diaboli, I've sent you some examples. Did you take a look at them?

  4. #14

    Assembly

    yeah, it didnt compile
    i think the forum changed some parts of it...

    anyways: i got it working now, so

  5. #15

    Re: Assembly

    Quote Originally Posted by Diaboli
    System: Dell M1710 (2,16GHz Duo|2048mb RAM|GeForce Go 7950GTX 512mb)
    IDE: Delphi 2006
    Lib: none

    I trying to make a function that stuffs a single into a string.
    A single is 4 bytes, so i thought it would be jsut as easy as stuffing an integer into a string, but i get a compile error ("Operand Size Mismatch") when i try to move the single into EAX. but a single is 4 bytes, and EAX has place for 4 bytes, so why doesent it work? an integer is 4 bytes too, and it works fine.

    [pascal]
    function IntToRaw(I: Integer): String;
    var
    S: Array[0..3] of Char;
    Int: Integer;
    begin
    asm
    MOV EAX,I //<- Works fine
    MOV S,EAX
    end;
    for int := 3 downto 0 do
    Result:= Result+S[int];
    end;

    function SingleToRaw(SI: Single): String;
    var
    S: Array[0..3] of Char;
    I: Integer;
    begin
    asm
    MOV EAX,SI //<- Operand size mismatch
    MOV S,EAX
    end;
    for I := 3 downto 0 do
    Result:= Result+S[I];
    end;
    [/pascal]

    [pascal]
    function IntToRaw(I: Integer): String;
    var
    S: Array[0..3] of Char absolute I;
    begin
    result := S;
    end;

    function SingleToRaw(SI: Single): String;
    var
    S: Array[0..3] of Char absolute SI;
    begin
    result := S;
    end;
    [/pascal]

    :roll:
    What you see is what you do.
    <br />
    <br />Sorry, I'm not English well.

  6. #16

    Assembly

    SI is a 16-bit-register, so using mov EAX,SI tells the compiler to take 16-bit-SI and copy it to 32-bit-EAX, which he moarns about.
    i've copied your source, renamed SI and it worked... altough the string was crap with a beep, actually :)

    what do you need this for?

    edit: "crap with a beep" = random ascii + the ascii-code for making a beep when outputting it via writeln *g*

  7. #17

    Assembly

    Code:
    function SingleToRaw&#40;vSI&#58; Single&#41;&#58; String;
    var
      S&#58; Array&#91;0..3&#93; of Char;
      p &#58; pointer;
      I&#58; Integer;
    
    begin
      p &#58;= @vSI;
      move&#40;byte&#40;p^&#41;,S&#91;0&#93;,4&#41;;
      for I &#58;= 3 downto 0 do
        singletoraw&#58;= singletoraw+S&#91;I&#93;;
    end;
    ... this works. but i encourage you to write an assembler-version that works :) ... and look up the x86-registers! *gg*

    edit: whoops, didn't realize the answers before mine. it's too early xDD

  8. #18

    Assembly

    ah... i thought it had checked that it wasnt a register.

    i now have a strange problem, though:
    when i use getint or getsingle, it sometimes doesent terurn the way it should.

    for instance in my CharEnum:

    [pascal]
    procedure HandleCharEnum(Data: String);
    var
    Name : String;
    Location : String;
    HP,
    MaxHP,
    MP,
    MaxMP : Single;
    CharID : Integer;
    SpriteID : Integer;
    NumItems : Integer;

    NewChar : TCharEnum;

    ItmName : String;
    ItmDesc : String;
    ItmIcon: Integer;

    i : Integer;
    begin
    Controller.CharEnums.Clear;
    while Length(Data) > 31 do
    begin
    CharID := GetInt(Data);
    GetInt(Data); <- had to add this
    Name := GetStr(Data);
    Location := GetStr(Data);
    SpriteID := GetInt(Data);
    GetInt(Data); <- had to add this
    HP := GetSingle(Data);
    MaxHP := GetSingle(Data);
    MP := GetSingle(Data);
    MaxMP := GetSingle(Data);
    GetSingle(Data); <- I had to add this
    NumItems := GetByte(Data);

    Controller.CharEnums.Add(CharID,Name,Location,HP,M axHP,MP,MaxMP,SpriteID);

    i:= 0;

    while (i <NumItems> 10) do
    begin
    ItmName := GetStr(Data);
    ItmDesc := GetStr(Data);
    ItmIcon := GetInt(Data);
    Controller.CharEnums.GetSelected.AddItem(ItmName,I tmDesc,ItmIcon);
    Inc(i);
    end;

    end;
    end;
    [/pascal]

    i've marked the special stuff with "<- i had to add this".

    the thing is: when a getsingle or getint is followed by some other get-function, it seems not to return properly. the getsingle randomly changed the MaxMP (before i added the ownerless GetSingle(Data; under it), and the CharID and SpriteID was left untouched (at their random init values, wich more often than rare is more than 400000)

    I will see if i can rewrite the functions to assembly within the next few hours (got to eat first, and so on. i'm not THAT slow at writing code). but i have a poker tourney later today, so i expect i wont get much work done....

    MVH
    Diaboli

    P.S:
    heres a pic of the CharEnum screen, as it should be (some graphics are not properly made yet, so it doesent all match):

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •