I have this code:
default REL
extern GetStdHandle
extern WriteFile
extern ExitProcess
section .data
true_msg db 'Yes', 0
true_msg_len equ $-true_msg
section .text
global _main
print_yes:
and rsp, -10h
sub rsp, 020h
mov rcx, -0Bh
call GetStdHandle
mov rcx, rax
mov rdx, true_msg
mov r8, true_msg_len
xor r9, r9
push r9
sub rsp, 20h
call WriteFile
add rsp, 40h
pop rax
ret ;// essentially identical to: pop [register] -> jmp [register]
_main:
mov rcx, 2
cmp rcx, 2
jne false1
call print_yes
false1:
mov rcx, 0 ; RCX - first argument.
call ExitProcess
xor rax, rax
ret
Why I must do something like this:
and rsp, -10h
sub rsp, 020h
Thereafter, I can call 'GetStdHandle', then I want to use 'WriteFile' and I must do this:
sub rsp, 20h
1) Why? How can I find this value for other functions?
Also, I must write this:
add rsp, 40h
pop rax
ret
In the first, I restore the original state of the stack pointer. In the second, 'pop rax' register. If I don't do this, my program will end. RIP will be zero?
2) I always 'pop rax' in other labels/functions?