News:

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

Main Menu

The simplest of byte copy seems to work OK.

Started by hutch--, June 26, 2016, 12:20:52 PM

Previous topic - Next topic

hutch--


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

main proc

    sub rsp, 40

    mov rcx, psrc
    mov rdx, pdst
    mov r8, LENGTHOF srcbuf
    call mcopy64

    invoke MessageBox,0,pdst,ADDR tmsg,0

    invoke ExitProcess,0

main endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

mcopy64 proc

    ; rcx = source address
    ; rdx = destination address
    ; r8  = byte count

    push rsi
    push rdi

    mov rsi, rcx
    mov rdi, rdx
    mov rcx, r8
    rep movsb

    pop rdi
    pop rsi

    retn

mcopy64 endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

hutch--

#1
This seems to work OK as well.


; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

mcopy64a proc

    ; rcx = source address
    ; rdx = destination address
    ; r8  = byte count

    push rsi
    push rdi

    cld
    mov rsi, rcx
    mov rdi, rdx
    mov rcx, r8

    shr rcx, 3
    rep movsq

    mov rcx, r8
    and rcx, 7        ; <<< modified on suggestion by sinsi.
    rep movsb

    pop rdi
    pop rsi

    retn

mcopy64a endp

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

sinsi


    mov rcx, r8
    and rcx, 3
    rep movsb

Surely "and rcx,7" when dealing with qwords?
Tá fuinneoga a haon déag níos fearr :biggrin:

hutch--

 :biggrin:

Yep, funny enough the and rcx, 3 worked OK.  :dazzled:

Warning, this is playpen material.  :P

rrr314159

Quote from: hutch-- on June 26, 2016, 04:49:42 PMYep, funny enough the and rcx, 3 worked OK.

and rcx, 3 would work as long as third least-significant bit of r8 (byte count) was 0. for instance, 11 bytes = 1011 binary. But not for 13 bytes = 1101 binary, which would copy only 9 bytes. That's why you need and rcx, 7
I am NaN ;)

mineiro

#5
When dealing with boolean instructions (and,or,not,xor) we should see numbers with binary eyes.
Words are 2 bytes, 10b, so a mask to be used with AND to get remainder is 1b.
Dwords are 4 bytes, 100b, so the mask is 11b.
Qwords are 8 bytes, 1000b, so the mask is 111b.
Owords are 16 bytes, 10000b, the mask is 1111b.
32 bytes are 100000b, the mask is 11111b.
We are dividing in essence, but getting remainder, if remainder is zero it's divisible, if not, remainder is not zero.
This way we can create an universal function.

----edited----
I forgot to say, you can do this on addresses so a function will be so much quickly. First stage will be to reach a divisible multiple, after that aligned to a multiple the function get's quickly.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

hutch--

 :biggrin:

Don't read too much into missing a size change in a conversion from 32 to 64 bit mnemonics. I barely have any testing facilities yet and am flying blind on most of it. As the toys add up and the reference material gets better I expect to get a lot more of this stuff up and going.

rrr314159

My theory is that since humans never lived past 63 until very recently, evolution provided only a 6-bit counter for age. So when you hit 64 the counter overflows and that carry bit wanders around randomly in the brain jamming up the works. That's why you think there's only 2 bits (i.e. "3") when there should be 3 (i.e. "7"): that third bit was stored to the left of your age counter, and was overwritten when it overflowed. It's annoying to be off by one bit like that but if you ever make it to 128 years you'll be off by two bits and that really causes trouble. - My theory may sound unlikely but it's proven by the fact that people are lousy at binary arithmetic past age 64. What do you think of it?
I am NaN ;)

habran

Your theory is very interesting :biggrin:
It would be great if that evolution provided at least 1 byte (unsigned) ;)
Cod-Father

mineiro

#9
What sir rrr314159 have said, I asked to 8 eletronic teachers about and only one explain to me into that way, other 7 don't answer me.
What he is saying is that an overflow, underflow, carry, ... can happen with only 2 bits, we don't need a byte.
The idea about the leftmost bit being a signal bit (2 complement) stay on limb (limbo) for years, nobody give value to this idea, until one person say: Hey, we can say that the left most bit is a signal, wow, 0 it's positive and 1 it's negative. But you get the point when you think minimalistic way. This way you meet invalid values like -0==+0?
If you deal with bytes, what's the signed number of value 00h?, other one, and about number 80h? I think NaN (not a number) will start making sense. And, this will never happens if we look to the things with unsigned eyes. We lost values like in any other way, but we can expand unsigned numbers ad infinitum, while on signed numbers we are creating a limit, a jail that's the leftmost digit.

I never see on books the easy way to convert betwen decimal to binary, the way I use is: start from number 1, go to left side multiplying to 2.

..  _  _ _ _ _
.. 16 8 4 2 1
So number 10 (now from left to right) in decimal base is 8+2, we just put numbers 1 on that digits, 01010b. We are subtracting 10-8=2, so we continue walking until find another one that is less or equal to remainder. If remainder is 0 we end, if not continue. So to multiply we use shl, to divide we use shr, but now we loose remainder, that's why that and mask.
NaN can happen with interger signed numbers. And here we are talking about how hackers use their buffer overflow exploit. Because we started count by 1 and not by 0. A byte have 256 possibilities, from 0 to 255 (not 256, 0 is a number, should be counted).
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

nidud

#10
deleted

mineiro

sir nidud,  :eusa_clap:
"Stairway To Heaven"

With a word she can get what she came for.
Cause you know sometimes words have two meanings.
There's a feeling I get when I look to the west,
Yes, there are two paths you can go by, but in the long run
When all are one and one is all
To be a rock and not to roll.

Can be too the singer Leonard Cohen, Anthem.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

mineiro

This is offtopic but I posted here about canadian Leonard Cohen.
Be in peace.
Rest in peace.

There is a crack in everything
That's how the light gets in.

Yes, the light of knowledge reach us.
I'd rather be this ambulant metamorphosis than to have that old opinion about everything