A program written in Delphi to reverse the order of the 4 bytes pointed to by the x pointer:
function toulong(x: pchar): longWord;
begin
result := (longword(x^) shl 24) or
(longword((x + 1)^) shl 16) or
(longword((x + 2)^) shl 8) or
(longword((x + 3)^));
end;
The following is the embedded assembly method using Delphi:
function toulong(x: pchar): longword;
asm
mov esi,eax
mov ax,[esi]
xchg ah,al
shl eax,16
mov ax,[esi+2]
xchg ah,al
end;
Note: By default, Delphi uses the "register" method. If the parameters are within 3,
eax, edx and ecx will be used respectively, and the stack will be used for more than 3 parameters. return parameters
The storage depends on the length. For example, 8-bit uses al to return, 16-bit uses ax, 32-bit uses eax, and 64-bit uses two.
32-bit register edx:eax, where eax is the low bit.
Efficiency: In this example, ASM is about 50% faster than Delphi or C.