Delphi / ASM code incompatible with 64 bit?

I have an example source code for OpenGL, I wanted to compile a 64-bit version (using Delphi XE2), but there is some ASM code that does not compile, and I don't know anything about ASM. Here is the code below, and I put two error messages in lines that don't work ...

// 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; 
+6
source share
2 answers

This procedure reverses the byte order of ABGR to ARGB and vice versa.
In 32bit, this code should do all the work:

 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 

Another option is to make a clean version of Pascal, which changes the byte order in the array representation: 0123 - 2103 (swapping the 0th and 2nd 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; 
+12
source

64 bits have different names for pointer registers and the difference is transmitted. The first four parameters for the built-in assembler functions are passed through RCX, RDX, R8 and R9, respectively

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

try it

 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} 
+3
source

Source: https://habr.com/ru/post/916317/


All Articles