STEVEN R. BAGLEY ARM: LOOPS AND ADDRESSING

Similar documents
Control Flow and Loops. Steven R. Bagley

Writing ARM Assembly. Steven R. Bagley

STEVEN R. BAGLEY ARM: PROCESSING DATA

Writing ARM Assembly. Steven R. Bagley

Photo David Wright STEVEN R. BAGLEY PIPELINES AND ILP

What are the differences between these three ARM instructions: (3 marks) ADD R1, R2, R3 ADDS R1, R2, R3 and ADDEQ R1, R2, R3

Topic Notes: MIPS Instruction Set Architecture

Control Flow Instructions

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

ARM Assembly Language. Programming

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

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

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.

ARM Cortex-M4 Programming Model Flow Control Instructions

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

Quick Review. lw $t0, 4($a0) Registers x Memory. $a0 is simply another name for register 4 $t0 is another name for register (green sheet)

Assembly Language Programming

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

Computer Architecture

Chapter 3. Instructions:

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Instructions: Language of the Computer

Support for high-level languages

MIPS Memory Access Instructions

University of California, San Diego CSE 30 Computer Organization and Systems Programming Winter 2014 Midterm Dr. Diba Mirza

May Saeid Nooshabadi. Overview ELEC2041. Compiler Assembler Linker Loader Example. Microprocessors and Interfacing

CS 61C: Great Ideas in Computer Architecture MIPS Instruction Formats

EE 3170 Microcontroller Applications

Processor Status Register(PSR)

CS1102: Macros and Recursion

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

Compilers and Interpreters

September, Saeid Nooshabadi. Overview COMP Compiler Assembler Linker Loader Example

Lab 3 (All Sections) Prelab: MIPS-Control Instructions

Topic 10: Instruction Representation

Chapter 2. Instructions:

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

Unsigned and signed integer numbers

STEVEN R. BAGLEY THE ASSEMBLER

ECE 571 Advanced Microprocessor-Based Design Lecture 7

Thomas Polzer Institut für Technische Informatik

The Assembly Language of the Boz 5

Lab 3 (Sections 300, 301 and 302) Prelab: MIPS-Control Instructions

Instructions: Assembly Language

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

ARM Assembler Workbook. CS160 Computer Organization Version 1.1 October 27 th, 2002 Revised Fall 2005

ARM Assembly Programming

Principles of Computer Science

Transfer of Control. Lecture 10 JMP. JMP Formats. Jump Loop Homework 3 Outputting prompts Reading single characters

Computer Systems Architecture

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

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

Lecture 9: Disassembly

sarm User Guide Note that a space must appear before the operation field because any word beginning in the first column is a label

Computer Science 61C Spring Friedland and Weaver. Instruction Encoding

CMPSCI 201 Fall 2005 Midterm #2 Solution

ARM Assembly Programming

CMPSCI 201 Fall 2004 Midterm #1 Answers

University of California at Santa Barbara. ECE 154A Introduction to Computer Architecture. Quiz #1. October 30 th, Name (Last, First)

Developing StrongARM/Linux shellcode

UCB CS61C : Machine Structures

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

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

CE1921: COMPUTER ARCHITECTURE SINGLE CYCLE PROCESSOR DESIGN WEEK 2

ARM Assembly Programming

This has already been done for you within the School of CS at Nottingham.

Comparison InstruCtions

ECE 571 Advanced Microprocessor-Based Design Lecture 8

We will study the MIPS assembly language as an exemplar of the concept.

Advanced Assembly, Branching, and Monitor Utilities

Slide Set 1 (corrected)

Architecture II. Computer Systems Laboratory Sungkyunkwan University

CSE A215 Assembly Language Programming for Engineers

Chapter 2. Instruction Set Architecture (ISA)

Procedures, Parameters, Values and Variables. Steven R. Bagley

Lecture 6 Decision + Shift + I/O

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

COMP 303 MIPS Processor Design Project 3: Simple Execution Loop

DECISION CONTROL AND LOOPING STATEMENTS

the SAP-2 I. Intro cmpt-150-arc Sections 8-8, 8-9, 9-4, 9-5, 9.6, We ll do this in bits and pieces, doing the beginning of each section first.

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

CS 107e Lecture 4: From ASM to C Part 1 Friday, October 5, 2018

Instruction Set Architecture. "Speaking with the computer"

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

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

Decision Making and Loops

Control MIPS unconditional branch instruction (i.e., jump)

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

Real instruction set architectures. Part 2: a representative sample

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

Control Structures in Java if-else and switch

ECE 571 Advanced Microprocessor-Based Design Lecture 9

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

CSCI 2321 (Computer Design), Spring 2018 Homework 3

Today s lecture. Translating an if-then statement

Transcription:

STEVEN R. BAGLEY ARM: LOOPS AND ADDRESSING

FROM C TO ASSEMBLER Plan your assembler programs in C first Much easier to get the logic right in C using ifs, whiles etc. Can then convert those high-level structures into machine code Already seen how to convert if statements At least to start with Difference between the logic behind a program and the syntax of the language

IF IN ASSEMBLER Do a CMP to test for the condition Branch if the condition is not met to skip over the code inside the if block Code is then executed only if the condition is met Otherwise we skip over it

if EXAMPLES C ASSEMBLER

if EXAMPLES if(r0 == 10) { C ASSEMBLER CMP R0, #10 BNE skip skip

if EXAMPLES if(r0 == 10) { if(r0 < R1) { C ASSEMBLER CMP R0, #10 BNE skip skip CMP R0, R1 BGE skip skip

if EXAMPLES if(r0 == 10) { if(r0 < R1) { if(r0 >= 10) { C ASSEMBLER CMP R0, #10 BNE skip skip CMP R0, R1 BGE skip skip CMP R0, #10 BLT skip skip

if EXAMPLES if(r0 == 10) { if(r0 < R1) { if(r0 >= 10) { if(r0!= 0) { C ASSEMBLER CMP R0, #10 BNE skip skip CMP R0, R1 BGE skip skip CMP R0, #10 BLT skip skip CMP R0, #10 BEQ skip skip

B main menu coke DEFB G51SYS Vending Machine \n\0 DEFB Have a bottle of Coke\n\0 ALIGN main SWI 1 ; Read a character CMP R0, #49 BNE skip ADR R0, coke SWI 3 skip SWI 2 Beginning of the Coke vending machine from G51PRG label name for skip unimportant but it needs to be unique

B main menu coke DEFB G51SYS Vending Machine \n\0 DEFB Have a bottle of Coke\n\0 ALIGN main SWI 1 ; Read a character CMP R0, #49 BNE skip ADR R0, coke SWI 3 skip SWI 2 Beginning of the Coke vending machine from G51PRG label name for skip unimportant but it needs to be unique

B main menu coke DEFB G51SYS Vending Machine \n\0 DEFB Have a bottle of Coke\n\0 ALIGN main SWI 1 ; Read a character CMP R0, #49 BNE skip ADR R0, coke SWI 3 skip SWI 2 Beginning of the Coke vending machine from G51PRG label name for skip unimportant but it needs to be unique

OR ELSE How do we handle the else clause? Already doing it If it s not true we are branching to skip Can put the else section there But the code that is executed when the condition is true will also get executed

OR ELSE But we can easily get around that Put a branch at the end of the true block to skip the else section This would be an unconditional branch

B main menu coke error DEFB G51SYS Vending Machine \n\0 DEFB Have a bottle of Coke\n\0 DEFB Incorrect option\n\0 ALIGN main SWI 1 ; Read a character CMP R0, #49 BNE skip ADR R0, coke SWI 3 B end skip ADR R0, error SWI 3 end SWI 2

B main menu coke error DEFB G51SYS Vending Machine \n\0 DEFB Have a bottle of Coke\n\0 DEFB Incorrect option\n\0 ALIGN main SWI 1 ; Read a character CMP R0, #49 BNE skip ADR R0, coke SWI 3 B end skip ADR R0, error SWI 3 end SWI 2

B main menu coke error DEFB G51SYS Vending Machine \n\0 DEFB Have a bottle of Coke\n\0 DEFB Incorrect option\n\0 ALIGN main SWI 1 ; Read a character CMP R0, #49 BNE skip ADR R0, coke SWI 3 B end skip ADR R0, error SWI 3 end SWI 2

COMBINED CONDITIONALS Should be possible to see how we can do combined conditionals too Just use multiple CMP/Bxx pairs Can also do lazy evaluation as with C But need to watch our branch conditions

AND CONDITIONALS if(r0 == 1 && R1 == 2) { printf("have \n ); CMP R0, #1 BNE skip CMP R1, #2 BNE skip ADR R0, mesg SWI 3 skip SWI 2 Could optimise this a lot though Highlighted code is only executed when r0 == 1 and r1 == 2 Note how both branches use the same condition code If it s ever false skip it

AND CONDITIONALS if(r0 == 1 && R1 == 2) { printf("have \n ); CMP R0, #1 BNE skip CMP R1, #2 BNE skip ADR R0, mesg SWI 3 skip SWI 2 Could optimise this a lot though Highlighted code is only executed when r0 == 1 and r1 == 2 Note how both branches use the same condition code If it s ever false skip it

AND CONDITIONALS if(r0 == 1 && R1 == 2) { printf("have \n ); CMP R0, #1 BNE skip CMP R1, #2 BNE skip ADR R0, mesg SWI 3 skip SWI 2 Could optimise this a lot though Highlighted code is only executed when r0 == 1 and r1 == 2 Note how both branches use the same condition code If it s ever false skip it

AND CONDITIONALS if(r0 == 1 && R1 == 2) { printf("have \n ); CMP R0, #1 BNE skip CMP R1, #2 BNE skip ADR R0, mesg SWI 3 skip SWI 2 Could optimise this a lot though Highlighted code is only executed when r0 == 1 and r1 == 2 Note how both branches use the same condition code If it s ever false skip it

OR COMBINATION if(r0 == 1 R1 == 2) { printf("have \n ); CMP R0, #1 BEQ code CMP R1, #2 BNE skip code ADR R0, mesg SWI 3 skip SWI 2 Could optimise this a lot though Highlighted code is only executed when r0 == 1 or r1 == 2 Note how the first branch branches if equal, the second if not equal Again it s lazy evaluation if the first test is true, then we don t need to do the second test so can just jump into the code

OR COMBINATION if(r0 == 1 R1 == 2) { printf("have \n ); CMP R0, #1 BEQ code CMP R1, #2 BNE skip code ADR R0, mesg SWI 3 skip SWI 2 Could optimise this a lot though Highlighted code is only executed when r0 == 1 or r1 == 2 Note how the first branch branches if equal, the second if not equal Again it s lazy evaluation if the first test is true, then we don t need to do the second test so can just jump into the code

OR COMBINATION if(r0 == 1 R1 == 2) { printf("have \n ); CMP R0, #1 BEQ code CMP R1, #2 BNE skip code ADR R0, mesg SWI 3 skip SWI 2 Could optimise this a lot though Highlighted code is only executed when r0 == 1 or r1 == 2 Note how the first branch branches if equal, the second if not equal Again it s lazy evaluation if the first test is true, then we don t need to do the second test so can just jump into the code

OR COMBINATION if(r0 == 1 R1 == 2) { printf("have \n ); CMP R0, #1 BEQ code CMP R1, #2 BNE skip code ADR R0, mesg SWI 3 skip SWI 2 Could optimise this a lot though Highlighted code is only executed when r0 == 1 or r1 == 2 Note how the first branch branches if equal, the second if not equal Again it s lazy evaluation if the first test is true, then we don t need to do the second test so can just jump into the code

LOOPS Important building blocks in programs while loops repeat a block of code whilst the condition holds But again, like if, ARM assembler does not provide a while loop Need to manufacture it out of simple components In ARM this is the B instruction In C, there is an equivalent in the goto instruction

LOOPS USING GOTO We can translate our while loop to using gotos Bad programming practice normally But a good halfway stage between structured programming and assembler Remember though that a while loop can execute zero or more times Run through an example in C Show that they both run and produce the same result.

while LOOP while(i < 8) { j = j + 3; i = i + 1; Note how we ve turned the structure of the while loop upside down We ve put the test at the end There s reasons for this madness Can then convert this very easily into machine code ASM version assumes i is in R1 and j is in R0

while LOOP while(i < 8) { j = j + 3; i = i + 1; goto while_cond; while_loop: j = j + 3; i = i + 1; while_cond: if(i < 8) goto while_loop; Note how we ve turned the structure of the while loop upside down We ve put the test at the end There s reasons for this madness Can then convert this very easily into machine code ASM version assumes i is in R1 and j is in R0

while LOOP goto while_cond; while_loop: j = j + 3; i = i + 1; while_cond: if(i < 8) goto while_loop; B while_cond while_loop ADD R0, R0, #3 ADD R1, R1, #1 while_cond CMP R1, #8 BLT while_loop Note how we ve turned the structure of the while loop upside down We ve put the test at the end There s reasons for this madness Can then convert this very easily into machine code ASM version assumes i is in R1 and j is in R0

WHILE LOOP Putting the conditional at the end has several advantages Don t have to invert the conditional (as we did with if) Will actually execute less instructions Code for a do while() loop is near identical

WHILE LOOP B while_cond while_loop ADD R0, R0, #3 ADD R1, R1, #1 while_cond CMP R1, #8 BLT while_loop while_cond CMP R1, #8 BGE end ADD R0, R0, #3 ADD R1, R1, #1 B while_cond end: ASM version assumes i is in R1 and j is in R0 Uses 4 instructions per loop iteration + 3 to start it off (35) second method takes 5 instructions per loop +2 extra (42) Take a look and see what the compiler does too

WORKED EXAMPLE Going to work through an example bit of code now To calculate the Highest Common Factor (HCF) Use Euclid s algorithm

EUCLID S HCF ALGORITHM Euclid was a famous Greek mathematician (~300BC) Devised a simple algorithm for finding the Highest Common Factor of two integers while(a!= b) { if(a > b) a = a - b; else b = b - a; Let s convert this into assembler

LOOPS Firstly, we need to remove the while loop Don t have such luxuries in assembly Only have branches, the equivalent of C s goto Rewrite our C program to use ifs and goto instead Eek GOTO alert Go and do this, show the program works

REGISTERS Now we can start assigning the variables to registers Small number here, so we ll use R0 to be equivalent to a, and R1 to be equivalent to b Actual registers used doesn t matter (more or less) If you run out, you can always store the value in memory and the load it back into a register later Okay now start developing an ARM version alongside the C version split screen then test in Komodo

FOR LOOPS Again, we can model a for loop in assembler Already seen how to do this in G51PGA Saw that there is an equivalent while loop for every for loop So when we want to use a for loop we can convert it to a while loop Then convert the while loop to assembler as before

for(expr1; expr2; expr3) statement; expr1; while(expr2) { statement; expr3;

for(expr1; expr2; expr3) statement; expr1; while(expr2) { statement; expr3; These are equivalent

for LOOP for(i = 0; i < 10; i++) { printf( %d\n, i); Note how we ve turned the structure of the while loop upside down We ve put the test at the end There s reasons for this madness Can then convert this very easily into machine code ASM version assumes i is in R1 and j is in R0

for LOOP for(i = 0; i < 10; i++) { printf( %d\n, i); i = 0; while(i < 10) { printf( %d\n, i); i++; Note how we ve turned the structure of the while loop upside down We ve put the test at the end There s reasons for this madness Can then convert this very easily into machine code ASM version assumes i is in R1 and j is in R0

for LOOP i = 0; while(i < 10) { printf( %d\n, i); i++; Note how we ve turned the structure of the while loop upside down We ve put the test at the end There s reasons for this madness Can then convert this very easily into machine code ASM version assumes i is in R1 and j is in R0

for LOOP i = 0; while(i < 10) { printf( %d\n, i); i++; i = 0; goto while_cond; while_loop: printf( %d\n, i); i++; while_cond: if(i < 10) goto while_loop; Note how we ve turned the structure of the while loop upside down We ve put the test at the end There s reasons for this madness Can then convert this very easily into machine code ASM version assumes i is in R1 and j is in R0

ADDRESSING Already seen how we can load and store values from memory LDR R0, _a STR R1, _b But this can only load from a fixed address, and the same address every time Okay for global variables, wouldn t work for an array This is known as Absolute Addressing Or at least it would be on most systems

ADDRESSING ON ARM Like most CPUs, ARM provides support for various addressing modes for accessing memory At their heart, they are all just mechanisms for automatically calculating the address to be accessed But provide very convenient mechanisms to handle the calculation Unlike most other CPUs, ARM doesn t actually have an Absolute Addressing mode

HANDLING LARGE ADDRESSES ARM encodes all instructions in one 32-bit word This encodes the instruction and also what it operates on (registers, immediate values, etc) Different to a CISC chip which will use multiple words to encode the data Much faster to hide numeric constants and data addresses in the instruction themselves

ADDRESSING ON ARM But the addresses are 32-bits wide How does ARM hide them inside the the 32-bit instruction when there are only 8- or 12-bits spare? Uses a scheme that generates a useful subset of all the 32-bit values Already seen this with immediate values in MOV instructions amongst others

IMMEDIATE VALUE Encoded as an 8-bit value (0-255), and a 4-bit rotation value Rotation shifts the bits around the 32-bits of the register If value cannot be expressed using the above, we can always just load it from memory

PSEUDO-INSTRUCTION Assembler recognises a pseudo-instruction which will automatically select whether to use a MOV or to load from memory LDR R1, =0x12345678 Gets compiled to either a MOV instruction (if it will fit), or loads it from memory using LDR Assembler designates part of the program a literal pool to store the constants Literal pool must be within ±4KB of the PC But assembler handles it (go demo) Pseudo-instruction because it doesn t exist assembler generates a real instruction based on the program s context

USING THE PC TO HELP Rotation isn t the only useful trick in generating a large constant (often intended as an address) Time to think about ADR in more detail Again, ADR is not an ARM instruction, but an assembler pseudo-instruction Assumes that the address to be loaded into a register is near the PC Look at ADR, then see how a similar trick is used by LDR/STR

ADR INSTRUCTION If address is in range, then destination register can be set using a singleinstruction by adding/subtracting an offset to the value in PC Offset must be expressible in 8-bits with rotation So ADR R0,one might become SUB R0, PC, #&18 or ADD R0, PC, #&80 Depends on the relationship between the label and the instruction s location

ADRL Not all address can be generated by this approach The offset must be expressible as a rotation of 8-bits Also have an ADRL pseudo-instruction This will use two (or more) instructions to generate the address Again usually ADD or SUB instructions Given an example use origin to demo it

ADRL IN AASM With aasm, ADRL will only generate a single instruction if possible Otherwise it may need two or more instructions Other assemblers may always generate two instructions The combination of PC-relative addressing with rotated constants is very powerful Can be important when writing code to know exactly how many instructions are executed

ORIGIN 0x0100 alpha DEFW 20 ORIGIN 0x1000 beta DEFW 30 ORIGIN 0x1010 ENTRY ADRL R0, alpha ADRL R0, beta ADRL R0, gamma 00001010 E24F0FC6 SUB R0 PC, #&318 00001014 E2400B03 SUB R0, R0, #&C00 00001018 E24F0020 SUB R0, PC, #&20 0000101C E28F0EED ADD R0, PC, #&ED0 00001020 E2800A02 ADD R0, R0, #&2000 ORIGIN 0x3EF4 gamma DEFW 40 Various examples of ADRL in action in aasm

POSITION INDEPENDENCE One other advantage of writing code like this is it is position-independent That is, providing all the access to addresses are relative to the PC then it doesn t matter where it is loaded in memory As soon as you hard-code an address (e.g. in a variable, or literal pool) then this breaks Doesn t mean the code cannot be loaded anywhere but the thing loading it will need to relocate it to the new memory address