问题
I have written a assembly function that handles an interrupt. I want to return to the instruction following the one that caused the interrupt. Here is my code,
pushl %ebp
movl %esp,%ebp
pushal
movl %esp, %eax
pushl %eax
pushl $0
call divzero
addl $8, %esp /* add 8 to the stack pointer to skip the two variables*/
popal
popl %ebp /* restore %ebp */
popl %eax /* pop return address from stack and store in eax */
add $4, %eax /* add 4 to value of eax to get the address of next instruction */
jmp %eax
The platform I am coding on is x86 32 bits. I have written comments from my understanding of how the stack works but I am getting invalid opcode interrupt. Here is the stack trace.
exception 6 (invalid opcode) currpid 3 (Main process)
CS EFC0008 eip 1028D4
eflags 10297
register dump:
eax 001028CC (1059020)
ecx 0EFC8FFC (251432956)
edx 00000000 (0)
ebx 00121000 (1183744)
esp 0EFC8FB8 (251432888)
ebp 0EFC8FB8 (251432888)
esi 00000000 (0)
edi 00000000 (0)
For reference I am adding my main function here:
process main(void)
{
asm("int $0");
kprintf("hello\n");
int i = 4 / 0; <- Casue of interrupt
kprintf("hello again\n"); -< The place I want to return to.
}
回答1:
This did the trick for me:
pushl %ebp
movl %esp,%ebp
pushal
movl %esp, %eax
pushl %eax
pushl $0
call divzero
addl $8, %esp /* add 8 to the stack pointer to skip the two variables*/
popal /* resotre the flags */
addl $4, 4(%ebp) /* add 4 to the return address */
popl %ebp /* restore %ebp */
iret /* return from the function */
来源:https://stackoverflow.com/questions/64037899/returning-to-the-next-instruction-following-the-one-stored-in-eip-register