eecs 373
play

EECS 373 Im not Prabal Design of Microprocessor-Based Systems You - PDF document

Announcements EECS 373 Im not Prabal Design of Microprocessor-Based Systems You probably noticed Homework 1 is due No office hours this week Branden Ghena University of Michigan Projects Continue thinking about them


  1. Announcements EECS 373 • I’m not Prabal Design of Microprocessor-Based Systems – You probably noticed • Homework 1 is due • No office hours this week Branden Ghena University of Michigan • Projects – Continue thinking about them Lecture 3: Assembly, Tools, and ABI September 9, 2014 Slides developed in part by Mark Brehob & Prabal Dutta 1 2 Today… Exercise: What is the value of r2 at done? ... start: Finish ARM assembly example from last time movs r0, #1 movs r1, #1 Software Development Tool Flow movs r2, #1 sub r0, r1 bne done Application Binary Interface (ABI) movs r2, #2 done: b done ... 3 4 Conditional execution: Application Program Status Register (APSR) Append to many instructions for conditional execution 1

  2. Solution: Real assembly example what is the value of r2 at done? .equ STACK_TOP, 0x20000800 /* Equates symbol to value */ .text /* Tells AS to assemble region */ ... .syntax unified /* Means language is ARM UAL */ .thumb /* Means ARM ISA is Thumb */ start: .global _start /* .global exposes symbol */ // r0  1, Z=0 movs r0, #1 /* _start label is the beginning */ // r1  1, Z=0 /* ...of the program region */ movs r1, #1 .type start, %function /* Specifies start is a function */ // r2  1, Z=0 movs r2, #1 /* start label is reset handler */ // r0  r0-r1 _start: sub r0, r1 .word STACK_TOP, start /* Inserts word 0x20000800 */ // but Z flag untouched /* Inserts word (start) */ start: // since sub vs subs movs r0, #10 /* We’ve seen the rest ... */ bne done // NE true when Z==0 movs r1, #0 loop: // So, take the branch adds r1, r0 movs r2, #2 // not executed subs r0, #1 bne loop done: deadloop: b done // r2 is still 1 b deadloop .end ... 7 8 What’s it all mean? What happens after a power-on-reset (POR)? .equ STACK_TOP, 0x20000800 /* Sets symbol to value (#define)*/ • ARM Cortex-M3 (many others are similar) .text /* Tells AS to assemble region */ .syntax unified /* Means language is ARM UAL */ .thumb /* Means ARM ISA is Thumb */ • Reset procedure .global _start /* .global exposes symbol */ /* _start label is the beginning */ – SP  mem(0x00000000) /* ...of the program region */ .type start, %function /* Specifies start is a function */ – PC  mem(0x00000004) /* start label is reset handler */ _start: .word STACK_TOP, start /* Inserts word 0x20000800 */ _start: /* Inserts word (start) */ .word __STACKTOP /* Top of Stack */ start: movs r0, #10 /* We’ve seen the rest ... */ .word Reset_Handler /* Reset Handler */ movs r1, #0 .word NMI_Handler /* NMI Handler */ loop: adds r1, r0 .word HardFault_Handler /* Hard Fault Handler */ subs r0, #1 .word MemManage_Handler /* MPU Fault Handler */ bne loop .word BusFault_handler /* Bus Fault Handler */ deadloop: b deadloop ... .end 9 10 Today… How does an assembly language program get turned into a executable program image? Binary program file (.bin) Walk though of the ARM ISA Assembly Object Executable files (.s) files (.o) image file Software Development Tool Flow ld (linker) as Application Binary Interface (ABI) (assembler) Memory layout Disassembled Linker code (.lst) script (.ld) 11 12 2

  3. What are the real GNU executable names for the ARM? Real-world example • Just add the prefix “arm -none-eabi- ” prefix • To the terminal! • Assembler (as) (code at https://github.com/brghena/eecs373_toolchain_examples) – arm-none-eabi-as • Linker (ld) – arm-none-eabi-ld • Object copy (objcopy) – arm-none-eabi-objcopy • Object dump (objdump) – arm-none-eabi-objdump • C Compiler (gcc) – arm-none-eabi-gcc • C++ Compiler (g++) – arm-none-eabi-g++ 13 14 How are assembly files assembled? A simple (hardcoded) Makefile example • $ arm-none-eabi-as – Useful options • -mcpu • -mthumb • -o all: arm-none-eabi-as -mcpu=cortex-m3 -mthumb example1.s -o example1.o arm-none-eabi-ld -Ttext 0x0 -o example1.out example1.o $ arm-none-eabi-as -mcpu=cortex-m3 -mthumb example1.s -o example1.o arm-none-eabi-objcopy -Obinary example1.out example1.bin arm-none-eabi-objdump -S example1.out > example1.lst 15 16 What information does the disassembled file provide? Linker script all: arm-none-eabi-as -mcpu=cortex-m3 -mthumb example1.s -o example1.o OUTPUT_FORMAT("elf32-littlearm") • Specifies little-endian arm in ELF arm-none-eabi-ld -Ttext 0x0 -o example1.out example1.o OUTPUT_ARCH(arm) format. ENTRY(main) arm-none-eabi-objcopy -Obinary example1.out example1.bin • Specifies ARM CPU arm-none-eabi-objdump -S example1.out > example1.lst • Should start executing at label named MEMORY “main” { • We have 64k of memory starting at /* SmartFusion internal eSRAM */ .equ STACK_TOP, 0x20000800 example1.out: file format elf32-littlearm ram (rwx) : ORIGIN = 0x20000000, LENGTH = 64k 0x20000000. You can read, write and .text execute out of it. We’ve named it } .syntax unified .thumb Disassembly of section .text: “ram” .global _start SECTIONS .type start, %function 00000000 <_start>: { 0: 20000800 .word 0x20000800 .text : _start: 4: 00000009 .word 0x00000009 • “.” is a reference to the current { .word STACK_TOP, start memory location . = ALIGN(4); start: 00000008 <start>: • First align to a word (4 byte) boundary movs r0, #10 8: 200a movs r0, #10 *(.text*) movs r1, #0 a: 2100 movs r1, #0 . = ALIGN(4); • Place all sections that include .text at loop: _etext = .; the start (* here is a wildcard) adds r1, r0 0000000c <loop>: } >ram • Define a label named _etext to be the subs r0, #1 c: 1809 adds r1, r1, r0 } bne loop e: 3801 subs r0, #1 current address. end = .; deadloop: 10: d1fc bne.n c <loop> • Put it all in the memory location b deadloop defined by the ram memory location. .end 00000012 <deadloop>: 12: e7fe b.n 12 <deadloop> 17 18 3

  4. How does a mixed C/Assembly program Real-world example #2 get turned into a executable program image? C files (.c) • To the terminal! Again! Binary program file (.bin) ld (code at https://github.com/brghena/eecs373_toolchain_examples) (linker) Assembly Object Executable files (.o) files (.s) image file gcc (compile as + link) (assembler) Memory layout Disassembled code (.lst) Linker Library object files (.o) script (.ld) 19 20 Today… Finish ARM assembly example from last time Walk though of the ARM ISA Software Development Tool Flow Application Binary Interface (ABI) 21 22 ABI Basic Rules Let’s write a simple ABI routine 1. A subroutine must preserve the contents of the • int bob(int a, int b) registers r4-11 and SP – returns a 2 + b 2 – Let’s be careful with r9 though. • Instructions you might need – add adds two values 2. Arguments are passed though r0 to r3 – mul multiplies two values – If we need more, we put a pointer into memory in one – bx branch to register of the registers. • We’ll worry about that later. Other useful factoids 3. Return value is placed in r0 • Stack grows down. – r0 and r1 if 64-bits. – And pointed to by “ sp ” • Address we need to go back to in “ lr ” 4. Allocate space on stack as needed. Use it as needed. Put it back when done… – Keep word aligned. 23 24 4

  5. When is this relevant? • The ABI is a contract with the compiler – All assembled C code will follow this standard • You need to follow it if you want C and Assembly Questions? to work together correctly Comments? • What if you are writing everything in Assembly by hand? Discussion? – Maybe less important. Unless you’re ever going to extend the code 25 26 5

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend