Question

I have some sample source code for OpenGL, I wanted to compile a 64bit version (using Delphi XE2) but there's some ASM code which fails to compile, and I know nothing about ASM. Here's the code below, and I put the two error messages on the lines which fail...

// Copy a pixel from source to dest and Swap the RGB color values
procedure CopySwapPixel(const Source, Destination: Pointer);
asm
  push ebx //[DCC Error]: E2116 Invalid combination of opcode and operands
  mov bl,[eax+0]
  mov bh,[eax+1]
  mov [edx+2],bl
  mov [edx+1],bh
  mov bl,[eax+2]
  mov bh,[eax+3]
  mov [edx+0],bl
  mov [edx+3],bh
  pop ebx //[DCC Error]: E2116 Invalid combination of opcode and operands
end;
Was it helpful?

Solution

This procedure swaps ABGR byte order to ARGB and vice versa.
In 32bit this code should do all the job:

mov ecx, [eax]  //ABGR from src
bswap ecx       //RGBA  
ror ecx, 8      //ARGB 
mov [edx], ecx  //to dest

The correct code for X64 is

mov ecx, [rcx]  //ABGR from src
bswap ecx       //RGBA  
ror ecx, 8      //ARGB 
mov [rdx], ecx  //to dest

Yet another option - make pure Pascal version, which changes order of bytes in array representation: 0123 to 2103 (swap 0th and 2th bytes).

procedure Swp(const Source, Destination: Pointer);
var
  s, d: PByteArray;
begin
  s := PByteArray(Source);
  d := PByteArray(Destination);
  d[0] := s[2];
  d[1] := s[1];
  d[2] := s[0];
  d[3] := s[3];
end;

OTHER TIPS

64 bit has different names for pointer registers and it is passed difference. The first four parameters to inline assembler functions are passed via RCX, RDX, R8, and R9 respectively

EBX -> RBX
EAX -> RAX
EDX -> RDX

try this

procedure CopySwapPixel(const Source, Destination: Pointer);
{$IFDEF CPUX64}
asm
  mov al,[rcx+0]
  mov ah,[rcx+1]
  mov [rdx+2],al
  mov [rdx+1],ah
  mov al,[rcx+2]
  mov ah,[rcx+3]
  mov [rdx+0],al
  mov [rdx+3],ah
end;
{$ELSE}
asm
  push ebx //[DCC Error]: E2116 Invalid combination of opcode and operands
  mov bl,[eax+0]
  mov bh,[eax+1]
  mov [edx+2],bl
  mov [edx+1],bh
  mov bl,[eax+2]
  mov bh,[eax+3]
  mov [edx+0],bl
  mov [edx+3],bh
  pop ebx //[DCC Error]: E2116 Invalid combination of opcode and operands
end;
{$ENDIF}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top