Is:

push eax
pop ebx ; copy eax into ebx


The same as:

mov ebx, eax


?
Posted on 2005-08-16 18:45:32 by Lenin
Yes and no; it has the same end-result.

The push/pop combination is slower, does memory read/write, and requires a valid stack.
Posted on 2005-08-16 18:48:37 by f0dder
So I should always try to use mov?
Posted on 2005-08-16 18:53:34 by Lenin
Generally, yes.

However, MOV can't do memory->memory transfers; to do that, you either need to use a register temporarily, or do a push/pop pair (since both push and pop can reference memory).

I prefer using a register temporarily, since it's two bytes shorter than push/pop, and a wee bit faster. But of course it requires that you have a register that you're not using for other purposes.
Posted on 2005-08-16 18:58:41 by f0dder
Thanks  ;)
Posted on 2005-08-16 19:01:24 by Lenin
Lenin,

It depends where you use it. In the middle of a critical algorithm, MOV instructions are faster than push/pop but in general purpose API code, push and pop are easily fast enough. API calls are some powers slower than assembler instructions so there is no loss in speed terms by using the memory based push/pop instead of using a register.
Posted on 2005-08-17 03:57:01 by hutch--
Still no reason to use slower & longer instructions when there isn't a rason to do so :)
Posted on 2005-08-17 03:59:49 by f0dder
Ho hum,

0040102E FF75FC                push    dword ptr
00401031 8F45F8                pop   
00401034 8B45FC                mov    eax,
00401037 8945F8                mov    ,eax


PS : Is your word "rason" a raisin (dried grape) or a reason ?
Posted on 2005-08-17 12:17:55 by hutch--
Same length when indexing indirectly with a register - a byte longer per instruction when dealing with static references. Not to mention slower in either case. Nice try ;)


.text:00000000 A1 16 00 00 00                          mov    eax, blabla
.text:00000005 A3 16 00 00 00                          mov    blabla, eax
.text:0000000A FF 35 16 00 00 00                      push    blabla
.text:00000010 8F 05 16 00 00 00                      pop    blabla

Posted on 2005-08-17 12:24:40 by f0dder
Right try,

3 bytes each instruction is 3 bytes anyway and most memory copy is done as locals, not offsets. This is the type of thinking that generates high speed message boxes. The technique still does not use a register and in terms of an API timing, push / pop is some powers faster.

Still, it may give you the fastest message box on the planet performing such a meaningful optimsation.  :P
Posted on 2005-08-17 13:17:17 by hutch--
4/5 bytes each instruction is 4/5 bytes anyway, and push/pop is still less optimal (even if it's barely timable).

No point in wasting cycles if it's not necessary... but I guess that's an alien thought to an assembly language programmer.
Posted on 2005-08-17 13:29:09 by f0dder
It depends on the situation which method we should use. Sometimes we'll have a spare register, sometimes not. On AthlonXP, usage of a register can produce additional dependency stalls, while a push/pop will just queue the memory operations and do them in parallel to the next code. If ~0.3 cycle is so much important for some code, of course we will make a benchmark and see which method suits the current situation.

I'm against caring too much about optimizations around most API, but not all API are slow. Many of them are just a few instructions long, you should debug a dummy-project that calls them and see how many cycles some of the API that you use take.

Posted on 2005-08-17 18:39:40 by Ultrano