button.c The little button that wouldn t

Size: px
Start display at page:

Download "button.c The little button that wouldn t"

Transcription

1 Goals for today The little button that wouldn't :( the volatile keyword Pointer operations => ARM addressing modes Implementation of C function calls Management of runtime stack, register use

2 button.c The little button that wouldn t

3 button.c: The little button that wouldn t Want cool diagrams like this? Check out fritzing.org

4 button.c: The little button that wouldn t Want cool diagrams like this? Check out fritzing.org

5 button.c: The little button that wouldn t // This program waits until a button is pressed (GPIO 10) // and turns on GPIO 20, then waits until the button is //released and turns off GPI0 20 unsigned int * const FSEL1 = (unsigned int *)0x ; unsigned int * const FSEL2 = (unsigned int *)0x ; unsigned int * const SET0 = (unsigned int *)0x C; unsigned int * const CLR0 = (unsigned int *)0x ; unsigned int * const LEV0 = (unsigned int *)0x ; void main(void) { *FSEL1 = 0; // configure GPIO 10 as input *FSEL2 = 1; // configure GPIO 20 as output while (1) { // wait until GPIO 10 is low (button press) while ((*LEV0 & (1 << 10))!= 0) ; // set GPIO 20 high *SET0 = 1 << 20; // wait until GPIO 10 is high (button release) while ((*LEV0 & (1 << 10)) == 0) ; } } // clear GPIO 20 *CLR0 = 1 << 20;

6 button.c: The little button that wouldn t // This program waits until a button is pressed (GPIO 10) // and turns on GPIO 20, then waits until the button is //released and turns off GPI0 20 unsigned int * const FSEL1 = (unsigned int *)0x ; unsigned int * const FSEL2 = (unsigned int *)0x ; unsigned int * const SET0 = (unsigned int *)0x C; unsigned int * const CLR0 = (unsigned int *)0x ; unsigned int * const LEV0 = (unsigned int *)0x ; void main(void) { *FSEL1 = 0; // configure GPIO 10 as input *FSEL2 = 1; // configure GPIO 20 as output while (1) { // wait until GPIO 10 is low (button press) while ((*LEV0 & (1 << 10))!= 0) ; // set GPIO 20 high *SET0 = 1 << 20; Compiling with -O2: Disassembly of section.text.startup: <main>: 0: ldr r3, [pc, #28] ; 24 <main+0x24> 4: ldr r0, [r3, #52] ; 0x34 8: mov r1, #0 c: mov r2, #1 10: tst r0, #1024 ; 0x400 14: stmib r3, {r1, r2} 18: bne 20 <main+0x20> 1c: b 1c <main+0x1c> 20: b 20 <main+0x20> 24:.word 0x } } // wait until GPIO 10 is high (button release) while ((*LEV0 & (1 << 10)) == 0) ; // clear GPIO 20 *CLR0 = 1 << 20;

7 button.c: The little button that wouldn t // This program waits until a button is pressed (GPIO 10) // and turns on GPIO 20, then waits until the button is //released and turns off GPI0 20 unsigned int * const FSEL1 = (unsigned int *)0x ; unsigned int * const FSEL2 = (unsigned int *)0x ; unsigned int * const SET0 = (unsigned int *)0x C; unsigned int * const CLR0 = (unsigned int *)0x ; unsigned int * const LEV0 = (unsigned int *)0x ; void main(void) { *FSEL1 = 0; // configure GPIO 10 as input *FSEL2 = 1; // configure GPIO 20 as output while (1) { // wait until GPIO 10 is low (button press) while ((*LEV0 & (1 << 10))!= 0) ; // set GPIO 20 high *SET0 = 1 << 20; Compiling with -O2: Disassembly of section.text.startup: <main>: 0: ldr r3, [pc, #28] ; 24 <main+0x24> 4: ldr r0, [r3, #52] ; 0x34 8: mov r1, #0 c: mov r2, #1 10: tst r0, #1024 ; 0x400 14: stmib r3, {r1, r2} 18: bne 20 <main+0x20> 1c: b 1c <main+0x1c> 20: b 20 <main+0x20> 24:.word 0x ?? } } // wait until GPIO 10 is high (button release) while ((*LEV0 & (1 << 10)) == 0) ; // clear GPIO 20 *CLR0 = 1 << 20;

8 button.c: The little button that wouldn t // This program waits until a button is pressed (GPIO 10) // and turns on GPIO 20, then waits until the button is //released and turns off GPI0 20 unsigned int * const FSEL1 = (unsigned int *)0x ; unsigned int * const FSEL2 = (unsigned int *)0x ; unsigned int * const SET0 = (unsigned int *)0x C; unsigned int * const CLR0 = (unsigned int *)0x ; unsigned int * const LEV0 = (unsigned int *)0x ; void main(void) { *FSEL1 = 0; // configure GPIO 10 as input *FSEL2 = 1; // configure GPIO 20 as output } while (1) { } // wait until GPIO 10 is low (button press) while ((*LEV0 & (1 << 10))!= 0) ; // set GPIO 20 high *SET0 = 1 << 20; // wait until GPIO 10 is high (button release) while ((*LEV0 & (1 << 10)) == 0) ; // clear GPIO 20 *CLR0 = 1 << 20; Compiling with -O2: Disassembly of section.text.startup: <main>: 0: ldr r3, [pc, #28] ; 24 <main+0x24> 4: ldr r0, [r3, #52] ; 0x34 8: mov r1, #0 c: mov r2, #1 10: tst r0, #1024 ; 0x400 14: stmib r3, {r1, r2} 18: bne 20 <main+0x20> 1c: b 1c <main+0x1c> 20: b 20 <main+0x20> 24:.word 0x What happened to our testing loops??

9 Peripheral Registers These registers are mapped into the address space of the processor (memory-mapped IO). These registers may behave differently than memory. For example: Writing a 1 into a bit in a SET register causes 1 to be output; writing a 0 into a bit in SET register does not affect the output value. Writing a 1 to the CLR register, sets the output to 0; write a 0 to a clear register has no effect. Neither SET or CLR can be read. To read the current value use the LEV (level) register.

10 volatile For an ordinary variable, the compiler can use its knowledge of when it is read/written to optimize accesses as long as it keeps the same externally visible behavior. However, for a variable that can be read/written externally (by another process, by peripheral), these optimizations will not be valid. The volatile qualifier applied to a variable informs the compiler that it cannot remove, coalesce, cache, or reorder references. The generated assembly must faithfully execute each access to the variable as given in the C code.

11 button.c: The little button that could Because we have GPIO pins on the Raspberry Pi, we need to give hints to the C compiler to not optimize out pin reads they can change externally to the program! So, we use the volatile keyword in front of hardware addresses to do this: volatile unsigned int * const FSEL1 = (unsigned int *)0x ; volatile unsigned int * const FSEL2 = (unsigned int *)0x ; volatile unsigned int * const SET0 = (unsigned int *)0x C; volatile unsigned int * const CLR0 = (unsigned int *)0x ; volatile unsigned int * const LEV0 = (unsigned int *)0x ;

12 button.c: The little button that could There are other times to use volatile, too delays have a similar problem: #define DELAY int main() { for (int i=0; i < DELAY; i++); } return 0; $ objdump -d testloop.o testloop.o: file format elf32-littlearm Disassembly of section.text.startup: <main>: 0: e3a00000 mov r0, #0 4: e12fff1e bx lr

13 button.c: The little button that could There are other times to use volatile, too delays have a similar problem: #define DELAY int main() { for (int i=0; i < DELAY; i++); } return 0; $ objdump -d testloop.o testloop.o: file format elf32-littlearm Disassembly of section.text.startup: <main>: 0: e3a00000 mov r0, #0 4: e12fff1e bx lr No loop it has been optimized out!

14 button.c: The little button that could There are other times to use volatile, too delays have a similar problem: #define DELAY int main() { for (volatile int i=0; i < DELAY; i++); } return 0; Disassembly of section.text.startup: <main>: 0: e24dd008 sub sp, sp, #8 4: e3a03000 mov r3, #0 8: e58d3004 str r3, [sp, #4] c: e59d3004 ldr r3, [sp, #4] 10: e59f2028 ldr r2, [pc, #40] ; 40 <main+0x40> 14: e cmp r3, r2 18: ca bgt 34 <main+0x34> 1c: e59d3004 ldr r3, [sp, #4] 20: e add r3, r3, #1 24: e58d3004 str r3, [sp, #4] 28: e59d3004 ldr r3, [sp, #4] 2c: e cmp r3, r2 30: dafffff9 ble 1c <main+0x1c> 34: e3a00000 mov r0, #0 38: e28dd008 add sp, sp, #8 3c: e12fff1e bx lr 40: 1dcd64ff.word 0x1dcd64ff The loop remains when we use volatile.

15 What is bare metal? The default build process for C assumes a hosted environment. It provides standard libraries, all the stuff that happens before main. To build bare-metal, our makefile disables these defaults; we must supply our own versions when needed.

16 Makefile settings Compile freestanding CFLAGS =-ffreestanding Link without standard libs and start files LDFLAGS = -nostdlib Link with gcc to support division (violates LDLIBS = -lgcc Must supply own replacement for libs/start That s where the fun is!

17 Pointers: more gain than pain! "The fault, dear Brutus, is not in our stars But in ourselves, that we are underlings. Julius Caesar (I, ii, ) Refer to data by address or relative position is very useful! - Sharing instead of copying - Access to fields of a struct - Array elements accessed by index - Construct linked structures (lists, trees, graphs)

18

19 loop: ldr r0, =0x C str r1, [r0] mov r2, #0x3F0000 wait1: subs r2, #1 bne wait1 ldr r0, =0x str r1, [r0] mov r2, #0x3F0000 wait2: subs r2, #1 bne wait2 b loop // set pin // delay loop // clear pin // delay loop Excerpted from blink.s

20 ldr r0, =0x C str r1, [r0] b delay ldr r0, =0x str r1, [r0] b delay b loop delay: mov r2, #0x3F0000 wait: subs r2, #1 bne wait // but... where to go next?

21 loop: ldr r0, =0x C str r1, [r0] mov r14, pc b delay ldr r0, =0x str r1, [r0] mov r14, pc b delay b loop delay: mov r2, #0x3F0000 wait: subs r2, r2, #1 bne wait mov pc, r14 We ve just invented our own link register!

22 ldr r0, =0x C str r1, [r0] mov r0, #0x3F0000 mov r14, pc b delay ldr r0, =0x str r1, [r0] mov r0, #0x3F0000 >> 2 mov r14, pc b delay b loop delay: subs r0, #1 wait: bne wait mov pc, r14 We ve just invented our own parameter passing!

23 Anatomy of C function call int sum(int n) { int total = 0; for (int i = 1; i < n; i++) total += i; return total; } Call and return Pass arguments Local variables Return value Scratch/work space Complication: nested function calls, recursion

24 Application binary interface ABI specifies how code interoperates: Mechanism for call/return How parameters passed How return value communicated Use of registers (ownership/preservation) Stack management (up/down, alignment) arm-none-eabi is ARM embedded ABI ( none refers to no hosting OS)

25 Mechanics of call/return Caller puts up to 4 arguments in r0-r3 Call instruction is bl (branch and link) mov r0, #100 mov r1, #7 bl sum // will set lr=pc-4 Callee puts return value in r0 Return instruction is bx (branch exchange) add r0, r0, r1 bx lr // pc=lr btw: lr is mnemonic for r14

26 Caller and Callee caller - function doing the calling callee - function called main is caller of binky binky is callee of main + caller of winky void main(void) { binky(3); } void binky(int a) { winky(10, a); } int winky(int x, int y) { return x + y; }

27 Register Ownership r0-r3 are callee-owned registers Callee can change these registers Caller cedes to callee, cannot assume value will be preserved across call to callee r4-r13 are caller-owned registers Callee must preserve values in these registers Caller retains ownership, expects value to be same after call as it was before call

28 Discuss 1. If the callee needs scratch space for an intermediate value, which type of register should it choose? 2. What must a callee do when it wants to use a caller-owed register? 3. What is the advantage in having some registers callee-owned and others callerowned? Why not treat all same? 4. How can we implement nested calls when we only have a single shared lr register?

29 The stack to the rescue! Region in memory to store local variables, scratch space, save register values LIFO: push adds value on top of stack, pop removes lastmost value r13 (alias sp) points to topmost value stack grows down newer values at lower addresses push subtracts from sp pop adds to sp push/pop are aliases for a general instruction (load/store multiple with writeback)

30 // start.s mov sp, #0x bl main gpio 0x // main.c void main(void) { binky(3); } Not to scale sp sp sp main binky 0x int binky(int a) { int arr[100]; return winky(arr, 100); } pc pc pc code 0x8000 Diagram not to scale 0x0

31 Single stack frame int winky(int a, int b) { int c = 2*a;... return c; } sp sp sp caller s frame saved regs locals/ scratch

32 Stack operations // PUSH (store reg to stack) // * sp = r0 // decrement sp before store push {r0} // equivalent to: str r0, [sp, #-4]! sp sp saved r0 // POP (restore reg from stack) // r0 = *sp++ // increment sp after load pop {r0} // equivalent to: ldr r0, [sp], #4 Full Descending stack

33 int winky(int a, int b) { int c = binky(a); return b + c; } If winky calls binky Why do they collide on use of lr? Is there similar collision for r0? r1? What do we do about it? use stack as temp storage!

34 example.c

35 r0 r1 r2 r3 lr sp pc 0x x7ffffffc 0x7ffffff8 0x7ffffff4 0x7ffffff0 0x7fffffec 0x7fffffe8 0x7fffffe4 0x7fffffe0

36 r0 r1 r2 r3 lr sp pc 0x x7ffffffc 0x7ffffff8 0x7ffffff4 0x7ffffff0 0x7fffffec 0x7fffffe8 0x7fffffe4 0x7fffffe0. 0x7ffffe70 0x7ffffe6c 0x7ffffe68 0x7ffffe64 0x7ffffe60 0x7ffffe5c 0x7ffffe58 0x7ffffe54.

37 sp in constant motion Access values on stack using sp-relative addressing, but. sp is constantly changing! (push, pop, add sp, sub sp) sp sp sp caller s frame saved regs locals/ scratch

38 Add frame pointer (fp) Dedicate fp register to be used as fixed anchor Offsets relative to fp stay constant! sp fp sp sp caller s frame saved regs locals/ scratch

39 APCS full frame APCS = ARM Procedure Call Standard Conventions for use of frame pointer + frame layout that allows for reliable stack introspection gcc CFLAGS to enable: -mapcs-frame r12 used as fp Adds a prolog/epilog to each function that sets up/tears down the standard frame and manages fp

40 Trace APCS Prolog push fp, r13, lr, pc set fp to first word of stack frame Body fp stays anchored access data on stack fp-relative offsets won t vary even if sp changing Epilog pop fp, r13, lr can t pop pc (why not?), manually adjust stack sp sp fp sp sp sp caller s frame pc lr r13/ip fp locals/ scratch/ call other fns

41 FPs form linked chain pc lr ip fp main other = additional saved regs, locals, scratch fp other pc lr ip fp other pc lr ip fp binky winky sp other

42 // start.s // Need to initialize fp = NULL // to terminate end of chain mov sp, #0x mov fp, #0 // fp = NULL bl main

43 APCS Pros/Cons + Anchored fp, offsets are constant + Standardized frame layout enables introspection + Backtrace for debugging + Unwind stack on exception - Expensive, every function call affected prolog/epilog add ~5 instructions 4 registers push/pop => add 16 bytes per frame consumes one of our precious registers

Hail the all-powerful C pointer!

Hail the all-powerful C pointer! This week Assign1 due tomorrow Congrats on having proved your bare-metal mettle! Prelab for lab2 Read info on gcc/make and 7-segment display Bring your tools if you have them! "Gitting Started" Ashwin

More information

CS 107e Lecture 4: From ASM to C Part 1 Friday, October 5, 2018

CS 107e Lecture 4: From ASM to C Part 1 Friday, October 5, 2018 CS 107e Lecture 4: From ASM to C Part 1 Friday, October 5, 2018 Computer Systems from the Ground Up Fall 2018 Stanford University Computer Science Department Lecturer: Chris Gregg Logistics Assignment

More information

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15

Stack Frames. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 15 Stack Frames Geoffrey Brown Bryce Himebaugh Indiana University September 2, 2016 Geoffrey Brown, Bryce Himebaugh 2015 September 2, 2016 1 / 15 Outline Preserving Registers Saving and Restoring Registers

More information

ECE 471 Embedded Systems Lecture 8

ECE 471 Embedded Systems Lecture 8 ECE 471 Embedded Systems Lecture 8 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 21 September 2018 Announcements HW#2 was due HW#3 will be posted today. Work in groups? Note

More information

NET3001. Advanced Assembly

NET3001. Advanced Assembly NET3001 Advanced Assembly Arrays and Indexing supposed we have an array of 16 bytes at 0x0800.0100 write a program that determines if the array contains the byte '0x12' set r0=1 if the byte is found plan:

More information

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri CS356: Discussion #6 Assembly Procedures and Arrays Marco Paolieri (paolieri@usc.edu) Procedures Functions are a key abstraction in software They break down a problem into subproblems. Reusable functionality:

More information

CprE 288 Introduction to Embedded Systems ARM Assembly Programming: Translating C Control Statements and Function Calls

CprE 288 Introduction to Embedded Systems ARM Assembly Programming: Translating C Control Statements and Function Calls CprE 288 Introduction to Embedded Systems ARM Assembly Programming: Translating C Control Statements and Function Calls Instructors: Dr. Phillip Jones 1 Announcements Final Projects Projects: Mandatory

More information

EECS 373 Design of Microprocessor-Based Systems

EECS 373 Design of Microprocessor-Based Systems EECS 373 Design of Microprocessor-Based Systems Mark Brehob University of Michigan Lecture 3: Toolchain, ABI, Memory Mapped I/O Sept. 12 th, 2018 Slides developed in part by Prof. Dutta 1 Announcements

More information

Processor state. Information about currently executing program. %rax, %rdi, ... %r14 %r15. %rsp. %rip CF ZF SF OF. Temporary data

Processor state. Information about currently executing program. %rax, %rdi, ... %r14 %r15. %rsp. %rip CF ZF SF OF. Temporary data Processor state Information about currently executing program Temporary data %rax, %rdi, current parameters, local variables Location of runtime stack %rsp Location of current instruction %rip Status of

More information

ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines

ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines ARM Cortex-M4 Architecture and Instruction Set 4: The Stack and subroutines M J Brockway February 13, 2016 The Cortex-M4 Stack SP The subroutine stack is full, descending It grows downwards from higher

More information

Programming the ARM. Computer Design 2002, Lecture 4. Robert Mullins

Programming the ARM. Computer Design 2002, Lecture 4. Robert Mullins Programming the ARM Computer Design 2002, Lecture 4 Robert Mullins 2 Quick Recap The Control Flow Model Ordered list of instructions, fetch/execute, PC Instruction Set Architectures Types of internal storage

More information

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones CprE 288 Introduction to Embedded Systems Course Review for Exam 3 Instructors: Dr. Phillip Jones 1 Announcements Exam 3: See course website for day/time. Exam 3 location: Our regular classroom Allowed

More information

CS A331 Programming Language Concepts

CS A331 Programming Language Concepts CS A331 Programming Language Concepts Lecture 9 Subroutines March 17, 2014 Sam Siewert Purpose of a Subroutine Control Abstraction perform a well defined operation E.g. transform a color image into a graymap

More information

Systems Architecture The Stack and Subroutines

Systems Architecture The Stack and Subroutines Systems Architecture The Stack and Subroutines The Stack p. 1/9 The Subroutine Allow re-use of code Write (and debug) code once, use it many times A subroutine is called Subroutine will return on completion

More information

Computer Organization & Assembly Language Programming (CSE 2312)

Computer Organization & Assembly Language Programming (CSE 2312) Computer Organization & Assembly Language Programming (CSE 2312) Lecture 15: Running ARM Programs in QEMU and Debugging with gdb Taylor Johnson Announcements and Outline Homework 5 due Thursday Midterm

More information

ARM Assembly Programming

ARM Assembly Programming ARM Assembly Programming Computer Organization and Assembly Languages g Yung-Yu Chuang 2007/12/1 with slides by Peng-Sheng Chen GNU compiler and binutils HAM uses GNU compiler and binutils gcc: GNU C compiler

More information

Support for high-level languages

Support for high-level languages Outline: Support for high-level languages memory organization ARM data types conditional statements & loop structures the ARM Procedure Call Standard hands-on: writing & debugging C programs 2005 PEVE

More information

Computer Organization & Assembly Language Programming (CSE 2312)

Computer Organization & Assembly Language Programming (CSE 2312) Computer Organization & Assembly Language Programming (CSE 2312) Lecture 16: Processor Pipeline Introduction and Debugging with GDB Taylor Johnson Announcements and Outline Homework 5 due today Know how

More information

ARM Assembly Programming

ARM Assembly Programming ARM Assembly Programming Computer Organization and Assembly Languages g Yung-Yu Chuang with slides by Peng-Sheng Chen GNU compiler and binutils HAM uses GNU compiler and binutils gcc: GNU C compiler as:

More information

ARM Assembly Exercise (1B) Young Won Lim 7/16/16

ARM Assembly Exercise (1B) Young Won Lim 7/16/16 ARM Assembly Exercise (1B) Copyright (c) 2014-2016 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2

More information

Compiling Code, Procedures and Stacks

Compiling Code, Procedures and Stacks Compiling Code, Procedures and Stacks L03-1 RISC-V Recap Computational Instructions executed by ALU Register-Register: op dest, src1, src2 Register-Immediate: op dest, src1, const Control flow instructions

More information

Run-time Environment

Run-time Environment Run-time Environment Prof. James L. Frankel Harvard University Version of 3:08 PM 20-Apr-2018 Copyright 2018, 2016, 2015 James L. Frankel. All rights reserved. Storage Organization Automatic objects are

More information

From last time. What is the effect on R0 (in terms of an equivalent multiplication) of each of the following ARM instructions? ADD R0, R0, R0,LSL #3

From last time. What is the effect on R0 (in terms of an equivalent multiplication) of each of the following ARM instructions? ADD R0, R0, R0,LSL #3 OMP15111 Lecture 9 1/26 From last time What is the effect on R0 (in terms of an equivalent multiplication) of each of the following ARM instructions? ADD R0, R0, R0,LSL #3 RSB R0, R0, R0,LSL #2 Write down

More information

ECE251: Tuesday September 11

ECE251: Tuesday September 11 ECE251: Tuesday September 11 Finish Branch related instructions Stack Subroutines Note: Lab 3 is a 2 week lab, starting this week and covers the Stack and Subroutines. Labs: Lab #2 is due this week. Lab

More information

ARM PROGRAMMING. When use assembly

ARM PROGRAMMING. When use assembly ARM PROGRAMMING Bùi Quốc Bảo When use assembly Functions that cannot be implemented in C, such as special register accesses and exclusive accesses Timing-critical routines Tight memory requirements, causing

More information

Stacks and Subroutines

Stacks and Subroutines Chapters 8 Stacks and Subroutines Embedded Systems with ARM Cortext-M Updated: Tuesday, March 6, 2018 Basic Idea llarge programs are hard to handle lwe can break them to smaller programs lthey are called

More information

ECE251: Tuesday September 12

ECE251: Tuesday September 12 ECE251: Tuesday September 12 Finish Branch related instructions Stack Subroutines Note: Lab 3 is a 2 week lab, starting this week and covers the Stack and Subroutines. Labs: Lab #2 is due this week. Lab

More information

ARM Assembly Programming II

ARM Assembly Programming II ARM Assembly Programming II Computer Organization and Assembly Languages Yung-Yu Chuang 2007/11/26 with slides by Peng-Sheng Chen GNU compiler and binutils HAM uses GNU compiler and binutils gcc: GNU C

More information

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary Control Instructions Computer Organization Architectures for Embedded Computing Thursday, 26 September 2013 Many slides adapted from: Computer Organization and Design, Patterson & Hennessy 4th Edition,

More information

Control Instructions

Control Instructions Control Instructions Tuesday 22 September 15 Many slides adapted from: and Design, Patterson & Hennessy 5th Edition, 2014, MK and from Prof. Mary Jane Irwin, PSU Summary Previous Class Instruction Set

More information

ECE 471 Embedded Systems Lecture 5

ECE 471 Embedded Systems Lecture 5 ECE 471 Embedded Systems Lecture 5 Vince Weaver http://www.eece.maine.edu/ vweaver vincent.weaver@maine.edu 17 September 2013 HW#1 is due Thursday Announcements For next class, at least skim book Chapter

More information

Architecture. Digital Computer Design

Architecture. Digital Computer Design Architecture Digital Computer Design Architecture The architecture is the programmer s view of a computer. It is defined by the instruction set (language) and operand locations (registers and memory).

More information

Basics. Crash Dump Analysis 2015/2016. CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics.

Basics. Crash Dump Analysis 2015/2016. CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics. Basics http://d3s.mff.cuni.cz Crash Dump Analysis 2015/2016 CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Processors Basic functionality of processors Execute machine code Sequence of

More information

MIPS Functions and the Runtime Stack

MIPS Functions and the Runtime Stack MIPS Functions and the Runtime Stack COE 301 Computer Organization Prof. Muhamed Mudawar College of Computer Sciences and Engineering King Fahd University of Petroleum and Minerals Presentation Outline

More information

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

An Introduction to Assembly Programming with the ARM 32-bit Processor Family An Introduction to Assembly Programming with the ARM 32-bit Processor Family G. Agosta Politecnico di Milano December 3, 2011 Contents 1 Introduction 1 1.1 Prerequisites............................. 2

More information

EECS 373 Design of Microprocessor-Based Systems

EECS 373 Design of Microprocessor-Based Systems EECS 373 Design of Microprocessor-Based Systems Prabal Dutta University of Michigan Lecture 2: Architecture, Assembly, and ABI January 13, 2015 Slides developed in part by Mark Brehob 1 Announcements Website

More information

Optimizing C For Microcontrollers

Optimizing C For Microcontrollers Optimizing C For Microcontrollers Khem Raj, Comcast Embedded Linux Conference & IOT summit - Portland OR Agenda Introduction Knowing the Tools Data Types and sizes Variable and Function Types Loops Low

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-09 In Class Example Handout Part A: A Simple, MIPS-based Procedure: Swap Procedure Example: Let s write the MIPS code for the following statement (and function call): if (A[i] > A

More information

ECE 598 Advanced Operating Systems Lecture 4

ECE 598 Advanced Operating Systems Lecture 4 ECE 598 Advanced Operating Systems Lecture 4 Vince Weaver http://www.eece.maine.edu/~vweaver vincent.weaver@maine.edu 28 January 2016 Announcements HW#1 was due HW#2 was posted, will be tricky Let me know

More information

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University Calling Conventions See P&H 2.8 and 2.12 Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University Goals for Today Review: Calling Conventions call a routine (i.e. transfer control to

More information

CSC 2400: Computer Systems. Using the Stack for Function Calls

CSC 2400: Computer Systems. Using the Stack for Function Calls CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing

More information

Lecture 5: Procedure Calls

Lecture 5: Procedure Calls Lecture 5: Procedure Calls Today s topics: Procedure calls and register saving conventions 1 Example Convert to assembly: while (save[i] == k) i += 1; i and k are in $s3 and $s5 and base of array save[]

More information

Advanced Operating Systems Embedded from Scratch: System boot and hardware access. Federico Terraneo

Advanced Operating Systems Embedded from Scratch: System boot and hardware access. Federico Terraneo Embedded from Scratch: System boot and hardware access federico.terraneo@polimi.it Outline 2/28 Bare metal programming HW architecture overview Linker script Boot process High level programming languages

More information

Raspberry Pi / ARM Assembly. OrgArch / Fall 2018

Raspberry Pi / ARM Assembly. OrgArch / Fall 2018 Raspberry Pi / ARM Assembly OrgArch / Fall 2018 The Raspberry Pi uses a Broadcom System on a Chip (SoC), which is based on an ARM CPU. https://en.wikipedia.org/wiki/arm_architecture The 64-bit ARM processor

More information

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be tricky) when you translate high-level code into assembler code. MIPS Programming This is your crash course in assembler programming; you will teach yourself how to program in assembler for the MIPS processor. You will learn how to use the instruction set summary to

More information

18-600: Recitation #3

18-600: Recitation #3 18-600: Recitation #3 Bomb Lab & GDB Overview September 12th, 2017 1 Today X86-64 Overview Bomb Lab Introduction GDB Tutorial 2 3 x86-64: Register Conventions Arguments passed in registers: %rdi, %rsi,

More information

Implementing Procedure Calls

Implementing Procedure Calls 1 / 39 Implementing Procedure Calls February 18 22, 2013 2 / 39 Outline Intro to procedure calls Caller vs. callee Procedure call basics Calling conventions The stack Interacting with the stack Structure

More information

Exam 1. Date: February 23, 2016

Exam 1. Date: February 23, 2016 Exam 1 Date: February 23, 2016 UT EID: Printed Name: Last, First Your signature is your promise that you have not cheated and will not cheat on this exam, nor will you help others to cheat on this exam:

More information

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture COMP 303 Computer Architecture Lecture 3 Comp 303 Computer Architecture 1 Supporting procedures in computer hardware The execution of a procedure Place parameters in a place where the procedure can access

More information

E85 Lab 8: Assembly Language

E85 Lab 8: Assembly Language E85 Lab 8: Assembly Language E85 Spring 2016 Due: 4/6/16 Overview: This lab is focused on assembly programming. Assembly language serves as a bridge between the machine code we will need to understand

More information

ECE 471 Embedded Systems Lecture 6

ECE 471 Embedded Systems Lecture 6 ECE 471 Embedded Systems Lecture 6 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 15 September 2016 Announcements HW#3 will be posted today 1 What the OS gives you at start Registers

More information

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention

Memory Usage 0x7fffffff. stack. dynamic data. static data 0x Code Reserved 0x x A software convention Subroutines Why we use subroutines more modular program (small routines, outside data passed in) more readable, easier to debug code reuse i.e. smaller code space Memory Usage A software convention stack

More information

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention Runtime Environment The Procedure Abstraction and Separate Compilation Topics we will cover The procedure abstraction and linkage conventions Runtime storage convention Non-local data access (brief) These

More information

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017 143A: Principles of Operating Systems Lecture 5: Calling conventions Anton Burtsev January, 2017 Stack and procedure calls Stack Main purpose: Store the return address for the current procedure Caller

More information

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;}

Subroutines. int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} Subroutines Also called procedures or functions Example C code: int main() { int i, j; i = 5; j = celtokel(i); i = j; return 0;} // subroutine converts Celsius to kelvin int celtokel(int i) { return (i

More information

CprE 288 Translating C Control Statements and Function Calls, Loops, Interrupt Processing. Instructors: Dr. Phillip Jones Dr.

CprE 288 Translating C Control Statements and Function Calls, Loops, Interrupt Processing. Instructors: Dr. Phillip Jones Dr. CprE 288 Translating C Control Statements and Function Calls, Loops, Interrupt Processing Instructors: Dr. Phillip Jones Dr. Zhao Zhang 1 Announcements Final Projects Projects: Mandatory Demos Deadweek

More information

ARM Shift Operations. Shift Type 00 - logical left 01 - logical right 10 - arithmetic right 11 - rotate right. Shift Amount 0-31 bits

ARM Shift Operations. Shift Type 00 - logical left 01 - logical right 10 - arithmetic right 11 - rotate right. Shift Amount 0-31 bits ARM Shift Operations A novel feature of ARM is that all data-processing instructions can include an optional shift, whereas most other architectures have separate shift instructions. This is actually very

More information

Comparison InstruCtions

Comparison InstruCtions Status Flags Now it is time to discuss what status flags are available. These five status flags are kept in a special register called the Program Status Register (PSR). The PSR also contains other important

More information

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

CSC 2400: Computing Systems. X86 Assembly: Function Calls CSC 24: Computing Systems X86 Assembly: Function Calls" 1 Lecture Goals! Challenges of supporting functions" Providing information for the called function" Function arguments and local variables" Allowing

More information

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12

Calling Conventions. Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University. See P&H 2.8 and 2.12 Calling Conventions Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University See P&H 2.8 and 2.12 Goals for Today Calling Convention for Procedure Calls Enable code to be reused by allowing

More information

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015 Branch Addressing Branch instructions specify Opcode, two registers, target address Most branch targets are near branch Forward or backward op rs rt constant or address 6 bits 5 bits 5 bits 16 bits PC-relative

More information

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero Do-While Example In C++ do { z--; while (a == b); z = b; In assembly language loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero 25 Comparisons Set on less than (slt) compares its source registers

More information

Subroutines and the Stack

Subroutines and the Stack 3 31 Objectives: A subroutine is a reusable program module A main program can call or jump to the subroutine one or more times The stack is used in several ways when subroutines are called In this lab

More information

Hakim Weatherspoon CS 3410 Computer Science Cornell University

Hakim Weatherspoon CS 3410 Computer Science Cornell University Hakim Weatherspoon CS 3410 Computer Science Cornell University The slides are the product of many rounds of teaching CS 3410 by Professors Weatherspoon, Bala, Bracy, McKee, and Sirer. compute jump/branch

More information

Anne Bracy CS 3410 Computer Science Cornell University

Anne Bracy CS 3410 Computer Science Cornell University Anne Bracy CS 3410 Computer Science Cornell University The slides are the product of many rounds of teaching CS 3410 by Professors Weatherspoon, Bala, Bracy, McKee, and Sirer. See P&H 2.8 and 2.12, and

More information

Systems I. Machine-Level Programming V: Procedures

Systems I. Machine-Level Programming V: Procedures Systems I Machine-Level Programming V: Procedures Topics abstraction and implementation IA32 stack discipline Procedural Memory Usage void swap(int *xp, int *yp) int t0 = *xp; int t1 = *yp; *xp = t1; *yp

More information

Topic 7: Activation Records

Topic 7: Activation Records Topic 7: Activation Records Compiler Design Prof. Hanjun Kim CoreLab (Compiler Research Lab) POSTECH 1 Storage Organization Stack Free Memory Heap Static Code 2 ELF file format example Executable Object

More information

System calls and assembler

System calls and assembler System calls and assembler Michal Sojka sojkam1@fel.cvut.cz ČVUT, FEL License: CC-BY-SA 4.0 System calls (repetition from lectures) A way for normal applications to invoke operating system (OS) kernel's

More information

Assembly Language Programming

Assembly Language Programming Assembly Language Programming ECE 362 https://engineering.purdue.edu/ee362/ Rick Reading and writing arrays Consider this C code again: int array1[100]; int array2[100]; for(n=0; n

More information

EEM870 Embedded System and Experiment Lecture 4: ARM Instruction Sets

EEM870 Embedded System and Experiment Lecture 4: ARM Instruction Sets EEM870 Embedded System and Experiment Lecture 4 ARM Instruction Sets Wen-Yen Lin, Ph.D. Department of Electrical Engineering Chang Gung University Email wylin@mail.cgu.edu.tw March 2014 Introduction Embedded

More information

ECE 571 Advanced Microprocessor-Based Design Lecture 4

ECE 571 Advanced Microprocessor-Based Design Lecture 4 ECE 571 Advanced Microprocessor-Based Design Lecture 4 Vince Weaver http://www.eece.maine.edu/ vweaver vincent.weaver@maine.edu 24 January 2013 Low-Level ARM Linux Assembly 1 System call number in r7 Arguments

More information

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number The plot thickens Some MIPS instructions you can write cannot be translated to a 32-bit number some reasons why 1) constants are too big 2) relative addresses are too big 3) absolute addresses are outside

More information

Chapter 2A Instructions: Language of the Computer

Chapter 2A Instructions: Language of the Computer Chapter 2A Instructions: Language of the Computer Copyright 2009 Elsevier, Inc. All rights reserved. Instruction Set The repertoire of instructions of a computer Different computers have different instruction

More information

ECE251: Tuesday September 18

ECE251: Tuesday September 18 ECE251: Tuesday September 18 Subroutine Parameter Passing (Important) Allocating Memory in Subroutines (Important) Recursive Subroutines (Good to know) Debugging Hints Programming Hints Preview of I/O

More information

CENG3420 Lecture 03 Review

CENG3420 Lecture 03 Review CENG3420 Lecture 03 Review Bei Yu byu@cse.cuhk.edu.hk 2017 Spring 1 / 38 CISC vs. RISC Complex Instruction Set Computer (CISC) Lots of instructions of variable size, very memory optimal, typically less

More information

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number

The plot thickens. Some MIPS instructions you can write cannot be translated to a 32-bit number The plot thickens Some MIPS instructions you can write cannot be translated to a 32-bit number some reasons why 1) constants are too big 2) relative addresses are too big 3) absolute addresses are outside

More information

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types 1 Lecture 08 Introduction to the MIPS ISA + Procedure Calls in MIPS Longer instructions = more bits to address registers MIPS Datapath 6 bit opcodes... 2 MIPS Instructions are 32 bits More ways to address

More information

Chapter 9 :: Subroutines and Control Abstraction

Chapter 9 :: Subroutines and Control Abstraction Chapter 9 :: Subroutines and Control Abstraction Programming Language Pragmatics, Fourth Edition Michael L. Scott Copyright 2016 Elsevier 1 Chapter09_Subroutines_and_Control_Abstraction_4e - Tue November

More information

EECS 373 Design of Microprocessor-Based Systems

EECS 373 Design of Microprocessor-Based Systems EECS 373 Design of Microprocessor-Based Systems Ronald Dreslinski University of Michigan Lecture 2: Architecture, Assembly, and ABI Jan. 9, 2018 Slides developed in part by Prof. Du7a 1 Admin Stuff Website

More information

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1 CSIS1120A 10. Instruction Set & Addressing Mode CSIS1120A 10. Instruction Set & Addressing Mode 1 Elements of a Machine Instruction Operation Code specifies the operation to be performed, e.g. ADD, SUB

More information

Implementing Subroutines. Outline [1]

Implementing Subroutines. Outline [1] Implementing Subroutines In Text: Chapter 9 Outline [1] General semantics of calls and returns Implementing simple subroutines Call Stack Implementing subroutines with stackdynamic local variables Nested

More information

CSE 410. Operating Systems

CSE 410. Operating Systems CSE 410 Operating Systems Handout: syllabus 1 Today s Lecture Course organization Computing environment Overview of course topics 2 Course Organization Course website http://www.cse.msu.edu/~cse410/ Syllabus

More information

Lecture 5: Procedure Calls

Lecture 5: Procedure Calls Lecture 5: Procedure Calls Today s topics: Memory layout, numbers, control instructions Procedure calls 1 Memory Organization The space allocated on stack by a procedure is termed the activation record

More information

CA Compiler Construction

CA Compiler Construction CA4003 - Compiler Construction David Sinclair When procedure A calls procedure B, we name procedure A the caller and procedure B the callee. A Runtime Environment, also called an Activation Record, is

More information

Instructions: Assembly Language

Instructions: Assembly Language Chapter 2 Instructions: Assembly Language Reading: The corresponding chapter in the 2nd edition is Chapter 3, in the 3rd edition it is Chapter 2 and Appendix A and in the 4th edition it is Chapter 2 and

More information

ARM Assembler Workbook. CS160 Computer Organization Version 1.1 October 27 th, 2002 Revised Fall 2005

ARM Assembler Workbook. CS160 Computer Organization Version 1.1 October 27 th, 2002 Revised Fall 2005 ARM Assembler Workbook CS160 Computer Organization Version 1.1 October 27 th, 2002 Revised Fall 2005 ARM University Program Version 1.0 January 14th, 1997 Introduction Aim This workbook provides the student

More information

Anne Bracy CS 3410 Computer Science Cornell University

Anne Bracy CS 3410 Computer Science Cornell University Anne Bracy CS 3410 Computer Science Cornell University The slides are the product of many rounds of teaching CS 3410 by Professors Weatherspoon, Bala, Bracy, McKee, and Sirer. compute jump/branch targets

More information

System Software Assignment 1 Runtime Support for Procedures

System Software Assignment 1 Runtime Support for Procedures System Software Assignment 1 Runtime Support for Procedures Exercise 1: Nested procedures Some programming languages like Oberon and Pascal support nested procedures. 1. Find a run-time structure for such

More information

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design Professor Sherief Reda http://scale.engin.brown.edu School of Engineering Brown University Spring 2016 1 ISA is the HW/SW

More information

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality Problem Chapter 10 Memory Model for Program Execution Original slides by Chris Wilcox, Colorado State University How do we allocate memory during the execution of a program written in C?! Programs need

More information

Exam 1. Date: February 23, 2018

Exam 1. Date: February 23, 2018 Exam 1 Date: February 23, 2018 UT EID: Printed Name: Last, First Your signature is your promise that you have not cheated and will not cheat on this exam, nor will you help others to cheat on this exam:

More information

ARM. Assembly Language and Machine Code. Goal: Blink an LED

ARM. Assembly Language and Machine Code. Goal: Blink an LED ARM Assembly Language and Machine Code Goal: Blink an LED Review Turning on an LED Connect LED to GPIO 20 3.3V 1k GND 1 -> 3.3V 0 -> 0.0V (GND) Two Steps 1. Configure GPIO20 to be an OUTPUT 2. "Set" GPIO20

More information

CSE Lecture In Class Example Handout

CSE Lecture In Class Example Handout CSE 30321 Lecture 07-08 In Class Example Handout Part A: J-Type Example: If you look in your book at the syntax for j (an unconditional jump instruction), you see something like: e.g. j addr would seemingly

More information

Lecture 6: Assembly Programs

Lecture 6: Assembly Programs Lecture 6: Assembly Programs Today s topics: Procedures Examples Large constants The compilation process A full example 1 Procedures Local variables, AR, $fp, $sp Scratchpad and saves/restores, $fp Arguments

More information

HW/SW Codesign w/ FPGAsGeneral Purpose Embedded Cores ECE 495/595

HW/SW Codesign w/ FPGAsGeneral Purpose Embedded Cores ECE 495/595 G.P. Embedded Cores (A Practical Intro. to HW/SW Codesign, P. Schaumont) The most successful programmable component on silicon is the microprocessor Fueled by a well-balanced mix of efficient implementations,

More information

A QUICK INTRO TO PRACTICAL OPTIMIZATION TECHNIQUES

A QUICK INTRO TO PRACTICAL OPTIMIZATION TECHNIQUES A QUICK INTRO TO PRACTICAL OPTIMIZATION TECHNIQUES 0. NO SILVER BULLETS HERE. 1. Set Compiler Options Appropriately: Select processor architecture: Enables compiler to make full use of instructions which

More information

Computer Systems Architecture I. CSE 560M Lecture 3 Prof. Patrick Crowley

Computer Systems Architecture I. CSE 560M Lecture 3 Prof. Patrick Crowley Computer Systems Architecture I CSE 560M Lecture 3 Prof. Patrick Crowley Plan for Today Announcements Readings are extremely important! No class meeting next Monday Questions Commentaries A few remaining

More information

ECE 471 Embedded Systems Lecture 6

ECE 471 Embedded Systems Lecture 6 ECE 471 Embedded Systems Lecture 6 Vince Weaver http://www.eece.maine.edu/ vweaver vincent.weaver@maine.edu 18 September 2014 Announcements I have a cold and my voice is gone! HW#3 will be posted tomorrow

More information

Hi Hsiao-Lung Chan, Ph.D. Dept Electrical Engineering Chang Gung University, Taiwan

Hi Hsiao-Lung Chan, Ph.D. Dept Electrical Engineering Chang Gung University, Taiwan ARM Programmers Model Hi Hsiao-Lung Chan, Ph.D. Dept Electrical Engineering Chang Gung University, Taiwan chanhl@maili.cgu.edu.twcgu Current program status register (CPSR) Prog Model 2 Data processing

More information

ECE232: Hardware Organization and Design

ECE232: Hardware Organization and Design ECE232: Hardware Organization and Design Lecture 6: Procedures Adapted from Computer Organization and Design, Patterson & Hennessy, UCB Overview Procedures have different names in different languages Java:

More information