flat assembler
Message board for the users of flat assembler.

Index > Main > mov eax,ecx vs push ecx pop eax?

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
Is there ever ANY difference between these (besides the mov being faster and smaller)?
Post 30 Dec 2008, 22:33
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
Azu wrote:
Is there ever ANY difference between these (besides the mov being faster and smaller)?


PUSH&POPE requires a valid stack with enough space and trashes the 4 bytes there in Wink
Post 30 Dec 2008, 22:46
View user's profile Send private message Reply with quote
asmcoder



Joined: 02 Jun 2008
Posts: 784
asmcoder
[content deleted]


Last edited by asmcoder on 14 Aug 2009, 14:54; edited 1 time in total
Post 30 Dec 2008, 22:48
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
Thanks guys.. four more questions ^^


DOS386 wrote:
Azu wrote:
Is there ever ANY difference between these (besides the mov being faster and smaller)?


PUSH&POPE requires a valid stack with enough space and trashes the 4 bytes there in Wink
How do you tell how much space the stack has? And how do you make sure you're using a valid one as opposed to an invalid one? Confused


asmcoder wrote:
sub esp,4
mov dword [esp], ecx

Why sub from it and then overwrite it with ecx? Doesn't whatever was in it get lost when you mov something else into it? Confused
Post 30 Dec 2008, 22:56
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
DOS386



Joined: 08 Dec 2006
Posts: 1901
DOS386
> How do you tell how much space the stack has? And how do you make
> sure you're using a valid one as opposed to an invalid one?

You get what the OS gave you or you brewed yourself.

> Doesn't whatever was in it get lost when you mov something else into it?

See above: trashes
Post 30 Dec 2008, 23:09
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
So why is that line there? Confused
Post 30 Dec 2008, 23:12
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17260
Location: In your JS exploiting you and your system
revolution
Every time you store something into the stack you must overwrite some old data, you get no choice. But mostly this does not matter since you usually don't need the old data that was previously there. Basically, don't worry about the use of stack trashing old data, it just doesn't matter.
Post 31 Dec 2008, 01:50
View user's profile Send private message Visit poster's website Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
I still don't understand, sorry.

What is the point in decrementing esp and then overwriting it with ecx? Why not just have the mov dword [esp], ecx without the sub esp,4? I feel like I'm missing something important and obvious here.. x_x

And wouldn't the mov always overwrite the data? Regardless of the stack thing? I thought that was the whole point of it.. to overwrite one piece of data with another..
Post 31 Dec 2008, 01:54
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17260
Location: In your JS exploiting you and your system
revolution
You have to make space for your data. In most cases [esp] would point to your return address. If you didn't "sub esp,4" first then you would overwrite some useful data you need (the return address).
Post 31 Dec 2008, 01:59
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2911
Location: [RSP+8*5]
bitRAKE
In long mode it's shorter to PUSH/POP than MOV a 64-bit register if both registers are not r8-r15! XCHG is shorter than a MOV if one of the registers is EAX/RAX, and the other is not r8-r15; and the source is not needed to retain the same value.
Post 31 Dec 2008, 02:45
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17260
Location: In your JS exploiting you and your system
revolution
bitRAKE: I think you should explain what you mean by "shorter". You are saying the instruction encoding is shorter, right? But that will not equate in any way to the amount of work required by the CPU to perform the task. I would suggest that mov is by far the best choice, clear and simple in the source, easier for the CPU to do, no cache or memory considerations etc.
Post 31 Dec 2008, 03:02
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2911
Location: [RSP+8*5]
bitRAKE
Sorry, I was not clear - absolutely, shorter in instruction bytes - code size. XCHG should be as fast as MOV with register operands, but certainly not as widely applicable. Why complicate things unless it's how you have fun? MOV was designed for the job.
Post 31 Dec 2008, 03:24
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
revolution,

Keyword is latency. CPU should update unnecessary bytes...

_________________
"Don't belong. Never join. Think for yourself. Peace." – Victor Stone.
Post 31 Dec 2008, 07:10
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17260
Location: In your JS exploiting you and your system
revolution
latency: mov would still have a lower latency than push/pop I would expect. Why is latency important?

I don't understand "CPU should update unnecessary bytes..." ?
Post 31 Dec 2008, 08:58
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4237
Location: 2018
edfed
troll?

mov ecx,eax vs

@@:
mov ecx,1
imul ecx,eax
xor ecx,ecx
or ecx,eax
cmp ecx,eax
jne @b
...

what is the faster?
Post 31 Dec 2008, 10:36
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17260
Location: In your JS exploiting you and your system
revolution
edfed: I think not posting at all would have been faster. Wink
Post 31 Dec 2008, 11:57
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2911
Location: [RSP+8*5]
bitRAKE
I did this recently...
Code:
    push rsp rsp rsp
    pop rcx rdx r8          ; pnArgc,ppArgv,ppEnv
       enter 8*20,0
        xor r9,r9               ; globbing = FALSE
  lea rax,[rbp-8*15]
  mov [rax-8],rax         ; STARTUPINFO
       call [MSVCRT.__getmainargs]
; nArgc  RBP-8
; pArgv        RBP
; pEnv   RBP+8    
...notice how PUSH RSP RSP RSP sets up an array of consecutive pointers.
(disclaimer: do not use above code)
Post 31 Dec 2008, 15:55
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
revolution,

That's exactly what I've meant: mov is better than push/pop because of unnecessary memory access (cache pollution, PF or even GP Wink) from latter.
Post 13 Jan 2009, 04:07
View user's profile Send private message Reply with quote
Azu



Joined: 16 Dec 2008
Posts: 1160
Azu
Thanks for the detailed explanations Smile
Post 13 Jan 2009, 12:22
View user's profile Send private message Send e-mail AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
IronFelix



Joined: 09 Dec 2004
Posts: 141
Location: Russia, Murmansk region
IronFelix
Some small comment:
It seems that before writing to stack a value is temporary stored somewhere,
because if you execute this:

Code:
push 100
push dword [esp]    

you will get two 100 values.
Maybe "push value" looks like this in abstract:
Code:
mov tmp_reg,value
sub esp,4
mov [esp],tmp_reg    

?
Thanks for attention.

_________________
Flat Assembler is the best!
Post 13 Jan 2009, 17:28
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.