The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: RedXVII on February 25, 2006, 03:28:02 PM

Title: Quick Speed Question
Post by: RedXVII on February 25, 2006, 03:28:02 PM
I keep coming across other peoples code with this in it;

push var1
pop var2


Is this faster/better than;

mov eax, var1
mov var2, eax


I know push/pop means you wouldnt have modify the eax value, but i see push/pop it alot when it really doesnt matter.
Just curious  :bg
Title: Re: Quick Speed Question
Post by: BogdanOntanu on February 25, 2006, 04:05:26 PM
You are right;)

1)  push /pop is slower but it does not touch or make dirty a register... compiler have a tendecy to use this but there is an MASM macro that does the same
2) mov reg, var1 and then mov var2,reg is faster but it does need an empty register. ...

There are no special meanings hidden beneath ;)

Compiler do strange things at times because they are forced to work with a fixed set of rules and have no real knowledege about the algorithic side.
But then again hummans get lazy much easyer ;)


Title: Re: Quick Speed Question
Post by: MichaelW on February 25, 2006, 04:21:17 PM
Running this:

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
    .586
    include timers.asm
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      var1 dd 0
      var2 dd 0
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    LOOP_COUNT EQU 1000000
    REPEAT_COUNT EQU 100

    REPEAT 4

      counter_begin LOOP_COUNT,HIGH_PRIORITY_CLASS
        REPEAT REPEAT_COUNT
          xor   edx, edx    ; Arbitrary separator
          push  var1
          pop   var2
          add   edx, 1      ; Arbitrary separator
        ENDM
      counter_end
      print ustr$(eax)," cycles",13,10

      counter_begin LOOP_COUNT,HIGH_PRIORITY_CLASS
        REPEAT REPEAT_COUNT
          xor   edx, edx    ; Arbitrary separator
          mov   eax, var1
          mov   var2, eax
          add   edx, 1      ; Arbitrary separator
        ENDM
      counter_end
      print ustr$(eax)," cycles",13,10

    ENDM
   
    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


On a P3 I get these results:

532 cycles
197 cycles
532 cycles
197 cycles
532 cycles
197 cycles
532 cycles
197 cycles


So on a P3 push/pop is much slower. Without the separator instructions I get:

399 cycles
196 cycles
398 cycles
196 cycles
398 cycles
196 cycles
398 cycles
196 cycles


I think indicating that push/pop do not combine well with other instructions.



[attachment deleted by admin]
Title: Re: Quick Speed Question
Post by: EduardoS on February 25, 2006, 04:32:27 PM
I got the same results both with separators and without on an Athlon 64:

398 cycles
196 cycles
398 cycles
196 cycles
398 cycles
196 cycles
398 cycles
196 cycles
Press any key to exit...