Introduction to RISC Assembly Language Programming Introduction to RISC Assembly Language Programming JOHN WALDRON School of Computer Applications Dublin City University Harlow, England • London • New York • Boston• San Francisco •Toronto •Sydney• Singapore • Hong Kong Tokyo •Seoul•Taipei • New Delhi• Cape Town • Madrid • Mexico City•Amsterdam• Munich•Paris•Milan Pearson Education Limited Edinburgh Gate H arlow E ssex CM20 2JE E ngland and Associated Comp anies throughout the world Visit us on the World Wide Web at: http://www.pearsoneduc.com © Addison Wesley Longman Limited 999 The right of John Waldron to be identified as author of this Work has been asserted by him in accordance with t he Copyright , Designs and Patents Act 98 All rights reserved N o p art of this p ublication may be reproduced, stored in a ret rieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, without either the prior written permission of the publisher or a licence permitting restricted copying in the United Kingdom issued by the Copyright Licensing Agency Ltd, 90 Tottenham Court Road, London WIT 4LP The programs in this book have been included for their instructional value They have been tested with care but are not guaranteed for any par ticular purpose The publisher does not offer any warrant ies or rep resent at ions nor does it accept any liabilities wit h respect t o t h e programs Many of the designations used by manufacturers and sellers to d stinguish their product s are claimed as trademarks Addison Wesley Longman Limited has made every attempt to supply trademark information about manufacturers and their p roducts mentioned in this book A list of the trademark designat ions and t heir owners appears on p age x Cover designed by OdB Design & Communication, Reading, UK in 10/ 12pt Times by 32 Printed and b ound in Great Britain by H enry Ling Limited, at the Dorset Press, Dorchester, DT l lH D Typeset First printed 998 ISBN 0-201-39 82 8-1 British Library Cataloguing-in-Publication Data A cat alogue record for this book is available from the British Library 10 07 06 05 04 03 Preface This book is based on a one-semester introductory computer architecture course for first-year computing students in the School of Computer Applications, Dublin City University, using SPIM, a virtual machine that runs programs for the MIPS R2000/R3000 computers The architecture of the MIPS is an ideal example of a simple, clean RISC (Reduced Instruction Set Computer) machine, which makes it easy to learn and understand The processor contains 32 general-purpose registers and a well-designed instruction set The existence of a simulator for the processor greatly simplifies the development and debugging of assembly language programs For these reasons, MIPS is the preferred choice for teaching computer architecture in the 2000s, just as the Motorola 68000 was during the 1980s The material assumes that the reader has never studied computer programming before, and is usually given at the same time as a programming course in a high-level language like Java or C The main data structures covered are strings, arrays and stacks The ideas of program loops, i f statements, procedure calls and some recursion are presented The philosophy behind the book is to speed up the learning process relative to other MIPS architecture books by enabling the reader to start writing simple assembly language programs early, without getting involved in laborious descriptions of the trade-offs involved in the design of the processor The most successful approach to computer architecture is to begin by writing numerous small assembly language programs, before going on to study the underlying concepts Thus this text does not address topics such as logic design or boolean algebra, but does contain example programs using the MIPS logical instructions While processors like the MIPS were designed for high-level language compilation and as such are targeted at compilers rather than human programmers, the only way to gain an appreciation of their functionality is to write many programs for the processor in assembly language The book is associated with an automatic program testing system (Mips Assembly Language Exam System) which allows a lecturer to set assembly language programming questions and collect and mark the assignments automatically, or a reader to test a MIPS assembly language program against several different cases and determine whether it works, as described in Appendix A The exam system is written as a collection of Unix C shell scripts If the instructor or student does not wish to adopt this learning approach the textbook can be used in a traditional manner A student who , VI PREFACE can write an assembly language program which converts a number to an ASCII string in hexadecimal format under exam conditions has demonstrated a thorough understanding of all the principles of introductory computer architecture There is little point in describing concepts such as pipelining, delayed branches of advanced compiler topics to students who are not yet familiar with simple program loops Assembly language programming is usually considered an arcane and complex discipline This view ari ses among those whose first experience of assembly language programming was the instructions and registers of architectures like the Intel 8086 family Programming in a RISC architecture is very different due to the elegant, compact and simple instruction set Students of this text who have never programmed before and begin to study it simultaneously with a course on C programming report it is easier and more logical to program in assembly! In addition, because of the programming exam system, there is a higher pass rate and level of proficiency achieved by students on the assembly course than on the more traditional C course The SPIM simulator is available in the public domain from the University of Wisconsin Madison at f : I I f c s wi s e edu/pub/ spim/ Overhead projector slides of lecture notes, all example programs and all exam questions are available from h t : I /www c ompapp dcu i e/ ""'j wa l dron The programs that correct the questions, together with test cases and solutions, are available to lecturers adopting the course The SPIM simulator software was designed and written by James R Laurus (laurus@cs.wisc.edu) This book was partly inspired by John Conry's course at the University of Oregon which he has made available on the Internet I would like to thank him for permission to use some of his example programs and material Thanks to Dr David Sinclair for reading an early dr aft and providing many important suggestions Also thanks to Karen Sutherland and Keith Mansfield at Addison Wesley Longman John Waldron, Dublin July 1998 Contents Preface v Introduction I.I Basic computer organization I Machine language 3 Assembly language Why program in assembly language? Outline of chapters Summary Exercises Essential background information 9 2 Introduction 2.2 Decimal and binary numbers 2.3 Hexadecimal numbers 10 2.4 Binary addition 12 2.5 Two's complement numbers 12 2.6 Bits, bytes and nibbles 14 2.7 Storing characters 14 2.8 Summary 16 Exercises 16 MIPS computer organization 17 3.1 Introduction 17 3.3 The MIPS design Memory layout 18 3.4 The MIPS registers 19 The SPI M simulator 21 110 organization 21 Summary 23 Exercises 23 3.2 17 VIII CONTENTS An example MIPS program 25 4.1 Introduction 25 Source code format 25 Description of hello a 29 4.4 Putting theory into practice 31 4.5 Load and store instructions 37 4.6 Arithmetic instructions 38 4.7 Multiplication and division 40 4.8 Programming example 42 4.9 Summary 48 Exercises 48 Control flow structures 49 Introduction 49 Control structures 49 Conditional branches 50 5.4 Example programs using loops 51 5.5 Summary 58 Exercises 58 Addressing modes 59 6.1 6.2 Introduction 59 MIPS instruction formats 59 6.3 MIPS register addressing 60 6.4 MIPS base addressing 62 MIPS immediate addressing 63 6.6 MIPS PC-relative addressing 64 6.7 Example program using base addressing 64 6.8 Example program using indexed addressing 6.9 Base register addressing vs indexed addressing 69 72 6.10 Summary 72 Exercises 72 Logical, shift and rotate instructions 73 7.1 Introduction 73 7.2 Shift and rotate instructions 73 7.3 Logical instructions 74 CONTENTS 7.4 7.5 An example program Summary Exercises Stacks and procedures IX 76 82 83 85 Introduction 85 8.2 The stack 85 8.3 Procedure calls 91 8.4 Passing parameters 92 8.5 Temporary and saved registers 93 8.6 Stack frames 93 8.7 Assembly code from a real compiler 00 8.8 Example recursive programs 104 8.9 Summary 19 Exercises 120 Appendix A A I A.2 MIPS programming exams Introduction MIPSMARK software 121 121 22 A.3 Exam questions using program loops 124 A.4 Exam question using bit manipulation 131 AS Exam questions using the stack A Exam questions using functions 32 140 Append ix B MIPS/SPIM instruction quick reference 145 Appendix C MIPS/SPI M instruction reference 15 Index 177 Trademark Notice Intel is a trademark of Intel Corporation Java is a t rademark or regist ered t rademark of Sun M icrosystems, Inc Jurassic Park i s a trademark or registered trademark o f Amblin Entertainment M acint osh is a regist ered t rademark of A pple C o mp uter, Inc M IPS R2000 is a t rademark or regist ered t rademark of M IPS T echnologies, Inc M ot orola 68000 is a trademark or registered Lrademark of Motorola, Inc Nintend o 64 is a t rademark or re gi st ered trademark of Nintendo of America, Inc SGI is a registered trademark of Silicon G ph ic s, Inc S PARC is a t rademark of SPARC I nternat ional Inc Star Wars is a t rademark o r registered trademark o f Lucas Films T o y St ory is a t rademark, © Disney UNIX is a trademark, l ice nsed through X/OPEN Company Ltd .. .Introduction to RISC Assembly Language Programming Introduction to RISC Assembly Language Programming JOHN WALDRON School of Computer Applications... entirely in assembly language and that company places great value on those with assembly language programming skills When you have to use assembly language, try to it via a high-level language Many... processor in assembly language The book is associated with an automatic program testing system (Mips Assembly Language Exam System) which allows a lecturer to set assembly language programming