Re: get cl number of bytes




Benjamin David Lunt wrote:
Hi,

I have come across this a number of times and have always
come up with a different version. It is the simple idea
that you need to copy/move/retrieve ecx bytes a dword
at a time.

However, if ecx is not a multiple of size dword, you have
to 'and ecx,3' and get cl number of bytes on the tail end.

There seems to be multiple ways:
(assuming edi -> next location, and we want eax to contain
the cl number of bytes.)

xor eax,eax
and cl,3
jz short none
@@: shl eax,8
mov al,[edi]
inc edi
loop @b

However, now eax contains cl bytes in Big Endian order, so
a byte swap instruction is needed.

Another way could be:

and cl,3
jz short none
mov eax,[edi]
cmp cl,3
jne short @f
and eax,00FFFFFFh
jmp short done
@@: cmp cl,2
.
.
.

This makes for a lot of code and jumps.

I am currently using:

and cl,3
jz short none
shl cl,3
mov edx,-1
shl edx,cl
not edx

why not use eax instead of edx then,
and eax,[edi]
instead of the next two lines...*

mov eax,[edi] *
and eax,edx *

If the processor would actually shift 32 bits, I could
dispense with the jump :-)

Maybe I am missing something here, but isn't there an easier,
least amount of code way of doing this?

Please note I am not using anything more than a 386.

Thanks,
Ben


Also...
xor eax,eax
inc eax ;maybe 'mov eax,1' would be better ?
shl cl,03
shl eax,cl ;256^cl
dec eax ;get our mask for eax
and eax,[edi] ;result

I'm pretty much a novice at this sort of thing but I'm sure there's
plenty of people here who can point out the pro's and con's.

.



Relevant Pages