Computer Systems 245 Lecture Notes PDF
Document Details
Uploaded by Deleted User
Dr WD Duckitt
Tags
Summary
These lecture notes cover the background behind computer systems, focusing on number representation (binary, hexadecimal, integers, floating-point) and development tools. They also discuss data types and characters/strings. Information on C and assembly code, as well as CPU architecture, is also included.
Full Transcript
1 Review – Background & Number Representation Hersiening – Agtergrond & Getalle V...
1 Review – Background & Number Representation Hersiening – Agtergrond & Getalle Voorstelling Computer Systems 245 / Rekenaarstelsels 245 Lecture 2 & 3 Dr WD Duckitt Engineering · EyobuNjineli · Ingenieurswese 1 Review – Background & Number Representation Hersiening – Agtergrond & Getalle Voorstelling Computer Systems 245 / Rekenaarstelsels 245 Lecture 2 & 3 Dr WD Duckitt Engineering · EyobuNjineli · Ingenieurswese 2 Lecture Overview The software we will use in this module Executing instructions on a CPU Number representation / Getalle voorstelling Number systems (binary, hexadecimal) Integer & Signed integer (2’s complement) Floating point Boolean type Data types Characters and strings Engineering · EyobuNjineli · Ingenieurswese 4 Code::Blocks C IDE Engineering · EyobuNjineli · Ingenieurswese 5 STM32CubeIDE STM32CubeIDE is an advanced C/C++ development platform with peripheral configuration, code generation, code compilation, and debug features for STM32 microcontrollers and microprocessors. It is based on the ECLIPSE framework and GCC toolchain for the development, and GDB for the debugging. Engineering · EyobuNjineli · Ingenieurswese 6 Development board Development board for the STM32F407VG microcontroller (STM32F4DISCOVERY Discovery Kit) Board and microcontroller manufactured by ST Microelectronics In the microcontroller: ARM Cortex-M4 with floating point unit 1 MB flash memory (program memory) 192 KB SRAM Can be ordered from: Other stuff on the board: USB OTG FS Digikey ST MEMS 3-axis accelerometer Mouser ST-MEMS audio sensor omni-directional digital microphone Other Audio DAC with integrated class D speaker driver User and reset push-buttons Eight LEDs: LD1 (red/green) for USB communication LD2 (red) for 3.3 V power on Four user LEDs, LD3 (orange), LD4 (green), LD5 (red) and LD6 (blue) Two USB OTG LEDs, LD7 (green) VBUS and LD8 (red) over-current Board connectors: USB with Micro-AB Stereo headphone output jack 2.54 mm pitch extension header for all LQFP100 I/Os for quick connection to prototyping board and easy probing Flexible power-supply options: ST-LINK, USB V , or external sources External application power supply: 3 V and 5 V Engineering · EyobuNjineli · Ingenieurswese 7 C vs Assembly Higher level programs (C, Java etc) will eventually translate into many simple machine instructions C-code Equivalent assembly (generated backwards from What does the machine code look like? compiled machine instructions) Engineering · EyobuNjineli · Ingenieurswese 8 Architecture / Argitektuur The architecture is the programmer's view of a computer. It is defined by the instruction set (language) and operand locations (registers and memory). Many different architectures exist, such as ARM, x86, MIPS, SPARC... We will specifically look at the ARM instruction set, although the concepts of assembly programming apply generally between the different instruction sets. ARM is a family of CPUs based on the RISC (reduced instruction set computer) architecture. ARM (prior to ARMv8) is called a 32-bit architecture because it operates on 32-bit data. Engineering · EyobuNjineli · Ingenieurswese 9 Instructions and operands / Instruksies en operande The first step in understanding any computer architecture is to learn its language. The words in a computer's language are called instructions. The computer's vocabulary is called the instruction set. All programs running on a computer use the same basic instruction set, which are instructions such as add, subtract, and branch. Computer instructions indicate both the operation to perform and the operands to use. The operands may come from memory, from registers, or from the instruction itself. ADD R0, R1, #5 Operation Operands Engineering · EyobuNjineli · Ingenieurswese 10 Machine language and assembly language Computer hardware only understand 1's and 0's, so instructions are encoded as binary numbers in a format called machine language. The ARM architecture represent each instruction as a 32-bit word. Microprocessors are digital systems that read and execute machine language instructions. These instructions are directly implemented in logic circuits. Machine language is hard to read for humans, so we prefer to represent the instructions in a symbolic format called assembly language. For example: MOV R1, R2 //move data from one place to another This same instruction translates into machine language as: (11100001101000000001000000000010)2 = (E1A01002)16 Engineering · EyobuNjineli · Ingenieurswese 11 Machine code / Masjien kode Engineering · EyobuNjineli · Ingenieurswese 12 Machine language and Integrated Circuits C program Machine instruction Assembly code Full Adder circuit Transistor AND gate Engineering · EyobuNjineli · Ingenieurswese 13 Instruction loading We have to give the CPU the “sequence-of-CPU- machine-instructions” to make it work. This is called the binary program. Or compiled program. (sometimes the words “firmware” or “image” are also used). Think of the program instructions as a sequence on a tape. Think of the CPU as a sequential processing machine with a single-instruction view. Program The “thing” inside the CPU that holds the Counter position of the current “viewed” instruction is the Program Counter (PC), which is stored in a register. Pipelined instructions High-performance CPUs will have a processing pipeline – start pre-processing following instructions. Engineering · EyobuNjineli · Ingenieurswese 14 Instruction loading - Memory The CPU will execute (run) a binary program after the program has been placed into memory Remember CPU does not have its own memory. Byte Address Data Microcontrollers (=CPU + other stuff in the same 0 0xAA 1 0xBB integrated device) might have integrated memory, but it 2 0xCC is still outside of the CPU. 3 0xDD 4 0xEE Lots of types (RAM, ROM, Flash etc.). More on this later. 5 0xFF 6 0x11 For now: memory = a matrix of storage elements that 7 0x22 allows data to be written and read at specific addresses, … … and each address refers to a byte (8 bits) of data. Even though data is byte addressed, CPUs can sometimes Word Address 32-bit data read/write more than 8-bits at a time – often referred to 0 0xDDCCBBAA as a word. 4 0x2211FFEE … … Little-Endian Engineering · EyobuNjineli · Ingenieurswese 15 Memory / Geheue Program memory is normally only read not written to. Data memory, memory that the program alters as a result of instructions being executed, is used for variables. Program and data memory can be physically different memory chips (Harvard Machine) or the same (Von Neumann Machine). https://vivadifferences.com/5-major-difference-between-von-neumann-and-harvard-architecture/ Engineering · EyobuNjineli · Ingenieurswese 16 Fetching instructions Instructions are fetched from memory at the current program counter (Program Counter) Engineering · EyobuNjineli · Ingenieurswese 17 Decoding instructions Logic circuits in the CPU will Decode the instruction (check if the instruction bit pattern matches) Engineering · EyobuNjineli · Ingenieurswese 18 Executing instructions The relevant logic gates will then Execute the instruction. Depending on the instruction, this might have various effects: Some instructions will alter the memory contents (write to memory at specific addresses) Some instructions may change the Program Counter (will cause the CPU to fetch instructions from somewhere else, i.e. a function) (Normally the PC is simply incremented so that instructions are executed in the order they are stored in memory) Engineering · EyobuNjineli · Ingenieurswese 19 Simplified pipelined processor Note that the ARM Cortex-M4 only has the Fetch, Decode and Execute states From CS214 Write Fetch Decode Execute Memory Back Engineering · EyobuNjineli · Ingenieurswese 20 Processor Cycles per instruction Even though ARM is a RISC architecture, not all instructions execute in a single processor cycle (since no Memory and Writeback pipeline stages). P=The number of cycles required for a pipeline refill. This ranges from 1 to 3 depending on the alignment and width of the target instruction, and whether the processor manages to speculate the address early. Instructions that alter the PC results in performance hit because the processing pipeline must be “flushed” Cortex-M4 instruction set summary: https://developer.arm.com/documentation/ddi0439/b/CHDDIGAC Engineering · EyobuNjineli · Ingenieurswese 21 ARM vs C – Simple program instruction Program instructions for the microcontroller Masjien instruksies is ook net ‘n patroon van bisse (machine instructions) is also just a pattern of met spesifieke interpretasie bits, with specific interpretation https://www.sciencedirect.com/topics/engineering/memory-layout Engineering · EyobuNjineli · Ingenieurswese 22 ARM vs C – Simple program instruction The Compiler translates C or C++ Die saamsteller verander C or C++ code into assembler code kode na saamsteltaal https://www.sciencedirect.com/topics/engineering/memory-layout Engineering · EyobuNjineli · Ingenieurswese 23 ARM vs. C - Simple program instructions The C-code can be translated to assembler code as follows: int main(void) main: { 080004e1: push {r7, lr} int i=0; Creates space 080004e3: sub sp, #8 i=i+1; 080004e5: add r7, sp, #0 … 66 080004e6: int i=0; movs r3, #0 on stack for i 080004e8: str r3, [r7, #4] 67 i=i+1; 080004ea: ldr r3, [r7, #4] 080004ec: adds r3, #1 080004ee: str r3, [r7, #4] Addresses of Assembly code from C code instructions LDR = load. Load current value of i from memory (R7 + 4) ldr r3, [r7, #4] adds r3, #1 Add 1 to the current value of i str r3, [r7, #4] STR = Store. Store new value for i back to memory Engineering · EyobuNjineli · Ingenieurswese 24 ARM vs C – Simple program instruction The Assembler translates assembly Die saamsteller verander programs into machine code saamsteltaal programme na masjien kode https://www.sciencedirect.com/topics/engineering/memory-layout Engineering · EyobuNjineli · Ingenieurswese 25 ARM vs C – Simple program instruction Object files contain machine code, Objek lêers bevat masjien kode, debug information and symbols, simbole en fout-opsporing-, relocation and linker information herposisionerings en koppel-inligting. https://www.sciencedirect.com/topics/engineering/memory-layout Engineering · EyobuNjineli · Ingenieurswese 26 ARM vs. C – Simple program instruction The Linker combines object files Die ‘Linker’ kombineer objek with library files into an lêers en biblioteek leers om executable file uitvoerbare program kode te lewer https://www.sciencedirect.com/topics/engineering/memory-layout Engineering · EyobuNjineli · Ingenieurswese 27 ARM Assembly program – Populate array Simple loop that stores incrementing numbers to memory High-level Code Arm-Assembly code //R0 array base address of data1, R1 = i uint8_t data1; mov R0, #0x5000 // R0 = base address int i; mov R1, #0 // i = 0 LOOP: cmp R1, #10 // i < 10? bge END // if i >= 10, exit loop for (i = 0; i < 10; i = i+1) strb R1, [R0, R1] // data1[i] = i data1[i] = (uint8_t)i; add R1, R1, #1 // i = i + 1 B LOOP // repeat loop END: Memory Contents: Addr 0 1 2 3 0x5000: 0x00 0x01 0x02 0x03 0x5004: 0x04 0x05 0x06 0x07 0x5008: 0x08 0x09 0x00 0x00 0x500C: 0x00 0x00 0x00 0x00 Engineering · EyobuNjineli · Ingenieurswese 28 ARM Assembly program – Populate array High-level Code Arm-Assembly code //R0 array base address of data1, R1 = i uint8_t data1; mov R0, #0x5000 // R0 = base address int i; mov R1, #0 // i = 0 LOOP: cmp R1, #10 // i < 10?... bge END // if i >= 10, exit loop for (i = 0; i < 10; i = i+1) strb R1, [R0, R1] // data1[i] = i data1[i] = (uint8_t)i; add R1, R1, #1 // i = i + 1 B LOOP // repeat loop END: Some notes: We used R1 for the variable i. i is not stored in memory. Should a variable always be stored in memory? If the C compiler is used to compile the code below, how will it differ from code written in assembler? Answer: Compiler optimization settings We used R0 to temporarily contain an address – the address that we are writing to. Engineering · EyobuNjineli · Ingenieurswese 29 Number systems / Getallestelsels Refers to the numerical representation of a value. W In digital logic we only work with to states – HIGH and LOW. 1 It is clumsy to work with numbers between 0 – 9 (decimal) to perform mathematics with a two state (binary) system. We use different number systems to make life easier when working with and designing digital systems. Rekenaars gebruik binêre voorstelling vir alles. Heksadesimaal word gebruik vir leesbaarheid en gerief Engineering · EyobuNjineli · Ingenieurswese 30 Decimal numbers / Desimale getalle Just as you (probably) have ten fingers, there are ten decimal digits from 0 to 9. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Each digit (right to left) represents a multiple of a power of 10. Decimal numbers are also called base-10 (or radix-10) numbers. 1000’s 100’s 10’s 1’s Indicating the base 9 7 4 2 of the number An N-digit decimal number represents one of 10N possibilities. This is called the range of the number. Three-digit decimal number represents one of 1000 possibilities in the range of 0 to 999. Engineering · EyobuNjineli · Ingenieurswese 31 Binary numbers / Binêre getalle Binary digits or “bits” represent one of two values, 0 or 1. Each bit (right to left) represent a multiple of a power of 2. Binary numbers are base-2. 16’s 8’s 4’s 2’s 1’s 1 0 1 1 0 An N-bit binary number represents one of 2N possibilities. A four-bit binary number represents one of 16 possibilities. For positive numbers, this is the range of 0 to 15. Binary numbers are convenient for logic circuits in computers, since they can represent 2 states, i.e. low and high voltages. Engineering · EyobuNjineli · Ingenieurswese 32 Hexadecimal numbers / Heksadesimale getalle Decimal numbers are useful, because they are used by people. Binary numbers are useful, because they are used by computers. Hexadecimal (base-16) numbers are useful, because they provide a shorthand notation for binary numbers. Each hexadecimal digit represents a group of four bits (a nibble). Hexadecimal numbers use the digits 0 to 9 along with the letters A to F, to represent a total of 16 possible values (from 0 to 15). Each digit (right to left) represents a multiple of a power of 16. 256’s 16’s 1’s 2 E D Engineering · EyobuNjineli · Ingenieurswese 33 Conversion table / Omskakelings tabel Remember: In C-code, prepend hexadecimal literals with ‘0x’ 0xFF = FF16 = 1111 11112 0xA5 = A516 = 1010 01012 In C-kode word heksadesimale konstantes voorafgegaan deur ‘0x’ Engineering · EyobuNjineli · Ingenieurswese 34 Binary representation / Binêre voorstelling In memory (SRAM, registers, etc) all we have is a pattern of bits. 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 0 0 0 0 0 0 1 1 1 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 E 8 9 0 0 0 Is it A number? (signed, unsigned, floating point) Character? Machine instruction? The computer does not know what it is! To the computer this is simply a bit pattern. How the data in memory (SRAM, register, etc.) is interpreted depends on the current state of the CPU, and which instruction is about to be executed – it depends on you! Engineering · EyobuNjineli · Ingenieurswese 35 Integers / Heelgetalle The maximum integer number that can be represented depends on the number of bits being used Unsigned numbers range from 0 to 2n-1 Signed numbers range from -2n-1 to +2n-1-1 Length No. of bits Unsigned range Signed range Byte 8 0 to 255 -128 to 127 Half-word 16 0 to 65535 -32768 to 32767 Word 32 0 to 4,294,967,295 –2,147,483,648 to 2,147,483,647 Double word 64 0 to 264-1 -263 to 263-1 Heelgetalle sonder teken bis kan strek van 0 tot 2n -1. Heelgetalle met teken-bis kan strek van - 2n-1 to +2n-1-1 (waar daar n bisse gebruik word vir die voorstelling) Engineering · EyobuNjineli · Ingenieurswese 36 Integers / Heelgetalle Signed integer numbers (numbers which can be positive or negative) are represented using 2s complement / Heelgetalle wat positief of negatief kan wees word deur 2s komplement voorgestel 7 6 5 4 3 2 1 0 Sign Number The processor will perform all arithmetic as if the number is unsigned. The difference is in the interpretation of the bit pattern. 2s complement are identical to unsigned binary numbers, but the most significant bit has a weight of -2N-1 instead of 2N-1. The most positive number is: 01…1112 = 2N-1-1 The most negative number is: 10…0002 = -2N-1 Engineering · EyobuNjineli · Ingenieurswese 37 C number types / C getal tipes Engineering · EyobuNjineli · Ingenieurswese 38 C number types / C getal tipes How to declare C variables for portable code: Platform provides a stdint.h header file, for instance: In your source file: #include uint32_t variable = 5; // guaranteed to be a 32-bit int Engineering · EyobuNjineli · Ingenieurswese 39 Integers / Heelgetalle Example – Satellite spin rate / Voorbeeld – satelliet spin tempo Satellite angular rate vector (how fast it is spinning about each axis) telemetry uses 16-bit signed integer with x1000 scaling (fixed point decimal representation) Angular rate can vary from -32.768 to 32.767 deg/s Initial angular rates were quite a lot higher than expected. Values “jumping” between +32 and - 32. What is the actual sign of the Y component? Engineering · EyobuNjineli · Ingenieurswese 40 Floating point numbers / Wisselpunt getalle Increase the dynamic range with which a number can be stored – use scientific notation / Verhoog die dinamiese bereik waarbinne ‘n getal gestoor kan word – gebruik wetenskaplike notasie Value = (-1)s × (1.M) × 2E−b S = Sign bit M= Mantissa (fraction) Single precision float E = Exponent b = Bias/excess Double precision float Single precision provides typically 6–9 digits of numerical precision, while double precision gives 15–17. Convenient online converter: http://babbage.cs.qc.cuny.edu/IEEE-754.old/Decimal.html Engineering · EyobuNjineli · Ingenieurswese 41 Characters / Karakters Numbers (bit patterns) can also be interpreted as characters (each character has a unique code) / Getalle kan ook geinterpreteer word as karakter kodes ASCII used most commonly / ASCII word algemeen gebruik ASCII code for ‘0’ is 48, ‘1’ is 49 … ASCII code for ‘A’ is 65, ‘B’ is 66 … char string1[] = "abc"; unsigned char string2[] = {97, 98, 99, 0}; Memory contents of string1 and string2 will be the same Engineering · EyobuNjineli · Ingenieurswese 42 ASCII Table Engineering · EyobuNjineli · Ingenieurswese 43 Special Characters / Spesiale karakters ASCII special character codes Engineering · EyobuNjineli · Ingenieurswese 44 C-string / C-stringe Strings in C are character arrays with “null termination” – a byte value of 0. char greeting = “Hello!”; Engineering · EyobuNjineli · Ingenieurswese 45 Boolean type / Boolse tipe C89 does not have built-in Boolean types (*) In stdbool.h: Is equivalent to (*) *sort of. Depends how the compiler implements the _Bool type of C99 standard of C language Engineering · EyobuNjineli · Ingenieurswese 46 sizeof(…) What does sizeof() return in C? Allocated storage size for variable sizeof(unsigned char) 1 sizeof(uint32_t) 4 sizeof(_Bool) implementation specific sizeof(int) implementation specific For arrays and pointers: char data; char* ddata = data; sizeof(data) 40 sizeof(ddata) num of bytes to store a pointer (address, e.g. 32 bits) NB! sizeof() is not an actual function. It is an operator. There is no code that will calculate how many elements there are in your array. It simply asks the compiler to replace the “sizeof(…)” text with its implementation for that variable Engineering · EyobuNjineli · Ingenieurswese 1 Introduction to Development Board and IDE Inleiding tot Ontwikkelsbord en IDE Computer Systems 245 / Rekenaarstelsels 245 Lecture 4 Dr WD Duckitt Engineering · EyobuNjineli · Ingenieurswese 2 COPYRIGHT Copyright © 2023 Stellenbosch University All rights reserved DISCLAIMER This content is provided without warranty or representation of any kind. The use of the content is entirely at your own risk and Stellenbosch University (SU) will have no liability directly or indirectly as a result of this content. The content must not be assumed to provide complete coverage of the particular study material. Content may be removed or changed without notice. The video is of a recording with very limited post-recording editing. The video is intended for use only by SU students enrolled in the particular module. Engineering · EyobuNjineli · Ingenieurswese 3 Lecture Overview Development board STM32CubeIDE Engineering · EyobuNjineli · Ingenieurswese 4 Development board / Ontwikkelingsbord Development board for the STM32F407VG microcontroller (STM32F4DISCOVERY Discovery Kit) Board and microcontroller manufactured by ST Microelectronics In the microcontroller: ARM Cortex-M4 with floating point unit 1 MB flash memory (program memory) 192 KB SRAM Other stuff on the board: USB OTG FS ST MEMS 3-axis accelerometer ST-MEMS audio sensor omni-directional digital microphone Audio DAC with integrated class D speaker driver User and reset push-buttons Eight LEDs: LD1 (red/green) for USB communication LD2 (red) for 3.3 V power on Four user LEDs, LD3 (orange), LD4 (green), LD5 (red) and LD6 (blue) Two USB OTG LEDs, LD7 (green) VBUS and LD8 (red) over-current Board connectors: USB with Micro-AB Stereo headphone output jack 2.54 mm pitch extension header for all LQFP100 I/Os for quick connection to prototyping board and easy probing Flexible power-supply options: ST-LINK, USB V , or external sources External application power supply: 3 V and 5 V Engineering · EyobuNjineli · Ingenieurswese 5 Development board / Ontwikkelingsbord B1 and B2 : buttons LD3..LD8 : LEDs MP45DT02 MEMS microphone CS43L22: Audio DAC LIS302DL: 3-axis accelerometer Engineering · EyobuNjineli · Ingenieurswese 6 Power and programming / Krag en programmering Board gets power from USB (from the PC that it is connected to) Programming also happens through USB Binary program has to be placed in flash memory – processor will start executing instructions from address 0. ST-Link: In-circuit debugger and programmer – included on the development board (interfaces using USB) Do not remove the jumpers!! (default selection for programming) Engineering · EyobuNjineli · Ingenieurswese 7 Development Environment / Ontwikkelings Omgewing IDE : Integrated Development Environment A source code editor, build automation and debugger STM32CubeIDE is an advanced C/C++ development platform with peripheral configuration, code generation, code compilation, and debug features for STM32 microcontrollers and microprocessors. It is based on the ECLIPSE™ framework and GCC toolchain for the development, and GDB for the debugging. Engineering · EyobuNjineli · Ingenieurswese 8 Development Environment / Ontwikkelings Omgewing What does it mean to ‘Build’ project : 1. Compile all (.c) source files to object files (compiler) 2. Assemble all (.s) assembly files to object files (assembler) 3. Link all object files (.o) to a binary executable file (.elf) (linker) The file with the ‘elf’ extension (elf = Extensible Linking Format) contains the program code that will be programmed into flash memory. The compiler, assembler and linker is called the toolchain. arm-none-eabi-gcc is the toolchain we use. This toolchain targets the ARM architecture, and does not target an operating system (i.e. targets a “bare metal” system). Cross-compile: is a compiler capable of creating executable code for a platform other than the one on which the compiler is running – typical of embedded systems. Engineering · EyobuNjineli · Ingenieurswese 9 Development Environment / Ontwikkelings Omgewing What does it mean to ‘Clean’ project : ‘Build’ is usually incremental. IDE is smart and only recompiles files that have changed, Remove all the.o files and.elf file. (Next time you press ‘Build’, everything will be recompiled from scratch What does it mean to ‘Debug’: Pressing this button will first cause program code to be loaded into the microcontroller flash memory. Program will start to execute (processor will start fetching decoding and executing instructions) Through a debugging interface, allow the IDE to ‘pause’ the processor at specific points in the program (breakpoint) Step through program code, line-for-line Inspect (or modify) variable values, register and memory contents while program is halted. Engineering · EyobuNjineli · Ingenieurswese 10 Project properties/ Projekverstellings Right-click on project , select ‘Properties’ Engineering · EyobuNjineli · Ingenieurswese 11 Project source files / Projek bron lêers Lots of source files… BSP = Board Support Package (driver code for peripherals on the development board) HAL = Hardware Abstraction Layer (driver code for peripherals on the microcontroller) Middelwares: FreeRTOS (operating system), JPG library, etc. Engineering · EyobuNjineli · Ingenieurswese 12 Creating project files / Skep van projek lêers Easily set up a project file with graphical user interface. Access again by opening the.ioc file: Engineering · EyobuNjineli · Ingenieurswese 13 Resources on SunLearn / Hulpmiddels op SunLearn Documentation for the development board and microcontroller (on SunLearn, under ‘Resources/Hulpmiddels’) Datasheet - hardware Reference manual – memory layout and registers Programming manual – all the supported instructions for STM32F4 Also Summary of ARM instruction set ARMv7 Architecture Reference Manual STM32CubeIDE Installation links Installation and setup guides Engineering · EyobuNjineli · Ingenieurswese 14 HAL Reference manual Refer to the manual on SunLearn for writing to and reading from GPIOs and other peripherals. Engineering · EyobuNjineli · Ingenieurswese 15 HAL Reference manual Example: HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET); Engineering · EyobuNjineli · Ingenieurswese 16 HAL Reference manual When you start typing the name and press Ctrl+Space you can see “Template Proposals” which give you all the possible values. You must still understand what you are doing, since this is only a list of constants, and not the correct value for the function argument. Engineering · EyobuNjineli · Ingenieurswese 17 Programming tips Programmerings wenke Lecture 4b Engineering · EyobuNjineli · Ingenieurswese 18 Hints/ Wenke 1. Reset the IDE ‘perspective’ – to restore all the windows to their default locations 2. Always build first, inspect ‘Console’ window, then ‘Debug’ (if there are no errors) Engineering · EyobuNjineli · Ingenieurswese 19 Hints/ Wenke 3. Try to ‘Clean’ build if you get unexpected behaviour (Clean = remove all the existing object files and build meta-files). Clean followed by Build Now, look at the console window to determine if there are problems Engineering · EyobuNjineli · Ingenieurswese 20 Common problems / Algemene probleme (after clicking Debug or Run ) 1. Errors exist in the active configuration of project… Cause There is an error in your code, causing the build process to fail. Look at the ‘console’ window for the reason. Solution Fix the code error. Note that if you press ‘Proceed’, the IDE will continue to program and run your previous binary file (assuming the build process was successful at some point) Engineering · EyobuNjineli · Ingenieurswese 21 Common problems / Algemene probleme (after clicking Debug or Run ) 2. ‘Launching ----’ has encountered a problem. Error initializing ST-Link device. Cause The IDE tried to program the binary file onto the development board, but could not. Possible causes The development board is not connected The cable or the development board is faulty The device driver for the STM board has not been installed Solution Unplug and reconnect the development board. Open the Windows ‘Device Manager’. If the board is plugged in, you should see an entry as in the image. If it is not there, there is a problem with the cable, development board or device driver. Engineering · EyobuNjineli · Ingenieurswese 22 Common problems /Algemene probleme (after clicking Debug or Run ) 3. ‘Launching ----’ has encountered a problem. Program file does not exist Cause There is an error in your code, causing the build process to fail. Look at the ‘console’ window for the reason. Solution Fix the code error. You can also try to clean the project (Project->Clean) and try again. If this does not work. See the next slide. Engineering · EyobuNjineli · Ingenieurswese 23 Common problems / Algemene probleme (after clicking Debug or Run ) 3. ‘Launching ----’ has encountered a problem. Program file does not exist Cause The Build settings settings are not correct. Solution 1. Open Debug Configurations… 2. If C/C++ Application is blank, then click on Search Project… 3. Select the binary 4. Click OK 5. Try building again Engineering · EyobuNjineli · Ingenieurswese 24 Common problems / Algemene probleme (after clicking Debug or Run ) 7. “Launching… Debug” has encountered a problem. Could not determine GDB version… Solution Clean the project (Project->Clean) Close and open the emulator and try again. Engineering · EyobuNjineli · Ingenieurswese 25 Common problems / Algemene probleme java.lang.NullPointerException This appears to be an issue in the IDE and the root cause has not been identified. Potential solutions: Maybe first try creating a new project and start again (maybe a PC restart could also help). But then, the only consistent way to solve this issue is by uninstalling the IDE and reinstalling it. Note that when you uninstall, there are a few additional steps required. 1.First uninstall using the Control Panel 2.Delete all the STM related folders under C:\Users\\ This inclues the.stm folders 3.Open the registry editor by pressing the Win+R key and typing "regedit" pressing OK 1. Type the following in the address bar at the top: HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\STMicroelectronics 2. Right click on the STMicroelectronics folder and click "Delete" 4.Now try to reinstall. Engineering · EyobuNjineli · Ingenieurswese 26 Common problems / Algemene probleme Strange IDE behaviour, project not building with strange errors. For example, “Internal Error” Possible Solution: Check your folder system for where the default workspace and STM folders are. It should be in C:\Users\\ If contains any spaces or special characters the STM32CubeIDE will not work properly. The easiest solution is to create a new user account on Windows, this time making sure there are no spaces or special characters in your username, and install the STM32CubeIDE on the new account. It is also advised that you properly uninstall the IDE installation on your original user account. Engineering · EyobuNjineli · Ingenieurswese 27 Mixing Assembly and C / Vermenging van Saamsteltaal en C kode There are two ways to add ARM assembly code to your project: 1. As a separate assembly (.s) source file 2. Inline assembly using asm() Engineering · EyobuNjineli · Ingenieurswese 28 Mixing Assembly and C / Vermenging van Saamsteltaal en C kode Separate asm (.s) file example Engineering · EyobuNjineli · Ingenieurswese 29 Mixing Assembly and C / Vermenging van Saamsteltaal en C kode Inline assembly example using asm() or NB! Space or Tab (\t) is required for non-labels (normal instructions) Engineering · EyobuNjineli · Ingenieurswese 30 Global variables and functions across source files/ Globale veranderlikes en funksies oor verskillende lêers Often with global variables, you might want to access the variables in separate C source files. In main.c In other.c >> 'count' undeclared (first use in this function) Engineering · EyobuNjineli · Ingenieurswese 31 Global variables and functions across source files/ Globale veranderlikes en funksies oor verskillende lêers Often with global variables, you might want to access the variables in separate C source files. Solution: tell the compiler the variable is declared elsewhere using the ‘extern’ keyword In main.c In other.c Engineering · EyobuNjineli · Ingenieurswese 32 Global variables and functions across source files/ Globale veranderlikes en funksies oor verskillende lêers Often with global variables, you might want to access the variables in separate C source files. Solution: making use of header files – (improved modularity) In main.c In other.c In other.h Engineering · EyobuNjineli · Ingenieurswese 33 Global variables and functions across source files/ Globale veranderlikes en funksies oor verskillende lêers Expanding on the previous idea, assume you have a software module, called “other” which has its own global variables and functions In main.c In other.c In other.h Engineering · EyobuNjineli · Ingenieurswese 34 Global variables and functions across source files/ Globale veranderlikes en funksies oor verskillende lêers Remember Never #include a.c file (only.h files) Always use ‘extern’ for variables listed in the.h file the implication is that variables are never defined in the header file, they are only made visible, or exported, from the.h file Engineering · EyobuNjineli · Ingenieurswese 1 Memory and low-level use Geheue en lae-vlak gebruik Computer Systems 245 / Rekenaarstelsels 245 Lecture 5 Dr WD Duckitt Engineering · EyobuNjineli · Ingenieurswese 2 COPYRIGHT Copyright © 2023 Stellenbosch University All rights reserved DISCLAIMER This content is provided without warranty or representation of any kind. The use of the content is entirely at your own risk and Stellenbosch University (SU) will have no liability directly or indirectly as a result of this content. The content must not be assumed to provide complete coverage of the particular study material. Content may be removed or changed without notice. The video is of a recording with very limited post-recording editing. The video is intended for use only by SU students enrolled in the particular module. Engineering · EyobuNjineli · Ingenieurswese 3 Lecture Overview Types of memory (background) Program and data memory Global vs. local C variables Stack Heap Dynamic memory allocation Engineering · EyobuNjineli · Ingenieurswese 5 Memory types / Geheue Tipes There exists two broad categories of memory. RAM = Random Access Memory Data can be read/written (a byte at a time) at almost equal speeds. Usually intended for program data (variables). Volatile, meaning that it loses its data when the power is turned off. ROM = Read Only Memory From a program execution point-of-view, can only be read. Can be re-programmed but requires higher supply voltage or special procedure. Usually used for program instructions. Non-volatile, meaning that it retains its data indefinitely, even without a power source. Engineering · EyobuNjineli · Ingenieurswese 6 Bit cells / Bis selle Memory is built as an array of bit cells, each of which stores 1 bit of data. For each combination of address bits, the memory asserts a single wordline that activates the bit cell in that row. When the wordline is HIGH, the stored bit transfers to or from the bitline. Otherwise, the bitline is disconnected from the bit cell. To read a bit cell, the bitline is initially left floating (Z). Then the wordline is turned ON, allowing the stored value to drive the bitline to 0 or 1. To write a bit cell, the bitline Is strongly driven to the desired value. Then the wordline is turned ON, connecting the bitline to the stored bit. The strongly driven bitline overpowers the contents of the bit cell, writing the desired value into the stored bit. Engineering · EyobuNjineli · Ingenieurswese 7 Bit cells organization example Engineering · EyobuNjineli · Ingenieurswese 8 Historic memory / Historiese geheue A 32 x 32 core memory plane storing Hand woven magnetic core memory 1024 bits (or 128 bytes) of data ⇒ Cores maintain state even after power switched off making them non- volatile. Engineering · EyobuNjineli · Ingenieurswese 9 DRAM and SRAM Dynamic RAM (DRAM) Stores a bit as the presence or absence of charge on a capacitor. Reading destroys the bit value on the capacitor, so the data must be restored (rewritten) after each read. Must be refreshed (read and rewritten) every few milliseconds, because the charge on the capacitor gradually leaks away. Can be made with higher density – used in PC for “bulk” system memory Static RAM (SRAM) Is static because the store bits do not need to be refreshed. The data bit is stored on cross coupled inverters (a bistable element) – which is a type of flip-flop. Much faster than ROM. Uses relatively many parts (transistors) per memory cell – lower density than DRAM. SRAM used for cache memory. In SRAM and DRAM, memory contents are volatile – they lose their state when power is turned off Engineering · EyobuNjineli · Ingenieurswese 10 DRAM and SRAM Engineering · EyobuNjineli · Ingenieurswese 11 Memory comparison / Geheue vergelyking Engineering · EyobuNjineli · Ingenieurswese 12 ROM and PROM ROM stores a bit as the presence or absence of a transistor. The contents of the ROM bit cell are specified during manufacturing by the presence or absence of a transistor in each bit cell. A programmable ROM (PROM) places a transistor in every bit cell but provides a way to connect or disconnect the transistor to ground. Here is an example of a fuse-programmable ROM which is only a one-time programmable ROM, since the fuse cannot be repaired. Engineering · EyobuNjineli · Ingenieurswese 13 EPROM Reprogrammable ROMs provide a reversible mechanism for connecting or disconnecting the transistor to GND. Erasable PROM (EPROM), which replaces the nMOS transistor and fuse with a floating gate transistor, with the gate not physically attached to any wires. If a suitably high voltage (higher than operating voltage) is applied the electrons tunnel through an insulator onto the floating gate, turning the transistor on. It is only erasable by exposing EPROM to intense ultraviolet (UV) light through quartz window shining on silicon Engineering · EyobuNjineli · Ingenieurswese 14 Historic memory / Historiese geheue First EPROM chip, an Intel 1702 from 1971. 256 bytes storage Erased by exposure to strong ultraviolet light. Engineering · EyobuNjineli · Ingenieurswese 15 EEPROM and Flash Electrically erasable PROM (EEPROM), as well as Flash memory, contains circuitry on the chip for erasing as well as programming it, without using UV light or removing it from the circuit. Higher programming voltage still required but generated inside chip using charge pumps. There are some differences between EEPROM and Flash. EEPROM Can be erased a byte or word at a time Erase and write takes relatively long Flash memory Erased and programmed in blocks (or entire chip at once) High density (SD cards & USB drives). Erase changes all bits in the device to ‘1’ Two types: NAND and NOR Engineering · EyobuNjineli · Ingenieurswese 16 Flash memory NAND Flash NOR flash Density High Low Cost Low per bit Higher per bit Read speed Medium High Write speed High Low Erase speed High Low Interface / access type Complex I/O interface – (serial transmission of Same as SRAM – Enough address pins to map entire chip – address/control/data signals) access to any random byte Application Frequent write operations - SD cards / USB drives Infrequent write operations, fast access – Program memory (boot code) Engineering · EyobuNjineli · Ingenieurswese 17 Now… That’s very interesting, but… The following is what you need to know as a programmer Engineering · EyobuNjineli · Ingenieurswese 18 Memory / Geheue A memory cell stores a single bit of information – depends on physical implementation: SRAM → output of bi-stable element (flip flop) DRAM → charge of a capacitor Flash memory → Floating-gate transistor Single bit of information is interpreted as a logical 0 or 1. 8 bits of information grouped together as a byte. Computer memory is organized in bytes, addressed in bytes, size is measured in bytes The system bus allows CPU to access memory directly (fetch instructions, load and store data). Some memory types cannot be accessed directly by CPU – e.g. hard disks, SD cards, etc. Engineering · EyobuNjineli · Ingenieurswese 19 Memory / Geheue Memory layout of a C program SRAM Flash memory Engineering · EyobuNjineli · Ingenieurswese 20 Program vs. data memory / Program en data geheue Address Physical space memory ‘n’ ‘a’ ‘m’ ‘e’ SRAM 110 97 109 101 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 11101110011000010110110101100101 6 E 6 1 6 D 6 5 0x2000 0000 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Flash 10001001001111001010001011110000 8 9 3 C A 2 F 0 0x0000 0000 Engineering · EyobuNjineli · Ingenieurswese 21 Program vs. data memory / Program en data geheue Address Physical Instruction pointer has address of next space memory instruction CPU fetches memory contents at that address (through system bus) and places it in the instruction register Instruction is decoded and executed SRAM 0x2000 0000 Activate logic for Instruction register LDR instruction (for example) 0xE591 0000 (LDR r0, [r1]) Flash Program Counter (r15) 0x0000 0000 0x0000 8010 Engineering · EyobuNjineli · Ingenieurswese 22 Program vs. data memory / Program en data geheue Address Physical Assume the memory contents that was loaded into space memory instruction register is LDR r0, [r1] Upon execution of this instruction: Causes CPU to load memory contents using address in r1, and place it into r0 SRAM r1 0x2000 0010 0x2000 0000 r0 0x6E61 6D65 Flash 0x0000 0000 Engineering · EyobuNjineli · Ingenieurswese 23 Program vs. data memory / Program en data geheue What about… Constant values? float energy = mass * 299792458 * 299792458; (where does the 299792458 go in memory?) Short-lived variables (i.e. loop variables)? for (int i = 0; i < 100; i++) { array[i] = 0;} (Will ‘i’ be stored in memory?) Variables with initializers? int studentnum = 12345678; studentnum = studentnum + 1; What else is stored in memory? Vector table… (more about this later) Engineering · EyobuNjineli · Ingenieurswese 24 Program vs. data memory / Program en data geheue How does program code end up at address 0 – 0x1FFF FFFF, and data (variables) end up at address 0x2000 0000 and onwards? ⇒ The linker! (takes all the object files and combines it into an executable) ⇒ The debugger/programmer copies the binary file to non-volatile memory (flash memory in our case) Engineering · EyobuNjineli · Ingenieurswese 25 Memory and Linking / Geheue en skakeling Linker command file: tells the linker where to put _sidata = LOADADDR(.data); everything {.data : FLASH (RX) : origin = 0x00000000, length = 0x00040000 { RAM (RWX) : origin = 0x20000000, length = 0x00008000 … } } >RAM AT> FLASH SECTIONS {. = ALIGN(4);.bss :.isr_vector : { { … … } >RAM } >FLASH.text : { … } >FLASH.rodata : { … } >FLASH Engineering · EyobuNjineli · Ingenieurswese 26 Global vs. Local variables / Globale en lokale veranderlikes Global variables Declared outside of a function Visible (can be used) in any function Located in the DATA or BSS segments Local variables Declared inside a function Can only be used inside that function Located on the STACK Global variables Local variable Engineering · EyobuNjineli · Ingenieurswese 27 Variables general tips / Veranderlikes algemene wenke Initialize variables before using them >>warning: ‘maxVal' is used uninitialized in this function [-Wuninitialized] Good practice is to declare all local variables in the beginning of the function and all global variables towards beginning of source file. Do not use the same name for a global and local variable (even though this is allowed). Use descriptive variables names. Use camelCase for variable and function names: E.g. newString, getNewString() Engineering · EyobuNjineli · Ingenieurswese 28 Variables general tips / Veranderlikes algemene wenke Keep global variables to a minimum helps to improve modularity The findMax function can now be re-used with different arguments Engineering · EyobuNjineli · Ingenieurswese 29 Local variables and the stack / Lokale veranderlikes en die stapel Local variables are created on the STACK. They persist only as long as the function is executing. The stack grows every time a function starts and shrinks after the function exits. Memory gets re-used. Engineering · EyobuNjineli · Ingenieurswese 30 Local variables and the stack / Lokale veranderlikes en die stapel For recursive functions, the LR and arguments must repeatedly be stored to the stack. ⇒ stack keeps on increasing ⇒ stack overflow Engineering · EyobuNjineli · Ingenieurswese 31 Dynamic memory allocation/ Dinamiese geheue allokasie Use malloc() and free() functions to dynamically allocate memory Must #include Dynamic memory is allocated from the HEAP Engineering · EyobuNjineli · Ingenieurswese 1 Functions and the Stack Funksies en die Stapel Computer Systems 245 / Rekenaarstelsels 245 Lecture 6 Dr WD Duckitt Engineering · EyobuNjineli · Ingenieurswese 2 COPYRIGHT Copyright © 2023 Stellenbosch University All rights reserved DISCLAIMER This content is provided without warranty or representation of any kind. The use of the content is entirely at your own risk and Stellenbosch University (SU) will have no liability directly or indirectly as a result of this content. The content must not be assumed to provide complete coverage of the particular study material. Content may be removed or changed without notice. The video is of a recording with very limited post-recording editing. The video is intended for use only by SU students enrolled in the particular module. Engineering · EyobuNjineli · Ingenieurswese 3 Lecture Overview Function calls Branching The stack Loading and storing multiple registers to memory at once Engineering · EyobuNjineli · Ingenieurswese 4 Function calls / Funksie roepe High-level languages support functions (also called procedures or subroutines) to reuse common code and to make a program more modular and readable. Functions have inputs, called arguments/parameters, and an output, called the return value. Functions should calculate the return value and cause no other unintended side effects to other parts of the program. arguments What is the difference between an argument and a parameter in C? A parameter is the variable in the function definition. Arguments are the data you pass into the function’s parameters. return value Engineering · EyobuNjineli · Ingenieurswese 5 Effect of function calls on program flow/ Effek van funksie roepe op programvloei A function call breaks the default program flow by branching to a different location. For non-branching instructions (ADD, ORR, LDR, etc.) the program counter (PC) simply increments by 4 after each instruction. There is a sequential processing of instructions. (Recall that instructions are 4 bytes long and ARM is a byte addressed architecture.) ‘Branching’ means the sequential program execution is broken, and program flow will resume from somewhere else (the instruction to which the branch instruction is directed). Specifically, the Program Counter is changed, to contain the address of the next instruction to process Branching instructions in assembly (aka a function call in C) take a label (a text name) that it branches to, which is simply a name associated with a certain PC address. E.g. B TARGET NOTE: labels cannot be reserved words, such as instruction mnemonics. Engineering · EyobuNjineli · Ingenieurswese 6 Branching and the CPU pipline / Vertakkings en die verwerker pyplyn On a side note: What happens when the processor is told to break execution with a branch instruction? (The CPU doesn’t know beforehand if the branch conditions will true – i.e. will it branch or not) ⇒ Branching has an impact on performance, especially if there are many steps in the processing pipeline. Engineering · EyobuNjineli · Ingenieurswese 7 Function calls and returns ARM uses the branch and link instruction (BL) to call a function. To return from a function the link register is moved to the PC ⇒ MOV PC, LR Recall that the program counter (PC) is the memory location of the current instruction being executed. In this code the main function calls the simple function. The simple function is called with no input arguments and generates no return value; it just returns to the caller. Instruction addresses Engineering · EyobuNjineli · Ingenieurswese 8 Function calls and returns (2) BL (branch and link) and MOV PC, LR are the two essential instructions needed for a function call and return. BL performs two tasks: it stores the return address of the next instruction (the instruction after BL) in the link register (LR), and it branches to the target instruction. In the previous slide, the main function calls the simple function by executing the branch and link instruction (BL). BL branches to the SIMPLE label and stores 0x00008024 in LR. The simple function returns immediately by executing the instruction MOV PC, LR, copying the return address from the LR back to the PC. The main function then continues executing at this address (0x00008024). Engineering · EyobuNjineli · Ingenieurswese 9 Input arguments and return values According to ARM convention, the calling function, main, places the function arguments from left to right into the input registers, R0–R3. The called function, diffofsums, stores the return value in the return register, R0. When a function with more than four arguments is called, the additional input arguments are placed on the stack. High-level Code Arm-Assembly code // R4 = y int main() MAIN: { mov R0, #5 // argument 0 = 5 int y; mov R1, #4 // argument 1 = 4 mov R2, #3 // argument 2 = 3 y = diffofsums (5, 4, 3, 2); mov R3, #2 // argument 3 = 2... bl DIFFOFSUMS// call function } mov R4, R0// y = returned value diffofsums (int f, int g, int h, int i) b DONE//R4 = result { DIFFOFSUMS: int result; add R0, R0, R1// R0 = f + g result = (f + g) − (h + i); add R2, R2, R3// R2 = h + i return result; sub R0, R0, R2// result = (f + g) - (h + i) } mov PC, LR// return to caller DONE: Engineering · EyobuNjineli · Ingenieurswese 10 Types of branching / Tipes vertakking Assembly mnemonic Branch type Meaning B Branch Simple branch to address, optionally making use of condition flags (i.e. BGT) BX Branch and Branch to an address that is stored in a register Exchange BL Branch with Link Store the current Program Counter in the Link Register, prior to branching to the new address (so that a sub-routine can return to the same point in the program) BLX Branch with Link Same as BL but the address to which to branch is held and Exchange in a register CBZ Compare and Combines the CMP and BEQ instructions (i.e. CMP r0, branch if zero #0 + BEQ label) CBNZ Compare and Combines the CMP and BNE instuctions branch if non- zero IT-blocks If-Then blocks Avoids branching for up to 4 instructions following the IF comparison Engineering · EyobuNjineli · Ingenieurswese 11 Branch and Exchange / Vertak en ruil om Branch and Exchange (BX), branches to an address contained in a register, i.e. BX R5 Typically used with link register, to return from a function ⇒ MOV PC, LR then simply becomes BX LR Engineering · EyobuNjineli · Ingenieurswese 12 Branching and C functions / Vertakking en C funksies Calling a function from another function (called nonleaf functions): loop func1 func2 … … … BL func1 BL func2 … … MOV R4, R0 … … !!! B loop MOV PC, LR MOV PC, LR Main loop calls function func1, which calls another function, func2 Function execution is implemented using BL (branch and save PC of next instruction to Link Register), and return from function by restoring PC to contents of saved LR The second BL func2 will overwrite the Link Register (and probably R0-R3). func1 now does not know where to return to anymore… ⇒ The Link Register value must be preserved. This is done in a special part of memory called the Stack Engineering · EyobuNjineli · Ingenieurswese 13 Branching and C functions / Vertakking en C funksies For recursive functions the LR and arguments must repeatedly be stored to the stack. Engineering · EyobuNjineli · Ingenieurswese 14 Assembly Function calls / Saamsteltaal funksie roepe ARM C to Assembly interface: When writing function in ARM assembly you must follow the AAPCS (Arm Architecture Procedure Call Standard). Describes contract between calling function and called function. R0 to R3 used as function arguments (parameters) and return value. Often only use R0 for return value. A double-word sized type is passed in two consecutive registers (e.g., R0 and R1 or R2 and R3). If there are more function arguments, use the stack to pass them. A function must preserve R4-R8, R10, R11, and SP (their content must be the same when the function is called and when the function returns). Preserving SP: in general this means matched PUSH and POP instructions ARM Compiler uses a full descending stack (more on this later) Engineering · EyobuNjineli · Ingenieurswese 17 The stack / Die stapel The stack is a section in volatile memory (RAM) that is used for passing parameters/saving state between function calls and interrupts. It is managed by a special register called the Stack Pointer or SP for short. In many CPU’s there are special instructions that place or remove values from the stack, usually aptly named PUSH and POP. ARM however uses normal memory instruction to access the stack, but the compiler adds auto increment or decrement parameters to the stack pointer. If a value is placed on the stack the stack pointer is decremented, and if a value is removed, the stack pointer increments. The stack therefore grows towards the lower end of memory when values are added and shrinks towards the higher end of memory when values are removed. Some assembly instructions automatically add or remove values from the the stack. Engineering · EyobuNjineli · Ingenieurswese 18 The stack / Die stapel In our ARM processors we need the stack to store: Function arguments that does not fit into R0-R3 R4-R8, R10, R11, and SP within the called function if we wish to use any of them for scratch space and should be restored before the function returns. Local function variables that does not fit into the available registers. The processor state (register values) before calling a function, or interrupt handler execution. e.g. LR before a function call, to ensure we do not overwrite the LR that is necessary to return the PC to the correct address. Engineering · EyobuNjineli · Ingenieurswese 19 The stack / Die stapel Functions may also use stack space to store local variables but must remove stored values before returning. The stack expands (uses more memory) as the processor needs more "scratch space" and contracts (uses less memory) when the processor no longer needs the variables stored there. The stack is a last-in-first-out (LIFO) queue. Like a stack of dishes, the last item pushed onto the stack (the top dish) is the first one that can be popped off. The top of the stack is the most recently allocated space. Engineering · EyobuNjineli · Ingenieurswese 20 The stack / Die stapel Memory layout of our microcontroller SRAM Flash memory Engineering · EyobuNjineli · Ingenieurswese 21 The stack pointer / Die staplewyser The stack pointer, SP (R13), is an ordinary ARM register that, by convention, points to the top of the stack from where elements are removed or added. SP simply stores the memory address of the top of the stack. It starts at a high memory address and decrements to expand as needed. In example (a) below, the stack pointer, SP, holds the address value 0XBEFFFAE8 and points to the data value 0xAB000001. Example (b) shows the stack expanding (by decrementing by 8) to allow two more data words of temporary storage. Engineering · EyobuNjineli · Ingenieurswese 22 Stack / Stapel On the ARM Cortex M4 each element in the stack is a 32-bit word The ARMv7-M architecture uses a full descending stack which means: When pushing, the hardware decrements the stack pointer to the end of the new stack frame before it stores data onto the stack. When popping, the hardware reads the data from the stack frame and then increments the stack pointer. Descending Stack Pointer decreases as stack grows (stack grows downwards from high to low addresses) Ascending Stack Pointer increases as stack grows (stack grows upwards from low to high addresses) Full SP points to the last item on the stack Empty SP points to the next free space on the stack Engineering · EyobuNjineli · Ingenieurswese 23 Stack / Stapel Full descending stack Stack base address is 0x00080000 SP is 0x0007FFDC (9 elements) After a push operation (add value 0x83596900 onto the stack): SP SP is decremented first (new SP = 0x7FFD8) Store new word to 0x7FFD8 Stack now has 10 elements Engineering · EyobuNjineli · Ingenieurswese 24 Stack / Stapel Full descending stack Stack base address is 0x00080000 SP is 0x0007FFDC (9 elements) After a pop operation (remove a SP previous value from the stack) Load word at current SP address (0x7FFDC) Get 0x110049FF Increment SP (new SP is 0x7FFE0) Stack now has 8 elements What is the value of SP when the stack is empty? 0x00080000 Engineering · EyobuNjineli · Ingenieurswese 25 Stack Example / Stapel Voorbeeld One of the important uses of the stack is to save and restore registers that are used by a function. A function should calculate a return value but have no other unintended side effects. The assembly function below violates this rule since it modifies R4, R8 and R9. High-level Code Arm-Assembly code // R4 = y int main() MAIN: {... int y; mov R0, #5 // argument 0 = 5 mov R1, #4 // argument 1 = 4 y = diffofsums (5, 4, 3, 2); mov R2, #3 // argument 2 = 3... mov R3, #2 // argument 3 = 2 } bl DIFFOFSUMS// call function diffofsums (int f, int g, int h, int i) mov R4, R0// y = returned value... { // R4 = result int result; DIFFOFSUMS: result = (f + g) − (h + i); add R8, R0, R1// R8 = f + g return result; add R9, R2, R3// R9 = h + i } sub R4, R8, R9// result = (f + g) - (h + i)