When i do a procedure call:
call @label
What it does is push the current instruction pointer (ip) into the stack and jump to the procedure addres or label:
push rip
jmp @label (or mov rip, offset @label)
And when i do a return from a process
ret
What it does, is to pop the instruction pointer back
pop rip
So, i am trying to simulate the return instruction. The idea is to do a procedure call and i would have the address of my next instruction on the stack. So what i have to do is to move that address onto a register, fix the stack pointer and do a regular jump to that address.
If you run the program, both rax and the label @return has the same address. However, when i try to do the jump with the register the program crashes while when i do it with the label; it is fine.
call @label
What it does is push the current instruction pointer (ip) into the stack and jump to the procedure addres or label:
push rip
jmp @label (or mov rip, offset @label)
And when i do a return from a process
ret
What it does, is to pop the instruction pointer back
pop rip
So, i am trying to simulate the return instruction. The idea is to do a procedure call and i would have the address of my next instruction on the stack. So what i have to do is to move that address onto a register, fix the stack pointer and do a regular jump to that address.
STD_OUTPUT_HANDLE equ -11
extern WriteFile:near
extern GetStdHandle:near
.data
nNumberOfBytesToWrite dq 0
lpNumberOfBytesWritten dq 0
lpBuffer db 16 dup (0)
.code
public main
main:
call _nest1
return:
extern ExitProcess:near
call ExitProcess
ret
_nest1:
mov rax, return
call _PrintInt
mov rax,
call _PrintInt
sub rsp, 8
;jmp return
jmp rax
ret
_PrintInt:
;mov rax, rsp
;sub rax, 8
mov rdi, offset lpBuffer
mov r10, 10
xor r8, r8
write_nchar:
xor rdx, rdx
div r10
add rdx, '0'
add r8, 1
push rdx
test rax, rax
jnz write_nchar
mov rax, r8
order_nchar:
pop rdx
mov , dl
add rdi, 1
sub rax, 1
jnz order_nchar
mov byte ptr , 10
add r8, 1
mov rcx, STD_OUTPUT_HANDLE
call GetStdHandle
mov rcx, rax
mov rdx, offset lpBuffer
mov r9, offset lpNumberOfBytesWritten
mov qword ptr , 0
call WriteFile
ret
end
If you run the program, both rax and the label @return has the same address. However, when i try to do the jump with the register the program crashes while when i do it with the label; it is fine.
sub rsp, 8
;jmp return
jmp rax
well at that part rax has been trashed by the printint routine... so get rax from again.. which should be the return address
....
sub rsp, 8
mov rax,
jmp rax
something like that... try debugging it, that way you can see the registers being trashed... (which you should have done to begin with)...
Yes, you are right. That happens when i do random procedures on the fly without saving registers first. :shock:
Thank you very much. ;)
Thank you very much. ;)