Overloaded Delphi 10.2 and Windows 10, but the errors are the same. Asm badly I just learn :)

one)

procedure u(a: cardinal);assembler; asm mov r15, [a] end; procedure TForm1.Button1Click(Sender: TObject); begin u(5); end; 

I put on the breakpoint on asm, turn on FreeMemoryContexts

 unit Vcl.Controls; procedure TWinControl.MainWndProc(var Message: TMessage); begin try try WindowProc(Message); finally FreeDeviceContexts; FreeMemoryContexts; end; except Application.HandleException(Self); end; end; 

And Further Access violation at address. If we change the parameter in the procedure, what would be the 64-bit procedure u (a: Int64) the same error, so the matter is not in the dimension of the parameter and register.


2) We continue the experiment:

 procedure u;assembler; asm mov rcx, 3 @N: nop loop @N nop end; procedure TForm1.Button1Click(Sender: TObject); begin u; end; 

In Delphi, the tick Optimization is not worth it, I put a breakpoint in the asm procedure, but it does not cycle three times, but passes 1 time, then resets the rcx and comes out easily, then at least without errors. It works, without a cycle, that with empty commands ( nop ), that with any other.

    2 answers 2

    1. You are trying to load the contents of the memory at the address in the variable a , and not the variable a itself. Correctly:

       procedure u(a: int64); assembler; // заметьте, что я сменил тип переменной!!! asm mov r15, a end; 
    2. Everything is correct, it should not "loop" in your understanding, it is not for in a high-level language (although for with good optimization it can be reduced to a loop ), switch to machine command mode to make it easier to understand. And for verification you can use, for example, the following code:

       procedure u;assembler; asm mov rax, 1 mov rcx, 3 @N: shl rax, 1 loop @N end; 

    In the RAX register, the output will be 8, our cycle was successfully completed.

    • I found out that simply “asm loop” through F8 does not work, the whole cycle without iterations visible at once, you need to go inside F7 then, so this was the apparent entrance to the cycle once, in fact, no, it passed three times - AntVa
     mov r15, [a] 

    This line copies to the register what lies at the address stored in a.

     u(5); 

    Thus, a call is made to the address $0000000000000005 , which does not refer to the address space of the program (this is generally the protection range of Windows addresses). AV is required to occur.

    To use the number 5, you need to remove the square brackets

    On the second question, there is no point in discussing meaningless code. Make at least a function and paste

      xor rax, rax ... inc eax //в цикле