My code below works perfectly when I call the procedure from main. But if I call the procedure from a couple procedures deep, the stack accumulates return addresses, throwing off the location of my stack parameters. How can I modify my procedure to correctly allocate the proper shadow space before the call, no matter where in my code it is called?
Another way of putting this is that I call the CreateFile procedure several different places in my code, and each call requires a different number of bytes to subtract from rsp to make it work, due to the varying states of the stack. This is just not acceptable. I want to have a single procedure that works correctly no matter where it is called from.
mov rcx, pFileName
mov rdx, GENERIC_READ or GENERIC_WRITE
mov r8, FILE_SHARE_WRITE or FILE_SHARE_READ
xor r9, r9
sub rsp, 110o
mov rax, OPEN_EXISTING
mov qword ptr [rsp + 40o], rax
mov rax, FILE_FLAG_SEQUENTIAL_SCAN
mov qword ptr [rsp + 50o], rax
xor rax, rax
mov qword ptr [rsp + 60o], rax
call CreateFileA
add rsp, 110o