Chapter 15: Higher Level Constructs CEG2400 - Microcomputer Systems CEg2400 Ch15 Higher Level Constructs V4b 1 Reference Steven Furber, ARM system-on-chip.

Slides:



Advertisements
Similar presentations
Chapter 4 Addressing modes CEG2400 Microcomputer Systems CEG2400 ch4 addressing modes v3a 1.
Advertisements

ARM versions ARM architecture has been extended over several versions.
Appendix D The ARM Processor
1 Lecture 3: MIPS Instruction Set Today’s topic:  More MIPS instructions  Procedure call/return Reminder: Assignment 1 is on the class web-page (due.
Goal: Write Programs in Assembly
Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
CS/COE0447 Computer Organization & Assembly Language
Branches Two branch instructions:
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
Control Structures in ARM Implementation of Decisions Similar to accumulator instructions One instruction sets the flags, followed by another instruction.
Embedded System Design Center Sai Kumar Devulapalli ARM7TDMI Microprocessor Load and store instruction.
Machine Instructions Operations 1 ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-1.ppt Modification date: March 18, 2015.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
Lecture 5 Sept 14 Goals: Chapter 2 continued MIPS assembly language instruction formats translating c into MIPS - examples.
Topics covered: ARM Instruction Set Architecture CSE 243: Introduction to Computer Architecture and Hardware/Software Interface.
MIPS Instruction Set Advantages
ARM Instructions I Prof. Taeweon Suh Computer Science Education Korea University.
Topic 8: Data Transfer Instructions CSE 30: Computer Organization and Systems Programming Winter 2010 Prof. Ryan Kastner Dept. of Computer Science and.
Lecture 4: Advanced Instructions, Control, and Branching cont. EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Topic 10: Instruction Representation CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and.
Lecture 4. ARM Instructions #1 Prof. Taeweon Suh Computer Science Education Korea University ECM586 Special Topics in Embedded Systems.
11/02/2009CA&O Lecture 03 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Chapter 5 The LC Instruction Set Architecture ISA = All of the programmer-visible components and operations of the computer memory organization.
Execution of an instruction
Lecture 4: MIPS Instruction Set
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Lecture 2: Advanced Instructions, Control, and Branching EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer.
Computer Organization CS224 Fall 2012 Lessons 7 and 8.
Lecture 6: Branching CS 2011 Fall 2014, Dr. Rozier.
Computer Organization Instructions Language of The Computer (MIPS) 2.
Lecture 8: Loading and Storing to Memory CS 2011 Fall 2014, Dr. Rozier.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee Control Unit.
Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables – Why not? Keep Hardware Simple Assembly Operands are registers.
Instruction Set Architectures Early trend was to add more and more instructions to new CPUs to do elaborate operations –VAX architecture had an instruction.
Lecture 6: Decision and Control CS 2011 Spring 2016, Dr. Rozier.
ARM Instructions ARM instructions are written as an operation code (opcode), followed by zero or more operands Operands may be constants (8-bit value),
Revision questions CENG2400 v.14b 1 CENG2400 Revision, Question 1 A system has an ARM processor with a 32-bit General Purpose Input Output (GPIO) module.
Deeper Assembly: Addressing, Conditions, Branching, and Loops
Chapter 15: Higher Level Constructs
Instruction sets : Addressing modes and Formats
MIPS Instruction Set Advantages
Chapter 15: Higher Level Constructs
Assembly Language Programming of 8085
Microprocessor T. Y. B. Sc..
Introduction to the ARM Instruction Set
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
Processor Instructions set. Learning Objectives
Chapter 4 Addressing modes
RISC Concepts, MIPS ISA Logic Design Tutorial 8.
The University of Adelaide, School of Computer Science
SCHOOL OF ELECTRONICS ENGINEERING Electronics and Communication
ARM Assembly Programming
Chapter 15: Higher Level Constructs (C and assembly relation)
Lecture 4: MIPS Instruction Set
CSC 3210 Computer Organization and Programming
ECE232: Hardware Organization and Design
ARM Control Structures
Optimizing ARM Assembly
Branching instructions
Overheads for Computers as Components 2nd ed.
Multiply Instructions
Immediate data Immediate operands : ADD r3, r3, #1 valid ADD r3, #1,#2 invalid ADD #3, r1,r2 invalid ADD r3, r2, #&FF ( to represent hexadecimal immediate.
Branch & Call Chapter 4 Sepehr Naimi
Introduction to Assembly Chapter 2
An Introduction to the ARM CORTEX M0+ Instructions
Presentation transcript:

Chapter 15: Higher Level Constructs CEG Microcomputer Systems CEg2400 Ch15 Higher Level Constructs V4b 1 Reference Steven Furber, ARM system-on-chip architecture Pearson

Introduction Part 1: ARM instructions Part 2 : using assembly to implement C statements. – Reference – CEg2400 Ch15 Higher Level Constructs V4b 2

Part 1 Instructions CEg2400 Ch15 Higher Level Constructs V4b 3

Instructions CEG2400 ARM Instruction quick reference.doc CEG2400 ARM Instruction quick reference.doc Instructions ArithmeticADD{condition}{S} Rd, Rn, ADC{condition}{S} Rd, Rn, SUB{condition}{S} Rd, Rn, SBC{condition}{S} Rd, Rn, RSB{condition}{S} Rd, Rn, RSC{condition}{S} Rd, Rn, MUL{condition}{S} Rd, Rm, Rs MLA{condition}{S} Rd, Rm, Rs, Rn UMULL{condition}{S} RdLo, RdHi, Rm, Rs UMLAL{condition}{S} RdLo, RdHi, Rm, Rs SMULL{condition}{S} RdLo, RdHi, Rm, Rs SMLAL{condition}{S} RdLo, RdHi, Rm, Rs MoveMOV{condition}{S} Rd, MVN{condition}{S} Rd, LogicalTST{condition} Rn, TEQ{condition} Rn, AND{condition}{S} Rd, Rn, EOR{condition}{S} Rd, Rn, ORR{condition}{S} Rd, Rn, BIC{condition}{S} Rd, Rn, CompareCMP{condition} Rn, CMN{condition} Rn, BranchB{condition} label BL{condition} label Load / StoreLDR{condition} Rd, STR{condition} Rd, StackLDM{condition} Rn{!}, STM{condition} Rn{!}, CEg2400 Ch15 Higher Level Constructs V4b 4

Required fields Immediate value# RegisterRm Logical shift left Rm, LSL # Logical shift right Rm, LSR # Arithmetic shift right Rm, ASR # Rotate right Rm, ROR # E.g. ADD R0, R2, R3,LSL#1 ; R0 = R2 + (R3 << 1) CEg2400 Ch15 Higher Level Constructs V4b 5

Addressing_modes Zero offset[Rn] Immediate offset[Rn, #+/- ] Register offset[Rn, +/-Rm] Post-indexedImmediate offset[Rn], #+/- Post-indexedRegister offset[Rn], +/-Rm Pre-indexedImmediate offset[Rn, #+/- ]! Pre-indexedRegister offset[Rn, +/-Rm]! Full DescendingFD Empty DescendingED Full AscendingFA Empty AscendingEA CEg2400 Ch15 Higher Level Constructs V4b 6

Optional fields CEG2400 ARM Instruction quick reference.doc CEG2400 ARM Instruction quick reference.doc Optional fields {condition} EQEqual NENot equal MINegative PLPositive or zero VSOverflow VCNo overflow HIUnsigned higher LSUnsigned lower or same GESigned greater than or equal LTSigned less than GTSigned greater than LESigned less than or equal CEg2400 Ch15 Higher Level Constructs V4b 7

Part 2 Using assembly to implement C statements CEg2400 Ch15 Higher Level Constructs V4b 8

Why? All higher languages (C, C++, Java) must be translated into assembly language/machine code for execution CEg2400 Ch15 Higher Level Constructs V4b 9 Higher level Language C, C++, java Compilation By a compiler Object code In assembly Machine Code in memory Assemble by an assembler

C statements 1)Pointers 2)Arrays 3)Conditional statements : If-then-else; Switch 4)For loop 5)Do while loop 6)While loop CEg2400 Ch15 Higher Level Constructs V4b 10

1) Pointers in C int *p; //integer is 32-bit p = p + 1; – Since p points to a 4-byte value, p must be incremented by 4 – Increment the pointer once, increment address 4 times – If p is in register r0 “add r0,r0,#4” p = p + t; – p must be incremented by 4 * t, so if t is in r1 – add r0,r0,r1,lsl #2; t in r1, old p in r0, lsl #2 = times 4 CEg2400 Ch15 Higher Level Constructs V4b 11 Each memory address is 8-bit So each 32-bit has 4 locations 8-bit

Recall : ADD ADD : Addition ADD,, dest = op_1 + op_2 ADD will add the two operands, placing the result in the destination register. Operand 1 is a register, operand 2 can be a register, shifted register, or an immediate value: ADD R0, R1, R2 ; R0 = R1 + R2 ADD R0, R1, #256 ; R0 = R ADD R0, R2, R3,LSL#1 ; R0 = R2 + (R3 << 1) The addition may be performed on signed or unsigned numbers. CEg2400 Ch15 Higher Level Constructs V4b 12

2) Arrays Same as pointers since a[i] is the same as *(a + i) CEg2400 Ch15 Higher Level Constructs V4b 13

;//3) Conditionals in C ; User Initial Stack & Heap AREA |.text|, CODE, READONLY EXPORT __main __main; ;// if (a > b) ;// c = a; ;//else c = b; ;//The assembly code is as follows; //a, b and c are in r0, r1, r2 resp. mov r0, #1 ; try #1 or #3 to see result mov r1,#2 cmp r0,r1 ; testing r0-r1 ; test a<=b ble L1 ; branch to L1"if b is less or equal than a " mov r2,r0 ; c=a, since (a b b exit ; done L1 mov r2,r1 ; c=b, because a<=b exit end CEg2400 Ch15 Higher Level Constructs V4b 14 a>b? (r0>r1)? c=a (r2=r0) c=b (r2=r1) yes no

exercise1 Conditionals in C if (a <= b) c = a; else c = b; a, b and c are in r0, r1, r2 resp. Write the assembly code CEg2400 Ch15 Higher Level Constructs V4b 15

Recall: CMP CMP : Compare CMP, status = op_1 - op_2 CMP allows you to compare the contents of a register with another register or an immediate value, updating the status flags to allow conditional execution to take place. It performs a subtraction, but does not store the result anywhere. Instead, the flags are updated as appropriate. CEg2400 Ch15 Higher Level Constructs V4b 16

Recall: MOV MOV : Move MOV, dest = op_1 MOV loads a value into the destination register, from another register, a shifted register, or an immediate value. You can specify the same register for the effect of a NOP instruction, or you can shift the same register if you choose: MOV R0, R0 ; R0 = R0... NOP instruction MOV R0, R0, LSL#3 ; R0 = R0 * 8 If R15 is the destination, the program counter or flags can be modified. This is used to return to calling code, by moving the contents of the link register into R15: MOV PC, R14 ; Exit to caller MOVS PC, R14 ; Exit to caller preserving flags (not 32-bit compliant) CEg2400 Ch15 Higher Level Constructs V4b 17

Conditionals The method in the previous slide is the most general case, can have many statements in the “if” and “else” parts of the condition For simple cases such as the example, it may be more efficient to use conditional execution, e.g. “conditional execution gt” For example the following 3 statements can implement if-then-else: CMP r0, #0 ; if (x <= 0) MOVLE r0, #0 ; x = 0; MOVGT r0, #1 ; else x = 1; See to-arm/conditional.htmlhttp:// to-arm/conditional.html CEg2400 Ch15 Higher Level Constructs V4b 18

Switches in C (application of if-then-else) switch (e) { case 0: point0; case 1: point1; … default: pointx; } Can be handled using “if” statements; use previous methods tmp = e; if (tmp == 0) { point0; } else if (tmp == 1) { point1; } … else { pointx; }… CEg2400 Ch15 Higher Level Constructs V4b 19

4) For loop in C for (i = 0; i < 10; i++) { a[i]=3; }// This code could be optimized //in a number of ways, please think about it AREA |.data|, DATA, READWRITE top_of_array DCD 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0 align ; User Initial Stack & Heap AREA |.text|, CODE, READONLY EXPORT __main __main; mov r1,#3;value 3 to save in a[i] ldr r2,=top_of_array; r2=pointer to a[0] mov r0,#0;i is r0 Lcmp r0,#10; if i >= 10 done bge exit ; branch exit if greater str r1,[r2] ; store r1(=3) in a[0]+i*4 add r2,#4;pointer to next destination add r0,#1;i++ b L exit end; CEg2400 Ch15 Higher Level Constructs V4b 20 r2=a[0] NEXT a[i] Top_of_array

Exercise2: For loop in C for (i = 0; i < 10; i++) { a[i]=i-1;} Write the assembly code. CEg2400 Ch15 Higher Level Constructs V4b 21

C-disassembly : for loop for (i=1;i<10;i++) { if (i <= 5) c = 0; else c = 1; } 0x E1A00000 NOP 12: for (i=1;i<10;i++) 13: { 0x C E3A00001 MOV R0,#0x x E59F122C LDR R1,[PC,#0x022C] 0x E STR R0,[R1] 0x EA00000F B 0x C 14: if (i <= 5) 0x C E59F0220 LDR R0,[PC,#0x0220] 0x E LDR R0,[R0] 0x E CMP R0,#0x x CA BGT 0x C 15: c = 0; 16: else 0x C E3A00000 MOV R0,#0x x E59F1210 LDR R1,[PC,#0x0210] 0x E STR R0,[R1] 0x EA B 0x : c = 1; ; and more// CEg2400 Ch15 Higher Level Constructs V4b 22 valuehttp://stackoverflow.com/questions/ /strange-behaviour-of-ldr-pc- value Note: When “program counter pc” is used for reading there is an 8- byte offset in ARM mode and 4-byte offset in Thumb mode. Memory window (130h+220h+8h=364h) 0x364: …

Recall: ADR (Pseudo instructions) ADR : load ADRess ADR, This loads the address referred into the given register: 00008FE4 OPT l% 00008FE4 E28F0004 ADR R0, text 00008FE8 EF SWI "OS_Write0" 00008FEC E1A0F00E MOV PC, R FF0.text 00008FF0 EQUS "Hello!" + CHR$13 + CHR$10 + CHR$ FFC ALIGN CEg2400 Ch15 Higher Level Constructs V4b 23

Recall: LDR or STR Single Data Transfer The single data transfer instructions (STR and LDR) are used to load and store single bytes or words of data from/to main memory. The addressing is very flexible. First, we'll look at the instruction: LDR R0, address STR R0, address LDRB R0, address ;(load byte) STRB R0, address; (store byte) For LDRLS, the optional field LS =Unsigned lower or same CEg2400 Ch15 Higher Level Constructs V4b 24

5) Do while one branch is enough do {..do_something.. body; } while (conditional expression); LOOP{…do_something..; …body…}; bne LOOP ;conditional expression EXIT CEg2400 Ch15 Higher Level Constructs V4b 25

6) While loop requires 2 branch instructions while (conditional_expr) {do_something …body;} LOOP…; not conditional_expr beqexit;branch exit if condition met …do_something…; body b LOOP EXIT CEg2400 Ch15 Higher Level Constructs V4b 26 The main loop has 2 branch instructions: slower

Alterative method ‘while’, it is more efficient advantage: The main loop has only one branch instruction: faster while (conditional expr){…do_something...} b TEST ; need this because it is a while _do loop LOOP… do_something..; loop body TEST…; exit test conditional expression bne LOOP ; loop back if exit condition not met EXIT CEg2400 Ch15 Higher Level Constructs V4b 27 “Goto exit if test exit condition is met”

Summary Studied the relation between assembly and higher level languages CEg2400 Ch15 Higher Level Constructs V4b 28

Appendix1 Recall: AND AND : Logical AND AND,, dest = op_1 AND op_2 AND will perform a logical AND between the two operands, placing the result in the destination register; this is useful for masking the bits you wish to work on. Operand 1 is a register, operand 2 can be a register, shifted register, or an immediate value: AND R0, R0, #3 ; R0 = Keep bits zero and one of R0, discard the rest. An AND table (result = both): Op_1 Op_2 Result CEg2400 Ch15 Higher Level Constructs V4b 29

//self studying exercises, no need to submit to the teacher. // testing_C_and_assembly.c //Appendix: testing C and assembly conversion // run this program and read contents in the disassembly window in uvision //Exercises: Use single step to run it, and answer the questions below. #include int a[4],b[4],c,i; main() { for(;;) //testing forever { //test 1, looping for (i=1;i<10;i++) { if (i <= 5) c = 0; else c = 1; } //Question1a: Where does the program store i and c? //Question1b: Draw the flow diagram of the program. //test 2, looping and array for (i = 0; i < 4; i++) {a[i]=0; b[i]=0;//clear the arrays } for (i = 0; i < 10; i++) {a[i]=3+i; //set values into the array }//question2a: Find a bug in the above program. //question2b: What is the actual result of running the code? //test 3, looping i=0,c=0; //clear i,c while (i<5) {c++; i++; } //Question3a: Where does the program store i and c? //Question3b: Draw the flow diagram of the program. //test 4, test switching i=0,c=1; //clear variables for(i=0;i<5;i++) { switch (i) { case 0: c=0; case 1: c=1; case 2: c=2; case 3: c=3; case 4: c=4; default: c=0; } } } //question4a: Explain how switch-case is implemented here. //question4b: Suggest an alternative method for the implementation. } CEg2400 Ch15 Higher Level Constructs V4b 30