Table Of ContentTECHNOLOGY IN ACTION™
RP2040 Assembly
Language
Programming
ARM Cortex-M0+ on the
Raspberry Pi Pico
—
Stephen Smith
RP2040 Assembly
Language
Programming
ARM Cortex-M0+
on the Raspberry Pi Pico
Stephen Smith
RP2040 Assembly Language Programming: ARM Cortex-M0+ on the
Raspberry Pi Pico
Stephen Smith
Gibsons, BC, Canada
ISBN-13 (pbk): 978-1-4842-7752-2 ISBN-13 (electronic): 978-1-4842-7753-9
https://doi.org/10.1007/978-1-4842-7753-9
Copyright © 2022 by Stephen Smith
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Aaron Black
Development Editor: James Markham
Coordinating Editor: Jessica Vakili
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233
Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
[email protected], or visit www.springeronline.com. Apress Media, LLC is a
California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc
(SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected]; for
reprint, paperback, or audio rights, please e-mail [email protected].
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available
to readers on GitHub via the book’s product page, located at www.apress.com/978- 1- 4842- 7752- 2.
For more detailed information, please visit http://www.apress.com/source- code.
Printed on acid-free paper
This book is dedicated to my beloved wife and
editor Cathalynn Labonté-Smith.
Table of Contents
About the Author ��������������������������������������������������������������������������������xv
About the Technical Reviewer ����������������������������������������������������������xvii
Acknowledgments �����������������������������������������������������������������������������xix
Introduction ���������������������������������������������������������������������������������������xxi
Chapter 1: How to Set Up the Development Environment ���������������������1
About the Raspberry Pi Pico ���������������������������������������������������������������������������������3
About the Host Computer ��������������������������������������������������������������������������������������4
How to Solder and Wire�����������������������������������������������������������������������������������������5
How to Install Software �����������������������������������������������������������������������������������������6
A Simple Program to Ensure Things Are Working �������������������������������������������������7
Create Some Helper Script Files ���������������������������������������������������������������������������8
Summary���������������������������������������������������������������������������������������������������������������9
Chapter 2: Our First Assembly Language Program ����������������������������11
10 Reasons to Use Assembly Language �������������������������������������������������������������12
Computers and Numbers ������������������������������������������������������������������������������������15
ARM Assembly Instructions ��������������������������������������������������������������������������������17
CPU Registers ������������������������������������������������������������������������������������������������18
ARM Instruction Format ���������������������������������������������������������������������������������19
RP2040 Memory��������������������������������������������������������������������������������������������������21
About the GCC Assembler �����������������������������������������������������������������������������������23
Hello World ����������������������������������������������������������������������������������������������������������23
v
Table of ConTenTs
Our First Assembly Language File �����������������������������������������������������������������������28
About the Starting Comment �������������������������������������������������������������������������28
Where to Start �����������������������������������������������������������������������������������������������29
Assembly Instructions �����������������������������������������������������������������������������������30
Data ���������������������������������������������������������������������������������������������������������������31
Program Logic �����������������������������������������������������������������������������������������������31
Reverse Engineering Our Program ����������������������������������������������������������������������33
Summary�������������������������������������������������������������������������������������������������������������36
Exercises �������������������������������������������������������������������������������������������������������������36
Chapter 3: How to Build and Debug Programs �����������������������������������39
CMake �����������������������������������������������������������������������������������������������������������������39
GNU Make �����������������������������������������������������������������������������������������������������������42
Print Statements �������������������������������������������������������������������������������������������������45
GDB ���������������������������������������������������������������������������������������������������������������������46
Preparing to Debug ����������������������������������������������������������������������������������������47
Beginning GDB �����������������������������������������������������������������������������������������������48
Summary�������������������������������������������������������������������������������������������������������������55
Exercises �������������������������������������������������������������������������������������������������������������56
Chapter 4: How to Load and Add ��������������������������������������������������������57
About Negative Numbers ������������������������������������������������������������������������������������57
About Two’s Complement ������������������������������������������������������������������������������57
About Raspberry Pi OS Calculator �����������������������������������������������������������������59
About One’s Complement ������������������������������������������������������������������������������60
Big- vs� Little-Endian ������������������������������������������������������������������������������������������60
About Bi-Endian ���������������������������������������������������������������������������������������������61
Pros of Little-Endian ��������������������������������������������������������������������������������������61
Cons of Little-Endian �������������������������������������������������������������������������������������62
vi
Table of ConTenTs
How to Shift and Rotate Registers ����������������������������������������������������������������������62
About Carry Flag ��������������������������������������������������������������������������������������������63
Basics of Shifting and Rotating ���������������������������������������������������������������������63
How to Use MOV ��������������������������������������������������������������������������������������������������65
Move Immediate ��������������������������������������������������������������������������������������������65
Moving Data from One Register to Another Using Register MOV �������������������65
ADD/ADC �������������������������������������������������������������������������������������������������������������66
Add with Carry �����������������������������������������������������������������������������������������������68
SUB/SBC��������������������������������������������������������������������������������������������������������������69
Shifting and Rotating ������������������������������������������������������������������������������������������70
Loading All 32 Bits of a Register ��������������������������������������������������������������������71
MOV/ADD/Shift Example �������������������������������������������������������������������������������������72
Summary�������������������������������������������������������������������������������������������������������������77
Exercises �������������������������������������������������������������������������������������������������������������78
Chapter 5: How to Control Program Flow �������������������������������������������79
Unconditional Branch ������������������������������������������������������������������������������������������79
About the CPSR ���������������������������������������������������������������������������������������������������80
Branch on Condition ��������������������������������������������������������������������������������������������81
About the CMP Instruction ����������������������������������������������������������������������������������82
Loops ������������������������������������������������������������������������������������������������������������������83
FOR Loops �����������������������������������������������������������������������������������������������������83
While Loops ���������������������������������������������������������������������������������������������������84
If/Then/Else ���������������������������������������������������������������������������������������������������������85
Logical Operators ������������������������������������������������������������������������������������������������86
AND ����������������������������������������������������������������������������������������������������������������87
EOR ����������������������������������������������������������������������������������������������������������������88
vii
Table of ConTenTs
ORR ����������������������������������������������������������������������������������������������������������������88
BIC �����������������������������������������������������������������������������������������������������������������88
MVN ���������������������������������������������������������������������������������������������������������������88
TST ����������������������������������������������������������������������������������������������������������������89
Design Patterns ���������������������������������������������������������������������������������������������������89
Converting Integers to ASCII �������������������������������������������������������������������������������90
Using Expressions in Immediate Constants���������������������������������������������������94
Storing a Register to Memory ������������������������������������������������������������������������94
Why Not Print in Decimal? �����������������������������������������������������������������������������95
Performance of Branch Instructions �������������������������������������������������������������������95
Summary�������������������������������������������������������������������������������������������������������������96
Exercises �������������������������������������������������������������������������������������������������������������97
Chapter 6: Thanks for the Memories ��������������������������������������������������99
How to Define Memory Contents ����������������������������������������������������������������������100
How to Align Data ����������������������������������������������������������������������������������������103
How to Load a Register �������������������������������������������������������������������������������������104
How to Load a Register with an Address �����������������������������������������������������104
How to Load Data from Memory ������������������������������������������������������������������106
Optimizing Small Read-Only Data Access ���������������������������������������������������108
Indexing Through Memory ���������������������������������������������������������������������������109
How to Store a Register ������������������������������������������������������������������������������������112
How to Convert to Uppercase ���������������������������������������������������������������������������112
How to Load and Store Multiple Registers ��������������������������������������������������������118
Summary�����������������������������������������������������������������������������������������������������������119
Exercises �����������������������������������������������������������������������������������������������������������119
viii
Table of ConTenTs
Chapter 7: How to Call Functions and Use the Stack ������������������������121
About Stacks on the RP2040 �����������������������������������������������������������������������������122
How to Branch with Link �����������������������������������������������������������������������������������123
About Nesting Function Calls ����������������������������������������������������������������������������124
About Function Parameters and Return Values ������������������������������������������������126
How to Manage the Registers ���������������������������������������������������������������������������127
Summary of the Function Call Algorithm ����������������������������������������������������������128
More on the Branch Instructions �����������������������������������������������������������������������129
About the X Factor ���������������������������������������������������������������������������������������130
Uppercase Revisited �����������������������������������������������������������������������������������������131
About Stack Frames ������������������������������������������������������������������������������������������137
Stack Frame Example ����������������������������������������������������������������������������������138
How to Create Macros ���������������������������������������������������������������������������������������139
About Include Directive �������������������������������������������������������������������������������142
How to Define a Macro ��������������������������������������������������������������������������������142
About Labels ������������������������������������������������������������������������������������������������143
Why Macros? �����������������������������������������������������������������������������������������������144
Summary�����������������������������������������������������������������������������������������������������������144
Exercises �����������������������������������������������������������������������������������������������������������145
Chapter 8: Interacting with C and the SDK ���������������������������������������147
How to Wire Flashing LEDs �������������������������������������������������������������������������������148
How to Flash LEDs with the SDK �����������������������������������������������������������������149
How to Call Assembly Routines from C �������������������������������������������������������������154
How to Embed Assembly Code Inside C Code ���������������������������������������������������156
Summary�����������������������������������������������������������������������������������������������������������160
Exercises �����������������������������������������������������������������������������������������������������������160
ix
Table of ConTenTs
Chapter 9: How to Program the Built-in Hardware ���������������������������161
About the RP2040 Memory Map �����������������������������������������������������������������������161
About C Header Files �����������������������������������������������������������������������������������������162
About the Raspberry Pi Pico Pins ����������������������������������������������������������������������164
How to Set a Pin Function ���������������������������������������������������������������������������165
About Hardware Registers and Concurrency ����������������������������������������������������167
About Programming the Pads ���������������������������������������������������������������������������169
How to Initialize SIO ������������������������������������������������������������������������������������������169
How to Turn a Pin On/Off �����������������������������������������������������������������������������������170
The Complete Program �������������������������������������������������������������������������������������171
Summary�����������������������������������������������������������������������������������������������������������174
Exercises �����������������������������������������������������������������������������������������������������������175
Chapter 10: H ow to Initialize and Interact with Programmable I/O ����177
About PIO Architecture ��������������������������������������������������������������������������������������178
About the PIO Instructions ��������������������������������������������������������������������������������180
Flashing the LEDs with PIO �������������������������������������������������������������������������������181
PIO Instruction Details and Examples ���������������������������������������������������������������187
JMP��������������������������������������������������������������������������������������������������������������187
WAIT ������������������������������������������������������������������������������������������������������������188
IN �����������������������������������������������������������������������������������������������������������������188
OUT ��������������������������������������������������������������������������������������������������������������189
PUSH ������������������������������������������������������������������������������������������������������������190
PULL ������������������������������������������������������������������������������������������������������������190
MOV �������������������������������������������������������������������������������������������������������������191
IRQ ���������������������������������������������������������������������������������������������������������������192
SET ��������������������������������������������������������������������������������������������������������������193
x