What is the x86 “ret” instruction equivalent to?

前端 未结 5 1935
独厮守ぢ
独厮守ぢ 2020-12-08 16:27

Say I\'m writing a routine in x86 assembly, like, \"add\" which adds two numbers passed as arguments.

For the most part this is a very simple method:



        
5条回答
  •  温柔的废话
    2020-12-08 16:52

    This does not need any free registers to simulate ret, but it needs 4 bytes of memory (a dword). Uses indirect jmp. Edit: As noted by Ira Baxter, this code is not reentrant. Works fine in single-threaded code. Will crash if used in multithreaded code.

    push ebp
    mov  ebp, esp
    mov  eax, [ebp+8]
    add  eax, [ebp+12]
    mov  ebp, [ebp+4]
    mov  [return_address], ebp
    pop  ebp
    
    add  esp,4
    jmp  [return_address]
    
    .data
    return_address dd 0
    

    To replace only the ret instruction, without changing the rest of the code. Not reentrant. Do not use in multithreaded code. Edit: fixed bug in below code.

    push ebp
    mov  ebp, esp
    mov  ebp, [ebp+4]
    mov  [return_address], ebp
    pop  ebp
    
    add  esp,4
    jmp  [return_address]
    
    .data
    return_address dd 0
    

提交回复
热议问题