Does the Push operation in Assembly (MASM) leave the new value of the register the same as the pushed value?
-
25-10-2019 - |
Question
I'm currently working on nested loops in Assembly for my class. What I'm wondering is if I push ecx will I need to move another value into ecx or does the current one have the same info that the pushed ecx had?
Example:
ArraySum PROC
push esi ; save esi, ecx
push ecx
mov eax, 0 ; set the sum to zero
L1:
add eax, [esi] ; add each integer to sum
add esi, TYPE DWORD ; point to next integer
loop L1 ; repeat for array size
pop ecx ; restore ecx and esi to original values
pop esi
ret ; sum is eax
ArraySum ENDP
When I reference ecx again does it contain the original value of 5 even though that value was pushed onto the stack? Lets say, using the same code as above, that I add a loop. Will the counter start at 5 or will I need to re-intialize ecx?
I guess maybe this isn't worded exactly as what is in my head. I'm using this out of my textbook, and it does work. My question I guess is why does it use the original value of ecx if we pushed it and didn't move another value into ecx after the push?
P.S. I know the code example I used isn't a nested loop, but this function is inside another loop. Not that it really has any bearing on my question anyway :)
Solution
Push reads the contents of the register and puts it on the stack, it makes a copy leaving ecx untouched. Ecx keeps what it had in it before the push. Pop DOES modify the register ecx taking what is on the stack and writing it in the register.