Quick question about Microsoft x64 calling convention

Started by bradr, August 03, 2007, 07:08:10 AM

Previous topic - Next topic

bradr

Hi,

First post here, so hi!

I've got some x86 inline assembler that I'm porting to x64 using ml64 and have a quick question about the Microsoft x64 calling convention.  I've read that parameters of size 8, 16 and 32 bits are right aligned in the appropriate register (rcx, rdx, r8, r9), but its not clear whether these parameter are zero padded or can there be garbage in the upper bits.

Assuming I need to zero extend to get a 64 bit value is the correct way to do this by "mov edx, edx" for 32 bit unsigned param?

fyi: I'm trying to write a wrapper around repnz scasb (couldn't fine an intrinsic for it).  See below for what I currently have (which seems to work ok).

Any comment greatly appreciated

Brad

;----------------------------------------------------------------------------------------------------
; extern "C" int ScanEQ(BYTE* pStart[rcx], unsigned int iSize[edx], unsigned int iFrom[r8d], BYTE bByte[r9l])
;----------------------------------------------------------------------------------------------------

ScanEQ proc frame

push rdi
.pushreg rdi
.endprolog

sub edx,r8d ; Subtract iFrom from iSize
jbe short not_found

mov edx,edx ; Zero extend     (Not sure if this is necessary)
mov r8d,r8d ; Zero extend     (Not sure if this is necessary)

mov rdi,rcx ; Load start pointer into rdi
mov r10,rcx ; Save a copy of start pointer in rdx
add rdi,r8 ; Add iFrom to start pointer

mov rcx,rdx

mov al,r9b ; Load the byte we're searching for
repnz scasb ; Scan for it
jnz short not_found ; Quit if not found

mov rax,rdi ; Load found address
sub rax,r10 ; Subtract start addres
dec rax ; Remove 1

pop rdi
ret

not_found:
mov eax,0FFFFFFFFh
pop rdi
ret

ScanEQ endp

MazeGen

Hi Brad, welcome :)

I also read this topic at MSDN a couple of times and it is really not much clear. You could expect them to be zero padded, but it is so easy to zero extend them so why not to do that?

And yes, mov edx, edx is right way to do that. The speed loss is very low.

tofu-sensei

At least parameters passed on the stack are not necessarily zero-padded. Just found out the hard way  :wink