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

Thread: Converting ASM to Pascal

  1. #11
    PGD Staff / News Reporter phibermon's Avatar
    Join Date
    Sep 2009
    Location
    England
    Posts
    524
    I'm glad you're here, I got as far as "ahh, it's pushing params onto the stack for the routine that's called", I'd worked out we'd need to see more of the code to understand the context, the function that's called etc meanwhile you've already sorted it
    When the moon hits your eye like a big pizza pie - that's an extinction level impact event.

  2. #12
    1 other thing to try is the pop's, try:

    Code:
    pop dword edi
    etc

    if all else fails you can always manually solve this by replacing the pop's with something like

    Code:
    mov rdi, dword ptr [rsp+4]
    mov rsi, dword ptr [rsp]
    add rsp, 8
    EDIT

    but actually hehe, i'm tired, anyways you just need to keep edi etc so in this case we can solve it easily..

    your code:
    Code:
         asm
           push esi
           push edi
           sub esp, StkUsage      // make room on stack
           mov edi, esp           // set destination of mem copy, it is the stack
           mov esi, StkAdr        // set source of mem copy, it is Addr(ExtStk)
           mov ecx, StkUsage      // prepare ecx to copy StkUsage bytes
           shr ecx, 2             // divide by 4 to perform DWORD-copy (is faster)
    
    
           //add edi, StkUsage;
           //sub edi,4
           //add esi, StkUsage;
           //sub esi,4
           cld                    // choose copy direction
           rep movsd              // do DWORD-copy
    
    
           cmp RegCall, true
           jnz @@EXEC
           mov EAX, _EAX
           mov EDX, _EDX
           mov ECX, _ECX
    
    
           @@EXEC:
           call Adr               // execute the external function
                                  // esp is restored by the external function
                                  // (except for cdecl-convention)
           pop edi                // restore edi...
           pop esi                // ...and esi
           mov _EAX,eax
         end;                     // asm
    so try this:

    Code:
         asm
           push rsi
           push rdi
           sub rsp, StkUsage      // make room on stack
           mov rdi, rsp           // set destination of mem copy, it is the stack
           mov rsi, StkAdr        // set source of mem copy, it is Addr(ExtStk)
           mov rcx, StkUsage      // prepare ecx to copy StkUsage bytes
           shr rcx, 4             // divide by 8 to perform QWORD-copy (is faster)
    
           cld                    // choose copy direction
           rep movsq              // do QWORD-copy
    
    
           cmp RegCall, true
           jnz @@EXEC
           mov RAX, dword _EAX
           mov RDX, dword _EDX
           mov RCX, dword _ECX
    
    
           @@EXEC:
           call Adr               // execute the external function
                                  // esp is restored by the external function
                                  // (except for cdecl-convention)
           pop rdi                // restore edi...
           pop rsi                // ...and esi
           mov _EAX,eax
         end;                     // asm
    for 64-bit you want to change _EAX etc to int64 and then remove dword casts. Note that this is only valid for fastcall, stdcall would be a big trouble for stack alignment across 32/64 bit
    Last edited by Colin; 18-03-2013 at 08:44 PM.
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  3. #13
    Okay, I changed the code as proposed. This is the [32BIT] part:
    Chip.pas(2042,8) Error: Unrecognized opcode
    Chip.pas(2042,8) Error: Assembler syntax error
    Chip.pas(2042,9) Error: Error converting binary 32
    Now the error from this code:
    Code:
       p := basicstring(resvalue);
    args.outVal.n.sbuf := p;
        p^ := ''
    Chip.pas(2078,9) Error: Illegal type conversion: "LongInt" to "AnsiString"
    Chip.pas(2080,5) Error: Illegal qualifier
    (1st and 3rd line)
    Best regards,
    Cybermonkey

  4. #14
    for assembly, see above as for the error i've just checked that source you posted (the link)

    seems basicstring is a record, so i think you need to check the function "basicstring2string"
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  5. #15
    Hm, the funny thing is there is no function basicstring, it's a type ...
    Code:
    TYPE basicstring = AnsiString;
    Best regards,
    Cybermonkey

  6. #16
    on your chip.pas you posted i see this:

    Code:
     126: 	 basicstring = packed record
     127: 	  size:integer;
     128: 	  data:pointer
     129: 	end;
    anyways as i remember this is the standard format for ansistring, does it doesn't really matter, but in that case what is resvalue?

    try this.

    Code:
        args.outVal.n.sbuf := pansistring(resvalue)^;
        pansistring(resvalue)^ := ''
    i go to sleep for couple hour now, good night and good luck.
    Last edited by Colin; 18-03-2013 at 09:28 PM.
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  7. #17
    Oops, I didn't know the versions differ so much ... I am not using the latest source of that page although the ASM part is the same ... because on newer versions there are even more problems.

    E. g.
    Code:
            Flags := Flags or tfCRLF;
    results in error
    Chip.pas(4170,15) Error: Identifier not found "Flags"
    Chip.pas(4170,24) Error: Identifier not found "Flags"
    Chip.pas(4170,33) Error: Identifier not found "tfCRLF"
    Anyway, the older version now compiles with your ASM code, for the basicstring I did this:
    Code:
    var p:integer;
     p := resvalue;
        args.outVal.n.sbuf := ^p;
        p := 0;
    But of course if I am trying to execute an external function I got a segmentation fault.
    This is the console app which uses Chip.pas, all internal functions are working so far but as said e.g. Test (1,2,3,4,5) or hi results in a segmentation fault.
    Code:
    program cmd;
    
    {$APPTYPE CONSOLE}
    uses SysUtils,Chip;
    
    
    function _Open(const fileName:string):integer;stdcall;
    begin
     Result:=FileOpen(fileName,fmOpenRead)
    end;
    
    
    function _Read(handle:integer; count:integer):string;stdcall;
    var buf:string;
    begin
     SetLength(buf,count);
     count:=FileRead(handle,buf[1],count);
     if count<0 then
      Result:=''
     else begin
      SetLength(buf,count);
      Result:=buf
     end
    end;
    
    
    procedure _Close(handle:integer);stdcall;
    begin
     FileClose(handle)
    end;
    
    
    procedure _Test(a,b,c,d,e:integer);
    begin
     WriteLn(Format('a=%d b=%d c=%d d=%d e=%d',[a,b,c,d,e]))
    end;
    
    
    type
     TMBasic=class(TBasic)
     private
      counter:integer;
     protected
      function Hallo:integer;
     end;
    
    
     function TMBasic.Hallo;
     begin
      inc(counter);
      Result:=counter
     end;
    
    
    var scr,scr2:TMBasic;
    var m:TMethod;
    type tf=procedure of object;
    type tp=function:integer of object;
    var p:tp;
    var pf:tf;
    var p1:pointer;
    begin
     _Test(1,2,3,4,5);
     scr:=TMBasic.Create;
     scr2:=TMBasic.Create;
     try
    
      scr.Map('System.Open',vkHandle or vkStdCall,chr(vkConst or vkString),@_Open);
      scr.Map('System.Read',vkString or vkStdCall,chr(vkHandle)+chr(vkInteger),@_Read);
      scr.Map('System.Close',vkNone or vkStdCall,chr(vkHandle),@_Close);
      scr.Map('Test',vkNone,chr(vkInteger)+chr(vkInteger)+chr(vkInteger)+chr(vkInteger)+chr(vkInteger),@_Test);
      scr.MapMethod('Hi',vkInteger,'',scr,@TMBasic.Hallo);
      scr.MapMethod('Hi2',vkInteger,'',scr2,@TMBasic.Hallo);
    
    
    //  scr.MapMethod('TObject.Free',vkNone,[vkHandle],TObject,@TObject.Free);
    
    
    //  pf:=tf(TMethod(@scr.Hallo));
    
    
    {  p:=tp(@scr.Hallo);
      p1:=@scr.Hallo;
      m:=TMethod(tp(@scr.Hallo));
      scr.MapMethod('Hi',vkInteger,[vkNone],TMethod(@scr.Hallo));}
    
    
    //  scr.Map('Hi',vkInteger or vkSelfMethod,[vkNone],@TMBasic.Hallo);
    
    
      scr.Run('')
     finally
      scr.Free;
      scr2.Free
     end
    end.
    Good night, I go to sleep, too ...
    Best regards,
    Cybermonkey

  8. #18
    don't forget that class functions require "this" to be passed, you can try make it a class static function. I've not took a deep look into chip app yet but if you're wanting to use also stack etc then you need to convert everything to 64-bit, this includes the variables also.
    Download the Ziron Assembler
    Get free hosting for Ziron related fan-sites and Ziron projects, contact me in private message.

  9. #19
    Thanks a lot for your help, but I think it isn't worth the effort. I will keep it 32 bit only, it might execute on a 64 bit Linux, too.

    EDIT: Ok, i compiled it on a 32 bit Linux and it works fine on 64 bit. I think I will go that way now. Nevertheless, thank you very much for your help. Maybe one day ...
    Last edited by Cybermonkey; 19-03-2013 at 09:22 PM.
    Best regards,
    Cybermonkey

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
  •