Thread: Need some pointers to how to convert c++ code into ASM

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Need some pointers to how to convert c++ code into ASM

    Hallo,

    Im close to done with my project now (Making a software blitter with SSE2).

    I first wrote the whole thing with c++ Intrinsics, but have started to rewrite it in inline assembly. It is going ok, mostly to the fact that almost every Intrinsics command maps to a assembly command. But I am having trouble with the _mm_setr command, as it consists of several ASM instructions. I have tried looking at the output from visual studio, but it is far to complex for my assembly "skills". If anyone has time, I would really appreciate some input on how to do the following:

    Convert this instruction into ASM:
    Code:
    __m128i screenRed= _mm_setr_epi16( screenDataPnt[2], screenDataPnt[6], screenDataPnt[10], screenDataPnt[14],
    				 screenDataPnt[18], screenDataPnt[22], screenDataPnt[26], screenDataPnt[30]);
    Here is the visual studio output:
    Code:
    mov	ecx, DWORD PTR _screenDataPnt$[ebp]
    	movzx	dx, BYTE PTR [ecx+30]
    	mov	eax, DWORD PTR _screenDataPnt$[ebp]
    	movzx	cx, BYTE PTR [eax+26]
    	mov	eax, DWORD PTR _screenDataPnt$[ebp]
    	movzx	ax, BYTE PTR [eax+22]
    	mov	esi, DWORD PTR _screenDataPnt$[ebp]
    	movzx	si, BYTE PTR [esi+18]
    	mov	edi, DWORD PTR _screenDataPnt$[ebp]
    	movzx	di, BYTE PTR [edi+14]
    	mov	WORD PTR tv510[ebp], di
    	mov	edi, DWORD PTR _screenDataPnt$[ebp]
    	movzx	di, BYTE PTR [edi+10]
    	mov	WORD PTR tv512[ebp], di
    	mov	edi, DWORD PTR _screenDataPnt$[ebp]
    	movzx	di, BYTE PTR [edi+6]
    	mov	WORD PTR tv514[ebp], di
    	mov	edi, DWORD PTR _screenDataPnt$[ebp]
    	movzx	di, BYTE PTR [edi+2]
    	movzx	edx, dx
    	movd	xmm0, edx
    	movzx	ecx, cx
    	movd	xmm1, ecx
    	movzx	edx, ax
    	movd	xmm2, edx
    	movzx	eax, si
    	movd	xmm3, eax
    	mov	cx, WORD PTR tv510[ebp]
    	movzx	edx, cx
    	movd	xmm4, edx
    	mov	ax, WORD PTR tv512[ebp]
    	movzx	ecx, ax
    	movd	xmm5, ecx
    	mov	dx, WORD PTR tv514[ebp]
    	movzx	eax, dx
    	movd	xmm6, eax
    	movzx	ecx, di
    	movd	xmm7, ecx
    	punpcklwd xmm7, xmm3
    	punpcklwd xmm5, xmm1
    	punpcklwd xmm7, xmm5
    	punpcklwd xmm6, xmm2
    	punpcklwd xmm4, xmm0
    	punpcklwd xmm6, xmm4
    	punpcklwd xmm7, xmm6
    	movdqa	XMMWORD PTR $T19310[ebp], xmm7
    	movdqa	xmm0, XMMWORD PTR $T19310[ebp]
    	movdqa	XMMWORD PTR _screenRed$19309[ebp], xmm0
    Thanks for your time

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Untested:
    Code:
    	mov	ecx, DWORD PTR screenDataPnt
    	xor     edx, edx
    	mov	dl, BYTE PTR [ecx+30]
    	xor	eax, eax
    	mov	al, BYTE PTR [ecx+26]
    	movd	xmm0, edx
    	movd	xmm1, eax
    	mov	dl, BYTE PTR [ecx+22]
    	mov	al, BYTE PTR [ecx+18]
    	movd	xmm2, edx
    	movd	xmm3, eax
    	movzx	dl, BYTE PTR [ecx+14]
    	movzx	al, BYTE PTR [ecx+10]
    	movd	xmm4, edx
    	movd	xmm5, eax
    	movzx	dl, BYTE PTR [ecx+6]
    	movzx	al, BYTE PTR [ecx+2]
    	punpcklwd xmm7, xmm3
    	punpcklwd xmm5, xmm1
    	punpcklwd xmm7, xmm5
    	punpcklwd xmm6, xmm2
    	punpcklwd xmm4, xmm0
    	punpcklwd xmm6, xmm4
    	punpcklwd xmm7, xmm6
    	movdqa	XMMWORD PTR screenRed[ebp], xmm7
    Are you sure you are compiling with optimization on?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Release mode and optimizing for speed is the settings I have now.

    Thanks for looking at the code Matsp if you dont mind I have a few questions

    Code:
    	// Moving the adress of screenDataPnt into ecx
    	mov	ecx, DWORD PTR screenDataPnt
    	// Why make edx 0?
    	xor     edx, edx
    	// Store screenDataPnt[28] in dl
    	mov	dl, BYTE PTR [ecx+30]
    	// Again, why 0
    	xor	eax, eax
    	mov	al, BYTE PTR [ecx+26]
    	// Move edx into xmm0, where does edx get its value? is it from dl?
    	movd	xmm0, edx
    	movd	xmm1, eax
    	mov	dl, BYTE PTR [ecx+22]
    	mov	al, BYTE PTR [ecx+18]
    	movd	xmm2, edx
    	movd	xmm3, eax
    	movzx	dl, BYTE PTR [ecx+14]
    	movzx	al, BYTE PTR [ecx+10]
    	movd	xmm4, edx
    	movd	xmm5, eax
    	movzx	dl, BYTE PTR [ecx+6]
    	movzx	al, BYTE PTR [ecx+2]
    	// Pack the data. Why use all the xmm registers?
    	punpcklwd xmm7, xmm3
    	punpcklwd xmm5, xmm1
    	punpcklwd xmm7, xmm5
    	punpcklwd xmm6, xmm2
    	punpcklwd xmm4, xmm0
    	punpcklwd xmm6, xmm4
    	punpcklwd xmm7, xmm6
    	// Take the packed value and assign it to screenRed
    	movdqa	XMMWORD PTR screenRed[ebp], xmm7
    Would a better option be to do this?:
    When the program start, create a SSE_screenDataPtrRed and have that pointer point to the different values in screenDataPrt?

    Something like: (This points the adress, so if screenDataPrt changes, so does SSE_screenRed)
    SSE_screenRed[0] = screenDataPrt[0]
    SSE_screenRed[1] = screenDataPrt[4]
    SSE_screenRed[2] = screenDataPrt[8]

    Or would that do something weird?

    EDIT:
    The last line gives an error:
    Unhandled exception at 0x004020c7 in testSSEBlitting.exe: 0xC0000005: Access violation writing location 0x0025fa10.
    Last edited by h3ro; 07-16-2008 at 05:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code to convert from pints
    By jadedreality in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2007, 11:18 PM
  2. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  3. ASM 'Out Instruction' crashes, code included.
    By Xei in forum C Programming
    Replies: 5
    Last Post: 03-03-2003, 05:42 PM
  4. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C Programming
    Replies: 0
    Last Post: 10-03-2002, 03:04 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM