Assembly Language Programming Introduction to ARM Zbigniew - - PowerPoint PPT Presentation

assembly language programming introduction to arm
SMART_READER_LITE
LIVE PREVIEW

Assembly Language Programming Introduction to ARM Zbigniew - - PowerPoint PPT Presentation

Assembly Language Programming Introduction to ARM Zbigniew Jurkiewicz, Instytut Informatyki UW December 19, 2017 Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM Historia ARM Ltd. company has been


slide-1
SLIDE 1

Assembly Language Programming Introduction to ARM

Zbigniew Jurkiewicz, Instytut Informatyki UW December 19, 2017

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-2
SLIDE 2

Historia

ARM Ltd. company has been created in 1990 as Advanced RISC Machines Ltd., joint venture of Acorn Computers, Apple Computer and VLSI Technology. The assets of company is intellectual property, is is said that it is “fabless” (from fabricate): does not produce chips by itself. The company concentrates on designing the processors from the ARM family. The designs are licenced to partner companies, which produce chips and sell them. Due to this schema the company is able to control the architecture of ARM.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-3
SLIDE 3

History

First ARM processor was designed in 1983 in English company Acorn, for the processor of the same name. None of the existing at that time 16-bit processors satisfied their requierements, so they designed own customized 32-bit processors. many sources say it was the first commercial RISC.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-4
SLIDE 4

History

Other companies became interested in this processor. Apple needed a processor for the project PDA (later renamed Newton MessagePad). This caused a split of processor group from Acorn company and creattion of Advanced RISC Machines Ltd.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-5
SLIDE 5

ARM

Market of (cheap) embedded processors 2 mld of processors in 2005 Cell phones, modems, brake control in cars The whole family of processors — designs customized to various needs. More than 50 supported operating systems. ARM core if often a part of larger circuit, sold by a final producer.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-6
SLIDE 6

Architecture

Now there exist 8 basic versions of architecture. Each versions is based on the previous one (but note the large change between Classic and Cortex ARM). Architecture version number is completely different from model numbers, for example the models of ARM9 can implement architecture version ARMv4 or ARMv5.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-7
SLIDE 7

Classic architecture

Mostly 32-bit processor, but Two (basic) sets of instructions: ARM (32 bits) and Thumb (16 bits) Owing to this the memory consumption could be less in necessary Usually no arithmetic division, simulated by code In base versions to floating-point numbers, simulated as above Extensions for processing signals (DSP — Digital Signal Processing) Efficient handling of exceptions.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-8
SLIDE 8

RISC

RISC architecture (Reduced Instruction Set Computer) Simple instructions of the same size, less classes of instructions The same execution time: one processor cycle = one instruction Pipeline processing, many registers All registers are universal, load-store architecture Complexity in compiler, not in processor Direct realization without microprogramming Complicated operations (e.g. division) often implemented in software

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-9
SLIDE 9

ARM architecture

Based on RISC concepts, but slightly different priorities Small power consumption, because usually supplied from battery. Dense instruction packing, because of limited memory. Small space taken by processor in single-chip circuits gives more place for other elements. Restrictions on costs.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-10
SLIDE 10

ARM architecture

Differences as compared to “pure” RISC Variable execution time for single instructions, e.g load-store-multiple, which loads a sequence of memory cells to consecutive registers. Barrel-shifter for one of input registers — additional increase in instruction execution time. Alternative 16-bit instruction set Thumb Conditionally executed instructions Extended instructions for DSP (e.g. 16x16 bits multiplication and saturation)

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-11
SLIDE 11

ARM architecture

Buses with AMBA protocol (Advanced Microcontroller Bus Architecture) All devices are memory-mapped

access to device registers same as to memory cells additional instructions unnecessary

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-12
SLIDE 12

ISA

32-bit registers Computational instructions: 2 source registers + destination register 7 working modes, normal one is called user.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-13
SLIDE 13

Registers

For modes user and system 16 active universal registers r0-r15 and processor state register cpsr. Some register have special purpose and additional names

r13 (sp) — stack pointer r14 (lr) — link register: to save return address when calling procedures r15 (pc) — program counter

There are instructions, which treat registers r14 and r15 in special ways.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-14
SLIDE 14

Processor state register

Actually there are two processor state registers cpsr — current program status register spsr — saved program status register: accessible only in priviledged modes. Fields: 4 flags for operation results: N, Z, C, V Additional flags in some models

J (Jazelle) to execute 8-bit Java instruction set Q for saturation in DSP extensions

Processor work mode Two interrupt masks I and F (ordinary nad fast interrupts) T: selection of Thumb instruction set

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-15
SLIDE 15

Processor work modes

user (usr): unpriviledged system (sys): like user, but arbitrary changing of status register is allowed abort (abt): after incorrect reference to memory undefined (und): after encountering undefined or unimplemented instructions fast interrupt request (fiq) and interrupt request (irq): for two interrupt levels supervisor (svc): initial state, used also for operating system kernel and after reset. Additional private register banks for priviledged modes, they replace registers r13 nad r14; for fast interrupts also r8–r12. They add spsr register.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-16
SLIDE 16

Instructions

We will start with the simplest instruction.

MOV r0,r1 MOV r4,#10

It stores in a register the contents of other register or constant. Sufix S causes setting of flags, ever in MOV instruction

MOVS r0,r1

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-17
SLIDE 17

Instructions

Second (or the only one) argument of an instruction may be additionally rotated or shifted, for example the instruction

MOV r0,r1,LSL #2

corresponds to shifting the contents of r1 two bitss to the left with storing the result in r0. Other example with the computed shift:

MOV r0,r1,ASR r3

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-18
SLIDE 18

Computed jump

If the destination register of an instruction (not only for MOV!) is pc register, the it works as a jump to the computed address, for example

MOV pc,r1

If r1 contains the address of jump table, then to select one

  • f them

ADD pc,r1,r2 LSL #2

In such situation flags may be set in priviledged modes.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-19
SLIDE 19

Conditional execution

Most instructions can have special suffix, restricting additionally (by state flags) when they will execute, e.g. the instruction MOVEQS r0,r1,ASR r3 will be executed only when the zero flag is set (and it will itself change flags). Some available suffixes: AL (always, default), EQ, NE, GT, LE, LT, GE, MI, PL.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-20
SLIDE 20

Conditional execution

Example: computing GCD of two integers

define function gcd (a :: <integer>, b :: <integer>) => (r :: <integer>) while (a ~= b) if (a > b) a := a - b; else b := b - a; end if; end while; a end function;

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-21
SLIDE 21

Conditional execution

For assembler version we assume, that arguments are in registers r0 and r1, and the result must be in r0. without conditional execution

gcd CMP r0,r1 BEQ finish BLT less SUB r0,r0,r1 B gcd less SUB r1,r1,r0 B gcd finish BX lr ;???

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-22
SLIDE 22

Conditional execution

with conditional execution

gcd CMP r0,r1 SUBGT r0,r0,r1 SUBLT r1,r1,r0 BNE gcd BX lr ;???

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-23
SLIDE 23

Arithmetic instructions

Addition and subtraction for integers with and without sign, as usual the first argument must be a register. ADD r0,r1,r1,LSL #1 ;multiplication by 3 Reversed subtraction RSB r0,r1,#0 ;negacja RSC r4,r1,r2,LSL #1

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-24
SLIDE 24

Logical instruction

Typical operations: AND, ORR (inclusive-or), EOR (exclusive-or) BIC (BIt Clear): clears (in first source argument) the bits indicated by the second argument. BIC r0,r1,#3 Negation as MVN: negates each bit of argument and stores the result.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-25
SLIDE 25

Comparison instructions

Usual suspects: arithmetic comparison CMP and logical TST, the result only in flags of the processor state register. Mostly used in 16-bit mode. TEQ (TEst for eQuality): uses XOR. TEQ r1,#3

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-26
SLIDE 26

Multiplication

MUL: ordinary multiplication. MLA: multiplication with accumulation of result, three

  • arguments. We multiply the first two and then add the

computed product to the third argument. “Long” multiplication instructions: SMLAL, SMULL, UMLAL, UMULL. Four arguments, first two are concatenated to make space for 64-bit result.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-27
SLIDE 27

Example

AREA |.text|,CODE,READONLY EXPORT square ;int square (int i) square MUL r1,r0,r0 MOV r0,r1 MOV pc,lr ;return END

Convention: result in r0, arguments in r0, r1, ... To return from a procedure we can use

BX lr

instead of mov.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-28
SLIDE 28

Control

The register r15 has alternate name: pc. The second name is nearly always used, finding r15 “in the field” is highly improbable. The reason: this is the instruction counter (known on x86 as ip — instruction pointer). Usually (in the absence of jump or function call) this register is increased by four after each instruction.

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM

slide-29
SLIDE 29

Unconditional (and unreasonable) jump

.text .global main main: mov r0, #2 b end mov r0, #3 end: bx lr

Zbigniew Jurkiewicz, Instytut Informatyki UW Assembly Language Programming Introduction to ARM