Unsigned and signed integer numbers

Similar documents
Flow Control In Assembly

Control Flow. September 2, Indiana University. Geoffrey Brown, Bryce Himebaugh 2015 September 2, / 21

Cortex M3 Programming

Control Flow Instructions

STEVEN R. BAGLEY ARM: PROCESSING DATA

ARM Assembly Language. Programming

ARM Cortex-M4 Programming Model Flow Control Instructions

Writing ARM Assembly. Steven R. Bagley

ARM Instruction Set Architecture. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Processor Status Register(PSR)

Comparison InstruCtions

ARM Shift Operations. Shift Type 00 - logical left 01 - logical right 10 - arithmetic right 11 - rotate right. Shift Amount 0-31 bits

Chapter 2 Instructions Sets. Hsung-Pin Chang Department of Computer Science National ChungHsing University

EE 3170 Microcontroller Applications

Outline. ARM Introduction & Instruction Set Architecture. ARM History. ARM s visible registers

Overview COMP Microprocessors and Embedded Systems. Lectures 14: Making Decisions in C/Assembly Language II

Exam 1 Fun Times. EE319K Fall 2012 Exam 1A Modified Page 1. Date: October 5, Printed Name:

Introduction to C. Write a main() function that swaps the contents of two integer variables x and y.

Introduction to Embedded Microcomputer Systems Lecture 8.1. Computers in the future may weigh no more than 1.5 tons Popular Science, 1949

ARM Cortex-M4 Architecture and Instruction Set 2: General Data Processing Instructions

Condition Code Register. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

(2) Part a) Registers (e.g., R0, R1, themselves). other Registers do not exists at any address in the memory map

ARM Cortex-M4 Architecture and Instruction Set 3: Branching; Data definition and memory access instructions

EE319K Spring 2015 Exam 1 Page 1. Exam 1. Date: Feb 26, 2015

EE319K Fall 2013 Exam 1B Modified Page 1. Exam 1. Date: October 3, 2013

Lecture 6 Assembly Programming: Branch & Iteration

The PAW Architecture Reference Manual

3 Assembly Programming (Part 2) Date: 07/09/2016 Name: ID:

Exam 1. Date: Oct 4, 2018

Branch Instructions. R type: Cond

EE319K Exam 1 Summer 2014 Page 1. Exam 1. Date: July 9, Printed Name:

Reconfigurable Computing Systems ( L) Fall 2012 Tiny Register Machine (TRM)

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

EE319K Spring 2016 Exam 1 Solution Page 1. Exam 1. Date: Feb 25, UT EID: Solution Professor (circle): Janapa Reddi, Tiwari, Valvano, Yerraballi

ARM Architecture and Instruction Set

Introduction to Embedded Systems EE319K (Gerstlauer), Spring 2013

CprE 288 Introduction to Embedded Systems ARM Assembly Programming: Translating C Control Statements and Function Calls

Topic 10: Instruction Representation

2) [ 2 marks] Both of the following statements cause the value $0300 to be stored in location $1000, but at different times. Explain the difference.

MNEMONIC OPERATION ADDRESS / OPERAND MODES FLAGS SET WITH S suffix ADC

CprE 288 Introduction to Embedded Systems Course Review for Exam 3. Instructors: Dr. Phillip Jones

18-349: Introduction to Embedded Real- Time Systems Lecture 3: ARM ASM

COMP MIPS instructions 2 Feb. 8, f = g + h i;

ARM ASSEMBLY PROGRAMMING

... Executing conditional instructions

INTRODUCTION TO BRANCHING. There are two forms of unconditional branching in the MC68000.

ECE 571 Advanced Microprocessor-Based Design Lecture 3

Lecture 8: Control Structures. Comparing Values. Flags Set by CMP. Example. What can we compare? CMP Examples

Vorlesung / Course IN2075: Mikroprozessoren / Microprocessors

ECE 471 Embedded Systems Lecture 6

Assembly language Simple, regular instructions building blocks of C, Java & other languages Typically one-to-one mapping to machine language

Assembly language Simple, regular instructions building blocks of C, Java & other languages Typically one-to-one mapping to machine language

ECE 372 Microcontroller Design Assembly Programming. ECE 372 Microcontroller Design Assembly Programming

EEM870 Embedded System and Experiment Lecture 4: ARM Instruction Sets

NET3001. Advanced Assembly

Who am I and what am I doing?

F28HS2 Hardware-Software Interfaces. Lecture 6: ARM Assembly Language 1

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Exam 1. Date: February 23, 2016

ARM Assembly Language

The Assembly Language of the Boz 5

The ARM Cortex-M0 Processor Architecture Part-2

COE538 Lecture Notes Week 3 (Week of Sept 17, 2012)

ECE251: Tuesday September 12

Architecture. Digital Computer Design

Raspberry Pi / ARM Assembly. OrgArch / Fall 2018

ARM Assembly Programming

ARM Instruction Set. Computer Organization and Assembly Languages Yung-Yu Chuang. with slides by Peng-Sheng Chen

Chapter 04: Instruction Sets and the Processor organizations. Lesson 08: Processor Instructions - Part 1

ARM Instruction Set. Introduction. Memory system. ARM programmer model. The ARM processor is easy to program at the

Xuan Guo. Lecture XIV: Review of Chapter 3 & 4. CSC 3210 Computer Organization and Programming Georgia State University. March 5, 2015.

Assembly Language Design

ECE331 Handout 3- ASM Instructions, Address Modes and Directives

Chapter 4 Simple Calculations

Lecture 15 ARM Processor A RISC Architecture

Programming the Motorola MC68HC11 Microcontroller

Basic ARM InstructionS

Assembler: Basics. Alberto Bosio October 20, Univeristé de Montpellier

Embedded assembly is more useful. Embedded assembly places an assembly function inside a C program and can be used with the ARM Cortex M0 processor.

The Motorola 68HC11 Instruc5on Set

Table 1: Mnemonics Operations Dictionary. Add Accumulators Add B to Y. Add with carry to B. Add Memory to B. Add 16-bit to D And B with Memory

EE 3170 Microcontroller Applications

Arithmetic for Computers

ECE/CS 3710 RISC Processor Specification

EE319K (Gerstlauer), Spring 2013, Midterm 1 1. Midterm 1. Date: February 21, 2013

6 THE ETRAX Introduction. Special registers. 6 The ETRAX 4

EE 3170 Microcontroller Applications

Chapter 15. ARM Architecture, Programming and Development Tools

Hi Hsiao-Lung Chan, Ph.D. Dept Electrical Engineering Chang Gung University, Taiwan

CS 310 Embedded Computer Systems CPUS. Seungryoul Maeng

H8/500 Series Programming Manual

Instruction Set Architecture (ISA)

The Original Instruction Pipeline

Introduction to the ARM Processor Using Intel FPGA Toolchain. 1 Introduction. For Quartus Prime 16.1

The ARM Instruction Set Architecture

EECS 427 RISC PROCESSOR

H8/300L Series Programming Manual

3. The Instruction Set

ARM Cortex M3 Instruction Set Architecture. Gary J. Minden March 29, 2016

Transcription:

Unsigned and signed integer numbers Binary Unsigned Signed 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 Subtraction sets C flag opposite of carry (ARM specialty)! - if (carry = 0) then C=1 - if (carry = 1) then C=0 Carry C 0101 5 5 0110 6 6 0111 7 7 1000 8-8 V A overflow B S A B S 1001 9-7 1010 10-6 1011 11-5 1100 12-4 1101 13-3 1110 14-2 1111 15-1 RAB Računalniška arhitektura 1

Unsigned and signed integer numbers Binary Unsigned Signed 1000 8-8 1001 9-7 1010 10-6 1011 11-5 1100 12-4 1101 13-3 V Overflow A B S A B S 1110 14-2 C 1111 15-1 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7 V RAB Računalniška arhitektura 2

Signed/unsigned extension to 32 bits when reading 8 and 16 bit operands from memory they need to be extended by sign or zeros to full length (registers are 32bit long). Only load and store instructions are accessing operands in memory. unsigned operands from memory are extended with zeros: ldrb ldrh 00000000 00000000 00000000 xxxxxxxx xxxxxxxx 00000000 00000000 yyyyyyyy xxxxxxxx xxxxxxxx yyyyyyyy signed operands from memory are extended with sign bit: ldrsb 00000000 00000000 00000000 0xxxxxxx 0xxxxxxx 11111111 11111111 11111111 1xxxxxxx 1xxxxxxx ldrsh 00000000 00000000 0yyyyyyy xxxxxxxx xxxxxxxx 0yyyyyyy 11111111 11111111 1yyyyyyy xxxxxxxx xxxxxxxx 1yyyyyyy RAB Računalniška arhitektura 3

Flags flags are 4 bits in status register CPSR, having values of : 1 flag is SET. 0 flag is NOT SET (unset). 31 28 27 8 7 6 5 4 0 N Z C V unused I F T mode Flags (can) be changed according to results of ALU operations: N = 0: bit 31 of the result is 0, N=1: bit 31 of the result is 1 (Negative) Z = 1: result is equal to 0, Z=0: result is not equal to 0 (Zero) C = 1: result has carry, C = 0: result doesn t have carry (Carry) V = 1: result has overflow, V = 0: result doesn t have overflow(overflow) If we want that ALU instruction changes flags, we have to add s to coresponding instruction!!!: movs r1, #3 @ r1 3 adds r2, r7, #0x20 @ r2 r7 + 32 subs r4, r5,#1 @ r4 r5-1 RAB Računalniška arhitektura 4

Comparisons Compare instructions always change the state of flags (ALU group of instructions): cmp (Compare): sets flags according to result of Rn - Op2 cmp R1, #10 @ R1-10 cmn (Compare negated): sets flags according to result of Rn + Op2 cmn R1, #10 @ R1+10 Instructions only influence state of flags, registers are not changed. Because their only intention is to change the state of flags, we don t have to add letter s to their names. RAB Računalniška arhitektura 5

Comparisons of unsigned numbers Example: Compare two unsigned numbers: - focus on the state of flags C and Z mov r1,#11 cmp r1,#10 @ C=1, Z=0 mov r1,#10 cmp r1,#10 @ C=1, Z=1 mov r1,#9 cmp r1,#10 @ C=0, Z=0 Summary: r1 > 10 C=1 in Z=0 Higher r1 >= 10 C=1 Higher or Same r1 = 10 Z=1 Equal r1 < 10 C=0 Lower r1 <= 10 C=0 ali Z=1 Lower or Same RAB Računalniška arhitektura 6

Comparisons of signed numbers Comparison uses subtraction/addition to compare numbers; the opeation is the same for unsigned and signed numbers. But to properly compare values of signed numbers, we have to watch another set of flags! - focus on the state of flags V, Z and N Example: mov r1,#0 cmp r1,#-1 @ C=0, Z=0, V=0, N=0 Flags don t corespond to relation > for unsigned numbers (C=1 in Z=0)! Condition for > for signed numbers is different from condition > for unsigned numbers. Correct signed number condition for > is N = V. RAB Računalniška arhitektura 7

Conditions Oznaka pogoja Pomen Stanje zastavic, ob katerem se ukaz izvede EQ Equal / equals zero Z set NE Not equal Z clear CS Carry set C set CC Carry clear C clear MI Minus / negative N set PL Plus / positive or zero N clear VS Overflow V set VC No overflow V clear HS Unsigned higher or same C set LO Unsigned lower C clear HI Unsigned higher C set and Z clear LS Unsigned lower or same C clear or Z set GE Signed greater than or equal N equals V LT Signed less than N is not equal to V GT Signed greater than Z clear and N equals V LE Signed less than or equal Z set or N is not equal to V Name Condition State of flags RAB Računalniška arhitektura 8

Branch instructions Sbranch is a of GOTO label instruction usually target line is denoted by label. In this case, address of the instruction with label is moved to PC. b (Branch) loop: sub r1, r1, #1 b loop @ GOTO loop This is eternal loop. r1 will be continually decremented, after value of 0, its value will change to 0xffffffff. If we want only finite number of loop repetitions (until r1 is non-zero), we need instruction of type IF cond THEN GOTO label. We need conditional branch instruction. RAB Računalniška arhitektura 9

Conditional branch instructions In ARM assembler condition is always determined with the state of flags (remember previous table with all possible conditions). Before using conditional branch instruction, we need to set the state of flags. This is commonly done with cmp instruction, or quite often also with ALU instructions. Loop, that ends when r1 reaches value of 0 could be implemented in following way: b (Branch) loop: sub r1, r1, #1 cmp r1, #0 bne loop @ IF Z=0 THEN GOTO loop Instruction b is combined with the proper condition name that determines, if the branch will take place. If condition is not met, branch will not be executed next instruction that follows will be next. Therefore Bxx instruction is denoted as Conditional branch. RAB Računalniška arhitektura 10

Conditional branch instructions Instruction cmp in previous example has set the Z flag for conditional branch instruction. We can set same flag already earlier when decrementing value of r1. We just need to add letter s to subtraction instruction (subs): b (Branch) loop: mov r1, #10 subs r1, r1, #1 @ sets flags accroding to r1! bne zanka @ IF Z=0 THEN GOTO loop mov r2, #10 Loop will repeat 10 times. When r1 reaches 0, subs will set flag Z to 1 and conditional branch will not take place anymore. Next instruction mov r2, #10 will be executed. Pseudo code for conditional branch is: IF condition THEN PC label ELSE PC PC+4 RAB Računalniška arhitektura 11

Conditional branch instructions Branch Interpretation Normal uses B Unconditional Always take this branch BEQ Equal Comparison equal or zero result BNE Not equal Comparison not equal or non-zero result BPL Plus Result positive or zero BMI Minus Result minus or negative BCC BLO Carry clear Lower Arithmetic operation did not give carry-out Unsigned comparison gave lower BCS BHS Carry set Higher or same Arithmetic operation gave carry-out Unsigned comparison gave higher or same BVC Overflow clear Signed integer operation; no overflow occurred BVS Overflow set Signed integer operation; overflow occurred BGT Greater than Signed integer comparison gave greater than BGE Greater or equal Signed integer comparison gave greater or equal BLT Less than Signed integer comparison gave less than BLE Less or equal Signed integer comparison gave less than or equal BHI Higher Unsigned comparison gave higher BLS Lower or same Unsigned comparison gave lower or same RAB Računalniška arhitektura 12

Conditional execution of instructions Conditional branch instructions are just specific case of common feature of conditional execution that can be applied to all ARM assembler instructions. If condition is appended to instruction, it will only be executed, if condition is true. cmp r0, #5 beq DONT add r1,r1,r0 sub r1,r1,r2 DONT.. if (r1<10) then r4=r1+5 else r4=r1+8 cmp r0, #5 addne r1,r1,r0 subne r1,r1,r2 cmp r1, 10 blo LOWER add r4,r1,#8 b CONT LOWER add r4,r1,#5 CONT... cmp r1,#10 addlo r4,r1,#5 addhs r4,r1,#8 RAB Računalniška arhitektura 13

Conditional execution of instructions if ((r0==r1) AND (r2==r3)) then r4=r4+1 cmp r0,r1 ; postavi Z, ce je r0=r1 cmpeq r2,r3 ; primerjaj le, ce je Z=1 in ; spet postavi Z, ce je r2=r3 addeq r4,r4,#1 ; sestej le, ce je Z=1 (r0=r1 in r2=r3) majority of if-then-else clauses can be implemented by conditional execution! if-then-else clauses with complex conditions (composed with AND or OR) can be implemented by conditional execution. use of conditional execution is usually more efficient for shorter sequences of instructions than using separate branches! Conditional execution takes at least 1 clock cycle regardless of condition being true or false! RAB Računalniška arhitektura 14