Author Topic: The simplest of byte copy seems to work OK.  (Read 3356 times)

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5940
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
The simplest of byte copy seems to work OK.
« on: June 26, 2016, 12:20:52 PM »

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

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 at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5940
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: The simplest of byte copy seems to work OK.
« Reply #1 on: June 26, 2016, 01:07:31 PM »
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

; ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
« Last Edit: June 26, 2016, 04:53:48 PM by hutch-- »
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

sinsi

  • Member
  • *****
  • Posts: 1089
Re: The simplest of byte copy seems to work OK.
« Reply #2 on: June 26, 2016, 02:31:52 PM »
Code: [Select]
    mov rcx, r8
    and rcx, 3
    rep movsb
Surely "and rcx,7" when dealing with qwords?
I can walk on water but stagger on beer.

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5940
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: The simplest of byte copy seems to work OK.
« Reply #3 on: June 26, 2016, 04:49:42 PM »
 :biggrin:

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

Warning, this is playpen material.  :P
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

rrr314159

  • Member
  • *****
  • Posts: 1382
Re: The simplest of byte copy seems to work OK.
« Reply #4 on: June 27, 2016, 01:01:35 AM »
Yep, 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

  • Member
  • ***
  • Posts: 450
Re: The simplest of byte copy seems to work OK.
« Reply #5 on: June 27, 2016, 06:53:18 AM »
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.
« Last Edit: June 27, 2016, 10:00:40 AM by mineiro »
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

hutch--

  • Administrator
  • Member
  • ******
  • Posts: 5940
  • Mnemonic Driven API Grinder
    • The MASM32 SDK
Re: The simplest of byte copy seems to work OK.
« Reply #6 on: June 27, 2016, 04:36:12 PM »
 :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.
hutch at movsd dot com
http://www.masm32.com    :biggrin:  :biggrin:

rrr314159

  • Member
  • *****
  • Posts: 1382
Re: The simplest of byte copy seems to work OK.
« Reply #7 on: June 27, 2016, 07:31:42 PM »
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

  • Member
  • *****
  • Posts: 1179
    • uasm
Re: The simplest of byte copy seems to work OK.
« Reply #8 on: June 27, 2016, 08:11:15 PM »
Your theory is very interesting :biggrin:
It would be great if that evolution provided at least 1 byte (unsigned) ;)
Cod-Father

mineiro

  • Member
  • ***
  • Posts: 450
Re: The simplest of byte copy seems to work OK.
« Reply #9 on: June 27, 2016, 08:42:44 PM »
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).
« Last Edit: June 27, 2016, 10:03:24 PM by mineiro »
I'd rather be this ambulant metamorphosis than to have that old opinion about everything

nidud

  • Member
  • *****
  • Posts: 1614
    • https://github.com/nidud/asmc
Re: The simplest of byte copy seems to work OK.
« Reply #10 on: June 27, 2016, 10:06:24 PM »
The real measure here must be the expansion time from toddler-size to full-grown and back again. It’s difficult to say the exact number of years this takes but from my brief research done from the picture below, where a full-grown is shrunken by half, my guess will be that the whole process will take more than 128 years.

So I think I have to go with habrans suggestion here and go for the unsigned byte size.

mineiro

  • Member
  • ***
  • Posts: 450
Re: The simplest of byte copy seems to work OK.
« Reply #11 on: June 28, 2016, 01:04:46 AM »
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

  • Member
  • ***
  • Posts: 450
Re: The simplest of byte copy seems to work OK.
« Reply #12 on: November 12, 2016, 08:04:37 AM »
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