lect14 - CMSC 216 Introduction to Computer Systems Lecture...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
CMSC 216 Introduction to Computer Systems Lecture 14 Assembly Language, cont. Jan Plane & Pete Keleher {jplane, keleher}@cs.umd.edu Administrivia • Project 3 – Due date – Read carefully • Continue reading Bryant and O’Hallaron Section 4.1 (Y86 subset) and Chapter 3, for more info on IA-32 instruction set architecture CMSC 216 - Wood, Sussman, Herman, Plane 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
P ROCEDURE I MPLEMENTATION Section 3.7, Bryant and O'Hallaron CMSC 216 - Wood, Sussman, Herman, Plane 3 Reminder: Y86 stack instructions Instruction Effect Description pushl R Reg[ %esp ] Reg[ %esp ] - 4; Mem[Reg[ %esp ]] Reg[R] Push on to stack popl R Reg[R] Mem[Reg[ %esp ]] ; Reg[ %esp ] Reg[ %esp ] + 4 Pop off of stack CMSC 216 - Wood, Sussman, Herman, Plane 4 pushl and popl provide quick ways to work with the program stack • Without pushl , a push operation would look like this (assuming the R above is %eax): irmovl $4, %esi subl %esi, %esp rmmovl %eax, (%esp)
Background image of page 2
Y86 procedure-related instructions Instruction Effect Description call Label push PC on stack PC Label Call function ret PC pop value from stack Return from function CMSC 216 - Wood, Sussman, Herman, Plane 5 • These two instructions are used to handle function calls in Y86 call instruction pushes the return address on the stack, then jumps to the destination address ret instruction returns from a call by getting the value that was pushed in the call • functions are responsible for ensuring the stack is properly adjusted before returning (or you won’t get the same value back during the return that you pushed on in the call) Quick example • What is the value of %eax after this sequence of instructions? call next next: popl %eax • Whatever the address of next is (the memory address where popl instruction is) • Note: this is NOT the PC, but it is related to it – PC is the address of the next instruction to be executed; %eax now has address of most recently executed instruction (the popl ) CMSC 216 - Wood, Sussman, Herman, Plane 6
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Implementing functions • When calling a function, the computer needs to know where execution will resume once the function returns (the return address ) • The call instruction stores this address on the stack; when the called function (the callee ) is done, we'll need to undo any changes made to the stack so that we can get back to the caller CMSC 216 - Wood, Sussman, Herman, Plane 7 Call stack %esp contains the address of the top of the stack (the address of the element most recently pushed) • The stack grows downward, from a maximum address of 0x1000 • To use it, an assembly program must first initialize the stack pointer: irmovl $0x1000, %esp • Then, it can push and pop as necessary CMSC 216 - Wood, Sussman, Herman, Plane 8
Background image of page 4
Simple function call example int n = 2; void f(); void g(); int main() { f(); n = 1; g(); return 0;
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/13/2012 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

Page1 / 12

lect14 - CMSC 216 Introduction to Computer Systems Lecture...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online