ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call /...

18
ITEC 352 Lecture 17 Functions in Assembly

Transcript of ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call /...

Page 1: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

ITEC 352

Lecture 17Functions in Assembly

Page 2: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Review

• Questions?• Branching• Call / Jump• Reminder exam on Friday, project

due next Friday

Page 3: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Outline

• Beyond the basics

Page 4: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Points to ponder

• So far you have seen the instructions available for the ARC machine

• Mapping between high level languages and assembly– Variables– Operations– Assignment– Conditionals– Loops– Arrays– Functions– Objects

Page 5: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Process

• How to take simple tools and build something complex

• Bootstrapping 101

Page 6: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

A little buildup

• Addressing modes– How to get to memory–Why more than one?

Page 7: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

ARC Methods

• Using the call instruction.begin.org 2048 f: ld %r1, %r2 …

.org 0main: …call f ! call routine at function f. ld [x], %r1 instruction that must be executed

after f finishes. halt.end

Page 8: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Writing methods in ARC (2).begin.org 2048 f: ld %r1, %r2 …jmpl %r15+4, %r0.org 0main: …call fld [x], %r1 <- This is at memory address %r15 + 4.

halt.end

Page 9: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Jumping

• Have ability to jump back / forth to blocks of code

• What capabilities are not made possible with this approach?

Page 10: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Registers

•Subroutine linkage with registers passes parameters in registers.

High level language equivalent?

Page 11: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Memory• Subroutine linkage with a data link area passes parameters in a separate

area in memory. The address of the memory area is passed in a register (%r5 here).

Page 12: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

sethi

• Example: • sethi 1, %r1 will load the number “1” in

the 11th position as follows: 0000000000000000000010000000000

The value in register %r1 is not 1!, It is 210 Hence, to load 1 into the register, we now need to

shift the register by 10 bits to the right.srl 10, %r1

10th position

Purpose: set a value in a particular place

Page 13: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Shifty

• Shift right (srl)– Shift a register by a certain # of bits and

store the result in another register (0s are padded)

– srl 10, %r1

• Shift left (sll)– Instead of the right, go left

• Why is shifting bits important?

Page 14: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Back to functions

• Limitations of each approach– Registers– Data link area

• Recursion• Possible solutions

Page 15: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Function calls

void f(){

printf(“enter f”);g();printf(“exit f”);

}void g(){

printf(“enter g”);h();printf(“exit g”);

}void h(){

printf(“enter h”);i();printf(“exit h”);

}

void i(){

printf(“enter i”);i();printf(“exit i”);

}void main(){

f();}

Write out what is called?

Does this remind you of any particular datastructure?

Page 16: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Terms

• Function activation–When its code is being activated– How many times is f activated?

• Function deactivation–When a function’s code goes from being

active to non-active

Page 17: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

OS / Languages

• Keeps track of what is active what is not

• Scheduling algorithms–Multi-tasking

• Scope of variables–Where do they live, when can they be

accessed?

Page 18: ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call / Jump Reminder exam on Friday, project due next Friday.

Functions + Assembly

Review

• Functions / Assembly intro• Next time– Stacks and the full implementation