FUNCTION 353156 – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.

Slides:



Advertisements
Similar presentations
Passing by-value vs. by-reference in ARM by value C code equivalent assembly code int a;.section since a is not assigned an a:.skip initial.
Advertisements

1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
OUTPUT INTERFACE – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
CONDITION CODE AND ARITHMETIC OPERATIONS – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
Lecture 8. MIPS Instructions #4 – Branch Instructions #2
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
MIPS Assembly Language
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
20/06/2015CSE1303 Part B lecture notes 1 Functions, part 2 Lecture B15 Lecture notes, section B15.
COMP3221 lec18-function-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 17 : Functions in C/ Assembly - III
ELEC2041 lec18-pointers.1 Saeid Nooshabadi COMP3221/9221 Microprocessors and Interfacing Lectures 7 : Pointers & Arrays in C/ Assembly
COMP3221 lec15-function-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 15 : Functions in C/ Assembly - I
Lecture 4: Advanced Instructions, Control, and Branching cont. EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
Runtime Environments Compiler Construction Chapter 7.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
RELATIONAL OPERATORS LOGICAL OPERATORS CONDITION Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Making Decision – Microprocessor
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
BITWISE OPERATIONS – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
1 Branches and Procedure Calls Lecture 14 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Procedure (Method) Calls Ellen Spertus MCS 111 September 25, 2003.
Computer Architecture CSE 3322 Lecture 4 crystal.uta.edu/~jpatters/cse3322 Assignments due 9/15: 3.7, 3.9, 3.11.
USER DEFINED FUNCTIONS Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Review : C Programming Language
ARM-7 Assembly: Example Programs 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Lecture 6: Branching CS 2011 Fall 2014, Dr. Rozier.
DR. SIMING LIU SPRING 2016 COMPUTER SCIENCE AND ENGINEERING UNIVERSITY OF NEVADA, RENO Session 12 Procedure Calling.
F28HS Hardware-Software Interface Lecture 11: ARM Assembly Language 5.
MULTI-DIMENSION ARRAY STRING Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Practical Session 3.
Writing Functions in Assembly
Computer Architecture and Assembly Language
ECE 382 Lesson 22 Readings Lesson Outline Writing Clean Code
Computer Science 210 Computer Organization
Lecture 5: Procedure Calls
Computer Architecture and Assembly Language
CSCI206 - Computer Organization & Programming
The University of Adelaide, School of Computer Science
April 2006 Saeid Nooshabadi
Procedures (Functions)
Writing Functions in Assembly
April 2006 Saeid Nooshabadi
Iteration statement while do-while
ARM Assembly Programming
CSCI206 - Computer Organization & Programming
Passing by-value vs. by-reference in ARM
Computer Science 210 Computer Organization
Solutions Chapter 2.
Multiple Entry Points Multiple entry points allow different function names, different parameter lists, default parameters, etc. Example int fn( int a =
Assembly Language Programming II: C Compiler Calling Sequences
MIPS Instructions.
The University of Adelaide, School of Computer Science
MIPS Functions.
Multi-modules programming
X86 Assembly Review.
Computer Architecture
Jump & Loop instructions
Branch & Call Chapter 4 Sepehr Naimi
Iteration Statement for
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
You are the manager of a team of ARM programmers
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

FUNCTION – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat

What is Function ?  Sometimes, you need to do an operation that involve many instructions and you want to use that operation often.  For example : you want to compute an equation f(N, R)  f(N, R) = N! + R! – (N – R)!  You need to compute N! (N – R)! R!  So, you need to code instructions to find factorial 3 times.

Factorial  Factorial of n = n * (n – 1)! = n * (n – 1) * (n – 2)! = n * (n – 1) * (n – 2) *…* 1  Remind : 0! = 1  Example :  5! = 5 * 4 * 3 * 2 * 1 = 120

Code for Computing Factorial i >= 1 Factorial = Factorial * i true false Factorial = 1 i = N i = i - 1 int Factorial = 1, i; i = N; while (i >= 1) { Factorial = Factorial * i; i = i – 1; } We use 3 variables. Thus, assume we map Factorial  a1 i  a2 N  v1 MOV a1, #1 MOV a2, v1 LOOP CMP a2, #1 BLT DONE MUL a1, a2, a1 SUB a2, a2, #1 B LOOP DONE

Code for Computing f(N,R) without using function  Example : Assume that we fix N = 5 and R = 3, f(N, R) = N! + R! – (N – R)! ;Compute N!(5!), A1(Factorial), A2(i), N = 5, Result in A1 MOV a1, #1 ; Factorial = 1 MOV a2, #5 ; i = 5 (=N) LOOP1 CMP a2, #1 ; Compare i and 1 BLT DONE1 ; if i < 1, jump to DONE1 MUL a1, a2, a1 ; Factorial = Factorial * i SUB a2, a2, #1 ; i = i - 1 B LOOP1 ; jump to Loop1 DONE1 MOV v1, a1 ; V1 stores result of 5! ;Compute R!(3!) A1(Factorial), A2(i), R = 3, Result in A1 MOV a1, #1 ; Factorial = 1 MOV a2, #3 ; i = 3 (=R) LOOP2 CMP a2, #1 ; Compare i and 1 BLT DONE2 ; if i < 1, jump to DONE2 MUL a1, a2, a1 ; Factorial = Factorial * i SUB a2, a2, #1 ; i = i - 1 B LOOP2 ; jump to Loop2 DONE2 MOV v2, a1 ; V2 stores result of 5! ;Compute (N-R)! -> (2!), A1(Factorial), A2(i), ;(N-R) = 2, Result in A1 MOV a1, #1 ; Factorial = 1 MOV a2, #2 ; i = 2 (=(N-R)) LOOP3 CMP a2, #1 ; Compare i and 1 BLT DONE3 ; if i < 1, jump to DONE1 MUL a1, a2, a1 ; Factorial = Factorial * i SUB a2, a2, #1 ; i = i - 1 B LOOP3 ; jump to Loop1 DONE3 ADD v1, v1, v2 ; N! + R! SUB a1, v1, a1 ; (N! + R!) – (N – R)! Wow, lot of code, isn’t it ?

Function : How does it work Input Output Function

Define a Function in C int power2(int x) { int y; y = x * x; return(y); } int power2(int x) { int y; y = x * x; return(y); } Type of function Function name Function input (parameters or arguments) Local variable Function instructions Return value from Function

Review : C function (1) #include int power3(int x) { return( x * x * x); } int main(int argc, char **argv) ‏ { int num; num = power3(2); printf(“2^3 = %d\n”, num); } power3 Main ? num x 2 8 2^3 = 8

Review : C function (2) #include int sum(int x, int y) { return(x + y); } int main(int argc, char **argv) ‏ { int a = 5, b = 10, f; f = sum(a, b); printf(“Summation of %d and %d = %f\n”, a, b, f); } average Main 5 10 a b x y 5 ? f 15 Summation of 5 and 10 = 15

Review : C function (3) int main(int argc, char **argv) ‏ { int N = 5, R = 3, N_R = 2; int factorialN = 1, factorialR = 1; int factorialN_R = 1, result; //Compute N! i = N; while (i >= 1) { factorialN = factorialN * i; i = i – 1; } //Compute R! i = R; while (i >= 1) { factorialR = factorialR * i; i = i – 1; } //Compute (N-R)! i = N_R; while (i >= 1) { factorialN_R = factorialN_R * i; i = i – 1; } //Compute the result of equation result = factorialN + factorialR – factorialN_R; }  Example : Assume that we fix N = 5 and R = 3, f(N, R) = N! + R! – (N – R)!

Review : C function (4) int Factorial(int x) { int i, factorial = 1; i = x; while (i >= 1) { factorial = factorial * i; i = i – 1; } return factorial; } int main(int argc, char **argv) ‏ { int N = 5; int R = 3; int N_R = 2; //Compute the result of equation result = Factorial(N) + Factorial(R) - Factorial(N_R); }  Example : Assume that we fix N = 5 and R = 3, f(N, R) = N! + R! – (N – R)! Factorial FunctionMain Function Call Functions

Basic of Function Call Set up arguments Jump to function Access result. Caller Access arguments Compute Set up return value Jump back to caller Callee

ARM Procedure Call Standard (APCS)  Conventions for use of register simplification Register nameSoftware nameUsage R0 – R3a1 – a4Arguments and Result of Function R4 – R10v1 – v7Local variables R9SBStatic variable base R10SLStack limit R11FP/v8Frame pointer/Local variables R12IPIntra procedure call R13SPStack pointer R14LRReturn Address R15PCProgram Counter

Instruction Support for Function Call..... sum(a, b);..... int sum(int x, int y) { return(x + y); } AddressInstructionsComment 1000MOV a1, v1Prepare argument a1 1004MOV a2, v2Prepare argument a2 1008MOV LR, #0x1010Set Return Address CB SUMJump to Label SUM 1010….. ……..…… 2000SUM : ADD a1, a1, a2Compute SUM 2004MOV PC, LRSet program counter with LR Assume a stores value in V1 register b stores value in V2 register How do you know the return address ?? MOV LR, #0x1010 ARM helps you with BL instruction BL SUM  MOV LR, return_address and then B SUM New version of ARM assembler recommends to use BX LR instead of MOV PC, LR

Example 1: ARM calculation without function  We want to compute  Output will be stored in register a1 AREA ex1_no_func, CODE, READONLY ENTRY start MOV a1, #2; Set a1 = 2 MUL v1, a1, a1; v1 = a1 * a1 MUL v1, a1, v1; v1 = a1 * v1 = 2 3 MOV a1, #3; Set a1 = 3 MUL v2, a1, a1; v2 = a1 * a1 MUL v2, a1, v2; v2 = a1 * v2 = 3 3 MOV a1, #1; Set a1 = 1 MUL v3, a1, a1; v3 = a1 * a1 MUL v3, a1, v3; v3 = a1 * v3 = 1 3 ADD a1, v1, v2; a1 = v1 + v2 = SUB a1, a1, v3 ; a1 = a1 – v3 = ( )- 1 3 END

Example 1: ARM calculation with function  We want to code a function called power3 which helps us to compute  Output will be stored in register a1 POWER3 MUL v1, a1, a1 ; v1 = a1 * a1 MUL a1, v1, a1 ; a1 = v1 * a1 BX LR ; return ASM code for computing power3 AREA ex1_func, CODE, READONLY ENTRY start MOV a1, #2; Set a1 = 2 BL POWER3; Call function MOV a2, a1 ; a2 = 2 3 MOV a1, #3; Set a1 = 3 BL POWER3; Call function MOV a3, a1; a3 = 3 3 MOV a1, #1; Set a1 = 1 BL POWER3 ; Call function a1 = 1 3 ADD a2, a2, a3; a2 = SUB a1, a2, a1 ; a1 = ( )- 1 3 B Exit Exit END

Example 1: ARM calculation with function  We want to code a function called power3 which helps us to compute  Output will be stored in register a1 POWER3 MUL v1, a1, a1 ; v1 = a1 * a1 MUL a1, v1, a1 ; a1 = v1 * a1 BX LR ; return ASM code for computing power3 AREA ex1_func, CODE, READONLY ENTRY start MOV a1, #2; Set a1 = 2 BL POWER3; Call function MOV a2, a1 ; a2 = 2 3 MOV a1, #3; Set a1 = 3 BL POWER3; Call function MOV a3, a1; a3 = 3 3 MOV a1, #1; Set a1 = 1 BL POWER3 ; Call function a1 = 1 3 ADD a2, a2, a3; a2 = SUB a1, a2, a1 ; a1 = ( )- 1 3 B Exit Exit END

Factorial Function in ARM Assembly a2 >= 1 a1 = a1 * a2 true false a1 = 1 a2 = a2 - 1 FACTORIAL MOV a1, #1 ;Set result = 1 LOOP CMP a2, #1 ; Compare a2 and 1 BLT DONE ; a2 < 1; jump to DONE MUL a1, a2, a1 ; a1 = a1 * a2 SUB a2, a2, #1 ; a2 = a2 - 1 B LOOP ; jump to Loop DONE BX LR ; return to caller We use 2 variables. a1  result of factorial a2  input number

Computing f(N,R) by using function  Example : Assume that we fix N = 5 and R = 3, f(N, R) = N! + R! – (N – R)! Area func, CODE, READONLY Entry START MOV a2, #5 ; Set argument = 5 BL Factorial ; Call function MOV v1, a1 ; Backup result to v1 (5!) MOV a2, #3 ; Set argument = 3 BL Factorial ; Call function MOV v2, a1 ; Backup result to v2 (3!) MOV a2, #2 ; Set argument = 2 BL Factorial ; Call function a1 = 2! ADD v1, v1, v2 ; v1 = 5! + 3! SUB a1, v1, a1 ; a1 = (5! + 3!) – 2! B EXIT ; End program Factorial MOV a1, #1 ;Set result = 1 LOOP CMP a2, #1 ; Compare a2 and 1 BLT DONE ; a2 < 1; jmp to DONE MUL a1, a2, a1 ; a1 = a1 * a2 SUB a2, a2, #1 ; a2 = a2 - 1 B LOOP ; jump to Loop DONE BX LR ; return to caller EXIT END

Comparing code with and without function AREA nofunc, CODE, READONLY ENTRY START MOV a1, #1 MOV a2, #5 LOOP1 CMP a2, #1 BLT DONE1 MUL a1, a2, a1 SUB a2, a2, #1 B LOOP1 DONE1 MOV v1, a1 MOV a1, #1 MOV a2, #3 LOOP2 CMP a2, #1 BLT DONE2 MUL a1, a2, a1 SUB a2, a2, #1 B LOOP2 DONE2 MOV v2, a1 MOV a1, #1 MOV a2, #2 LOOP3 CMP a2, #1 BLT DONE3 MUL a1, a2, a1 SUB a2, a2, #1 B LOOP3 DONE3 ADD v1, v1, v2 SUB a1, v1, a1 END AREA func, CODE, READONLY ENTRY START MOV a2, #5 BL Factorial MOV v1, a1 MOV a2, #3 BL Factorial MOV v2, a1 MOV a2, #2 BL Factorial ADD v1, v1, v2 SUB a1, v1, a1 B EXIT Factorial MOV a1, #1 LOOP CMP a2, #1 BLT DONE MUL a1, a2, a1 SUB a2, a2, #1 B LOOP DONE BX LR EXIT END

Assignment 8  Write a program in ARM assembly to compute 5 4 –  Define function called POWER4 to compute x 4  Input argument of function prepares in register a2  Output of function stores in register a1  Output of Program stores in register a1