I have this piece of code:
openFileToRead:
sub sp,4
add sp,4
ret
then I have in my code
call openFileToRead
And now we have a problem. It all works when there is a sub before add, but when I change the sub with add like here:
openFileToRead:
add sp,4
sub sp,4
ret
all hell breaks loose. In ret he jumps to some strange location and memory. There shouldn’t be any difference between those two labels, but there is. Why is that?
I compile with masm on windows xp with Intel CPU, I use 16 bit linker.
You shouldn’t ever have an
addto the stack pointer before asub, in the same way that you shouldn’t ever have apopbefore apush.When you
addto the stack, you’re basically saying that anything at an address less than what’s pointed to by the stack pointer is free memory to be used. When yousubfrom the stack is when you’re essentially allocating memory. The stack pointer starts at high memory and moves towards low memory as things are pushed onto the stack.In the comment above, Hans has a point with the interrupts. If anything takes control after the
addand uses the stack, it’ll overwrite your return address. That’s because you’ve basically “deallocated” it by adding to the stack pointer.