Chapter 04 ARM Assembly
Chapter 04 ARM Assembly
Chapter 04 ARM Assembly
PROPRIETARY MATERIAL. © 2014 The McGraw-Hill Companies, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced or distributed in any form or by any means, without the
prior written permission of the publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual course preparation. PowerPoint Slides are being provided only
to authorized professors and instructors for use in preparing for classes using the affiliated textbook. No other use or distribution of this PowerPoint slide is permitted. The PowerPoint slide may not be sold and may 1
not be distributed or be used by any student or any other third party. No part of the slide may be reproduced, displayed or distributed in any form or by any means, electronic or otherwise, without the prior written
permission of McGraw Hill Education (India) Private Limited.
1
2nd version
www.basiccomparch.com
Download the pdf of the book
videos
Print version
The pdf version of the book and
(Publisher: WhiteFalcon, 2021)
all the learning resources can be
Available on e-commerce sites.
freely downloaded from the
website: www.basiccomparch.com
ARM Assembly Language
3
Outline
* Basic Instructions
* Advanced Instructions
* Branch Instructions
* Memory Instructions
* Instruction Encoding
4
ARM Machine Model
* 16 registers – r0 … r15
* The PC is explicitly visible
5
Data Transfer Instructions
6
Arithmetic Instructions
7
Example
Write an ARM assembly program to compute: 4+5 - 19. Save the result in
r1.
Optimal solution.
mov r1, #4
add r1, r1, #5
sub r1, r1, #19
8
Logical Instructions
9
Example
Answer:
10
Multiplication Instruction
11
Example
Answer:
/* load test values */
mov r0, #12
mov r1, #1
12
Outline
* Basic Instructions
* Advanced Instructions
* Branch Instructions
* Memory Instructions
* Instruction Encoding
13
Shifter Operands
Generic format
reg1 lsl #shift_amt
, lsr reg2
asr
ror
Examples
0 1 1 00
lsl #1
1 0 1 10
0 1 0 11
lsr #1
1 0 1 10
1 1 0 11
asr #1
1 0 1 10
0 1 0 11
ror #1
1 0 1 10
14
Examples of Shifter Operands
Write ARM assembly code to compute: r1 = r2 / 4.
Answer:
mov r1, r2, asr #2
Answer:
add r1, r2, r3, lsl #2
15
Compare Instructions
17
Instructions that use the Flags
18
64 bit addition using 32 bit registers
Answer:
adds r5, r1, r3
adc r6, r2, r4
The (adds) instruction adds the values in r1 and r3. adc(add with carry) adds
r2, r4, and the value of the carry flag. This is exactly the same as normal
addition.
19
Outline
* Basic Instructions
* Advanced Instructions
* Branch Instructions
* Memory Instructions
* Instruction Encoding
20
Simple Branch Instructions
* b (unconditional branch)
* b<code> (conditional branch)
21
Branch Conditions
Number Suffix Meaning Flag State
0 eq equal Z=1
1 ne notequal Z=0
2 cs/hs carry set/ unsigned higher or equal C=1
3 cc/lo carry clear/ unsigned lower C=0
4 mi negative/ minus N=1
5 pl positive or zero/ plus N=0
6 vs overflow V=1
7 vc no overflow V=0
8 hi unsigned higher (C = 1) ∧ (Z = 0)
9 ls unsigned lower or equal (C = 0) ∨ (Z = 1)
10 ge signed greater than or equal N=0
11 lt signed less than N=1
12 gt signed greater than (Z = 0) ∧ ( N = 0)
13 le signed less than or equal (Z = 1) ∨ (N = 1)
14 al always
15 – reserved
22
Example
Write an ARM assembly program to compute the factorial of a positive
number (> 1) stored in r0. Save the result in r1.
Answer:
ARM assembly
mov r1, #1 /* prod = 1 */
mov r3, #1 /* idx = 1 */
.loop:
mul r1, r3, r1 /* prod = prod * idx */
cmp r3, r0 /* compare idx, with the input (num) */
add r3, r3, #1 /* idx ++ */
bne .loop /* loop condition */
23
Branch and Link Instruction
24
Example
ARM assembly
C foo:
int foo() { mov r0, #2
return 2; mov pc, lr
}
void main() { main:
int x = 3; mov r1, #3 /* x = 3 */
int y = x + foo(); bl foo /* invoke foo */
} /* y = x + foo() */
add r2, r0, r1
25
The bx Instruction
Semantics Example Explanation
bx reg bx r2 (1) Jump unconditionally to the address
contained in register, r2
26
Example
foo:
mov r0, #2
bx lr
main:
mov r1, #3 /* x = 3 */
bl foo /* invoke foo */
/* y = x + foo() */
add r2, r0, r1
27
Conditional Variants of Normal Instructions
* Otherwise
* Do not execute at all
28
Write a program in ARM assembly to count the number of 1s in a 32 bit number stored in r1. Save
the result in r4.
Answer:
/* loop condition */
cmp r2, #32
ble .loop
29
Outline
* Basic Instructions
* Advanced Instructions
* Branch Instructions
* Memory Instructions
* Instruction Encoding
30
Basic Load Instruction
●
ldr r1, [r0]
ldr r1, [r0]
memory
register
file
r0
r1
31
Basic Store Instruction
●
str r1, [r0]
str r1, [r0]
memory
register
file
r0
r1
32
Memory Instructions with an Offset
33
Table of Load/Store Instructions
34
Example with Arrays
C
void addNumbers(int a[100]) {
int idx;
int sum = 0;
for (idx = 0; idx < 100; idx++){
sum = sum + a[idx];
}
}
Answer:
ARM assembly
/* base address of array a in r0 */
mov r1, #0 /* sum = 0 */
mov r2, #0 /* idx = 0 */
.loop:
ldr r3, [r0, r2, lsl #2]
add r2, r2, #1 /* idx ++ */
add r1, r1, r3 /* sum += a[idx] */
cmp r2, #100 /* loop condition */
bne .loop
35
Advanced Memory Instructions
36
Pre-Indexed Addressing Mode
* Consider
* ldr r0, [r1, #4]!
37
Example with Arrays
C
void addNumbers(int a[100]) {
int idx;
int sum = 0;
for (idx = 0; idx < 100; idx++){
sum = sum + a[idx];
}
}
Answer:
ARM assembly
/* base address of array a in r0 */
mov r1, #0 /* sum = 0 */
add r4, r0, #400 /* set r4 to address of a[100] */
.loop:
ldr r3, [r0], #4
add r1, r1, r3 /* sum += a[idx] */
cmp r0, r4 /* loop condition */
bne .loop
38
Memory Instructions in Functions
Instruction Semantics
ldmfd sp!, {list of registers } Pop the stack and assign values to registers
in ascending order. Update sp.
stmfd sp!, {list of registers } Push the registers on the stack in descending
order. Update sp.
39
Example
Write a function in C and implement it in ARM assembly to compute x n,
where x and n are natural numbers. Assume that x is passed through r0, n
through r1, and the return value is passed back to the original program via
r0. Answer:
ARM assembly
power:
cmp r1, #0 /* compare n with 0 */
moveq r0, #1 /* return 1 */
bxeq lr /* return */
40
Outline
* Basic Instructions
* Advanced Instructions
* Branch Instructions
* Memory Instructions
* Instruction Encoding
41
Generic Format
* Generic Format
4 2
cond type
32 29 28 27
42
Data Processing Instructions
4 2 4 4 4 12
cond 00 I opcode S rs rd shifter operand/
32 29 28 27 26 25 22 21 20 17 16 13 immediate
12 1
rot payload
46
Examples
Encode the decimal number 42.
Answer:
42 in the hex format is 0x2A, or alternatively 0x 00
00 00 2A. There is no right rotation involved. Hence,
the immediate field is 0x02A.
47
Encoding the Shifter Operand
5 2 4
shift imm shift type 0 rt
Shift type
12 8 7 6 5 4 1
lsl 00
(a)
4 2 4 lsr 01
asr 10
shift reg shift type 1 rt ror 11
12 9 8 7 6 5 4 1
(b) (c)
48
Load/Store Instructions
4 2 6 4 4 12
49
I, P, U, B, W, and L bits
Bit Value Semantics
0 last 12 bits represent an immediate value
I
1 last 12 bits represent a shifter operand
0 post-indexed addressing
P
1 pre-indexed addressing
0 subtract offset from base
U
1 add offset to base
0 transfer word
B
1 transfer byte
0 do not use pre or post indexed addressing
W
1 use pre or post indexed addressing
0 store to memory
L
1 load from memory
50
Branch Instructions
4 3 24
51
Branch Instructions - II
* What does the processor do
* Expands the offset to 32 bits (with proper sign
extensions)
* Shifts it to the left by 2 bits (because offset is in
terms of memory words)
* Adds it to PC + 8 to generate the branch target
* Why, PC + 8 ?
* Read chapter 9
52
THE END
53