Could we have functions? Code re-use might be really nice... What do we need? To jump execution to somewhere else With a way to come back This forms an implicit stack Parameters We could decide on registers to hold these Or we could put them in memory Registers would run faster Return value Again, could be in a register or a memory location A caveat: Is it ok to mess up the caller's register usage? We'll probably need *some* registers to work with Callee vs. caller preserved registers Book and "The C convention" Kinda...it's the C convention with 32-bit Linux NOT 64-bit Why? 64-bit as extra registers, so we use them The stack in memory: Last time we used push a couple times in FreeBSD Special thing about %esp: push and pop change it automatically You could pop by just increasing esp Could do move by copying the item, then adjusting esp Top of stack is always at (%esp) %ebp holds the base pointer In other words, the previous stack pointer Since the stack might change a lot call: Pushes the return address onto the stack Sets %eip to the next instruction ret does the opposite Could we change the return address and return somewhere else? Sure Function general overview: Push old base pointer onto stack, to save it for later Move the current stack pointer into %ebp Subtract enough from %esp so we have room for local variables Access the parameters from the base pointer --- Now we're ready to do our actual work Put the return value where it needs to be Move the current base pointer to the stack pointer Get the old base pointer back off the stack Return Do you really need to do all that? No If you didn't use the stack, for example, you can skip the stack part We need to make our own example now, after looking at the book's example Print something a given number of times How about getting a character from the keyboard and returning it in eax? Active Learning: Using today's example, change the function so it takes a paramter for number of characters to print