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 |
|
Azu 30 Dec 2008, 22:33
Is there ever ANY difference between these (besides the mov being faster and smaller)?
|
|||
30 Dec 2008, 22:33 |
|
asmcoder 30 Dec 2008, 22:48
[content deleted]
Last edited by asmcoder on 14 Aug 2009, 14:54; edited 1 time in total |
|||
30 Dec 2008, 22:48 |
|
Azu 30 Dec 2008, 22:56
Thanks guys.. four more questions ^^
DOS386 wrote:
asmcoder wrote: sub esp,4 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? |
|||
30 Dec 2008, 22:56 |
|
DOS386 30 Dec 2008, 23:09
> 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 |
|||
30 Dec 2008, 23:09 |
|
Azu 30 Dec 2008, 23:12
So why is that line there?
|
|||
30 Dec 2008, 23:12 |
|
revolution 31 Dec 2008, 01:50
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.
|
|||
31 Dec 2008, 01:50 |
|
Azu 31 Dec 2008, 01:54
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.. |
|||
31 Dec 2008, 01:54 |
|
revolution 31 Dec 2008, 01:59
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).
|
|||
31 Dec 2008, 01:59 |
|
bitRAKE 31 Dec 2008, 02:45
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.
|
|||
31 Dec 2008, 02:45 |
|
revolution 31 Dec 2008, 03:02
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.
|
|||
31 Dec 2008, 03:02 |
|
bitRAKE 31 Dec 2008, 03:24
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.
|
|||
31 Dec 2008, 03:24 |
|
baldr 31 Dec 2008, 07:10
revolution,
Keyword is latency. CPU should update unnecessary bytes... _________________ "Don't belong. Never join. Think for yourself. Peace." – Victor Stone. |
|||
31 Dec 2008, 07:10 |
|
revolution 31 Dec 2008, 08:58
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..." ? |
|||
31 Dec 2008, 08:58 |
|
edfed 31 Dec 2008, 10:36
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? |
|||
31 Dec 2008, 10:36 |
|
revolution 31 Dec 2008, 11:57
edfed: I think not posting at all would have been faster.
|
|||
31 Dec 2008, 11:57 |
|
bitRAKE 31 Dec 2008, 15:55
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 (disclaimer: do not use above code) |
|||
31 Dec 2008, 15:55 |
|
baldr 13 Jan 2009, 04:07
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 ) from latter. |
|||
13 Jan 2009, 04:07 |
|
Azu 13 Jan 2009, 12:22
Thanks for the detailed explanations
|
|||
13 Jan 2009, 12:22 |
|
IronFelix 13 Jan 2009, 17:28
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! |
|||
13 Jan 2009, 17:28 |
|
Goto page 1, 2 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.