Author Topic: Succesive Writes vs PUSHes  (Read 799 times)

coder

  • Member
  • **
  • Posts: 103
Succesive Writes vs PUSHes
« on: June 13, 2017, 12:59:16 PM »
I wonder how may successive memory writes can be considered fast before it hits something hard, if compared to multiple PUSHes. For example;

Code: [Select]
sub rsp,20h
mov [rsp],reg-a
...
mov [rsp+n*8],reg-n

vs

Code: [Select]
push reg-a
...
push reg-n

My test code says 6 where it becomes (much) much slower than 6 successive PUSHes.

Can we confirm this?


coder

  • Member
  • **
  • Posts: 103
Re: Succesive Writes vs PUSHes
« Reply #1 on: June 13, 2017, 01:53:33 PM »
sorry, i thought I was on 64-bit. This test code is for 32-bit win. Add more MOV and PUSH to test beyond 7.




aw27

  • Member
  • ****
  • Posts: 706
Re: Succesive Writes vs PUSHes
« Reply #2 on: June 13, 2017, 03:12:57 PM »
sorry, i thought I was on 64-bit. This test code is for 32-bit win. Add more MOV and PUSH to test beyond 7.
We must not use pushes on 64-bit because there is a need to keep the alignment (in many cases).

coder

  • Member
  • **
  • Posts: 103
Re: Succesive Writes vs PUSHes
« Reply #3 on: June 13, 2017, 08:27:00 PM »
We must not use pushes on 64-bit because there is a need to keep the alignment (in many cases).
Indeed, this has many things to do with the 64-bit assembly. For example, it can explain the peculiar designs of MS 64-bit ABI, microcodes, RISC vs CISC and stuff. I am not really into calling conventions and high-level stuff.

sinsi

  • Member
  • ****
  • Posts: 996
Re: Succesive Writes vs PUSHes
« Reply #4 on: June 13, 2017, 08:28:15 PM »
We must not use pushes on 64-bit because there is a need to keep the alignment (in many cases).
Of course you can. If you follow the ABI your proc starts with a stack alignment of 8. Push a register, bingo you are aligned to 16.
I can walk on water but stagger on beer.

coder

  • Member
  • **
  • Posts: 103
Re: Succesive Writes vs PUSHes
« Reply #5 on: June 13, 2017, 08:46:49 PM »
Now my tests say 7 successive writes. Beyond that, you're better off using PUSHes all the way. The speed gains/losses is significantly large.


 


jj2007

  • Member
  • *****
  • Posts: 7551
  • Assembler is fun ;-)
    • MasmBasic
Re: Succesive Writes vs PUSHes
« Reply #6 on: June 13, 2017, 11:03:14 PM »
Interesting test! I've extended it a little bit:
Code: [Select]
1*2*6
MOV  = 422 ticks
PUSH = 405 ticks

1*2*6
MOV  = 406 ticks
PUSH = 405 ticks

2*2*6
MOV  = 593 ticks
PUSH = 593 ticks

2*2*6
MOV  = 593 ticks
PUSH = 593 ticks

4*2*6
MOV  = 1107 ticks
PUSH = 1030 ticks

8*2*6
MOV  = 1872 ticks
PUSH = 1856 ticks

16*2*6
MOV  = 3635 ticks
PUSH = 3635 ticks

32*2*6
MOV  = 7301 ticks
PUSH = 7347 ticks

Results are remarkably similar. This is on a Core i5, I wonder whether there are any differences on older CPUs.

Pure Masm32 attached. I've modified the source a little bit so that it assembles on a standard Masm32 installation.

TWell

  • Member
  • ****
  • Posts: 748
Re: Succesive Writes vs PUSHes
« Reply #7 on: June 13, 2017, 11:28:16 PM »
AMD E1
Code: [Select]
1*2*6

MOV  = 1344 ticks
PUSH = 1328 ticks

1*2*6

MOV  = 1359 ticks
PUSH = 1375 ticks

2*2*6

MOV  = 1891 ticks
PUSH = 1843 ticks

2*2*6

MOV  = 1907 ticks
PUSH = 1828 ticks

4*2*6

MOV  = 3000 ticks
PUSH = 2906 ticks

8*2*6

MOV  = 5172 ticks
PUSH = 5000 ticks

16*2*6

MOV  = 9406 ticks
PUSH = 9172 ticks

32*2*6

MOV  = 19516 ticks
PUSH = 19234 ticks

felipe

  • Member
  • ***
  • Posts: 282
  • I love assembly language programming.
Re: Succesive Writes vs PUSHes
« Reply #8 on: June 13, 2017, 11:57:17 PM »
I did the test, but i wasn't able to copy the output  :(. The results were in the middle of the two above, with the final time around the 10000 ticks. I got a Celeron  :redface:.
Felipe.

aw27

  • Member
  • ****
  • Posts: 706
Re: Succesive Writes vs PUSHes
« Reply #9 on: June 14, 2017, 02:45:44 AM »
We must not use pushes on 64-bit because there is a need to keep the alignment (in many cases).
Of course you can. If you follow the ABI your proc starts with a stack alignment of 8. Push a register, bingo you are aligned to 16.
I mean that you must know exactly what you are doing to keep the pushes balanced with the alignment. Also, in many cases the alignment is not important even if you call a function. However, this is not a good programming practice, but we can always play with bad programming practices and see what happens and learn by trial and error. :lol:

FORTRANS

  • Member
  • ****
  • Posts: 945
Re: Succesive Writes vs PUSHes
« Reply #10 on: June 14, 2017, 04:21:08 AM »
Hi Jochen,

I wonder whether there are any differences on older CPUs.

Code: [Select]
P-III

1*2*6
MOV  = 3225 ticks
PUSH = 2864 ticks

1*2*6
MOV  = 3235 ticks
PUSH = 2914 ticks

2*2*6
MOV  = 4266 ticks
PUSH = 4707 ticks

2*2*6
MOV  = 4316 ticks
PUSH = 4426 ticks

4*2*6
MOV  = 7531 ticks
PUSH = 8262 ticks

8*2*6
MOV  = 11797 ticks
PUSH = 11627 ticks

16*2*6
MOV  = 20930 ticks
PUSH = 20119 ticks

32*2*6
MOV  = 40478 ticks
PUSH = 38896 ticks

P-MMX

A:\>timeit
1*2*6
MOV  = 14684 ticks
PUSH = 10795 ticks

1*2*6
MOV  = 12925 ticks
PUSH = 10785 ticks

2*2*6
MOV  = 18535 ticks
PUSH = 14505 ticks

2*2*6
MOV  = 18510 ticks
PUSH = 14495 ticks

4*2*6
MOV  = 26685 ticks
PUSH = 21895 ticks

8*2*6
MOV  = 48355 ticks
PUSH = 41935 ticks

16*2*6
MOV  = 85131 ticks
PUSH = 69705 ticks

32*2*6
MOV  = 159150 ticks
PUSH = 128936 ticks

Pentium M

1*2*6
MOV  = 1402 ticks
PUSH = 1232 ticks

1*2*6
MOV  = 1202 ticks
PUSH = 1212 ticks

2*2*6
MOV  = 1682 ticks
PUSH = 1713 ticks

2*2*6
MOV  = 1682 ticks
PUSH = 1722 ticks

4*2*6
MOV  = 2704 ticks
PUSH = 2644 ticks

8*2*6
MOV  = 4677 ticks
PUSH = 4867 ticks

16*2*6
MOV  = 8592 ticks
PUSH = 8552 ticks

32*2*6
MOV  = 16434 ticks
PUSH = 15893 ticks

   You should consider having the program output redirectable
to a file for ease of use.

HTH,

Steve N.

jj2007

  • Member
  • *****
  • Posts: 7551
  • Assembler is fun ;-)
    • MasmBasic
Re: Succesive Writes vs PUSHes
« Reply #11 on: June 14, 2017, 05:51:46 AM »
You should consider having the program output redirectable to a file for ease of use.

Thanks, Steve. From a DOS prompt:
Code: [Select]
timeit >results.txt
results.txt

FORTRANS

  • Member
  • ****
  • Posts: 945
Re: Succesive Writes vs PUSHes
« Reply #12 on: June 14, 2017, 06:37:56 AM »
Hi,

   Well...

F:\TEMP\TEST>timeit > a:x

F:\TEMP\TEST>dir a: /od
 Volume in drive A is TRANSPORT
 Volume Serial Number is E6D5-D015

 Directory of A:\

{Snip}
13-06-17  01:13p                 1,765 TIMEIT.TXT
13-06-17  03:00p                 3,072 TIMEIT.EXE
13-06-17  03:20p                     0 x
              16 File(s)        404,460 bytes
               2 Dir(s)         608,256 bytes free


   That doesn't seem to work.  (And it does work for DIR > y and
most other programs.)  I had to cut and paste the results from
the screen.

Cheers,

Steve N.

jj2007

  • Member
  • *****
  • Posts: 7551
  • Assembler is fun ;-)
    • MasmBasic
Re: Succesive Writes vs PUSHes
« Reply #13 on: June 14, 2017, 08:22:20 AM »
   That doesn't seem to work.  (And it does work for DIR > y and
most other programs.)  I had to cut and paste the results from
the screen.

Steve, I rarely use the timeit >results.txt version, but I wouldn't have suggested that without testing it OK, on Win7-64. Now I have also tested it on WinXP SP3 and Win 10.0.15063 - no problems. Which Windows version are you using?

felipe

  • Member
  • ***
  • Posts: 282
  • I love assembly language programming.
Re: Succesive Writes vs PUSHes
« Reply #14 on: June 14, 2017, 11:45:54 AM »
I did the test, but i wasn't able to copy the output  :(. The results were in the middle of the two above, with the final time around the 10000 ticks. I got a Celeron  :redface:.

Code: [Select]
1*2*6
MOV  = 1109 ticks
PUSH = 1031 ticks

1*2*6
MOV  = 1141 ticks
PUSH = 1000 ticks

2*2*6
MOV  = 1313 ticks
PUSH = 1265 ticks

2*2*6
MOV  = 1344 ticks
PUSH = 1250 ticks

4*2*6
MOV  = 1734 ticks
PUSH = 1875 ticks

8*2*6
MOV  = 2828 ticks
PUSH = 3157 ticks

16*2*6
MOV  = 5984 ticks
PUSH = 6578 ticks

32*2*6
MOV  = 10547 ticks


 :t
Felipe.