18
ITEC 352 Lecture 19 Functions in Assembly

ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Embed Size (px)

Citation preview

Page 1: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

ITEC 352

Lecture 19Functions in Assembly

Page 2: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Review

• Questions?• Project due on Friday• Stacks• Function activation / deactivation

Page 3: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Outline

• Functions in assembly• Intro to JVM

Page 4: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Activation record

• An activation record is the memory allocated for each function activation. It contains the following data:

return address

memory for each local variable.

memory for parameter values passed from caller function

memory addresses of dynamically allocated memory.

Page 5: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Step 1

• Consider a program:1. int f( int x, int y) {2. int a = 10; 3. int b = 5;4. }5. int main() {6. int z = 5; 7. f(z, z) ; 8. }

Initially the stack is empty.

Execution of this program starts with the function main. Hence, an activation record for main is created as shown on the stack.

PROGRAM STACK

Return address

Activation record for main. Memory for z

Stack pointer (%sp)

Page 6: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Step 2

• Consider a program:1. int f( int x, int y) {2. int a = 10; 3. int b = 5;4. }5. int main() {6. int z = 5; 7. f(z, z) ; 8. }

Next: The function “f” is invoked. We have to create and then push the activation record of “f”. However, before we do that, we have to create space for the actual parameters that we want to pass to function f (here it is z, z)

PROGRAM STACK

Return address

Parameters to be passed to f.

Memory for z

Stack pointer (%sp)

5

5

return addressIn ARC this is stored in %r15 (address of line 8)

Page 7: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Step 2B

• Consider a program:1. int f( int x, int y) {2. int a = 10; 3. int b = 5;4. }5. int main() {6. int z = 5; 7. f(z, z) ; 8. }

Next: This is a continuation from previous slide. You can see that we now have the complete activation record of function f.

PROGRAM STACK

Return address

Activation record of function f.

Memory for z

Stack pointer (%sp)

5

5

return address(%r15)

a (value 10)

b (value 5)

Page 8: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Final step

• Consider a program:1. int f( int x, int y) {2. int a = 10; 3. int b = 5;4. }5. int main() {6. int z = 5; 7. f(z, z) ; 8. }

Next: After f finishes execution its activation record is no longer “live” -- it is popped out.

PROGRAM STACK

Return address

Program stack after f has finished execution. You can see that the local variables a and b are no longer accessible,

Memory for z

Stack pointer (%sp)

Page 9: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Page 10: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Using stacks: in assembly language

Stack pointer is stored in register %r14

A stack grows from high memory region to low memory region. Hence, when we push parameters onto the stack, we subtract a word from

the current stack pointer to update its value.

Page 11: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

I/O in Assembly

• Based on what you studied so far, how do you think I/O must be done in architecture? – E.g., how can a program read from, say

a touchscreen?

Page 12: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

I/O in Assembly

• Two choices: – Use special instructions to access each

I/O device.• Any disadvantages?

– Use memory mapped I/O.• What is memory mapped I/O?

Page 13: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Memory mapped I/O

• Devices are accessed as if they are memory locations. – Devices occupy sections of the address space where no other

memory exists.

0216

217

219

222

223 -4

Reserved for BIOS

Add in Video Memory #1

Add in Video Memory #2

Unused

Working memory

System Stack

Screen flash Touchscreen x Touchscreen y

224 -4

Stack Pointer

I/O Space

Page 14: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Next:

• Example of assembly in the Java Virtual machine

Page 15: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Java Virtual Machine Architecture

Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring

Page 16: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Page 17: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

A Java Class File

Page 18: ITEC 352 Lecture 19 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Stacks Function activation / deactivation

Functions + Assembly

Review

• Functions in assembly• Intro to JVM + Bytecode