COMP3221 lec08-arith.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 8: C/Assembler Data Processing

Slides:



Advertisements
Similar presentations
4.
Advertisements

Instruction Set-Intro
Lecture 6 Programming the TMS320C6x Family of DSPs.
COMP3221 lec16-function-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lectures 16 : Functions in C/ Assembly - II
Embedded System Design Center ARM7TDMI Microprocessor Data Processing Instructions Sai Kumar Devulapalli.
COMP3221 lec10-logical-II&mul.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 10: C/Assembler Logical and Shift – II & Multiplication.
Chapter 2.
COMP3221 lec9-logical-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 9: C/Assembler Logical and Shift - I
COMP3221 lec-12-mem-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 12: Memory Access - II
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
Operand And Instructions Representation By Dave Maung.
Modified from the notes by
COMP3221 lec05-numbers-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 5: Number Systems – I
CS61C L05 Introduction to MIPS Assembly Language : Arithmetic (1) Garcia, Fall 2011 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS 61C L06 MIPS Intro (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #6: Intro to MIPS
CS61C L08 Introduction to MIPS Assembly Language: Arithmetic (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS61C L6 Intro MIPS ; Load & Store (1) Garcia, Fall 2005 © UCB Hate EMACS? Love EMACS? Richard M. Stallman, a famous proponent of open- source software,
Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 4: Memory Access March,
Normal C Memory Management °A program’s address space contains 4 regions: stack: local variables, grows downward heap: space requested for pointers via.
COMP3221 lec06-numbers-II.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 6: Number Systems - II
Elec2041 lec-11-mem-I.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 11: Memory Access - I
CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 I´ve been getting this a lot lately So, what are you teaching this term? Computer.
CS61C L08 Introduction to MIPS Assembly Language : Arithmetic (1) Garcia, Spring 2008 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
CS1104 Assembly Language: Part 1
CS 61C L08 Introduction to MIPS Assembly Language: Arithmetic (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
COMP3221 lec07-numbers-III.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 7: Number Systems - III
From C to Assembly Language Jen-Chang Liu, Spring 2006 Adapted from
On Textbook CD: SPIM Jen-Chang Liu, Simulation of a virtual machine Virtual machine (simulator) Differences between SPIM and real MIPS? No delayed.
MIPS Instruction Set Advantages
BASIC COMPUTING CONCEPTS Part 3 Dr. Abdel-Rahman Al-Qawasmi INSIDE THE MACHINE by Jon Stokes.
COMP3221 lec04--prog-model.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 4: Programmer’s Model of Microprocessors
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Reformatting a Machine 125 Million Miles Away! Mars rover “Opportunity” got its flash drive reformatted 9/8/14, reason: rover keeps rebooting. Happy news:
BITWISE OPERATIONS – Microprocessor Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Chapter 10 The Assembly Process. What Assemblers Do Translates assembly language into machine code. Assigns addresses to all symbolic labels (variables.
Chapter 2 Instructions: Language of the Computer Part I.
CS2100 Computer Organisation MIPS Part I: Introduction (AY2015/6) Semester 1.
What is a program? A sequence of steps
Computer Organization Instructions Language of The Computer (MIPS) 2.
Assembly Variables: Registers Unlike HLL like C or Java, assembly cannot use variables – Why not? Keep Hardware Simple Assembly Operands are registers.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Intel Xscale® Assembly Language and C. The Intel Xscale® Programmer’s Model (1) (We will not be using the Thumb instruction set.) Memory Formats –We will.
CS61C L05 Introduction to MIPS Assembly Language : Arithmetic (1) Garcia, Spring 2014 © UCB TA/Guest Lecturer: Shreyas Chand inst.eecs.berkeley.edu/~cs61c.
Assembly Language Basic job of a CPU: execute lots of instructions. Instructions are the primitive operations that the CPU may execute. Different CPUs.
Instructor: Dr. Mike Turi Department of Computer Science and Computer Engineering Pacific Lutheran University Lecture slides adapted from Part 4, EE 334.
CS2100 Computer Organisation
Computers’ Basic Organization
IT 251 Computer Organization and Architecture
Lecturer PSOE Dan Garcia
MIPS Instruction Set Advantages
ARM Registers Register – internal CPU hardware device that stores binary data; can be accessed much more rapidly than a location in RAM ARM has.
March 2006 Saeid Nooshabadi
A Closer Look at Instruction Set Architectures
March 2006 Saeid Nooshabadi
IT 251 Computer Organization and Architecture
Chapter 1 C for Embedded Systems.
Instructions - Type and Format
Chapter 1 C for Embedded Systems.
The University of Adelaide, School of Computer Science
Topic 6: Bitwise Instructions
Hello to Jessy VanDivner listening from Ohio!
Introduction to Micro Controllers & Embedded System Design
COMS 361 Computer Organization
Instructions in Machine Language
Branching instructions
March 2006 Saeid Nooshabadi
8051 ASSEMBLY LANGUAGE PROGRAMMING
March 2006 Saeid Nooshabadi
Review In last lecture, done with unsigned and signed number representation. Introduced how to represent real numbers in float format.
CPU Structure CPU must:
Presentation transcript:

COMP3221 lec08-arith.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 8: C/Assembler Data Processing August, 2003 Saeid Nooshabadi

COMP3221 lec08-arith.2 Saeid Nooshabadi Overview °C operators, operands °Variables in Assembly: Registers °Comments in Assembly °Data Processing Instructions °Addition and Subtraction in Assembly

COMP3221 lec08-arith.3 Saeid Nooshabadi Review C Operators/Operands (#1/2) °Operators: +, -, *, /, % (mod); 7/4==1, 7%4==3 °Operands: Variables: lower, upper, fahr, celsius Constants: 0, 1000, -17, 15.4 °Assignment Statement: Variable = expression Examples: celsius = 5*(fahr-32)/9; a = b+c+d-e;

COMP3221 lec08-arith.4 Saeid Nooshabadi C Operators/Operands (#2/2) °In C (and most High Level Languages) variables declared first and given a type Example: int fahr, celsius; char a, b, c, d, e; °Each variable can ONLY represent a value of the type it was declared as (cannot mix and match int and char variables).

COMP3221 lec08-arith.5 Saeid Nooshabadi Assembly Design: Key Concepts °Keep it simple! Limit what can be a variable and what can’t Limit types of operations that can be done to absolute minimum -if an operation can be decomposed into a simpler operation, don’t include it. -For example 7%4 operation is complex. We break it into simpler operations in Assembly

COMP3221 lec08-arith.6 Saeid Nooshabadi Assembly Variables: Registers (#1/4) °Unlike HLL, assembly cannot use variables Why not? Keep Hardware Simple °Assembly Operands are registers limited number of special locations built directly into the hardware operations can only be performed on these! °Benefit: Since registers are directly in hardware, they are very fast

COMP3221 lec08-arith.7 Saeid Nooshabadi Assembly Variables: Registers (#2/4) °Drawback: Since registers are in hardware, there are a predetermined number of them Solution: ARM code must be very carefully put together to efficiently use registers °16 registers in ARM Why 16? Smaller is faster °Each ARM register is 32 bits wide Groups of 32 bits called a word in ARM

COMP3221 lec08-arith.8 Saeid Nooshabadi Assembly Variables: Registers (#3/4) °Registers are numbered from 0 to 15 °Each register can be referred to by number or name °Number references: r0, r1, r2, … r15 °r15 = pc has special significant: °r15 is program counter pointing to instructions being fetched from memory

COMP3221 lec08-arith.9 Saeid Nooshabadi Assembly Variables: Registers (#4/4) °By convention, each register also has a name to make it easier to code °For now: r0 – r3  a1 – a4 (correspond to C functions arguments. Used for scratch pad too!) r4 – r10  v1 – v7 (correspond to function variables) °In general, use names to make your code more readable

COMP3221 lec08-arith.10 Saeid Nooshabadi Comments in Assembly °Another way to make your code more readable: comments! °Hash (;) is used for ARMS comments anything from (;) mark to end of line is a comment and will be ignored GNU ARM assembler accepts instead of (;) as well °Note: Different from C. C comments have format /* comment */, so they can span many lines GNU ARM assembler accepts /* comments*/ as well.

COMP3221 lec08-arith.11 Saeid Nooshabadi Assembly Instructions °In assembly language, each statement (called an Instruction), executes exactly one of a short list of simple commands °Unlike in C (and most other High Level Languages), each line of assembly code contains at most 1 instruction

COMP3221 lec08-arith.12 Saeid Nooshabadi Data processing Instructions °Largest category of ARM instructions, all sharing the same instruction format. °Contains: Arithmetic operations Comparisons (no results saved - just set condition code flags NZCV) Logical operations Data movement between registers °This is a load / store architecture NOTThese instruction only work on registers, NOT memory. °They each perform a specific operation on operands. 4 field Format: 1 2, 3, 4 where: 1) operation by name 2) operand getting result (“destination”) 3) 1st operand for operation (“source1”) 4) second operand: register or shifted register or immediate (numerical constant)

COMP3221 lec08-arith.13 Saeid Nooshabadi Using the Barrel Shifter: The Second Operand Register, optionally with shift operation applied. Shift value can be either be: 5 bit unsigned integer add a1, v1, v3, lsl #8 ;a1  v1 +(v3 << 8 bits) Specified in bottom byte of another register. add a1, v1, v3, lsl v4 ;a1  v1 +(v3 << v4 bits) Operand 1 Result ALU Barrel Shifter Operand 2 °Immediate value. 8 bit number Can be rotated right through an even number of positions. Assembler will calculate rotate for you from constant. add a1, v1, #10 ;a1  v1 + 10

COMP3221 lec08-arith.14 Saeid Nooshabadi Addition and Subtraction (#1/3) °Addition in Assembly Example: addv1,v2,v3 (in ARM) Equivalent to: a = b + c (in C) where registers v1,v2,v3 are associated with variables a, b, c °Subtraction in Assembly Example: subv4,v5,v6 (in ARM) Equivalent to: d = e - f (in C) where registers v4,v5,v6 are associated with variables d, e, f

COMP3221 lec08-arith.15 Saeid Nooshabadi Addition and Subtraction (#2/3) °How do we do this? f = (g + h) - (i + j); °Use intermediate register add v1,v2,v3; f = g + h add a1,v4,v5; a1 = i + j ; need to save i+j, but can’t use f, so use a1 sub v1,v1,a1; f=(g+h)-(i+j)

COMP3221 lec08-arith.16 Saeid Nooshabadi Addition and Subtraction (#3/3) °How do the following C statement? a = b + c + d - e; °Break into multiple instructions add v1, v2, v3 ; a = b + c add v1, v1, v4 ; a = a + d sub v1, v1, v5 ; a = a - e °Notice: A single line of C may break up into several lines of ARM instructions. °Notice: Everything after the (;) mark on each line is ignored (comments)

COMP3221 lec08-arith.17 Saeid Nooshabadi Addition/Subtraction with Immediates (#1/2) °Immediates are numerical constants. °They appear often in code, so there are special instructions for them. °Add Immediate: add v1,v2,#10 (in ARM) f = g + 10 (in C) where registers v1,v2 are associated with variables f, g °Syntax similar to add instruction with register, except that last argument is a number instead of a register. This number should be preceded by (#) symbol

COMP3221 lec08-arith.18 Saeid Nooshabadi Addition/Subtraction with Immediates (#2/2) °Similarly add v1,v2,#-10 f = g - 10 (in C) where registers v1,v2 are associated with variables f, g °OR sub v1,v2,#10 f = g - 10 (in C) where registers v1,v2 are associated with variables f, g

COMP3221 lec08-arith.19 Saeid Nooshabadi Data Movement Instruction °Addition with zero is conveniently used to move content of one register to another register, so: add v1,v2,#0 (in ARM) f = g (in C) where registers v1,v2 are associated with variables f, g °This is so often used in code that ARM has an specific instruction for it: mov v1, v2 °Another useful instruction often used to provide delay in a loop is mov v1, v1;this also called nop (No Operation) This does nothing useful

COMP3221 lec08-arith.20 Saeid Nooshabadi Reverse Subtraction Instruction °Normal Subtraction: Example: subv4,v5,v6 (in ARM); v4  v5 – v6 Equivalent to: d = e - f (in C) where registers v4,v5,v6 are associated with variables d, e, f °Reverse Subtraction: Example: rsbv4,v5,v6 (in ARM) ; v4  v6 – v5 Equivalent to: d = - (e) + f (in C) where registers v4,v5,v6 are associated with variables d, e, f °rsb is useful in many situations

COMP3221 lec08-arith.21 Saeid Nooshabadi COMP3221 Reading Materials (Week #3) °Week #3: Steve Furber: ARM System On-Chip; 2nd Ed, Addison-Wesley, 2000, ISBN: We use chapters 3 and 5 °ARM Architecture Reference Manual –On CD ROM

COMP3221 lec08-arith.22 Saeid Nooshabadi “And in Conclusion…” °New Instructions: add sub mov °New Registers: C Function Variables: v1 – v7 Scratch Variables: a1 – a4