F28PL1 Programming Languages Lecture 3: Assembly Language 2.

Slides:



Advertisements
Similar presentations
Getting started with MPLAB Launch MPLAB Set Toolbar as in the next slide by clicking the leftmost icon Open a new Source file by choosing [FILE][NEW] Type.
Advertisements

Chapter 4 Addressing modes CEG2400 Microcomputer Systems CEG2400 ch4 addressing modes v3a 1.
Computer Science Education
Embedded Software 1. General 8051 features (excluding I/O) CPU 8 bit microcontroller The basic registers include (more discussed later) The 8-bit A (accumulator)
Slides created by: Professor Ian G. Harris Efficient C Code  Your C program is not exactly what is executed  Machine code is specific to each ucontroller.
ARM Assembly Programming Computer Organization and Assembly Languages Yung-Yu Chuang with slides by Peng-Sheng Chen.
Compiler Construction Sohail Aslam Lecture Code Generation  The code generation problem is the task of mapping intermediate code to machine code.
ARM versions ARM architecture has been extended over several versions.
CSC 3210 Computer Organization and Programming
Embedded Systems Programming
Appendix D The ARM Processor
Cosc 2150: Computer Organization
Overheads for Computers as Components 2nd ed.
Suranaree University Of Technology มทส  2002 Anant Oonsivilai 2002/2/27 Microcomputers and Microprocessors Chapter Assembly Language Programming.
Target Code Generation
CPU performance CPU power consumption
MIPS Assembly Tutorial
Passing by-value vs. by-reference in ARM by value C code equivalent assembly code int a;.section since a is not assigned an a:.skip initial.
ITEC 352 Lecture 13 ISA(4).
Tutorial 2 IDE for ARM 7 board (2). Outline Introduce the Debug mode of uVision4 2.
Lecture 6 Programming the TMS320C6x Family of DSPs.
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Code Composer Department of Electrical and Computer Engineering
Compiler Construction Sohail Aslam Lecture ExampleExample a = b + c t1 = a * a b = t1 + a c = t1 * b t2 = c + b a = t2 + t2.
The 8051 Microcontroller and Embedded Systems
Exceptions. Exception Types Exception Handling Vectoring Interrupts Interrupt Handlers Interrupt Priorities Interrupt applications 6-2.
1 CS 201 Compiler Construction Machine Code Generation.
F28PL1 Programming Languages Lecture 2: Assembly Language 1.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Assembly Language Working with the CPU.
Lecture 8: MIPS Instruction Set
F28PL1 Programming Languages Lecture 5: Assembly Language 4.
Arithmetic Expression Consider the expression arithmetic expression: (a – b) + ((c + d) + (e * f)) that can be represented as the following tree.
IDE for ARM 7 board Tutorial 3 IDE of Keil4 V3.a 1.
ARM C Language & Assembler. Using C instead of Java (or Python, or your other favorite language)? C is the de facto standard for embedded systems because.
MIPS Instruction Set Advantages
Chapter 2 Software Tools and Assembly Language Syntax.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Evolution of Programming Languages Generations of PLs.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Introduction Purpose  This training module provides an overview of debugging features.
Bit-DSP-MicrocontrollerTMS320F2812 Texas Instruments Incorporated European Customer Training Center University of Applied Sciences Zwickau (FH)
1 Code Generation. 2 Position of a Code Generator in the Compiler Model Front-End Code Optimizer Source program Symbol Table Lexical error Syntax error.
MIPS coding. Review Shifting – Shift Left Logical (sll) – Shift Right Logical (srl) – Moves all of the bits to the left/right and fills in gap with 0’s.
F28PL1 Programming Languages Lecture 4: Assembly Language 3.
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
F28HS2 Hardware-Software Interfaces Lecture 7: ARM Assembly Language 1.
Lecture 6: Branching CS 2011 Fall 2014, Dr. Rozier.
F28HS Hardware-Software Interface Lecture 11: ARM Assembly Language 5.
Lecture 6: Decision and Control CS 2011 Spring 2016, Dr. Rozier.
GCSE COMPUTER SCIENCE Computers 1.5 Assembly Language.
Tutorial 2 IDE of Keil for the ARM 7 board(2)
Format of Assembly language
MIPS Instruction Set Advantages
Chapter 15: Higher Level Constructs
ARM Registers Register – internal CPU hardware device that stores binary data; can be accessed much more rapidly than a location in RAM ARM has.
Assembly Language Assembly Language
The Cortex-M3/m4 Embedded Systems: Cortex-M3/M4 Instruction Sets
The 8051 Microcontroller and Embedded Systems
PRU-ICSS Programming with CCS
Chapter 4 Addressing modes
CENG2400 Tutorial 1 Keil IDE CENG2400 tutorial 1 v.7a.
ARM Assembly Programming
Code Generation.
MIPS coding.
Optimizing ARM Assembly
Overheads for Computers as Components 2nd ed.
DAT2343 LMC Simulator Usage © Alan T. Pinck / Algonquin College; 2003.
Chapter 6 Programming the basic computer
Branch & Call Chapter 4 Sepehr Naimi
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

F28PL1 Programming Languages Lecture 3: Assembly Language 2

Running assembly programs on the ARM-MDK ARM-MDK does not simulate a bare CPU oriented to embedded systems with memory, peripherals etc STM32-Discovery board is: – STMicroelectronics - STM32F103VB assumes no operating system must provide start up file – code to initialise peripherals, interrupts etc STM32-Discovery uses: – STM32F10x.s –.s == assembler

Running assembly programs on the ARM-MDK start up file associates reset button on board with: ; Reset Handler Reset_Handler PROC EXPORT Reset_Handler [WEAK] IMPORT __main LDR R0, =__main BX R0 ENDP ; text == comment Reset_Handler – labelled procedure start PROC – start of procedure EXPORT Reset_Handler – make Reset_Handler visible externally

Running assembly programs on the ARM-MDK ; Reset Handler Reset_Handler PROC EXPORT Reset_Handler [WEAK] IMPORT __main LDR R0, =__main BX R0 ENDP IMPORT – make external label __main visible internally LDR R0,=__main – load R0 with address for __main BX R0 – branch indirect on R0 i.e. set PC to address for __main from R0 ENDP - end procedure – not a return !

Running assembly programs on the ARM-MDK ARM-MDK expects every project to include a main.c – usually defining a main function – compiler generates assembly with EXPORT ed __main label – start up then IMPORT s __main can’t embed Thumb2 assembly language in C provide a dummy main.c containing only: /* main.c */ GJM’s main.c

Running assembly programs on the ARM-MDK provide own assembly language file with __main : AREA ASMmain, CODE ; Main __main PROC EXPORT __main ENDL B ENDL ENDP END AREA – introduces block of code or data – name + attributes __main – labelled procedure start EXPORT __main – make __main visible externally

Running assembly programs on the ARM-MDK AREA ASMmain, CODE ; Main __main PROC EXPORT __main ENDL B ENDL ENDP END ENDL B ENDL – endless loop at end of program END – end of source file template in GJM’s ASMmain.s

Create new project create a new folder for new project copy dummy main.c to folder copy ASMmain.s template to folder run Keil μVision 4 Project -> New μVision Project – in new folder – give project same name

Create new project Select Device for Target -> STMicroelectronics -> STM32F103VB – OK Copy STM32 Startup Code to Project Folder and Add Project to File? – No Project Window now shows: +Target 1 select + Project Window now shows: +Target 1+Source Group 1

Create new project select inner + Project Window now shows: +Target 1+Source Group 1 Source Group 1 /right mouse button… …Add Files To Group ‘Source Group 1’ Add: STM32F10x.s Add: main.c and (possibly renamed) ASMmain.s – Close

Compile/assemble ASMmain.s /double left mouse button ASMmain.s opens in tabbed window add your assembler code Project -> Build Target – details in lower window fix program and repeat until: “project.axf" - 0 Error(s), 0 Warning(s).

Execute program on simulator Debug -> Start/Stop Debug Session – OK View -> Register Window View -> Disassembly Window select STM32F10x.s tab

Execute program on simulator select reset button: yellow arrow points at: LDR R0,=__main repeatedly select step button: watch indicated instruction & registers changing to leave simulator: Debug -> Start/Stop Debug Session RST

Multiply & divide MUL {Rd, }Rn, Rm  Rd = Rn * Rm UDIV {Rd, }Rn, Rm  Rd = Rn / Rm unsigned rounds down i.e. 7/3 == 2

Expressions for: var 1 = var 2 op var 3 with: var 1 RN R 1 var 2 RN R 2 var 3 RN R 3  op R 1,R 2, R 3 e.g. x = y*z;  x RN 1 y RN 2 z RN 3 MUL x,y,z

Expressions for: var 1 = var 2 op 1 var 3 op 2 var 4 if op 1 and op 2 have same precedence or op 1 has higher precedence than op 2  op 1 R 1,R 2, R 3 op 2 R 1,, R 4 e.g. x = y*z-a; ... a RN 4 MUL x,y,z SUB x,a

Expressions for: var 1 = var 2 op 1 var 3 op 2 var 4 if op 2 has higher precedence than op 1 must evaluate op2 expression in new register  op 2 R i,R 3, R 4 op 1 R 1,, R 2,R i e.g. x = y+z*a;  MUL R5,z,a ADD x,y,R5 e.g. x = y*(z+a)  ADD R5,z,a MUL x,y,R5

Expressions draw expression tree allocate registers to nodes – from bottom left – if expression in assignment then start with register for destination variable accumulate into register for left operand can re-use any register whose value is no longer required

Example: Pythagorus int x; /* R1 */ int y; /* R2 */ int z; /* R3 */ int p; /* R4 */ x = 3; y = 4; z = 5; if(x*x+y*y==z*z) p=1; else p=0; == + * xx * yy * zz

Example: Pythagorus int x; /* R1 */ int y; /* R2 */ int z; /* R3 */ int p; /* R4 */ x = 3; y = 4; z = 5; if(x*x+y*y==z*z) p=1; else p=0; == + * R5 xx * yy * zz

Example: Pythagorus int x; /* R1 */ int y; /* R2 */ int z; /* R3 */ int p; /* R4 */ x = 3; y = 4; z = 5; if(x*x+y*y==z*z) p=1; else p=0; == + * R5 xx * R6 yy * zz

Example: Pythagorus int x; /* R1 */ int y; /* R2 */ int z; /* R3 */ int p; /* R4 */ x = 3; y = 4; z = 5; if(x*x+y*y==z*z) p=1; else p=0; == + R5 * R5 xx * R6 yy * zz

Example: Pythagorus int x; /* R1 */ int y; /* R2 */ int z; /* R3 */ int p; /* R4 */ x = 3; y = 4; z = 5; if(x*x+y*y==z*z) p=1; else p=0; == + R5 * R5 xx * R6 yy zz

Example: Pythagoras int x; /* R1 */ int y; /* R2 */ int z; /* R3 */ int p; /* R4 */ x = 3; y = 4; z = 5; if(x*x+y*y==z*z) p=1; else p=0; == CMP + R5 * R5 xx * R6 yy zz

Example: Pythagoras int x; int y; int z; int p; x = 3; y = 4; z = 5; if(x*x+y*y==z*z) p=1; else p=0; AREA PYTH, CODE ; Main __main PROC EXPORT __main x RN 1 y RN 2 z RN 3 p RN 4 MOV x,#3 MOV y,#4 MOV z,#5

Example: Pythagoras int x; int y; int z; int p; x = 3; y = 4; z = 5; if(x*x+y*y==z*z) p=1; else p=0; MUL R5,x,x MUL R6,y,y ADD R5,R6 MUL R6,z,z CMP R5,R6 BEQ SAME MOV p,#0 B ENDL SAMEMOV p,#1 ENDLB ENDL ENDP END

Structured programming assembly language requires intricate use of labels & branches easy to produce “spaghetti code” design assembly programs using high level program structures – condition – iteration use template to translate high level to label + branch

Structured programming: if IF exp 1 op exp 2 THEN command 1 ELSE command 2 not( = )  NE not(! = )  EQ not( < )  GE not( <= )  GT not( > )  LE not(> = )  LT Ri = exp 1 Rj = exp 2 CMP Ri,Rj B not(op) IFALSE command 1 B IEND IFALSE command 2 IEND

Structured programming: if IF exp 1 op exp 2 THEN command Ri = exp 1 Rj = exp 2 CMP Ri,Rj B not(op) IEND command IEND

Structured programming: while WHILE exp 1 op exp 2 DO command WLOOP Ri = exp 1 Rj = exp 2 CMP Ri,Rj B not(op) WEND command B WLOOP WEND

Example: is x prime? dreadful algorithm...! int x; int n; int p; x = 23; n = 2; p = 1; while(n<x) if(x%n==0) { p = 0; break; } else n = n+1; AREA PYTH, CODE ; Main __main PROC EXPORT __main x RN 1 n RN 2 p RN 3 MOV x,#25 MOV n,#2 MOV p,#1

Example: is x prime? dreadful algorithm...! int x; int n; int p; x = 23; n = 2; p = 1; while(n<x) if(x%n==0) { p = 0; break; } else n = n+1; TESTCMP n,x BEQ ENDL UDIV R4,x,n MUL R4,n CMP R4,x BNE NOTP MOV p,#0 B ENDL NOTPADD n,#1 B TEST ENDLB ENDL ENDP END n<x x%n==0  (x/n)*n==x n = n+1 p=0; break