Introduction to Saturn Assembly Language
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Saturn Assembly Language Written by Gilbert Fernandes Edited and Maintained by Eric Rechlin Authors: Gilbert Henri Fernandes Eric Rechlin 1 rue des Gâte Ceps 3212 Winnipeg Drive F-92210 Saint-Cloud Bismarck, ND 58503-0453 France USA [email protected] [email protected] ICQ: 78327407 ICQ: 783944 This book, as well as the included examples, is a free work. You may print this for your own purposes or for any other person. This book may be transmitted or reproduced in any form or by any means as long as credit to the original authors is not modified. In plain English: This is free, so widely distribute it! Just make sure you give credit to us. Hewlett-Packard and HP are registered trademarks of the Hewlett-Packard Company. Edition 3: July 16, 2005 Copyright 1999-2005 Gilbert Fernandes and Eric Rechlin Distribute freely to all your HP calculator-owning friends. Preface Assembly language is very different from most other programming languages. When learning a typical programming language, one of the first lessons will usually explain how to make a simple “Hello World” program. But with assembly language, it’s not that simple. First, before any assembly language code can be written, one must understand the concepts of how the processor works and how the operating system works with it. In addition, many commands must be learned in order to do something as simple as printing to the display. In this book you will have to go through forty pages of introductory material before even picking up your calculator. But don’t worry; you’ll get to start programming soon enough! After being introduced to the concepts, assembly language commands, known as instructions, will be learned in small groups, beginning with an extremely simple program. Because machine code is binary, you will learn assembly language mnemonics that will represent this binary code. This book is aimed at owners of all HP 48 and HP 49 series calculators. We use the MASD syntax, because MASD is built into the 49 series and comes with Meta Kernel for the 48GX. For those without MASD, a small assembler known as HP-ASM, which is compatible with the limited subset of the MASD syntax that we use here, can be used. The syntax of this assembler is quite easy and is also supported by HP Tools on the PC. The HP syntax for assembly language, supported by Jazz and HP Tools, is very similar, so one should be able to easily switch between the two, though we will not cover the differences here. We think many more people could learn assembly language. Coders are needed, and the main roadblock is that information about assembly language coding, particularly Saturn assembly, is too hard to get. Most good Saturn assembly language tutorials are in French, and the only English-language one is James Donnelly’s “An Introduction to HP 48 System RPL and Assembly Language Programming,” and although it is a great book for System RPL, its assembly language section goes by too quickly for most people. With this book, we hope to fill a gap in the documentation that already exists for the HP 48 and 49. Though we are not what one would call experienced coders, we would like to share our knowledge. We still consider ourselves newbies as well, so this is not a definitive introduction to assembly language. If you notice any mistakes, please don’t flame us, but instead email us at the addresses below. Some, like Jean-Yves Avenard, Cyrille de Brébisson, and Gerald Squelart, three of the developers of the 49, have many years of coding experience, but don’t be intimidated. Everyone is a newbie at some point in time. This book goes from the very basics of how processors work, including tutorials covering the binary and hexadecimal bases, to coding complex routines. It is intended both as an introductory manual to beginning programmers who know nothing about the low-level operation of a computer, and also as a reference manual for experienced programmers. We spent many, many hours on this book, but if you have problems, feel free to email us and we will help as we can and add to the tutorial if needed. This online version of the book is free, so distribute it widely to all HP calculator fans! In addition, we would like to thank the following people for their contributions to this book (in alphabetical order): Arnaud Amiel, Jean-Yves Avenard, Cyrille de Brébisson, Christoph Giesselink, Chris Griffiths, Eduardo Kalinowski, Jan Kozicki, John H Meyers, Aldiney Oliveira, Mark Power, Gerald Squelart, and Richard Steventon. The help of Christoph Giesselink and Eduardo Kalinowski is especially appreciated, as they wrote entire sections. Updates to this book are available at <http://www.hpcalc.org>. Please, please, all that we ask is for you to send feedback to our email addresses: Gilbert Fernandes <[email protected]> Eric Rechlin <[email protected]> Table of Contents PART I: PROGRAMMING CONCEPTS.....................................................................................................................................1 1 INTRODUCTION TO USER RPL AND SYSTEM RPL...........................................................................................................1 2 ASSEMBLY LANGUAGE AND MACHINE LANGUAGE ...........................................................................................................2 3 THE BINARY BASE AND NIBBLES........................................................................................................................................2 4 CONVERTING FROM BINARY TO DECIMAL........................................................................................................................4 5 MOST SIGNIFICANT BIT AND LEAST SIGNIFICANT BIT ......................................................................................................5 6 FRACTIONAL NUMBERS.....................................................................................................................................................6 7 CALCULATING USING BASE 2 ............................................................................................................................................7 7.1 Adding .........................................................................................................................................................................7 7.2 Subtracting...................................................................................................................................................................7 7.3 Negative numbers........................................................................................................................................................7 7.3.1 The signed binary: the first method .....................................................................................................................8 7.3.2 Two’s complement: the second method ..............................................................................................................8 7.4 Multiplying ................................................................................................................................................................10 7.5 Dividing.....................................................................................................................................................................11 8 HEXADECIMAL BASE .......................................................................................................................................................12 9 CONVERTING FROM HEXADECIMAL TO DECIMAL..........................................................................................................13 10 WRITING GROUPS OF FOUR BITS (NIBBLES) USING HEXADECIMAL ...............................................................................14 11 IS ALL THAT NUMBER CRUNCHING NEEDED? .................................................................................................................15 12 BCD: “BINARY CODED DECIMAL” ................................................................................................................................15 13 CONVERTING A DECIMAL TO ITS BCD FORM.................................................................................................................16 14 NORMAL BCD AND “EXTENDED BCD” .........................................................................................................................17 15 CALCULATING USING BCD.............................................................................................................................................17 15.1 When a nibble overflows.......................................................................................................................................17 15.2 When a carry needs to be moved...........................................................................................................................17 16 LOGICAL OPERATIONS ....................................................................................................................................................18 16.1 Logical OR ............................................................................................................................................................19 16.2 Logical AND .........................................................................................................................................................19 16.3 Inverting ................................................................................................................................................................20