[ Pobierz całość w formacie PDF ]
.If we simply want to access the value on the top of the stack without removing it,we can simply use the%espregister in indirect addressing mode.For example, thefollowing code moves whatever is at the top of the stack into%eax:movl (%esp), %eaxIf we were to just do this:movl %esp, %eaxthen%eaxwould just hold the pointer to the top of the stack rather than the valueat the top.Putting%espin parenthesis causes the computer to go to indirectaddressing mode, and therefore we get the value pointed to by%esp.If we want toaccess the value right below the top of the stack, we can simply issue thisinstruction:movl 4(%esp), %eaxThis instruction uses the base pointer addressing mode (see the Section calledData Accessing Methods in Chapter 2) which simply adds 4 to%espbeforelooking up the value being pointed to.In the C language calling convention, the stack is the key element forimplementing a function s local variables, parameters, and return address.Before executing a function, a program pushes all of the parameters for thefunction onto the stack in the reverse order that they are documented.Then theprogram issues acallinstruction indicating which function it wishes to start.Thecallinstruction does two things.First it pushes the address of the nextinstruction, which is the return address, onto the stack.Then it modifies theinstruction pointer (%eip) to point to the start of the function.So, at the time thefunction starts, the stack looks like this (the "top" of the stack is at the bottom on54 Chapter 4.All About Functionsthis example):Parameter #N.Parameter 2Parameter 1Return Address [ Pobierz całość w formacie PDF ]