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

Similar documents
An Introduction to Assembly Programming with the ARM 32-bit Processor Family

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

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

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

The Assembly Language of the Boz 5

Comparison InstruCtions

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

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

STEVEN R. BAGLEY ARM: PROCESSING DATA

Cortex M3 Programming

ARM Cortex-A9 ARM v7-a. A programmer s perspective Part 2

Processor Status Register(PSR)

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

ARM Assembly Language. Programming

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

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

Exam 1. Date: Oct 4, 2018

ECE 471 Embedded Systems Lecture 5

Writing ARM Assembly. Steven R. Bagley

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

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

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

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

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

Course Administration

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

Chapter 15. ARM Architecture, Programming and Development Tools

Computer Architecture

Control Flow Instructions

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

Unsigned Binary Integers

Unsigned Binary Integers

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

Architecture. Digital Computer Design

MIPS Memory Access Instructions

EEM870 Embedded System and Experiment Lecture 4: ARM Instruction Sets

ECE 498 Linux Assembly Language Lecture 5

Assembly Language Programming

EE251: Tuesday September 5

CS3350B Computer Architecture MIPS Instruction Representation

ARM-7 ADDRESSING MODES INSTRUCTION SET

The PAW Architecture Reference Manual

Chapter 15. ARM Architecture, Programming and Development Tools

Chapter 2. Instructions: Language of the Computer

Basic ARM InstructionS

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

Flow Control In Assembly

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

TSK3000A - Generic Instructions

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

ARM Assembly Programming

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

F28HS Hardware-Software Interface. Lecture 10: ARM Assembly Language 5

MIPS Instruction Format

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

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

Systems Architecture The ARM Processor

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

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

Introduction to the ARM Processor Using Altera Toolchain. 1 Introduction. For Quartus II 14.0

ARM Cortex-M4 Programming Model Logical and Shift Instructions

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

ARM Cortex-M4 Programming Model Flow Control Instructions

Part 1 (70% of grade for homework 2): MIPS Programming: Simulating a simple computer

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

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

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

Problem Sheet 1: ANSWERS (NOT ) (NOT ) 3. 37, ,

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

Introduction to Embedded Systems EE319K (Gerstlauer), Spring 2013

Embedded Systems Ch 12B ARM Assembly Language

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

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

Lecture 4 (part 2): Data Transfer Instructions

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

CMPSCI 201 Fall 2004 Midterm #2 Answers

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

Instruction Set Architecture (ISA)

CS 351 Exam 2 Wed. 4/5/2017

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

ARM Instruction Set 1

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

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

Topic 10: Instruction Representation

ECE 471 Embedded Systems Lecture 6

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Overview COMP Microprocessors and Embedded Systems. Lecture 11: Memory Access - I. August, 2003

Chapters 3. ARM Assembly. Embedded Systems with ARM Cortext-M. Updated: Wednesday, February 7, 2018

ISA: The Hardware Software Interface

CS 351 Exam 2, Section 1 Wed. 11/2/2016

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.

A block of memory (FlashROM) starts at address 0x and it is 256 KB long. What is the last address in the block?

Instructions: Language of the Computer

The ARM Instruction Set

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

ECE251: Tuesday Aug. 29

The ARM Cortex-M0 Processor Architecture Part-2

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

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

Transcription:

Assembler: Basics Alberto Bosio bosio@lirmm.fr Univeristé de Montpellier October 20, 2017

Assembler Program Template. t e x t / S t a r t o f the program code s e c t i o n /.data / V a r i a b l e s / x :. b y t e 0. g l o b a l main / d e c l a r e s the main i d e n t i f i e r / main : / Address o f the main f u n c t i o n / / Program code would go h e r e / bx l r / Return to the c a l l e r /. e n d / End o f the program / 2

Assembler Program Template The.text directive indicates the start of a section of the assembly program that contains code. The.data directive indicates the start of a section of the assembly program that contains global variables. The.global main line specifies that main is a globally visible identifier, which can be referred to from outside this file. This is necessary, because main is the program entry point, and the corresponding function will be invoked from outside the program! main: bx lr is the first (and only) instruction of this program. The.end directive specifies the end of the program. 3

Assembly Language Instructions can be divided in three different sets: Arithmetic-Logic instructions perform mathematical operations on data: these can be arithmetic (sum, subtraction, multiplication), logic (boolean operations), or relational (comparison of two values). Branch instructions change the control flow, by modifying the value of the Program Counter (R15). They are needed to implement conditional statements, loops, and function calls. Load/Store instructions move data to and from the main memory. Since all other operations only work with immediate constant values (encoded in the instruction itself) or with values from the registers, the load/store instructions are necessary to deal with all but the smallest data sets. 4

Arithmetic-Logic Instructions Syntax ADD Rd Rn Rm/#imm SUB Rd Rn Rm/#imm MUL Rd Rn Rm AND Rd Rn Rm/#imm EOR Rd Rn Rm/#imm ORR Rd Rn Rm/#imm MVN Rd Rm/#imm CMP Rn Rm/#imm MOV Rd Rm/#imm Semantics Rd = Rn + Rm/#imm Rd = Rn - Rm/#imm Rd = (Rn * Rm/#imm)%2 32 (i.e., truncated at 32 bits) Rd = Rn & Rm/#imm (bitwise and) Rd = Rn ^Rm/#imm (bitwise exclusive or) Rd = Rn Rm/#imm (bitwise or) Rd = Rm/#imm (bitwise negation) Status <- comparison (<, >, ==) of Rn and Rm /#imm Rd = Rm/#imm where Rd, Rn and Rm are any three registers, and #imm are 8-bit immediates 5

Arithmetic-Logic Instructions: Example C program i n t i =0, j =2,k=4; i=k j ; / B i t w i s e or / ASM program mov r4 #0 mov r5 #2 mov r6 #4 o r r r4 r5 r6 / i=k j ; / 6

Branch Instructions Syntax B label BEQ label BNE label BGT label BGE label BLT label BLE label BL label BX Rd Semantics jump to label (unconditional) j,jump to label if previously compared values were equal jump to label if previously compared values were different jump to label if previously compared Rn >Rm/#imm jump to label if previously compared Rn >= Rm/#imm jump to label if previously compared Rn <Rm/#imm jump to label if previously compared Rn <= Rm/#imm function call (label is the function name/entry point) return from function (always as BX lr) 7

Branch Instructions B label is the basic branch. Control is changed to run the instruction labeled with label next. BL label performs a function call, which means it jumps as in B label, but also saves the value of the Program Counter in the Link Register Return from function is performed by BX LR. It is almost the same as MOV PC LR, but the PC should not be manipulated explicitly by programmer, unless there is a very good reason to do so. 8

Translating Conditional Statements C program i n t x, y ;... i f ( x >0) { y =1; } e l s e { y =2; } return y ; Assuming x and y are stored in R4 and R5 respectively 9

Translating Conditional Statements ASM program e l s e : end : cmp r4,#0 b l e e l s e mov r5, #1 b end mov r5, #2 mov r0, r5 bx l r 10

Translating While and Do-While Statements C program i n t x, y ;... y =1; while ( x >0) { y =x ; x ; } return y ; 11

Translating While and Do-While Statements ASM program loop : end : mov r5 #1 cmp r4 #0 b l e end mul r6, r5, r4 mov r5, r6 sub r4, r4, #1 b loop mov r0, bx l r r5 12

Translating While and Do-While Statements C program i n t x, y ;... y =1; do { y =x ; x ; } while ( x >0); return y ; 13

Translating While and Do-While Statements ASM program loop : end : mov r5 #1 mul r6, r5, r4 mov r5, r6 sub r4, r4, #1 cmp r4 #0 bgt loop mov r0, bx l r r5 14

Translating While and Do-While Statements What about for loop? i n t x, y ;... f o r ( y =1; x >0; x ) y =x ; return y ; 15

Load/Store Instructions LDR and STR: op{type} Rt, [Rn {, #offset}] ; immediate offset op{type} Rt, [Rn, Rm {, LSL #n}] ; register offset Where: op is either LDR (load register) or STR (store register) type is one of the following: B: Unsigned byte, zero extends to 32 bits on loads SB: Signed byte, sign extends to 32 bits (LDR only) H: Unsigned halfword, zero extends to 32 bits on loads SH: Signed halfword, sign extends to 32 bits (LDR only) Rt is the register to load or store Rn is the register on which the memory address is based offset is an offset from Rn. If offset is omitted, the address is the contents of Rn Rtm is a register containing a value to be used as the offset LSL #n is an optional shift, with n in the range 0 to 3 16

Global Variables.data Directives.int,.byte,.string respectively reserve memory for a 32-bit integer, 8-bit integer and a a zero-terminated string (the size of the string is one byte per character, plus one for the terminating zero).data x :. i n t 42 s :. s t r i n g " h e l l o! " b :. b y t e h, e, l, l, o,!,0. t e x t l d r r0, =s l d r r1, =b 17

Global Variables Exercice Write a program able to calculate the string length. 18

Global Variables Array. equ N, 10 / c o n t s t a n t N /. data v e c t o r :. space N, 0 x :. i n t 0, 0, 0, 0... 19

Global Variables Bidimensional Array (Matrix) 5 Row _ 0 0 1 2 3 4 1 5 6 7 8 9 3 2 1 0 11 12 13 1 4 _ _ Column 0 1 2 3 4 20

Global Variables Bidimensional Array (Matrix). equ Row, 3. equ Column, 5. equ N, Row Column. data m a t r i x :. space N, 0... m a t r i x [ i ] [ j ] = a r r a y [ i Column + j ] 21

Global Variables Exercice Write a program able to visit the matrix diagonal (NxN). 22