Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Assembly

  1. #1

    Assembly

    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]

  2. #2

    Re: Assembly

    Why not use copymemory(or movemem or what it's called)
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  3. #3

    Assembly

    Why not use FloatToStr or at least look at how FloatToStr is coded (to get an idea on how it's done), in Delphi or Free Pascal. it might give you some pointers.
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  4. #4

    Assembly

    Quote Originally Posted by technomage
    Why not use FloatToStr or at least look at how FloatToStr is coded (to get an idea on how it's done), in Delphi or Free Pascal. it might give you some pointers.
    floattostr converts a float to a string representation of that float.

    like:
    f: Single = 1.526543;
    s: string;

    s:= floattostr(f);

    then s is: "1.526543"

    that is not what i need. i need the resulting string to be 4 bytes long, containing the data from the single.

    besides: have you tried figuring out of that code? it looks like egyptian for me

    a few lines of assembly, i can read, buti'm not too good at it, to say the least...

  5. #5

    Assembly

    [pascal]type
    TStrFloat = packed array[1..4] of char;

    function IntToRaw(I: Integer): String;
    begin
    result := TStrFloat(I);
    end;

    SingleToRaw(SI: Single): String;
    begin
    result := TStrFloat(SI);
    end;[/pascal]
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  6. #6

    Assembly

    Quote Originally Posted by JSoftware
    [pascal]type
    TStrFloat = packed array[1..4] of char;

    function IntToRaw(I: Integer): String;
    begin
    result := TStrFloat(I);
    end;

    SingleToRaw(SI: Single): String;
    begin
    result := TStrFloat(SI);
    end;[/pascal]
    That is better than mine

    [pascal]
    function SingleToRaw(s: Single): string;
    type
    TFloatToByte = packed record
    case Byte of
    0: (s: Single);
    1: (a:array[0..4] of Byte);
    end;
    var f: TFloatToByte;
    i: Integer;
    begin
    f.s := s;
    for I := 3 downto 0 do
    Result:= Result+ Char(f.a[I]);
    end;
    [/pascal]
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  7. #7

    Assembly

    Well your's would probably be safer in the context of types. But you use 0..4 and if you used char's instead of bytes you could just implicitly cast a to a string
    Peregrinus, expectavi pedes meos in cymbalis
    Nullus norvegicorum sole urinat

  8. #8

    Assembly

    That code was all from the head and not tested, so yes the Record Structure should be array[0..3] of char, that would remove need for the loop
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  9. #9

    Assembly

    Since I'm guessing that your doing this more for the learning element then for the actual usefulness of the code. Might I suggest you read up a bit more on assembly: http://courses.ece.uiuc.edu/ece390/b.../inst-ref.html

    Thats one of the x86 assembly instruction set sites on the web. The intel specific is at http://www.intel.com/products/proces...uals/index.htm

    Why you would perform the move in assembly and then perform the loop in standard pascal is what is confusing to me. If your going to do it in assembly, perform the entire operation in assembly. Otherwise just use the code posted using records and arrays instead .

    Using MOVSB (Move Single Byte) would also make more sense if your wanting to reverse order the bytes on the out.

    Example from Gavin's Guide to assembly (http://burks.brighton.ac.uk/burks/language/asm/)for moving a string (and yes, you could load your single instead and use decrement):
    Code:
    cld 			; clear direction flag
    mov si,OFFSET String1 	; make ds&#58;si point to String1
    mov di,OFFSET String2 	; make es&#58;di point to String2
    mov cx,18 		; length of strings - In your case this would be 4
    rep movsb 		; copy string1 into string2

    BTW: Technomage, your array should be 0..3 not 0..4 and should be of type char instead of byte. I know the compilers take care of the matter for you, but since the cast is local it makes more sense to just have the type as Char to start with. That is, unless the 64 bit versions are suddenly going to re-define what a char is .

  10. #10

    Assembly

    well, the code is actually highly useful to me. I am supposed to use it to send data between client and server for a rpg i'm trying to code.
    of course, the project is not one i believe i will complete anytime soon, if ever, in the current form, but struggling with this kind of problems tears on my motivation.

    I have, thanks to you guys, now gotten past this irritating problem.
    I find it rather amusing that the solution was VERY similar to what i myself had tried. i believe the word "packed" is the largest difference.
    funny that that word could have saved me 12 hours of "pulling my hair".

Page 1 of 2 12 LastLast

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
  •