News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

quickest way to retrieve HO and LO words of a dword ?

Started by gelatine1, November 12, 2014, 01:11:50 AM

Previous topic - Next topic

gelatine1

I implemented this piece of code and I was wondering if there isn't a quicker/ better way to do this:

;variables used in code below
;x2 dd ?
;y2 dd ?

mov eax,lParam ;Get coords from lParam and store in x2 and y2
and eax,0FFFFh ;retrieve LO word
mov x2,eax
mov eax,lParam
and eax,0FFFF0000h ;retrieve HO word
ror eax,16 ;put HO word as LO word
mov y2,eax


Thanks in advance,
Jannes

hutch--


qWord

low word = WORD ptr lParam
high word = WORD ptr lParam[2]
MREAL macros - when you need floating point arithmetic while assembling!

gelatine1

thanks , but do those work ? since I need to have the low word and high word stored in a dword and not just in a word like your codes are implying ?

qWord

use movzx or movsx according to the type.
e.g.:
movsx eax,WORD ptr lParam
movsx edx,WORD ptr lParam[2]
mov x,eax
mov y,edx
MREAL macros - when you need floating point arithmetic while assembling!

gelatine1


Gunther

Hi gelatine1,

both solutions (Hutch and qWord) should work fine. It could be that Hutch's code gets a few penalty cycles inside a 32-bit segment. But that's all.

Gunther
You have to know the facts before you can distort them.

hutch--

You got it in the form of 16 bit because that is what you asked for, the HI and LO WORDS of a dword.

Tedd

What's wrong with the classic:

    mov eax,lParam
    mov edx,eax
    shr eax,16      ;high word
    and edx,0FFFFh  ;low word
    mov y2,eax
    mov x2,edx

Potato2


dedndave


hutch--

These are my 2, I would normally use the first.


    LOCAL tx    :DWORD
    LOCAL ty    :DWORD

  ; ------------------------------------- 1

    movsx eax, WORD PTR [lParam]
    movsx ecx, WORD PTR [lParam+2]
    mov tx, eax
    mov ty, ecx

    print uhex$(tx),"h  loword",13,10
    print uhex$(ty),"h  hiword",13,10

  ; ------------------------------------- 2

    mov edx, lParam
    movzx eax, dx
    rol edx, 16
    movzx ecx, dx
    mov tx, eax
    mov ty, ecx

    print uhex$(tx),"h  loword",13,10
    print uhex$(ty),"h  hiword",13,10

  ; -------------------------------------

Gunther

Quote from: hutch-- on November 12, 2014, 08:09:38 AM
These are my 2, I would normally use the first.

yes, me to. It's a fast and clear solution.

Gunther
You have to know the facts before you can distort them.

jj2007

Speed-wise I can't see much of a difference. Anyway, in a WndProc it won't matter if it's 2 cycles or 1.8 ( ::) )

Intel(R) Core(TM) i5-2450M CPU @ 2.50GHz (SSE4)

997     cycles for 500 * movzx
879     cycles for 500 * shr & and FFFFh
899     cycles for 500 * movsx

994     cycles for 500 * movzx
874     cycles for 500 * shr & and FFFFh
897     cycles for 500 * movsx

970     cycles for 500 * movzx
871     cycles for 500 * shr & and FFFFh
898     cycles for 500 * movsx

17      bytes for movzx
23      bytes for shr & and FFFFh
17      bytes for movsx

dedndave

a bit disappointed with the movzx/movsx speed
I wonder how much of that may be attributed to the unaligned word
my sister's machine: (I'm in Michigan this week)

Intel(R) Core(TM)2 Duo CPU     T7700  @ 2.40GHz (SSE4)

2035    cycles for 500 * movzx
1042    cycles for 500 * shr & and FFFFh
2042    cycles for 500 * movsx

2040    cycles for 500 * movzx
1042    cycles for 500 * shr & and FFFFh
2115    cycles for 500 * movsx

2034    cycles for 500 * movzx
1052    cycles for 500 * shr & and FFFFh
2041    cycles for 500 * movsx