Writing Functions in Assembly

Slides:



Advertisements
Similar presentations
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.
Advertisements

COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
UEE072HM Linking HLL and ALP An example on ARM. Embedded and Real-Time Systems We will mainly look at embedded systems –Systems which have the computer.
Introduction to Embedded Systems Intel Xscale® Assembly Language and C Lecture #3.
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
The University of Adelaide, School of Computer Science
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
F28PL1 Programming Languages Lecture 5: Assembly Language 4.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Chapter 11-14, Appendix D C Programs Higher Level languages Compilers C programming Converting C to Machine Code C Compiler for LC-3 Please return breadboards.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Stacks and Frames Demystified CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
System Calls 1.
Chapter 7 Evaluating the Instruction Set Architecture of H1: Part 1.
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.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Lesson 13 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Topic 9: Procedures CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Procedure Basics Computer Organization I 1 October 2009 © McQuain, Feng & Ribbens Procedure Support From previous study of high-level languages,
MAL 3 - Procedures Lecture 13. MAL procedure call The use of procedures facilitates modular programming. Four steps to transfer to and return from a procedure:
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Chapter 2 — Instructions: Language of the Computer — 1 Conditional Operations Branch to a labeled instruction if a condition is true – Otherwise, continue.
More on Assembly 1 CSE 2312 Computer Organization and Assembly Language Programming Vassilis Athitsos University of Texas at Arlington.
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.
Function Calling. Mips Assembly Call and Return Steps for procedure calling –Save the return address –Jump to the procedure (function) –Execute the procedure.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Intel Xscale® Assembly Language and C. The Intel Xscale® Programmer’s Model (1) (We will not be using the Thumb instruction set.) Memory Formats –We will.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Subroutines reasons for subroutines −repeat same code, or similar code with slightly different parameters −hide design decisions or design complexity −partition.
Intel Xscale® Assembly Language and C. The Intel Xscale® Programmer’s Model (1) (We will not be using the Thumb instruction set.) Memory Formats –We will.
Procedures Procedures are very important for writing reusable and maintainable code in assembly and high-level languages. How are they implemented? Application.
Lecture 3 Translation.
Chapter 14 Functions.
Computer Architecture & Operations I
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Computer structure: Procedure Calls
Chapter 4 Copying Data.
ECE 3430 – Intro to Microcomputer Systems
ENERGY 211 / CME 211 Lecture 25 November 17, 2008.
The University of Adelaide, School of Computer Science
The Stack.
Microprocessor and Assembly Language
Chapter 4 Addressing modes
Introduction to Compilers Tim Teitelbaum
Computer Architecture and Organization Miles Murdocca and Vincent Heuring Chapter 4 – The Instruction Set Architecture.
Writing Functions in Assembly
Multiplication and Division Revisited
April 2006 Saeid Nooshabadi
The HP OpenVMS Itanium® Calling Standard
Chapter 9 :: Subroutines and Control Abstraction
ARM Assembly Programming
Application Binary Interface (ABI)
MIPS Instructions.
The University of Adelaide, School of Computer Science
ECE 3430 – Intro to Microcomputer Systems
Procedures and Calling Conventions
Runtime Environments What is in the memory?.
Computer Architecture
Where is all the knowledge we lost with information? T. S. Eliot
Computer Organization and Assembly Language
Computer Operation 6/22/2019.
Topic 2b ISA Support for High-Level Languages
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

Writing Functions in Assembly Chapter 3 Writing Functions in Assembly

Approach Most of a program is written in a high-level language (HLL) such as C easier to implement easier to understand easier to maintain Only a few functions are written in assembly to improve performance, or because it’s difficult or impossible to do in a HLL.

Most of code, including function main Creating a Program C sourcecode ASM sourcecode Most of code, including function main A few small functions Compiler Assembler Object code Library header files Linker Execu-table Program Librarycode

Consequence Assembly functions must be compatible with: How the C compiler invokes the execution of a function How the C compiler passes parameters to functions How the C compiler expects to receive results from functions How the C compiler uses CPU registers

FUNCTION CALL AND RETURN Simple Call-Return in C Suspend the current sequence Record the return address Transfer control to the function Branch with Link Instruction (BL) ● f1() ; void f1(void) { return ; } Execute the function Use the recorded return address to go back and resume the suspended code. Branch Indirect Instruction (BX)

FUNCTION CALL AND RETURN ARM instructions used to call and return from functions Format Operation Branch with Link BL label Function Call: LR  return address, PC  address of label Branch Indirect BX LR Function Return: PC  LR LR (aka R14) and PC (aka R15) are two of the 16 registers inside the CPU. LR is the “Link Register” – used w/function calls to hold the return address PC is the “Program Counter” – holds the address of the next instruction.

FUNCTION CALL AND RETURN Simple Call-Return in Assembly The “Branch with Link” instruction (BL) saves the the address of the instruction immediately following it (the return address) in the Link Register (LR). ● BL f1 f1: ● BX LR Consider: There is only one Link Register. What if inside function f1 there is a call to another function? The “Branch Indirect” instruction (BX) copies the return address from LR into PC, thus transferring control back to where the function had been called.

FUNCTION CALL AND RETURN Nested Call-Return in Assembly Saves the contents of LR on the stack. Saves the return address in LR ● BL f1 f1: BL f2 BX LR f2: ● BX LR Modifies the link register (LR), writing over f1’s return address! PUSH {LR} POP {LR} Restores the contents of LR from the stack. Copies the saved return address from LR back into PC

FUNCTION CALL AND RETURN ARM instructions used in functions that call other functions SP is one of the 16 CPU registers (R13) – holds the address of the top of the stack. 1000 Pushed data 996 992 Top of stack 988 984 980 976 972 (register R13) SP 992 Unused Stack Space Instruction Format Operation Push registers onto stack PUSH register list SP  SP – 4 × #registers Copy registers to mem[SP] Pop registers from stack POP Copy mem[SP] to registers, SP  SP + 4 × #registers (memory) “register list” format: { reg, reg, reg-reg, …}

Optimizing Function Calls f1: PUSH {LR} ● BL f2 POP {LR} BX LR f1: PUSH {LR} ● BL f2 POP {PC}

Optimizing Function Calls ● BL f1 f1: BL f2 BX LR f2: ● BX LR PUSH {LR} POP {LR} ● BL f1 f1: ● B f2 f2: ● BX LR

ARM PROCEDURE CALL STANDARD Registers used as input parameters AAPCS Name Usage R0 A1 1st parameter R1 A2 2nd parameter R2 A3 3rd parameter R3 A4 4th parameter One register is used for each 8, 16 or 32-bit parameter. A sequential register pair is used for each 64-bit parameter.

ACCESSING PARAMETERS INSIDE THE FUNCTION How parameters are passed to a function by the compiler C Function Call Compiler Output int8_t x8 ; int32_t y32 ; int64_t z64 ; ● foo(x8, y32, z64) ; foo(5, -10, 20) ; LDRSB R0,x8 // R0 <-- x8 LDR R1,y32 // R1 <-- y32 LDRD R2,R3,z64 // R3.R2 <-- z64 BL foo ● MOV R0,5 // R0 <-- 5 MOV R1,-10 // R1 <-- -10 MOV R2,20 // R3.R2 <-- 20 MOV R3,0 Inside foo, you must use the register copies of the actual arguments.

ARM PROCEDURE CALL STANDARD Registers used to return function result AAPCS Name Usage R0 A1 8, 16 or 32-bit result, or the least-significant half of a 64-bit result R1 A2 The most-significant half of a 64-bit result

PREPARING THE RETURN VALUE Functions that return an 8, 16 or 32-bit result Functions must provide a full 32-bit representation of return value, even for 8 and 16-bit results.

Review Summary Call functions using Branch with Link (BL) Saves the return address in Link Register (LR) Copies the target address into Program Counter (PC) Return from a function using BX LR Writing functions that call other functions: Using BL to call another function changes LR Use PUSH {LR} / POP {LR} to preserve LR Pass parameters using registers R0-R3 64-bit parameters in consecutive register pair Return 8, 16 and 32 bit results using R0 64-bit result returned in R1.R0 register pair

Review Summary BX LR void f2(void) ; f2(int32_t, int32_t) ; { . } f1: . . PUSH {LR} int32_t f1(void) MOV R0,1 MOV R1,2 BL f2 f2(1, 2) ; f2() ; return 10 ; MOV R0,10 POP {PC} BX LR

PREPARING THE RETURN VALUE Promoting a return value from 8 to 16 or 32 bits C Function Call Compiler Output uint8_t save8, get8(void) ; uint16_t save16 ; uint32_t save32 ; uint64_t save64 ; ● save8 = get8() ; save16 = (uint16_t) get8() ; save32 = (uint32_t) get8() ; save64 = (uint64_t) get8() ; BL get8 STRB R0,save8 STRH R0,save16 STR R0,save32 MOV R1,0 STRD R0,R1,save64 8, 16 and 32-bit func-tions must always provide a 32-bit result. Promotion to 64-bits requires extending the result after the call

PREPARING THE RETURN VALUE Functions that return 64-bit result Functions that return a 64-bit result leave it in R0 and R1 before returning.

REGISTER USAGE CONVENTIONS ARM PROCEDURE CALL STANDARD AAPCS Name Usage Notes R0 A1 Argument / result /scratch register 1 R1 A2 Argument / result /scratch register 2 R2 A3 Argument / scratch register 3 R3 A4 Argument / scratch register 4 R4 V1 Variable register 1 Must preserve original contents R5 V2 Variable register 2 R6 V3 Variable register 3 R7 V4 Variable register 4 R8 V5 Variable register 5 R9 V6 Variable register 6 R10 V7 Variable register 7 R11 V8 Variable register 8 R12 IP Intra-Procedure-call scratch register R13 SP Stack Pointer Reserved, Do not use R14 LR Link Register R15 PC Program Counter

Using only R0-R3, R12 and calling another function FUNCTION CODING CONVENTIONS Functions that modify only registers R0 - R3, R12 Using only R0-R3, R12 and no function call Using only R0-R3, R12 and calling another function   f1: ● ● ● OK to modify ● R0-R3 and R12 BX LR f2: PUSH {LR} ● OK to modify R0–R3, R12 BL f3 POP {PC} Function f3 might modify R0 – R3, R12

Using R4 and R5 and calling another function FUNCTION CODING CONVENTIONS Functions that modify for example registers R4 and R5 Using R4 and R5 and no function call Using R4 and R5 and calling another function   f1: PUSH {R4,R5} ● ● OK to modify ● R0-R5 and R12 POP {R4,R5} BX LR f2: PUSH {R4,R5,LR} ● R0–R5 and R12 BL f3 POP {R4,R5,PC} Function f3 might modify R0-R3 and R12, but preserves R4-R11

FUNCTION CODING CONVENTIONS Two functions with parameters, one calling the other C Version Assembly Version   int32_t f1(int32_t x) { return f2(4) + x ; } f1: PUSH {R4,LR} // Preserve R4 MOV R4,R0 // Keep x safe in R4 MOV R0,4 // R0 <-- f2’s arg BL f2 // R0 <-- f2(4) ADD R0,R0,R4 // R0 <-- f2(4) + x POP {R4,PC} // Restore R4  On entry to function: parameter x some value R0 R4 After 1st MOV instruction: parameter x R0 R4 After 2nd MOV instruction: constant 4 parameter x R0 R4 After the BL instruction: f2 return value parameter x R0 R4 After the ADD instruction: f1 return value parameter x R0 R4 After the POP instruction: f1 return value some value R0 R4

Lab 1, Part 1: Sample Program RUN DEMO

void InitializeHardware(char *, char *) ; Must be first executable statement Initializes processor, CPU clock cycle counter, the display and user push button. Creates stack and heap and initializes static variables. Formats display area

uint32_t GetClockCycleCount(void) ; start = GetClockCycleCount() ; // do some calculation here stop = GetClockCycleCount() ; printf("Cycles=%d\n", stop-start) ; Returns the number of processor clock cycles since initialization. STM32F429I-DISCO Cycles=1788 GetClockCycles

void WaitForPushButton(void) ; Causes program to pause and wait for user to push the blue push button on the board.

void ClearDisplay(void) ; Sine Function STM32F429I-DISCO Erases the scrollable area of the display.

Lab 1 (Part 2): Decimal/Binary Conversions void Dec2Bin(float x, int bin[8]) { // To be implemented by student // Input Parameter: 0.0 < X < 1.0 // Output Parameter: bin[0] = Least significant bit // bin[7] = Most significant bit // Return value: None } float Bin2Dec(int bin[8]) // Input Parameter: bin[0] = Least significant bit // bin[7] = Most significant bit // Return value: 0.0 < float < 1.0

Bin2Dec (The "easier" one) Use a modified Polynomial Evaluation: Assume the 8 bits represent an integer. E.g., 100100112 = 14710 Convert the integer to fraction: Note: 0.10010011 = 10010011/28 E.g., 147/256 = 0.5742187510

Dec2Bin (A little more difficult) Binary Rounding: Result must be limited to 8 bits. If 9th bit = 1, add 1 to the 8-bit result. Easier if scaled first: 0.0 ≤ x < 1.0  0.0 ≤ 28x < 256.0 Extract integer and fractional parts of x: Add 1 to integer if fractional part ≥ 0.5 Convert integer to binary (as your output)