ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call /...
-
Upload
philomena-hensley -
Category
Documents
-
view
215 -
download
1
Transcript of ITEC 352 Lecture 17 Functions in Assembly. Functions + Assembly Review Questions? Branching Call /...
ITEC 352
Lecture 17Functions in Assembly
Functions + Assembly
Review
• Questions?• Branching• Call / Jump• Reminder exam on Friday, project
due next Friday
Functions + Assembly
Outline
• Beyond the basics
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
Functions + Assembly
Process
• How to take simple tools and build something complex
• Bootstrapping 101
Functions + Assembly
A little buildup
• Addressing modes– How to get to memory–Why more than one?
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
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
Functions + Assembly
Jumping
• Have ability to jump back / forth to blocks of code
• What capabilities are not made possible with this approach?
Functions + Assembly
Registers
•Subroutine linkage with registers passes parameters in registers.
High level language equivalent?
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).
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
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?
Functions + Assembly
Back to functions
• Limitations of each approach– Registers– Data link area
• Recursion• Possible solutions
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?
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
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?
Functions + Assembly
Review
• Functions / Assembly intro• Next time– Stacks and the full implementation