assembly language programming: arm cortex-m3 - language...¼controller constructed around an...

Download Assembly Language Programming: ARM Cortex-M3 - Language...¼controller constructed around an ARM Cortex-M3 core. The choice of this ¼controllerrestsonthedesiretoexplain: –a32-bitprocessor:

Post on 23-Apr-2018

253 views

Category:

Documents

4 download

Embed Size (px)

TRANSCRIPT

  • Assembly Language Programming

  • Assembly LanguageProgramming

    ARM Cortex-M3

    Vincent Mahout

  • First published 2012 in Great Britain and the United States by ISTE Ltd and John Wiley & Sons, Inc.

    Apart from any fair dealing for the purposes of research or private study, or criticism or review, aspermitted under the Copyright, Designs and Patents Act 1988, this publication may only be reproduced,stored or transmitted, in any form or by any means, with the prior permission in writing of the publishers,or in the case of reprographic reproduction in accordance with the terms and licenses issued by theCLA. Enquiries concerning reproduction outside these terms should be sent to the publishers at theundermentioned address:

    ISTE Ltd John Wiley & Sons, Inc.27-37 St Georges Road 111 River StreetLondon SW19 4EU Hoboken, NJ 07030UK USA

    www.iste.co.uk www.wiley.com

    ISTE Ltd 2012

    The rights of Vincent Mahout to be identified as the author of this work have been asserted by him inaccordance with the Copyright, Designs and Patents Act 1988.____________________________________________________________________________________

    Library of Congress Cataloging-in-Publication Data

    Mahout, Vincent.Assembly language programming : ARM Cortex-M3 / Vincent Mahout.p. cm.Includes bibliographical references and index.ISBN 978-1-84821-329-61. Embedded computer systems. 2. Microprocessors. 3. Assembler language (Computer programlanguage) I. Title.TK7895.E42M34 2012005.2--dc23

    2011049418

    British Library Cataloguing-in-Publication DataA CIP record for this book is available from the British LibraryISBN: 978-1-84821-329-6

    Printed and bound in Great Britain by CPI Group (UK) Ltd., Croydon, Surrey CR0 4YY

  • Table of Contents

    Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix

    Chapter 1. Overview of Cortex-M3 Architecture . . . . . . . . . . . . . . . . 1

    1.1. Assembly language versus the assembler . . . . . . . . . . . . . . . . . . 11.2. The world of ARM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21.2.1. Cortex-M3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31.2.2. The Cortex-M3 core in STM32. . . . . . . . . . . . . . . . . . . . . . 7

    Chapter 2. The Core of Cortex-M3 . . . . . . . . . . . . . . . . . . . . . . . . . 15

    2.1. Modes, privileges and states . . . . . . . . . . . . . . . . . . . . . . . . . . 152.2. Registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172.2.1. Registers R0 to R12 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192.2.2. The R13 register, also known as SP . . . . . . . . . . . . . . . . . . . 192.2.3. The R14 register, also known as LR. . . . . . . . . . . . . . . . . . . 202.2.4. The R15 or PC register. . . . . . . . . . . . . . . . . . . . . . . . . . . 212.2.5. The xPSR register . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

    Chapter 3. The Proper Use of Assembly Directives . . . . . . . . . . . . . . . 25

    3.1. The concept of the directive . . . . . . . . . . . . . . . . . . . . . . . . . . 253.1.1. Typographic conventions and use of symbols . . . . . . . . . . . . . 26

    3.2. Structure of a program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273.2.1. The AREA sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28

    3.3. A section of code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293.3.1. Labels. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293.3.2. Mnemonic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313.3.3. Operands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323.3.4. Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343.3.5. Procedure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

  • vi Assembly Language Programming

    3.4. The data section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363.4.1. Simple reservation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363.4.2. Reservation with initialization . . . . . . . . . . . . . . . . . . . . . . 373.4.3. Data initialization: the devil is in the details . . . . . . . . . . . . . . 39

    3.5. Is that all? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393.5.1. Memory management directives . . . . . . . . . . . . . . . . . . . . . 403.5.2. Project management directives . . . . . . . . . . . . . . . . . . . . . . 413.5.3. Various and varied directives . . . . . . . . . . . . . . . . . . . . . . . 44

    Chapter 4. Operands of Instructions . . . . . . . . . . . . . . . . . . . . . . . . 47

    4.1. The constant and renaming. . . . . . . . . . . . . . . . . . . . . . . . . . . 484.2. Operands for common instructions . . . . . . . . . . . . . . . . . . . . . . 494.2.1. Use of registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 494.2.2. The immediate operand . . . . . . . . . . . . . . . . . . . . . . . . . . 53

    4.3. Memory access operands: addressing modes . . . . . . . . . . . . . . . . 574.3.1. The pointer concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . 584.3.2. Addressing modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59

    Chapter 5. Instruction Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

    5.1. Reading guide . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 635.1.1. List of possible condition suffixes. . . . . . . . . . . . . . . . . . . 65

    5.2. Arithmetic instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 665.3. Logical and bit manipulation instructions . . . . . . . . . . . . . . . . . . 705.4. Internal transfer instructions . . . . . . . . . . . . . . . . . . . . . . . . . . 755.5. Test instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 765.6. Branch instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775.7. Load/store instructions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805.7.1. Simple transfers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 805.7.2. Multiple transfers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 825.7.3. Access to the system stack . . . . . . . . . . . . . . . . . . . . . . . . 84

    5.8. System instructions and others . . . . . . . . . . . . . . . . . . . . . . . 85

    Chapter 6. Algorithmic and Data Structures . . . . . . . . . . . . . . . . . . . 87

    6.1. Flowchart versus algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . 876.2. Alternative structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 896.2.1. Simple (or shortened) alternative. . . . . . . . . . . . . . . . . . . . . 896.2.2. Complete alternative . . . . . . . . . . . . . . . . . . . . . . . . . . . . 906.2.3. Special case of the alternative . . . . . . . . . . . . . . . . . . . . . . 936.2.4. Multiple choice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

    6.3. Iterative structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 986.3.1. The RepeatUntil loop . . . . . . . . . . . . . . . . . . . . . . . . . . 98

  • Table of Contents vii

    6.3.2. TheWhileDo loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1026.3.3. The For loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

    6.4. Compound conditions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1066.4.1. Alternative with AND . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076.4.2. Iteration with AND. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1086.4.3. Alternative with OR . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1096.4.4. Iteration with OR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

    6.5. Data structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1116.5.1. Table in one dimension . . . . . . . . . . . . . . . . . . . . . . . . . . 1116.5.2. Tables in multiple dimensions . . . . . . . . . . . . . . . . . . . . . . 1126.5.3. Registration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1136.5.4. Non-dimensional table, character string. . . . . . . . . . . . . . . . . 1136.5.5. Queue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1146.5.6. Stack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

    Chapter 7. Internal Modularity . . . . . . . . . . . . . . . . . . . . . . . . . . . 119

    7.1. Detailing the concept of procedure . . . . . . . . . . . . . . . . . . . . . . 1197.1.1. Simple call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1197.1.2. Nested calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1197.1.3. Red wire example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

    7.2. Procedure arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1237.2.1. Usefulness of arguments. . . . . . . . . . . . . . . . . . . . . . . . . . 1237.2.2. Arguments by value and by reference . . . . . . . . . . . . . . . . . . 1237.2.3. Passing arguments by general registers . . . . . . . . . . . . . . . . . 1237.2.4. Passing arguments by a stack . . . . . . . . . . . . . . . . . . . . . . . 1267.2.5. Passing arguments by the system stack . . . . . . . . . . . . . . . . . 1337.2.6. On the art of mixing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

    7.3. Local data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1367.3.1. Simple reservation of local data . . . . . . . . . . . . . . . . . . . . . 1377.3.2. Using a chained list. . . . . . . . . .

Recommended

View more >