Friday 15 May 2015

assembly - Alternative to popl %esp -



assembly - Alternative to popl %esp -

in section 3.4.2, ia32 popl instruction described copying result top of stack destination register , incrementing stack pointer. so, if had instruction of form popl reg, equivalent code sequence:

movl (%esp),reg //read reg stack addl $4,%esp //increment stack pointer

a. in lite of analysis done in problem 4.7, code sequence correctly describe behavior of instruction popl %esp? explain.

b. how rewrite code sequence correctly describes both cases reg %esp other register?

problem 4.7:

the next assembly-code function lets determine behavior of instruction popl %esp ia32:

1 .text 2 .globl poptest 3 poptest: 4 pushl %ebp 5 movl %esp, %ebp 6 pushl $0xabcd force test value 7 popl %esp pop stack pointer 8 movl %esp, %eax set popped value homecoming value 9 leave restore stack , frame pointers 10 ret

we find function returns 0xabcd. imply behavior of popl %esp? other y86 instruction have exact same behavior?

i've been torn on whether or not code sequence in first problem correctly describes behavior of instruction popl %esp. @ first thought yes, because it's getting reg stack popl homecoming value (i wrong on this), , increments esp 4 remove instance stack.

but came across statement "the popl %esp instruction increments stack pointer before info @ old top of stack written destination."

if that's case, increment 4 esp should have occurred before putting value destination register, making

movl (%esp),reg //read reg stack addl $4,%esp //increment stack pointer

an wrong representation of popl %esp.

can clarify on whether or not not correctly describing behavior or popl %esp?

indeed that's wrong equivalent pop. funnily enough, that's 1 intel uses in official instruction set reference too. @ to the lowest degree create things clear in text. improve equivalent code is:

leal 4(%esp), %esp ; utilize lea preserve flags (thanks @sparky) movl -4(%esp), reg

this logical equivalent, because in reality (such interrupt or signal handler) destroy value on stack between 2 instructions. original code doesn't suffer problem.

note works memory operands too, manual says: "if esp register used base of operations register addressing destination operand in memory, pop instruction computes effective address of operand after increments esp register.". got case covered well.

assembly x86 stack cpu-registers ia-32

No comments:

Post a Comment