I'm currently writing x86 assembly (FASM) by hand and one typical mistake I often make is to push an argument on the stack, but return before the pop is executed.
This causes the stack offset to change for the caller, which will make the program crash.
This is a rough example to demonstrate it:
proc MyFunction
; A loop:
mov ecx, 100
.loop:
push ecx
; ==== loop content
...
; Somewhere, the decision is made to return, not just to exit the loop
jmp .ret
...
; ==== loop content
pop ecx
loop .loop
.ret:
ret
endp
Now, the obvious answer is to pop the proper number of elements off the stack, before issuing a ret. However, it's easy to overlook something in 1000+ lines of handcrafted assembly.
I was also thinking about using pushad / popad always, but I'm not sure what the convention is for that.
Question: Is there any pattern that I could follow to avoid this issue?