14 Assembly

14 Assembly - #$ CMSC 216 Introduction to Computer Systems...

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, [email protected] Administrivia • Continue reading Bryant and O’Hallaron Section 4.1 (Y86 subset) and Chapter 3, for more info on IA-32 instruction set architecture 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 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 4 pushl and popl provide quick ways to work with the program stack • Without pushl , a push operation would look like this: 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 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 • functions are responsible for ensuring the stack is properly adjusted before returning Quick example • What is the value of %eax after this sequence of instructions? call next next: popl %eax • Whatever the address of next (the 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 ) 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 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 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; } void f() { g(); } void g() { while (n > 0) printf("%d", n--); } main: irmovl $0x1000, %esp call f irmovl $1, %ebx rmmovl %ebx, n call g halt f: call g ret g: mrmovl n, %ecx Loop: irmovl $0, %eax addl
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 11/16/2011 for the course CMSC 216 taught by Professor Plane during the Spring '11 term at Maryland.

Page1 / 12

14 Assembly - #$ CMSC 216 Introduction to Computer Systems...

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