C20 1645 5_A_Programmers_Introduction_To_IBM_System360_Assembler_Language_Jul69 5 A Programmers Introduction To IBM System360 Assembler Language Jul69
C20-1645-5_A_Programmers_Introduction_To_IBM_System360_Assembler_Language_Jul69 C20-1645-5_A_Programmers_Introduction_To_IBM_System360_Assembler_Language_Jul69
User Manual: C20-1645-5_A_Programmers_Introduction_To_IBM_System360_Assembler_Language_Jul69
Open the PDF directly: View PDF .
Page Count: 159
Download | |
Open PDF In Browser | View PDF |
---- ------- --- --=- -= = --=~= A Programmer's Introduction to IBM System /360 Assembler Language_ Student Text ~ ~ ~ §~§ A Programmer's Introduction to IBM System /360 Assembler Language Student Text MAJOR REVISION (July 1969) This edition, C20-1646-5, is a major revision and obsoletes all preceding editions of C20-1646,. previously titled A Programmer's Introduction to the IBM System/360 Architecture, Instructions, and Assembler Language. The text and program examples have been revised throughout to bring them up to date and in line with current programming conventions. In addition, significant new material appears in Chapters 1, 2, 6, 7, 8, and 9. Chapter 2 contains selected material from the original chapters on System/360 architecture, automatic interrupts, and number systems. Useful reference material is given in the Appendix, and an index is included. Requests for copies of IBM publications and forms should be made to the IBM branch office serving your locality. A form for readers' comments is provided at the back of this publication. If the form has been removed, comments may be addressed to IBM Corporation, DPD Education Development, Education Center, Endicott, New York 13760. ©Copyright International Business Machines Corporation 1965, 1969. ii Preface This student text is an introduction to System/360 assembler language coding. It provides many examples of short programs shown in assembled form. Some elementary programming techniques and the specific instructions illustrated in the programs are discussed in simple, relatively nontechnical terms. Much of the text is based on information in IBM System/360 Principles of Operation (A22-6821). This includes a brief review of relevant System/360 concepts and descriptions of selected assembler language instructions for arithmetic, logical, and branching operations. Standard (fIxed-point), decimal, and floatingpoint arithmetic are discussed. The book also includes an elementary introduction to assembler language and the assembler program, and chapters on base register addressing and on program linkages and relocation. The coding of many other common programming techniques, such as the use of branches, loops, and counters, is shown. The use of macro instructions is demonstrated, but not covered in detail. Program flowcharting and input/output operations are beyond the scope of the book. The publication is a sampler rather than a comprehensive textbook. It is intended for supplementary reading for the student in a regular course of study on System/360 assembler language coding, and for the novice programmer. In general, the reader will fInd that the program examples are quite simple at the beginning of each chapter, or -major subject division, and become progressively more complex. If the going seems difficult, it is suggested that he simply skip to the next subject and come back later. The student should have access to two IBM System/360 System Reference Library (SRL) manuals for reference purposes: the Principles of Operation and the assembler specification manual for one of the System/360 operating systems. (All publications and their form numbers are listed at the end of the Preface.) He should also be familiar with fundamental concepts of data processing and the basic operating principles of System/360. Two IBM programmed instruction (p. I.) courses, or their equivalent, are prerequisite to a full understanding of this student text: Computing System Fundamentals and Introduction to System/360. The student who is not enrolled in a comprehensive programming course will fmd the P. I. book Fundamentals of Programming a valuable guide to problem analysis and program flowcharting. The text and programs of this book have been revised throughout, mainly to reflect changes in programming conventions attributable to the development of System/360 operating systems. Chapter 1 is new, and several sections in other chapters have been entirely rewritten. The sample programs have been reassembled under the widely used Disk Operating System (DOS). As far as possible, usages limited to DOS have been avoided, and the programs and text in general are applicable to System/360 models 25, 30, 40,50,65, and 75, under any of the operating systems. IBM publications that may be useful to the student are: IBM System/360 Principles of Operation (SRL manual A22-6821) IBM System/360 Reference Data (card X20-1703) IBM System/360 System SumflUlry (SRL manual A22-6810) Number Systems (Student Text C20-1618) Introduction to IBM System/360 Architecture (Student Text C20-1667) Introduction to System/360 (P.1. Course R29-0256 through -0259) Computing System Fundamentals (p. I. Course R29-0241 through -0249) Fundamentals of Programming (P. I. Course R29-0019) System/360 Assembler Language Coding (P. I. Course R29-0231 through -0235) The form numbers of the assembler specification manuals for the various System/360 programming systems are: Basic Programming Support (Tape System)-C24-3355 Basic Operating System-C24-3361 Tape Operating System } C24-3414 Disk Operating System Operating System-C 28-6514 Contents 1 1 1 1 4 5 5 5 Chapter 1: Introduction .. What Is Assembler Language? Machine Language . . . . Assembler Language .. . Why Learn Assembler Language? . The Assembler Program . . . . The System Environment .. Functions of the Assembler Final Processing . . . . . . Use of the Coding Form . . . . An Assembler Language Program Writing the Program . . . . .. The Assembly Listing . . . . . Error Analysis by the Assembler. . Modifying an Assembler Language Program . 9 10 12 15 Chapter 2: System/360 Review Main Storage . . . . . . . . . . . Bytes and Data Field Lengths Addressing . . . . . . . . . . Positioning on Integral Boundaries . Central Processing Unit . . . . . . . . General and Floating-Point Registers . Arithmetic and Logical Unit . . . Program Execution . . . . . . . . . Sequential Instruction Execution. Branching . . . . . . . . . . . . Instruction Format . . . . . . . . Generation of Main Storage Addresses Interruptions and the Program Status Word Hexadecimal Numbers . . . . . . Hexadecimal Code . . . . . . Hexadecimal Number System. 17 17 17 18 18 19 19 19 23 23 23 23 24 25 26 26 26 Chapter 3: Fixed-Point Arithmetic Addition and Subtraction . . . . . Multiplication . . . . . . . . . . . Multiplication and Division with Decimal Points. Shifting and Data Manipulation Branches and Decision Codes The Condition C~de . . . . A Sorting Procedure . . . . Further Decisions: The Social Security Problem Simple Loops: Finding a Sum . . . . . . . . Case Study: Averaging a List of Temperatures Questions and Exercises . . . . . . . . . . . . 29 29 32 34 37 39 39 Chapter 4: Programming with Base Registers and the USING Instruction ., The USING Instruction iv 6 7 9 39 42 45 48 50 51 51 An Example . . . . . . . . . . . . . . . . . . . . More than One Base Register . . . . . . . . . . . Separate Base Registers for Instructions and Data. Questions and Exercises . . . . . . . . . . . . . . 52 54 56 58 Chapter 5: Decimal Arithmetic . . . Addition and Subtraction in Decimal Decimal Multiplication . . Decimal Division . . . . . Shifting of Decimal Fields. Shifting to the Right . . Shifting to the Left Decimal Division with Shifting Format and Base Conversions. Decimal Comparison: Overtime Pay The Social Security Problem in Decimal . The "Indian" Problem. . Questions and Exercises. . . . . . . . . 60 61 63 64 66 66 67 68 69 71 72 73 74 Chapter 6: Logical Operations on Characters and Bits. Alphameric Comparison: An Address Sort Logical Tests. . . . . . . . The Wallpaper Problem. . . . . Setting Bits On and Off. . . . . A Self-Checking Number Routine. A Final Example. . . . . Questions and Exercises . . . . . . 75 76 78 78 79 80 82 84 Chapter 7: Edit, Translate, and Execute Instructions. 85 The Edit Instruction. . . . . . 86 The Edit and Mark Instruction. 91 92 The Translate Instruction How it Works. . . . . . . . 92 An Example . . . . . . . . 93 The Translate and Text Instruction and the Execute Instruction . . . . . . . . . . . . . . . . . .. 97 An Assembler Application of Translate and Test and Execute . . . . . . . . . . . . . . . . 100 Processing Variable-Length Blocked Records 102 Questions and Exercises . . . . . . . . . . . 105 Chapter 8: Subroutine Linkages and Program Relocation......... Subroutine Linkages. . . . Standard Linkage Registers Program Relocation . . . . The Linkage Editor. . . The CALL and PDUMP Macros. Reading a Dump . . . . . . . . 107 108 111 115 115 116 117 Communication between Separate Programs Questions and Exercises . . . . . . . . 120 124 125 126 130 134 Appendix............ System/360 Machine Instructions Condition Code Settings Extended Mnemonic Codes . . . EBCDIC Chart . . . . . . . . . System/360 Assembler Instructions. Types of Assembler Language Constants 139 139 140 140 140 142 142 Chapter 9: Floating-Point Arithmetic Floating-Point Numbers .. Floating-Point Instructions Questions and Exercises . . Answers to Questions and Exercises 135 Index . . . . . . . . . . . . . . . . . . 143 FIGURES Assembly Ustings of Programs 1-2 1-5 1-7 3-2 3-5 3-9 3-10 3-11 3-12 PROGA PROGC PROGB STOCK GROSS INTA INTB SHIFTA SHIFTB 3-15 3-18 3-19 3-20 3-21 3-22 SORT FICAI FICA2 SUMA SUMS SUMC 3-23 AVGTEMP 4-1 4-3 PROGE PROGF 4-4 LOOPA 5-3 5-5 STOCK 1 INTC 5-6 AVG 5-7 CONVERT 5-8 OTPAY 5-9 FICA3 5-10 INDIAN 6-1 SORTABC The program in Figure 1-1 . . . . . . . . . . . . . . . . . . . . . . . The program rewritten with deliberate errors . . . . . . . . . . . . . The same program modified to store the binary contents of register 6. The program in Figure 3-1 . . . . . . . . . . . . . . . . . . . . . . . A program to perform binary (fixed-point) multiplication. . . . . . . A program involving binary multiplication and division with the result rounded off. A different version of the program of Figure 3-9, using a scale modifier for constants. A program to separate three quantities stored in one fullword . . . . . . . . . . . . Modified version of the program of Figure 3-11, making it operate correctly with negative quantities. . . . . . . . . . . . . . . . . . . . . . . . . . . . . A program to carry out the sorting procedure chartel! in Figure 3-14 . A program based on the flowchart in Figure 3-17 . . . . . . . . . . . A much better version of the program to calculate Social Security tax. First version of a program to form the sum of 20 numbers . . . . . . Second version of program to form the sum of 20 numbers . . . . . . Third and shortest version of program to form the sum of 20 numbers, using the BXLE instruction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A program to compute average monthly temperature, which takes into account the possibility of omitted readings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . , A program to show how the assembler calculates and supplies addresses of all storage operands .. An incomplete program with an Origin (ORG) assembler instruction to simulate a length of over 4096 bytes, thus requiring two base registers . . . . . . . . . . . . . . . . . . . . ., Program with separate base registers for processing and data, showing how a base register can be used to provide indexing for loop control . . . . . . . . . . . . . . . . . . . . . . . . . , The decimal arithmetic program in Figure 5-2 . . . . . . . . . . . . . . . . . . . . . . . . . , A program that performs decimal multiplication; step-by-step results to be expected during execution are shown in the comments field . . . . . . . . . . . . . . . . . . . . . . . . . . , Assembled program showing decimal division and "shifting"; step-by-step results to be expected during execution are included in the comments field . . . . . . . . . . . . . . . . . . . . , Assembled program showing various instructions for changing the format of data; contents of registers 5 and 6 to be expected during execution are given in the comments field . . . . ., Assembled program that computes a man's gross pay, including any overtime pay, in decimal arithmetic; results expected during execution are shown in the comments field . . . . . . , Assembled program to calculate Social Security tax in decimal arithmetic; results expected during execution are shown in the comments field . . . . . . . . . . . . . . . . . . . . .. Assembled program to compute compound interest, with counting in binary and calculations in decimal arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. A program to sort three 13-character items into ascending sequence on keys in the middle of each item . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ., 3 12 15 30 32 34 35 37 38 41 43 44 45 46 47 48 52 54 56 61 63 68 69 71 72 73 76 v 6-3 6-6 ACCTNO FORMAT 7-14 SORTABC2 7-16 MAILLIST 7-18 ASSMBLR 7-19 VARBLK 8-1 8-3 8-5 8-7 LINK 1 LINK2 LINK3 LINK4 8-11 MAIN 1 8-12 SUBR 9-7 SHORTFP 9-9 LONGFP A self-checking account number routine that recalculates a check-digit and verifies it. . . . .. A program that checks a decimal field at NUMBER for validity and converts a composite field at COMB into separate binary and packed decimal quantities ., . . . . . . . . . . . . .. A program to sort three fields named A, B, and C into ascending sequence on file-character keys in each field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A program to print names and addresses. . . . . . . . . . . . . . . . . . . . . . . . . . . .. A program to break down the operands of an assembler language instruction into its constituent parts, using TRT and EX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. A program to prepare for printing a series of variable-length blocked records, each consisting of four fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Listing of a single program that consists of a main, or calling, routine and a subroutine. . . The program of Figure 8-1 modified to give the subroutine a choice between two return points A program with a subroutine that averages a series of numbers . . . . . . . . . . . . . . . A slightly different version of the program in Figure 8-5, modified by use of two macro instructions, CALL and PDUMP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The same main program assembled separately; the EXTRN assembler instruction and the Load macro have been added. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. The same subroutine assembled separately; the START and ENTRY assembler instructions and the PDUMP macro have been added . . . . . . . . . . . . . . . . . . . . . . . . . . . .. Assembly listing of a program to perform simple computations in short floating-point arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Assembly listing of the same program as in Figure 9-7, modified to perform all computations in long floating-point arithmetic. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 80 83 95 98 101 103 108 109 112 116 120 121 131 133 Other Figures Useful f9r Reference 1-1 1-3 1-6 2-1 2-2 2-3 An assembler language program as prepared by the programmer . . . . . . . Assembly of a problem program, PROGA. . . . . . . . . . . . . . . . . . . Assembly listing of diagnostic error messages for the program in Figure 1-5 Sample data formats . . . . . . . . . . . . . . . . . . . . Integral boundaries for halfwords, words, and doublewords Functions of the central processing unit 2-4 General and floating-point registers 2-5 Fixed-point number formats . 2-6 Packed decimaI"number format .. 2-7 Zoned decimal number format .. 2-8 Short and long floating-point number formats 2-9 Fixed- and variable-length logical information 2-10 Machine instruction formats . . . . . . . . . 3-14 Program flowchart of a method of sorting three numbers into descending sequence 3-17 Program flowchart of a procedure for computing Social Security tax . . 4-2 Symbol cross-reference table constructed and listed by the assembler . . . . . . . 5-1 Formats of packed and zoned decimal numbers . . . . . . . . . . . . . . . . . . 6-4 Alphabetic input for COMB that can be viewed as two numbers: 12345678 and binary 11001010 6-5 A flowchart of the steps required to solve the problem 7-1 Results of Editing source data in left-hand column . . . . 7-2 Editing results with an asterisk as the fill character . . . . 7-3 Editing results with blank fill and the insertion of commas 7-4 Editing results with blank fill and the insertion of comma and decimal point . 7-5 Editing results with blank fill, comma and decimal point insertion, and significance starter. 7-6 Editing results with blank fill, comma and decimal point insertion, significance starter, and CR symbol for negative numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-7 Same with asterisk fill . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-8 Editing results showing the blanking of zero fields by the use of two additional instructions 7-9 Same with zero fields filled with asterisks . . . . . . . . . . . . . . . . . . . . . . . . . . vi 2,7 5 13 17 18 19 19 20 20 21 21 21 24 40 42 53 60 82 82 86 87 87 87 88 89 89 89 89 7-10 7-11 7-12 7-13 8-8 8-9 8-10 8-13 8-14 8-15 9-1 9-2 9-3 9-4 9-5 9-6 Examples of multiple edits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Examples of the application of the Edit and Mark instruction to get a floating currency symbol Baudot teletypewriter code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Table for translation of Baudot code to EBCDIC . . . . . . . . . . . . . . . . . . . . . . . . Hex dump of registers and storage produced by execution of the PDUMP macro in the program in Figure 8-7 Hex dump of the program (Figure 8-7) loaded at 3000 . . . . Hex dump of the program (Figure 8-7) loaded at 4000 . . . . . . . First dump produced by the subroutine in Figure 8-12, SUBR . . Second dump produced by the subroutine in Figure 8-12, SUBR . . . . Dump produced by the main program in Figure 8-11, MAIN 1 . . . . . Assembly listing of decimal integers specified as short floating-point constants. . A listing of the same examples as in Figure 9-1, showing them in the comments field in a form that is easy to read. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The same values shown as negative numbers . . . . . . . . . . . . . . . . . . . . . . . Some fractional and mixed decimal numbers expressed as short floating-point constants Some long floating-point constants . . . . . . . . . . . . . . . . . . . . Some decimal values with exponents expressed as floating-point constants. . . . . . . . 90 91 92 93 117 118 118 122 122 122 127 128 128 129 129 129 TA8LES 2-1 2-2 2-3 3-1 7-1 8-1 9-1 9-2 Extended Binary Coded Decimal Interchange Code (EBCDIC) for Graphic Characters Hexadecimal Code . . . . . . . . . . . . . . . . . . Hexadecimal and Decimal Integer Conversion Table . Masks for testing various states of the condition code Summary of Editing Functions . . . . . . . . . . . . DOS Linkage Registers . . . . . . . . . . . . . . . . Equivalent Values of the Characteristics of Some Floating-Point Numbers Instruction Set for the System/360 Floating-Point Feature . . . . . . . 22 26 27 39 88 111 127 130 vii Chapter 1: Introduction WHAT IS ASSEMBLER LANGUAG E? Machine Language Assembler Language A computer is a willing servant. It will invariably and reliably do exactly what it is told to do, as long as it is told in its own language. This is true of any computer. Let's take a quick look at the language that System/360-the machine itself-understands. If an IBM System/360 computer is given the instruction 1B67, it will subtract whatever amount is in register 7 from the amount in register 6. When the operation is finished, the contents of register 7 will be the same as they were originally, but the contents of register 6 will be the difference between the two original quantities. The code 1B signifies to the computer (1) just what operation it is to perform, (2) what format it can expect the two quantities to be in, and (3) whether they are in registers or in main storage. Specifically, 1B indicates that the computer is to subtract two 32-bit binary numbers, both of which are in registers. The two quantities to be operated on are called operands. The one that is written first is called the first operand and in this case is in register 6. The second operand is in register 7. The instruction 1B6 7 is in machine language. It is a representation in the hexadecimal number system (base of 16) of the actual binary arrangement in the computer. The computer responds to it in a particular way because its circuitry has been designed to do so whenever it senses this combination of signals. Let's take another example of a machine language instruction, say 5A20B02A. The operation code 5A causes the computer to add two 32-bit binary numbers (the first in a register and the second in main storage) and to place the result in the first operand location. In this case, the first operand is in register 2, and the second operand is in main storage, beginning at the location designated by OB02A. Not many years ago all programs were written in machine language. The most valuable tool the programmer had was an eraser. He was concerned with an enormous amount of clerical detail. He had to remember dozens of numerical codes for the computer operations and try not to make a mistake when using them. He had to keep track of the storage space he used for instructions, data, and work areas, and actually calculate any addresses he needed to refer to in his program. Revising a program (a very frequent occurrence then, as it is now), often meant changing every address that followed the revisions. All this detail meant many errors and much time spent on checking, calculating, keeping tables, and other clerical tasks. The realization that the computer itself was better suited than man for doing this type of clerical work led to the development of assembler languages (each computer has its own assembler language). In System/360 assembler language, every operation code is written in alphabetic letters that are easy to remember, called mnemonics, and the addresses of locations in storage can be given symbolic names like PAY, HOURS, and RATE by the programmer. The machine language instruction 1B67 would be written in assembler language as SR 6,7 (SR for Subtract Register). The instruction 5A20B02A might be A 2,CON (A for Add), with another instruction to define CON as a certain value. We do not have to say where it is-the computer will take care of that. An assembler language program as prepared by a programmer is shown in Figure 1-1. The operations to be performed start in column 10, the operands in column 16. As we said at the beginning, however, the computer cannot understand any language except its own machine language. Therefore, a program that translates our symbolic program into machine language or object code is needed. Such a program, actually a component part of an IBM System/360 operating system, is brought from the system "library" into a separate area in main storage when needed, and it does the job. This program is called an assembler. Besides translating the problem program statements into machine language, it calculates storage locations for all instructions, keeps track of any symbols like CON that are used, and performs a number of other necessary functions. The program written by the programmer is not executed during the assembly process; it will be executed later, after further processing. Figure 1-2 shows the listing produced by the assembler for our sample program. Machine Instructions All the columns to the left of the statement number (STMT) column are in machine language. The LOC, ADDR1, and ADDR2 columns have to do with address arithmetic handled by the assembler, and will be discussed later. The heart of our program has been translated into the code headed OBJECT CODE. The circled area at the left contains the code for every executable instruction in the entire program. What we mean by an executable instruction is one that, when the problem program is run, will tell the computer to perform an actual operation in the machine itself. Each of the executable instructions has a corresponding System/360 machine operation code; these operation codes Introduction 1 IBM IBM System/3S0 Assembler Coding Form )(28-6509 'pAoG-4 PROGRAM PUNCHING I JJ. .JONes PROGRAMMER INSTRUCTIONS DATE l 1 I GRAPHIC 1 PUNCH STATEMENT No_ 8 I 20 25 ViS./1J6 b-lt1!ti1i : ! ! I ) I [ ~' ! i i I I I I i I S; I !I tJ I Ai ; i [l;)A i7~! i i I !c'ON 1Ja ~i ~.eS()LJ7i 8IN'tZ, J)ee l j : j i I i I I l'S! i OCI ll>Q I P't ~I ~I i . i i I I ! ! I ']) , I I I I i I I 65 73 71 80 II ) I ! i ! I I 1 , : i V;. (J}./;VE ~r ro [)~I rf1:;fLi. !3II:l>1 o~ ~Je i I II Ii i i; i : i ' 1 : I' ! ! Ii I \ I I i I I 1 I ~y ~ .1, I I i I i I II 1 ; I I !I , I ! ' i ! ! I ' i . ' I : I I I i , I ! i i ,I I I I ! 1 I i ! i I ! I. I I ! 1 . 1 " 1 i I "'il I I, I, I,f. I" ]" , I I I I i I 1 I'·' I I i I I I ~'.~ : : 1< i I I I i ! I i i I i :1 : I I ./; I . I " i 1 : I I I I i II i iii I I I : I I I I ! I I I I If I I 1.1 i ' r F}·J i I I I I ! i I i 60 I I ~iG/,4J l ldentificationSequence "" ; I I I :I i OF CARD ELECTRO NUMBER , I I I ; i 11 II I I I I i i i ILDA~' ~i£G-/S -ri£~ ~ : I IA)1) .L~ 1~t1S ~IS t 1J1,e'~C7 idA ~It; 1L111'/V! 1£1> /~~ WOI"-:E ~!.e!,l.A-: 71VE: ~ .!>;.J~1 lei ... IWtA12~ IA. CltlW 45 40 I Il " I ~5 30 : , ~/~ \6 17/1TfL£ \ i~~it.£ 5!r~Ai7 IV~i .f~ok!;:~"",~1 ! i : 1c;~~7 I~il I ' i : ! [ I iii ~~~ !J1.f!4(tJ, iii t IP'(>OGA 14 10 1 C Opero;lnd Opel'Qtion 1 1 I i I,., .[ Figure 1-1. An assembler language program as prepared by the programmer are represented by the first two characters (the first two hexadecimal numbers, really) in the circled object code. In the example, the executable instructions include one of the branching instructions (BALR, op code 05), Load (L, op code 58), Add (A, op code SA), one of the Shift Left instructions (SLA, op code 8B), Subtract (S, op code 5B), Store (ST, op code 50), and so on. In assembler language, the executable instructions are called machine instructions. Not counting floating-point arithmetic instructions, System/360 assembler language has about 100 different machine instructions. It is fairly easy to recognize and remember all of the mnemonics for them-certainly easier than remembering the machine language operation codes. Some other examples are C for Compare, CVD for Convert to Decimal, SH for Subtract Halfword, STH for Store Halfword, M for Multiply, and BC for Branch on Condition. A full list of System/360 machine instructions appears in the Appendix; floating-point instructions are given in the chapter on that subject. Each machine instruction and what it does is described in complete detail in the IBM Systems Reference Library (SRL) manual IBM System/360 Principles of Operation (A22-6821). Many will be described in this book in nontechnical language, but not in complete detail. Assembler Instructions What about the TITLE, START, and USING instructions that have not generated any object code in the assembly 2 listing in Figure 1-2? The mnemonic TITLE does not even show up at all (it was in the source program), but we see that the assembly listing has the heading ILLUSTRATIVE PROGRAM. TITLE is an instruction to the assembler that tells it to print a heading or title at the top of each page in the listing. Similarly, START and USING are instructions to the assembler; these concern the addressing plan it is to follow. Although they will affect the way in which the assembler assigns addresses, they will have no direct function in the execution of the problem program. In contrast to machine instructions, they are called assembler instructions. They may be defined as instructions to the assem bIer program itself. Skipping the EO] for the moment, we see the mnemonics DC (Define Constant) and DS (Define Storage). These two instructions are also assembler instructions. DC's generate object code for the values they define, but no operation codes. DS's actually reserve storage space of a specific size, but they too do not generate operation codes. In other words, DC's cause the assembler to create object code for actual values and DS's reserve actual storage spaces, but they do not themselves give rise to any action during program execution. Instead, they are used for either information or space by other instructions in the program. If we look again at the assembly listing, we see that DATA, CON, RESULT, etc., are operands of some of the executable instructions. Assembler-instruction mnemonics, which are also listed Machine ihstrlJctions in machine language LOC instructiolJs in ass em bIer longvu96 2 PROGA 3 BEGIN 00010 00010 00010 00010 00010 00010 00010 00011 00011 00011 00011 4 B022 B02A 0001 B026 B02E B032 B036 B03E _ __ 00000019 OOOOOOOF OOOOOOOA 00012~:.t!A.:.u.. 000124 000128 00012C 000130 000134 000138 000140 000100 Machille STMT OBJECT CODE OOOOOOOC 0000004E 00124 0012C 00001 00128 00130 00134 00138 00140 LOAD REGISTER 2 ADD 10 THIS HAS EFFECT OF MULTIPLYING BY 2 NOTE RELATIVE ADDRESSING 5 6 7 8 9 10 11 12 13 14+* 15+ 16 DATA 17 18 19 20 21 22 23 CON RESULT BIN1 BIN2 DEC CONVERT TO DECIMAL END OF JOB LEVEL 3-0 F'15' F '10' F F '12' F'78' D BEGIN Figure 1-2. Assembly listing of the program in Figure 1-1. The executable instructions (see text) are circled in both assembler language and the machine language translation. in the Appendix, generally suggest their purpose. USING indicates a particular register to be used by the assembler for keeping track of storage addresses, EJECT tells the assembler to start a new page in the program listing, and END to terminate the assembly program. Assemblerinstructions and the functions of the assembler program are described fully in each of the SRL assembler language manuals for the various IBM operating or programming support systems (see Preface for list). It should be explained that variations of the System/360 assembler program are available for different operating systems and sizes of computers. Basically, they all work similarly, but some are more flexible and versatile than others. Many differences do exist, however, in the input/output (I/O) programming for different systems. Largely for this reason, the subject of I/O will not be covered in this book. Macro Instructions In an entirely different category, System/360 assembler language includes another type of instruction, called a macro instruction or macro. If a programmer writes a series of instructions for a routine that will be needed again and again during the program, he does not have to code the entire sequence each time. He can make up his own code name to represent the sequence, and, by using his code word in a single statement whenever it is needed, he can cause the sequence of instructions to be assembled and inserted. Incorporated in the system library, the sequence can also be used in entirely separate programs and by all programmers associated with a computer installation simply by writing one statement in the source program. The mnemonics used for macro instructions are often unique to an installation. Some macros are prepared and supplied by IBM; they have mnemonics like EOJ, READ, WRITE, OPEN, CLOSE, WAIT, WAITF, DTFCD, DTFIS, etc. The mnemonics for both the user-prepared and the IBMsupplied macros constitute an extension to System/360 assembler language. The macros supplied by IBM are mainly for procedures that affect other components of the IBM operating system, like the supervisor and the input/output control system, and they ensure accuracy and consistency in maintaining the interrelations within the operating system. The EOJ (End of Job) in the program example is a supervisor macro instruction. It generates just two statements, which are indicated in the listing by plus signs. The flISt is simply for identification, and the second is the executable Supervisor Call instruction (SVC, op code OA). Most I/O routines are long and complicated, and for any particular device and operating system are programmed in exactly the same way in program after program. Most of the macros supplied by IBM are for these I/O routines. Some of the Disk Operating System (DOS) macro instructions we shall use in this book, besides EOJ, are CALL, SAVE, RETURN, and PDUMP. The book does not cover the preparation of new macros, but shows, in the chapter on subroutines, another method for reusing a sequence of instructions. However, the programmer can save much time and effort by using the macros that are already available in his system library. Their use will also ensure accuracy and standardization of frequently repeated procedures. Summary To summarize, these are the three kinds of instructions used in System/360 assembler language, and what each does: 1. A machine instruction specifies an actual operation Introduction 3 to be performed by the computer when the object program is executed. The operation may be arithmetic, or the comparison, movement, or conversion of data, or performing a branch. The instruction generates executable object code. 2. An assembler instruction specifies an instruction to the assembler program itself and is effective only at assembly time. It does not generate executable object code. 3. A macro instruction specifies a sequence of machine and assembler instructions to perform a frequently needed routine. The machine instructions generate executable object code. Why Learn Assembler Language? The most important single thing to realize about assembler language is that it enables the programmer to use all System/ 360 machine functions as if he were coding in System/360 machine language. Of all the programming languages, it is closest to machine language in form and content. The highlevel languages such as FORTRAN, COBOL, and PL/I are problem-oriented rather than machine-oriented. Their languages are much like English or mathematical notation. Depending on what is involved, one statement in these languages may be compiled into a series of two or eight or fifty machine language instructions. The problem-oriented languages have the advantage of letting the programmer concentrate on what he wants to accomplish and not on how it is to be done by the computer, and they may save considerable time in programming, program modification, and program testing. Choice of a programming language in any given situation usually involves weighing the cost of 4 programming time against the cost of machine time. A com· plex mathematical problem that can be run in a few minutes and will be run only once is a very different situation from a program that runs for several hours and will be repeated every week. Here we can appreciate one of the important advantages of assembler language over the high-level languages: its efficient use, in the hands of a skillful programmer, of computer storage and time. High-level languages produce generalized routines so that a wide range of data processing needs can be met with a minimum of programming effort. A routine can be written in assembler language exactly to fit some particular data processing need, thus saving storage space and execution time. As we shall see in the course of this book, there are often many ways of accomplishing the same data processing results. Sometimes the overall programming requirements of a computer installation strain its capacity. If the particular problem arises of either not enough main storage space or not enough processing time, the problem may be solved by assembler language. In such a situation, its flexibility permits the programmer to choose those programming techl1iques that will provide just the kind of economy needed-time or space. A knowledge of assembler language has some important benefits for a programmer working in a high-level language. It can be helpful to him in analyzing and debugging programs. It also enables him to include certain assembler language routines in his program to meet special systems or other requirements. THE ASSEMBLER PROGRAM The System Environment As a Hrst step in the assembly process, the handwritten problem program has to be put into a form that can be read by the computer system. Punched cards are frequently used; they are convenient and easy to substitute in case of error. The program is punched by a keypunch operator, each line on a separate card. The original program and these cards are called the source program, or the cards may be called the source deck. The assembler program is loaded into main storage and executed, using the source deck as input. It is important to realize that th"e basic function of the assembler is to translate the source program. It does not execute the program. The final output of the assembler program is called the object program. It contains the machine language equivalent of the source program, and is put on cards, tape, or disk by a system output device. It is this object program that will later be subjected to further processing and will itself be executed. The assembler output also includes several listings to aid the programmer, which are produced by a line printer. Figure 1-3 shows the assembly process in outline. Before going into detail about the functions of the assembler, it may be helpful to look at the overall system environment into which a programmer-written problem program goes. As we already know, the assembler program is a component of the IBM operating system. It functions under the control of another, very important component, the control program. (To avoid confusion in terminology, perhaps it should be mentioned that the control program is often referred to as the control system. The supervisor is one element of the- control program, and the most powerful. The job control program is another element.) 0) 0 0 0 0 Programmer writes source program, named PROGA, on coding sheets. The System/360 control program is, in effect, a traffic director. It supervises the movement of data, the assignment of all the devices attached to the system, and the scheduling of jobs. Working under a set of priorities for various kinds of situations, it handles the flow of operations in the central processing unit (CPU), with the aim of keeping it constantly busy and the entire system at its most productive level. The control program sees to it that needed IBM processing programs, like the assembler program and the linkage editor program, are brought from the system library and loaded into main storage at the right time. These two kinds of programs combined-that is, the control program and the processing programs-make up what is called the IBM operating system (or, for smaller installations, the IBM programming support system). With an operating system at work, the programmer is relieved of practically all concern about having on hand for either processing or execution of his problem program the system resources available at his installation. Functions of the Assembler During execution of the assembler program, the assembler scans the source program statements a number of times. Its Hrst activities are to process any macro instructions it fmds, and to store the complete sequences of individual instructions generated by the macros. They are then standing by, ready to be inserted into the assembled problem program at the points indicated by the programmer. Afterwards, the assembler proceeds to translate the one-for-one assembler language statements into machine instructions. MAIN STORAGE Keypunch operator copies PROGA source program on cards. Assembler language translator program is loaded into main storage. PROGA source program is read into a work area of the assembler program. Assembler program is executed, using PROGA source program as input data. Output of assembler program is PROGA object program and assembly listings. Object program may be on cards, tape, or disk. Figure 1-3. Assembly of a problem program, PROGA. Note that PROGA is not executed during the assembly process. Introduction 5 Briefly, here is how the assembler works. It reads source statements as input data, checking for errors and flagging them for further processing. At fIrst, it translates the parts of the input (such as operation codes) that do not need further analysis or calculation. Meanwhile, it constructs a table of all the symbols used, in which it collects, as it goes along, such information as each symbol's length, its value or location, and the statements in which it is referred to. From this table and other analyses of the source statements, the assembler can then assign relative storage addresses to all instructions, constants, and storage areas. It uses a location counter for this purpose (see LOC column in Figure 1-2). It does all the clerical work involved in maintaining the base register addressing scheme of System/360 computers. During its operations, the assembler continues to note errors and to resolve any it can. As shown in Figure 1-3, there are two kinds of output from the assembler program. The primary output is the object program in machine language; included with it is certain information tabulated by the assembler, which is needed for relocating the program to an actual main storage location and for establishing links with separate programs. (This information will later be passed on to the linkage editor for the next step in the processing.) The other output from the assembler is a series of printed listings that are valuable to the programmer for documentation and analysis of his program: 1. The listing of the program (samples of these will be shown throughout this book) includes the original source program statements side by side with the object program instructions created from them. Most programmers work from this assembly listing as soon as it is available, hardly ever referring to their coding sheets again. 2. Probably next in interest to the programmer is the diagnostics listing, which cites each statement in which an error condition is encountered and includes a message describing the type of error. 3. The cross-reference listing shows the symbol table compiled by the assembler. 4. The external symbol dictionary (ESD) describes any references in the problem program needed for establishing links with separate programs. It is possible for the programmer to combine his program with others, or to use portions of separate programs, or to make certain portions of his program available to other programs. The ESD is part of the tabular information passed on to the linkage editor. 6 It always contains at least the name of the problem program, its total length, and its starting address on the assembler's location counter. 5. The relocation dictionary (RLD) describes the address constants that will be affected by program relocation. This list is also passed on to the linkage editor. We have now reached the end of the assembly process. What happens next? Our object program is in relocatable form, but it will not be executable until it has been processed by the linkage editor. Final Processing The linkage editor program is another component of the IBM operating system. Its functions, which will not be described fully here, can provide great flexibility and economy in the use of main storage. The linkage editor also makes it possible for a long and complicated program to be divided into separate sections, which can be programmed, assembled, and debugged by different programmers, and then linked together to be executed. The linkage editor is loaded into main storage and operates as a separate program under control of the control program, just as the assembler did. Input to the linkage editor may be a single assembled program or many separate programs. The linkage editor works on one after the other, building up composite dictionaries of ESD and RLD data to resolve all references between individual programs and to set up necessary linkages. It also searches the system library and retrieves any programs referred to. It relocates the individual programs as necessary in relation to each other, assigns the entire group to a specific area of main storage, and modifIes all necessary address constants to the relocated values of their symbols. After completion of the link-editing, our problem program can be loaded into main storage and executed under supervision of the control program. Unless specified otherwise, each machine instruction is executed in sequence, one after the other. If there is separate input data, it can be brought in by I/O instructions in the program. Output-the results of program execution-also requires I/O instructions. The scope of this book does not go beyond the assembly process. For a clear understanding of the detailed program examples, however, it is essential for the reader to be able to visualize at just what stage in the entire process each action occurs. For this reason, the complete process from programmer-written program to its final execution has been outlined in this section. USE OF THE CODING FORM the assembler permits nearly complete freedom of format. However, lining up entries as shown makes it simpler to read a program, and following the form permits the programmer to painlessly observe the few essential rules required by the assembler. Some of these rules are as follows. (1) The entries must be in proper sequence. (2) If a name is used, it must begin in column 1. (3) The entries must be separated by at least one space, because a space (except in a comment or in certain terms enclosed in single quotes) is the signal to the assembler that it has reached the end of an entry. (4) Spaces must not appear within an entry, except as noted. (5) A statement must not extend beyond the statement boundaries, normally columns 1 - 71. We have been using that word "normally" because the programmer can override the specific column designations by an ICTL (Input Format Control) assembler instruction, which can specify entirely different begin, end, and continuation columns. A statement is normally continued on a new line in column 16, with some character (often an X) inserted in column 72 of the preceding line. Since the normal spacing is generally the most convenient and is easiest for a keypunch operator to follow, we shall use the spacing indicated on the form throughout this book. The purpose of using a name in a statement is to be able to refer to it elsewhere. It is a symbol of eight characters or Assembler language programs are usually written on special coding forms like the one in Figure 1-1, which will be repeated here for convenience. Space is provided at the top for program identification and instructions to keypunch operators, but none of this information is punched into cards. The body of the form is completely keypunched in corresponding columns of 80-column cards. Use of the Identification-Sequence field (columns 73 - 80) is optional and has no effect in the assembled program. Program identification and statement sequence numbers can be written in part or all of the field. They are helpful for keeping the source cards in order and will also appear on the assembly listing. Indeed, the programmer can use an assembler instruction (ISEQ) to request the assembler to check the input sequence of these numbers. The statement field is for our program instructions and comments, which are normally limited to columns 1 - 71. Each statement can be continued on one or more lines, depending upon which assembler program is used. A statement consists of: 1. A name entry (sometimes) 2. An operation entry (always) 3. An operand entry (usually) 4. Any comment we wish to make It isn't necessary to use the spacing shown on the form, since IBM Systeml360 A••• mbl.r Coding Form IBM X286509 I--PR_OGAA_M_L...L:J?J~~O~G-.4':""';"'______"----_ _ _ _ _ _ _ _---'--_ _- - - - - - 1 J J. PROGRAMMER I ...JOIVE..S- DATE I PUNCHING /INSTRUCTlONS I I I I I GRAf'HIC PUNCH I I I I Identifi(:otion- B , I 10 Operotion 14 16 O~~nd 20 30 S7 ~Rtr .2.sz1 iii BA ~ 1jl.~i; I IPRoGA U sll W6 :2 • ~ CII ! • A I i -Jt-.:1.111 i I I ! ! i I i Iii i 45 i II : iii I Ii: ! I 40 I! i ! :15 T I I. 1 I' T I I j iii Ii i Sequence 50 1 I! I' I I I I I i i : , I i i , I ! I I L i I ~ AIN t I , 1 I I ! ! i : : Iii I I I I I ! M i i pr2.S'/ iii: iii : I I I: i I ! I· lA/Nt ! I I I I ?)Si ])EJd i I ' I F I! T : I I ! : I I I Did I I' i I i i .i : Ii I ! I DSI . i ~Mf}, ,; '79'/ i I 1);i 8eG-IN: I 1'-;-. i I I i! ! • : i i • ! i • i i ! I i I i i I I I : :I i : ! I j! i I I i I ! I i! I. i I I I I I I I I I ! ! I I I I ! I j I I i I i I I i i i I I I i I I I I I i I I . i I I I I I : i: : i I ! II i : , I ! ! I i j i I I i i I I I I I I;: i j i I ! i i I I I i I i ! I i I , , Aj~ 1. ~ i i i.' ! ! i I I I i ! i I ! i I I i I i I I i I I I ! i Figure 1-1. An assembler language program as prepared by the programmer Introduction 7 less, created by the programmer. It may identify a program, a location in storage, a specific value, or a point in the program to which the programmer may plan to branch. As we know, the assembler compiles a symbol table, keeping track of where each name is defined and where each reference to it appears. These references occur when the name is used as an operand in an instruction. Each instruction must include an operation entry, which may be a machine, assembler, or macro mnemonic. They are limited to five characters in length (some systems allow longer macro mnemonics) and begin in column 10 of the form. Operand entries are always required for machine instructions and usually for assembler instructions. They begin in column 16 and may be as long as necessary, up to the maximum statement size the assembler can handle. An operand entry is the coding that identifies and describes the data to be acted upon by the instruction. All operands in a statement must be separated from each other by commas, without blank spaces. Comments may be used freely, at the programmer's 8 discretion, to document the purpose of coding or the approach used in the programming. These notes can be helpful during debugging and other phases of program checkout and also during later maintenance of a program. They have no effect in the assembled program, but are only printed in the assembly listing. If a programmer wishes to include extensive notes in the printed record, he can use entire lines just for comments by inserting an asterisk in column 1 of each line. A comment that is part of an instruction statement may begin anywhere beyond the operand entry, provided there is at least one blank space after the operand. Most programmers like to line up all comments in some convenient column for easier reading. A word of caution may be in order about leaving "illegal" blanks in operand entries. If, in our sample program, we were to write: L 2, DATA LOAD REGISTER 2 the assembler, on finding a blank after the comma, would interpret DATA as the first word of the comment and give us an error message MISSING OPERAND. AN ASSEMBLER LANGUAGE PROGRAM Writing the Program Let's look at some of the actual instructions in the program in Figure 1-1. This program does not have any particular task to accomplish; it merely demonstrates the use of some serviceable assembler language instructions. In later chapters, each program example will be prefaced by a clear statement of the problem to be solved, which is good practice, but for now let's just get started. The TITLE assembler instruction in the first line will cause a heading to be printed on every page of the assembly listing. The heading will be ILLUSTRATIVE PROGRAM, which is written within single quotes as the operand entry. The START instruction specifies to the assembler what the initial value of the location counter for this program should be. Although zero is the usual practice, we specify decimal 256, which is the equivalent of hexadecimal 100. The assembler assumes in most cases that any numeral we use in an operand is a decimal number, unless specified otherwise. We are also using the START statement to give our program a name, PROGA, which is another good programming practice. The next two instructions are important ones that will appear in every program. To understand their effect, we had better look at these two statements in the order in which they will actually take effect. During assembly, the USING statement will tell the assembler: (1) that it should use register 11 for address calculations and (2) that the address of the next machine instruction, which is L 2,DATA, will be in register 11 when PROGA is finally executed. To fulfill this promise, the Branch and Link (BALR) will, when PROGA is ?/l.O~ PROGRAM J. 'J. PROGRAMMER i! JDIII e.S" : iPRO<.AA ! I I I ~A~~ 1111 .. rzJ Iv S IIJ6 ~r.:t!ti I' '- ~!. 13/N t I! i; i I ! ii' j I I .~ I , I ! ' I I , '! i ! , I ! : I I I I i !:. 'I I : i ! I I ~()J l1)C 81 if i I !DC: 81N2: I lll~ M7~' J)Ee. ! i 1 ; : I iii lpr.zS: 1 i ~z;ts I I : Iii JJ:'J2' 1pt'1Si' I I I ~ I' ! I I ~ i eth> I! I I! I I' ~~ rllia I Iii il iii !: L i I : I i ( I '\ executed, actually put the address of the L 2,DATA instruction into register 11. The BALR and USING combination is generally the most efficient way of setting up a register for use as a base register in the System/360 addressing scheme. This subject will be discussed in detail in a separate chapter. So much for the preliminaries. The body of the program starts with the L 2,DATA instruction. Lis the mnemonic for the machine instruction Load, which in this case will place in register 2 the contents of a location in storage that has the symbolic address DATA. Looking down the coding sheet, we see that DATA is in the name field of a DC assembler instruction that defines a constant value of 25, occupying four bytes. The name DATA refers to the address of the first byte; the length is implied by the F, for full word. The A 2,CON is a similar type of instruction. It adds to register 2 the contents (that is, the constant value 10) of a full word that has its first byte at the symbolic location CON. The next. instruction (SLA 2,1) is quite different. SLA stands for the algebraic Shift Left Single. The contents of register 2 are to be shifted left one binary place. There is no symbolic address in this case; the second operand simply indicates the extent of the shift. The Subtract instruction that comes next(S 2,DATA+4) includes an example of relative addressing: the address is given relative to another address. This address is specified as four bytes beyond DATA. Looking at the constant area of the program, we see that four bytes (one full word) beyond DATA there is indeed another fullword constant, the number 15. The Store instruction (ST 2,RESULT) specifies that the contents of register 2 are to be placed in a storage area with the symbolic address RESULT. Looking below again, we see RESULT in the name field of a DS for a fullword area. As a machine operation, Store has one somewhat unusual feature. In most System/360 machine instructions, the result of an operation replaces the first operand. In Store, however, the result is stored in the second operand location. The same is also true of CVD, which we shall come to shortly. The following two statements, the Load and the Add, present no new assembler language concepts. They will form a sum in register 6, in the same way as before. The Convert to Decimal (CVD) converts the contents of register 6, which are binary, to a decimal number, and stores the result in the eight-byte area beginning at DEC. The operation of the machine instruction CVD requires that this location be a double word , aligned on a doubleword boundary. More on this later. The next instruction, EOJ, is a macro instruction that will, after PROGA has been executed, return control to the supervisor, so that the computer can immediately go on Introduction 9 with other jobs. EO] is the last executable ( or machine) instruction in our program example. The DC's and DS's follow the executable part of the program in a group, as is customary. These assembler instructions were discussed earlier in this chapter. Define Storage (DS) is used to defme and reserve an area of storage, which may be used during execution of the program for work areas or for storing a varying value. Define Constant (DC) allows us to introduce specific data into a program (a constant simply means an unchanging value). Each DC and DS must have a type specification that designates the particular data format in which it is to be entered into internal machine storage. Some of the data formats are the eight-bit character code (type C), the four-bit hexadecimal code (type X), zoned decimal numbers (type Z), packed decimal numbers (type P), and fixed-point binary numbers (type F or H). A more complete list appears in the Appendix and in the assembler language specification manuals listed in the Preface. In the program at hand and in Chapter 3, where we shall be studying System/360fixedpoint binary operations, however, all the constants are type F or H (the F is for fullword, H for halfword, implying length as well as giving the type). Fixed-point operations work on fixed-length operands and in most systems require that they be located in storage on halfword, fullword, or double word boundaries. In other words, the addresses must be multiples of 2, 4, or 8. When F or H is used to signify the length of a DC or DS (D for doubleword may also be used in a DS), the assembler will perform the necessary alignment, skipping a few bytes if necessary. In our program all the F -type constants and areas will be on four-byte boundaries. The DS at DEC will reserve an eight-byte space, aligned on a doubleword boundary. If the programmer modifies these terms, for example, by specifying 2F instead of D, the assembler will not perform alignment, and it becomes the programmer's responsibility. The END assembler instruction specifies that nothing further follows, and it terminates the assembly process. The END instruction must always be the last statement in a source program. The Assembly Listing Let's inspect the assembly listing, repeated here as Figure 14, to see how the assembler handled things. We see that, except for the TITLE statement, the original source program has been reproduced without change on the righthand side of the listing. The object code created from the source instructions is listed under that heading. The location counter setting of each statement is shown in the leftmost column. The address of the second operand in each instruction is under the heading ADDR2. (All first operands here happen to be in registers.) All entries to the left of the statement number column are in the hexadecimal number system, which is the alphabet, so to speak, of System/360 machine language. The assembler instructions TITLE, START, USING, and END did not produce any object code, and, as we can see from an inspection of the location counter readings, do not use any space in the object program. The location shown on each of these lines is simply the current setting of the location counter, which, after assembly of each instruction that will use storage space, was updated to show the next available byte. The START 256 sets the assembler's location counter to hexadecimal 000100, or 100. The object code that is actually at location 100 (in bytes 100 and 101) and will be at the equivalent location in core storage is 05BO, the machine language translation of the BALR instruction. Hex 05 is the BALR operation code, B is register 11 (B is the ILLUSTRATIVE PROGRAM LOC 000100 000100 000102 000102 000106 00010A 00010E 000112 000116 00011A OOOllE 000122 000124 000128 0001ZC 000130 000134 000138 000140 000100 OBJECT CODE AoDR 1 ADDR2 05BO 5820 5A20 8B20 5B20 5020 5860 5A60 4E60 B022 B02A 0001 B026 B02E B032 B036 B03E OAOE 00000019 OOOOOOOF OOOOOOOA OOOOOOOC 0000004E 00124 0012C 00001 00128 00130 00134 00138 00140 STMT SOURCE ST ATEMENT 2 PROGA 3 BEGIN 4 5 6 7 8 START 256 BALR 11,0 USING *,11 2,DATA L A 2,CON 2,1 SLA 2,DATA+4 S ST 2,RESULT 9 6,BINI 10 L A 6, BI N2 11 6, DEC CVD 12 EOJ 13 CHANGE 14+* 360N-CL-453 EOJ 15+ SVC 14 16 DATA DC F' Z5' DC 17 F' 15 • DC 18 CON F' 10' F 19 RE SUL T DS 20 BINI DC F • 12' 21 B I N2 DC F'78' 22 DEC DS D 23 END BEGIN Figure 1-4. Assembly listing of the program in Figure 1-1 10 LOAD REGISTER 2 ADD 10 THI S HAS EFFECT OF MULTIPLYING BY 2 NOTE REL AT IVE ADDRESS ING CONVERT TO DECIMAL END OF JOB LEVEL 3-0 hex equivalent of 11), and 0 is register zero (which means, in effect, no register and no branching). This instruction is in the RR (register-to-register) machine format, which has a length of two bytes and looks like this in storage (contents are shown here in hex rather than binary): Op Code e 1 o 5 Reg1 I B 7 8 Reg2 I 0 11 12 Op Code I 1 8 IS The subscripts 1 and 2 refer, both here and in other instruction formats, to the fKst and second operands. In the RR format both operands are in registers. After the BALR was assembled, the location counter read 102, which was the next available byte, and stayed that way until additional object code was generated. USING did not generate object code, so 102 was the setting when the L 2,DATA was assembled. The asterisk in the USING means the current, updated location counter setting, which at that point was 102. The next instruction, Load, is the frrst that will actually process program data. It is an RX (register-and-indexedstorage) instruction, which has a machine format of four bytes. It occupies bytes 102 to 105: Op Code 15 o Reg1 Index2 Base2 81 7 8 2 Displacement2 \0\BI0 1112 IS 16 19 20 (The subscript 10 is used to indicate a number in the decimal system. A subscript of 16 is used for hexadecimal, and 2 for binary.) SLA is in the RS (register-and-storage) machine instruction format, also four bytes in length, and it is in bytes lOA, lOB, lOC, and 100. 2 2 31 In this format, the first operand is in a register, the second in main storage. Reading the assembled bytes from left to right, we have the op code 58 for Load and register 2 for the register to be loaded, and the remaining code gives the address of the second operand. Zero means there is no index register, B (hex for 11) is the base register, and 022 is the displacement in bytes. The effective address, formed by the assembler, is the sum of the contents of the base register (102), the contents of the index register (0 or no register), and the displacement (022). These add up to hexadecimal 124. Looking down to the assembled location of DATA, we see that it is 124, as it should be. The Add instruction that follows is also in the RX format, and again no index register is used. The base register contents of 102 (25810 ) plus the displacement of02A (4210 ) gives a sum of 12C (30010 ), which is the location of CON. o B 7 8 1112 IS 16 19 20 31 The op code is 8B and the frrst operand is in register 2. The next four bits are never used in a shift operation, the next four could be used for a base register for the second operand but are not in this case, and the fmal 001 merely indicates a shift of one binary place. No provision is made for using an index register in this format. As we shall see later, some RS instructions, like Branch on Index High (BXH) and Store Multiple (STM) , have a third operand. The next five instructions are all in the RX format and offer no new concepts. The reader may wish to brush up on hexadecimal numbers and check that the displacements have been computed correctly, taking into account the relative address in the Subtract. We can see even in this simple example how much of the clerical burden the assembler takes over by automatically assigning base registers and calculating displacements. The assembled entries for the OC's are simply the requested constants, in hexadecimal. We note that the DS entered nothing, but simply reserved space. A study of the address for the double word constant at DEC shows that boundary alignment was performed. The fullword constant BIN2 was placed at 138. Counting in hexadecimal, BIN2 occupies four bytes: 138,139, 13A, and 13B. Although 13C was available for DEC, it is not on a doubleword boundary, nor is 13D, 13E, or 13F. So the assembler skipped these four bytes and assigned DEC to 140.. The END assembler instruction terminates the assembly of the program. The operand indicates the point to which we wish control to be transferred when the program is loaded. In this case, it is to our first instruction in the object program, named BEGIN, where actual execution of the program is to begin. Note that the location counter shows the value 100 at the END statement. Introduction 11 ERROR ANALYSIS BY THE ASSEMBLER Certain kinds of programming errors can be detected rather simply by the assembler. In fact, some errors make it impossible for the assembler to generate an instruction and complete the assembly. The assembler carries out the assembly as completely as possible, regardless of the number of errors, even if the first error detected makes it impossible for the object program to be executed. The idea is that, if there are more errors, the programmer needs to know about all of them, not just the first one the assembler encounters. Figure 1-5 is the assembly listing of a program written deliberately with a number of errors in it, to demonstrate what the assembler can do and how it announces its findings. The first announcement is made on the program listing itself, where every statement with a discernable error is followed by a line prominently reading *** ERROR from the assembler program, which was described earlier in this chapter. The diagnostics listing for our program example is shown in Figure 1-6. The assembler always gives a summary message, shown at the bottom, of the total number of statements in error. If no errors are found, the happy message NO STATEMENTS FLAGGED IN THIS ASSEMBLY is printed at the end of the symbol crossreference table, and no diagnostic listing is printed. Let's see what the assembler has to tell us about statement 6. The message is on the first line of the diagnostics listing: UNDEFINED OPERATION CODE. We check the mnemonic for Shift Left Single and find of course that we should have written SLA instead of SLS. The assembler program cannot assume SLA was meant; we might have meant SL, SLR, SLL, or any other valid operation code. Since it cannot tell what was intended, it flags the statement and does not assemble the object code or even assign space to the instruction. The diagnostic message for statement 7 is UNDEFINED SYMBOL. The undefined symbol is DATA4. This is accepted as a valid symbol, since it follows all the rules governing the writing of symbols. That is, it begins with a letter, uses only letters and numbers, does not contain special characters or blanks, and isn't more than eight characters. Looking at the symbols or names listed in the source statements, we see we have defined DATA and remember that we intended DATA+4 as the address of the *** When the programmer is warned of the existence of an error, he can often see rather quickly what is wrong. Looking over the listing in Figure 1-5, he would probably notice at once that the comma between operands in statement 9 is omitted, and that statement 19 is, from his point of view (but not the assembler's), a bundle of typographical keypunching errors. Some errors may not be so obvious. To help the programmer analyze them, the assembler prints a separate listing of diagnostic messages. This is part of the output LOC ADDR 1 ADDR2 OBJECT CODE 000100 000100 05BO 000 102 000102 5820 BOlE 000106 5A20 B026 *** ERROR 00010A 0000 0000 *** ERROR 00010E 5020 B02A 000112 0000 0000 *** ERROR 000116 5A60 B02E 000 11 A 0000 0000 *** ERROR 000 11 E OAOE 000120 00000019 000124 4CB016EA *** ERROR 000128 oaOOOOOA 00012C *** ERROR 00012C 0000 0000 *** ERROR 000130 0000004E 000138 000140 00000019 *** ERROR 000100 STMT SOURCE STATEMENT 1 PROGC 2 BEGIN 3 4 5 6 START BALR USING L A SLS 256 11 ,0 *,11 2,DATA 2,CON 2.1 00000 7 S 2,DATA4 0012C 00000 8 9 ST L 2,RESULT 6BIN1 00130 00000 10 11 A CVD 6,BIN2 6,BIN1 00120 00128 *** *** *** *** EOJ 12 CHANGE LEVEL'3-0 13+* 360f\l-CL-453 EOJ 14+ SVC 14 15 DATA DC F' 25' DC F'9876543210' 16 *** *** *** 00000 17 CON 18 RESULT DC DS F' 10' 19 IN1 C ' 12 ' 20 BIN2 0 22 DATA DC OS DC 23 END 8EGIN 21 DEC *** Figure 1-5. Assembly listing of the program rewritten with deliberate errors 12 F' 78' F' 25' DI AGNOSTICS STMT 6 7 9 9 11 16 18 18 19 19 22 ERROR CODE MESSAGE IJQ088 IJQ024 IJQ039 IJQ039 IJQ024 IJQ017 IJQ03l IJQOO9 IJQ039 IJQ018 IJQ023 UNDEFINED OPERATI ON CODE UNDEF INED SYMBOL INVALID DELIMITER INVALID DELIMITER UNDEFINED SYMBOL DATA ITEM TOO LARGE UNKNOWN TYPE MISSING OPERAND INVALID DELIMITER INVAL 10 SYMBOL PREVIOUSLY DEFINED NAME 8 STATEMENTS FLAGGED IN THIS ASSEMBLY Figure 1-6. Assembly listing of diagnostic error messages for the program in Figure 1-5 next constant. To the assembler there is no relationship at all between DATA4 and DATA; they are simply different symbols. But if we write DATA+4, the assembler program will recognize the plus sign as a special character that, among other things, delimits the symbol DAT A. Confronted with DATA4, the assembler does not assemble the object code. This time, however, the valid mnemonic S indicates that this instruction will be in RX format. So the assembler assigns four bytes to the instruction. In statement 9, the Load instruction, we already know that our error was the omission of the comma in 6,BINl. This made the assembler give two identical diagnostic messages: INVALID DELIMITER. From the mnemonic L, the assembler anticipates an RX format, the L to be followed by a register number, a comma, and a storage operand. Finding a B instead of a comma probably led to the first message. What about the second message? What does it mean? Here the error code in the second column of the diagnostic listing may help. The meaning of each message is given in expanded form in a table of error codes in the assembler manuals. (The letters IJQ here simply stand for a particular assembler program, the Disk Operating System D assembler.) If we were to look up IJQ039 in the table, we would find that it means "any syntax error". About a dozen possibilities are listed. An invalid delimiter is the usual error in assembler language syntax, hence the wording of the message. Some other possibilities are (1) an unpaired parenthesis, (2) an embedded blank, (3) a missing delimiter, (4) a missing operand, and (5) a symbol beginning with other than an alphabetic character. Well, the first two obviously don't apply to 6BIN 1, and it would be difficult and unrewarding to make a choice among the others, especially considering the compounded error in the symbol in statement 19. What the two messages signify is that there is no reliable evidence of what was intended or just which specification was really violated. The programmer is amply warned that an error exists. It is his job to make his intentions known. UNDEFINED SYMBOL appears again for statement 11. From the programmer's viewpoint, a reverse situation exists from the one in statement 7. This time the instruction statement is as it should be, but the DC defining the symbol shows IN 1 instead of BIN 1. There is no indication that these are related in any way or that one is not correct. Statement 16 elicits the message DATA ITEM TOO LARGE. This is perfectly clear. The decimal value 9,876,543,210 cannot be contained in a 32-bit binary fullword, and the hexadecimal value shown as four bytes has evidently been truncated. Statement 18 was awarded two error messages: UNKNOWN TYPE when the assembler program found no type designation, and MISSING OPERAND when it scanned further on. Jumping ahead for a moment, we find that statement 22 has the message PREVIOUSLY DEFINED NAME, and we see that DATA has already been given in statement 15. In statement 19 the first letter of each entry is omitted. The messages are INVALID DELIMITER, which may mean almost any error of syntax, and INVALID SYMBOL, which apparently applies to the name INI. What's the matter with IN 1? It begins with a letter and violates no rules we know of. It should be perfectly acceptable to the assembler. We are the only ones who know it is misspelled. Also, when the message UNKNOWN TYPE is available, why single out the operand with its missing F as a syntax error? Four bytes of zeros have been generated. Why did the assembler assign a specific length? Also, apparently no fault was found with the mnemonic C. How is that? The point is precisely that C is a valid operation code. So the assembler, being given this definite "fact" (the most important single fact in any instruction), performs its syntaX scan and other operations as if it were dealing with a Compare. The mnemonic C indicates that the instruction is in the RX format requiring Introduction 13 four bytes, that the first operand must be a number between 0 and 15 followed by a comma, and that the second operand may be a symbol. But the operand field of this Compare instruction contains simply the characters " '12' ". This then is the "symbol" the second message refers to. Indeed, both messages evidently apply to the operand field. To the assembler program nothing is wrong with the name or the operation code mnemonic. For such reasons as these, the diagnostic messages given by the assembler may often seem quite inaccurate from the programmer's point of view. In many cases, the assembler simply does not have enough clues to pin down the precise error, and the messages should not be taken literally. The assembler program was designed to be as helpful as it can be, and the messages are an effort to help the programmer diagnose the trouble. Usually the error flag on the program 14 listing is enough. The programmer will be interested in the message itself only when he cannot identify the mistake. This review of how the assembler analyzes programming errors should also make it clear that many errors are beyond the power of the assembler even to recognize. When we incorrectly write DATA4 for DATA+4, the assembler can detect it, but not if DATA4 itself is a legitimate symbol. If we write SLL for SLA, the assembler will assume that SLL is what we mean; both are valid operation codes with the same format. The ability of the assembler to detect and analyze errors can be very helpful to the programmer. However, the message NO STATEMENTS FLAGGED IN THIS ASSEMBLY cannot be taken to mean that a program has no errors or that it will necessarily produce the right answers when it is executed. MODIFYING AN ASSEMBLER LANGUAGE PROGRAM After a program has been written, assembled, and completely debugged, it frequently happens that some change must be made later. Many types of revisions are simple to make in an assembler language program. But let us see what happens to the locations of instructions and data when even a minor change is made. We shall base the example on the correct version of the program, as it appeared assembled in Figure 1-4. Let us suppose that for some unspecified reason it is necessary to store the sum of BINI and BIN2 in binary before converting it to decimal. We must insert an instruction: ST 6,BINANS just before the CVD. This is a rather simple sort of change and one that is representative of the kind of modification made with routine frequency on many programs. Yet it can have the effect of LOC 000100 000100 000102 000102 000106 00010A oa010E 000112 000116 00011A 00011E 000122 000126 000128 00012C 000130 000134 000138 00013C 000140 000148 000100 OBJECT CODE ADDR1 ADDR2 05BO 5820 5A20 8B20 5B20 5020 5860 5A60 5060 4E60 B026 B02E 0001 B02A B032 B036 B03A B046 B03E OAOE 00000019 OOOOOOOF OOOOOOOA OOOOOOOC 0000004E 00128 00130 00001 0012C 00134 00138 0013C 00148 00140 STMT changing almost every effective address in the program! The insertion of the four-byte instruction "pushes down" the storage spaces for the DC's and DS's, requiring a change in the displacements of all the instructions that refer to the constants. Figure 1-7 is the assembly listing of the modified program. Scanning down the assembled instructions, we see that the displacements have been computed to reflect the change in locations. Continuing the comparison, however, we see that ADDR2 and the displacement in the Convert to Decimal instruction are the same as in the earlier version. Has there been a mistake? The answer is the boundary alignment of the doubleword constants. In the earlier version, it was necessary to skip four bytes to provide an address for DEC that was on a double word boundary. The inserted instruction, in effect, filled that skipped space. The reassembly therefore left the assembled address for DEC unchanged. SOURCE STATEMENT START 256 1 PROGB 2 BEGIN BALR II ,0 3 USING *,11 4 2,DATA L 5 A 2,CON 6 SLA 2,1 7 2,DATA+4 S ST 2,RESULT 8 9 6, BINI L A 10 6,BIN2 ST 6,BINANS 11 CVD 6,DEC 12 EOJ 13 14+* 3 6 0 N- C L-4 5 3 EOJ CHANGE 15+ SVC 14 16 DATA DC F'25' DC 17 F '15 ' DC 18 CON F' 10' 19 RESULT OS F 20 BINI DC F' 12 ' 21 B I N2 DC F' 78 ' OS 22 DEC D 23 B I NANS OS F 24 END BEGIN LOAD REGISTER 2 ADD 10 THIS HAS EFFECT OF MULTIPLYING BY 2 NOTE RELATIVe ADDRESSING END OF JOB LEV EL 3-0 Figure 1-7. Assembly listing of the same program modified to store the binary contents of register 6 Introduction 15 Chapter 2: System/360 Review The reader may find it helpful at this point to review some basic facts about System/360 that are directly relevant to assembler language programming. These are stated as briefly as possible in this chapter and will serve mainly as a reminder. A student who is familiar with the material may skip any or all of the sections without loss. A student who needs more than a reminder is urged to go back to the textbook or course materials he originally studied for an introduction to System/360. The basic structure of a System/360 consists of main storage, a central processing unit (CPU), the selector and multiplexor channels, and the input/output (I/O) devices attached to the channels through control units. For basic information that applies to the material in this book, we are concerned principally with the CPU and main storage. In this chapter, discussion will essentially be limited to these machine units and their basic operating principles. Since a knowledge of hexadecimal numbers is necessary in assembler language programming, these will also be explained. MAIN STORAGE Main storage is also called core or processor storage to distinguish it from storage on tape, disk, or other auxiliary devices. It is closely involved in the operation of the CPU, although it may be either physically integrated with it or constructed as a stand-alone unit. Capacity may be from 8,192 bytes to several million bytes, depending on the system model. Protection features are available that make it possible to protect the contents of main storage from access or alteration. In general, instructions and data are stored along with each other in whatever order they are presented to the machine. Particular areas of storage may be used over and over again by a succession of programs or groups of programs being executed. Each group overlays, or replaces, the instructions and data of the one preceding. The programmer must therefore specify blanks or zeros where he needs them; he can never assume he is writing on a clean slate. During execution of his program, he can obtain a printout or "dump" of an area of storage at any point in the program by use of suitable instructions. Bytes may be handled separately or grouped together in fields. A halfword is a group of two consecutive bytes and is the basic building block of instructions. A word is a group of four consecutive bytes; a doubleword is a field consisting of two words (Figure 2-1). The location of any field or group of bytes is specified by the address of its leftmost byte. Byte 11 1 ooi 00 o d Halfword 11 1 0 1J0 0 0 111 1 0 1Ko 7 8 0 1 01 15 Word 1 11100 100 1111 OOBOO 10111 o 7 8 15 16 a 1~ 3 10011111 00 111 23 2.4 31 Bytes and Data Field Lengths Figure 2·1. Sample data formats The system transmits information between main storage and the CPU in units of eight bits, or a multiple of eight bits at a time. Each eight-bit unit of information is called a byte, the basic building block of all formats. A ninth bit, the parity or check bit, is transmitted with each byte and carries odd parity on the byte. The parity bit cannot be affected by the program; its only purpose is to cause an interruption when a parity error is detected. References in this book to the size of data fields and registers exclude the mention of the associated parity bits. The length of fields is either implied by the operation to be performed or stated explicitly as part of the instruction. When the length is implied, the information is said to have a fixed length, which can be either one, two, four, or eight bytes. When the length of a field is not implied by the operation code, but is stated explicitly, the information is said to have variable field length. This length can be varied in one-byte increments. System/360 Review ] 7 Within any program format or any fixed-length operand format, the bits making up the format are consecutively numbered from left to right starting with the number O. This general information on data formats and field lengths will be supplemented later by further details. Lengths and the specific form of the contents of the fields are discussed in the section on the arithmetic and logical unit, under the headings for logical operations and the specific types of arithmetic. Addressing Byte locations in storage are consecutively numbered starting with 0; each number is considered the address of that byte. A group of bytes in storage is addressed by the leftmost byte of the group. The number of bytes in the group is either implied or explicitly defined by the operation. The addressing arrangement uses a 24-bit binary address to accommodate a maximum of 16,777,216 byte addresses. This set of main-storage addresses includes some locations reserved for the supervisor and other special purposes. How storage addresses are generated is described in the section on program execution. The available storage is normally contiguously addressable, starting at address O. An addressing exception is recognized when any part of an operand is located beyond the maximum available capacity of an installation. Except for a few instructions, the addressing exception is recognized only when the data are actually used and not when the operation is completed before using the data. The addressing exception causes a program interruption. Positioning on Integral Boundaries Fixed-length fields, such as halfwords and doublewords, must be located in main storage on an integral boundary for that unit of information. A boundary is called integral for a unit of information when its storage address is a multiple of the length of the unit in bytes. For example, words (four bytes) must be located in storage so that their address is a multiple of the number 4. A halfword (two bytes) must have an address that is a multiple of the number 2, and doublewords (eight bytes) must have an address that is a multiple of the number 8. For greatest efficiency in storage addressing, address arithmetic is done exclusively in binary. In binary, integral boundaries for halfwords, words, and doublewords can be specified only by the binary addresses in which one, two, or three of the low-order bits, respectively, are zero (Figure 2-2). For example, the integral boundary for a word is a binary address in which the two low-order positions are zero. Variable-length fields are not limited to integral boundaries, and may start on any byte location. low-order Four Bits of Binary Address OO.QQ. 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 Byte Byte Byte Byte Byte Byte Byte Byte Byte Byte Byte h l Halfword Halfword Halfword Halfword Halfword ) l ~ Word Word Word l ~ Double Word Double Word , t Figure 2-2. Integral boundaIies for halfwords, words, and doublewords CENTRAL PROCESSING UNIT The central processing unit (Figure 2-3) contains the facilities for addressing main storage, for fetching or storing information, for arithmetic and logical processing of data, for sequencing instructions in the desired order, and for initiating the communication between storage and external devices. The system control section provides the normal CPU control that guides the CPU through the functions necessary to execute the instructions. The programmer-trainee will probably be glad to know that the result of executing a valid instruction is the same for each model of System/360. ,----, Storage Address .1 I MAIN STORAGE ---r-_J II> operand bits and must have an even address, and the implied register, containing the low-order operand bits, has the next higher address. R Field Reg No. 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 General Registers Floating-Point Registers i64Bitsi 1:::::::,:",:,:",:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:,:.:.:.:.:.:.:.:.:.••:••••.•.:.:.:.:.:.:.) :.:...:...:.:...:.:.:...:.:.•...::.::..:.:., 2 3 4 5 6 7 8 9 10 11 12 13 14 15 :.: .•.:.:.....:.:.:.•.•.:.:..-:.:::::::::::, I:·:·:·:.·.·.·.···· .............•.,.,.,.....................................,..:...:.:-:.:j 1::-:-:-:.·:·.·.··.·.··.·.·.·.·.:.:.:.:-:.:1 ,.;.:.:.; ...............................................•.·.•••.·.•••••.....·.·.·.:.;.:.;.:.:.1 I:·:·:·:·····························:···:··) ,.:.:.::;..:.:.;••.; ...................................···•··············...·······:·.·:·:·:·ii 1:·:-:-:·:·:·;··················-:·:·:·:·:) •.;.•.••;.•.:•.•;.•.••;.;.;•••:.;.:.:.;.:.j 1;:·:;;::;;·;·.·:···.·.·.···.·.···:·;·;·:-;·, 1·:·:;;·',;,:-;,;,;,;,:,;",;,;,;,:::;,::;,:,' ,·:-:·;·:·······················:·;·;·:·.·.1 ,·;·:·;·;······.... ·.···.·.·.··.:·:·;·;.;.:1 ... Instructions Figure 2-4. General and floating-point registers Arithmetic and Logical Unit Generated Computer System ~ Address Control 1-----"""'T'""----.--------1 Fixed-Point Operations 16 General Registers VariableF ixed- Length Operations I Floating-Poi nt Operations Floating-p:int Registers I Figure 2-3. Functions of the central processing unit General and Floating-Point Registers The CPU provides 16 general registers for ftxed-point operands and four [loating-point registers for floating-point operands. Physically, these registers may be in special circuitry, in a local storage unit, or in a separate area of main storage. In each case, the address and functions of these registers are identical. The CPU can address information in 16 general registers. The general registers can be used as index registers, in address arithmetic and indexing, and as accumulators in flXed-point arithmetic and logical operations. The registers have a capacity of one word (32 bits). The general registers are identified by numbers 0-15 and are specified by a four-bit R fteld in an instruction (Figure 2-4). Some instructions provide for addressing multiple general registers by having several R fields. F or some operations, two adjacent general registers are coupled together, providing a two-word capacity. In these operations, the addressed register contains the high-order F our floating-point registers are available for floatingpoint operations. They are identifted by the numbers 0, 2, 4, and 6 (Figure 24). These floating-point registers are two words (64 bits) in length and can contain either a short (one word) or a long (two words) floating-point operand. A short operand occupies the high-order bits of a floatingpoint register. The low-order portion of the register is ignored and remains unchanged in short-precision arithmetic. The instruction operation code determines which type of register (general or floating-point) is to be used in an operation, and if floating-point whether short or long precision. Arithmetic and Logical Unit The arithmetic an'd logical unit can process binary integers and floating-point fractions of fixed length, decimal integers of variable length, and logical information of either fIXed or variable length. Arithmetic and logical operations performed by the CPU fall into four classes: fixed-point arithmetic, decimal arithmetic, floating-point arithmetic, and logical operations. These classes differ in the data formats used, the registers involved, the operations provided, and the way the field length is stated. Data formats are discussed under each of the headings in this section. General information on fteld lengths was given in the section on main storage. Fixed-Point Arithmetic The basic arithmetic operand is the 32-bit fixed-point binary number. Sixteen-bit halfword operands may be specified in most operations for improved performance or storage utilization (see Figure 2-5). To preserve precision, some products and all dividends are 64 bits long. A System/360 Review 19 fixed-point number is a signed value, recorded as a binary integer. It is called fIxed point because the programmer determines the fixed positioning of the binary point. In both halfword (16 bits) and word (32 bits) lengths, the first bit position (0) holds the sign of the number. The remaining bit positions (1-15 for halfwords and 1-31 for full words) are used to designate the value of the number. Positive fixed-point numbers are represented in true binary form with a zero sign bit. Negative fIxed-point numbers are represented in two's complement notation with a one bit in the sign position. In all cases, the bits between the sign bit and the leftmost significant bit of the integer are the same as the sign bit (i. e. all zeros for positive numbers, all ones for negative numbers). The filled-in examples in Figure 2-5 show the equivalent of decimal +62 and -62 in fIXed-point halfwords. Halfword Is I o Integer 1 15 Full Word 31 1 Decimal arithmetic lends itself to data processing procedures that require few computational steps between the source input and the documented output. This type of processing is frequently found in commercial applications. Because of the limited number of arithmetic operations performed on each item of data, conversion from decimal to binary and back to decimal is not justified, and the use of registers for intermediate results yields no advantage over storage-tostorage processing. Hence, decimal arithmetic is provided, and both operands and results are located in storage. Decimal arithmetic includes addition, subtraction, multiplication, division, and comparison. Decimal numbers are treated as signed integers with a variable-field-length format from one to 16 bytes long. Negative numbers are carried in true form. The decimal digits 0-9 are represented in the four-bit binary-coded-decimal (BCD) form by 0000-1001, respectively, as follows. Digit Binary Code 0 0000 0001 0010 0011 0100 1 2 Integer o Decimal Arithmetic 3 4 II 0 000 \1\111 0000 0011 1110 1111 1100 0010 I = + 62 10 1=- 6210 Figure 2-5. Fixed-point number formats. In the example the negative number is in two's complement notation Because the 32-bit word size readily accommodates a 24-bit address, fixed-point arithmetic can be used both for integer operand arithmetic and for address arithmetic. This combined usage provides economy and permits the entire fIXed-point instruction set and several logical operations to be used in address computation. Thus, multiplication, shifting, and logical manipulation of address components are possible. Additions, subtractions, multiplications, divisions, and comparisons are performed upon one operand in a register and another operand either in a register or from storage. Multiple-precision operation is made convenient by the two's-complement notation and by recognition of the carry from one word to another. A word in one register or a double word in a pair of adjacent registers may be shifted left or right. A pair of conversion instructions-Convert to Binary and Convert to Decimal-provides transition between decimal and binary number bases without the use of tables. Multiple-register loading and storing instructions facilitate subroutine switching. 20 Digit 5 6 7 8 9 Binary Code 0101 0110 0111 1000 1001 The codes 1010-1111 are not valid as digits and are reserved for sign codes. The sign codes generated in decimal arithmetic depend upon the character set code used. When the extended binary coded decimal interchange code (EBCDIC) is used, the codes are 1100 for a plus sign and 1001 for a minus. (When the USASCII set, expanded to eight bits, is preferred, the sign codes are 1010 and 1011. The choice between the two code sets is determined by a mode bit.) Decimal operands and results are represented by four-bit BCD digits packed two to a byte (see Figure 2-6). They appear in fields of variable length and are accompanied by a sign in the rightmost four bits ofthe low-order byte. Operand fields may be located on any byte boundary, and may have length up to 31 digits and sign (l6 bytes). Operands participating in an operation may have different lengths. Packing of digits within a byte and use of variable-length fields within storage results in efficient use of storage, in High-order Byte Low-order Byte Figure 2-6. Packed decimal number format. The three-byte example shows decimal value +89,732 increased arithmetic performance, and in an improved rate of data transmission between storage and files. Decimal numbers may also appear in a zoned format in the regular EBCDIC eight-bit alphameric character format (Figure 2-7). This representation is required for I/O devices that are character-set sensitive. A zoned format number carries its sign in the leftmost four bits of the low-order byte. The zoned format is not used in decimal arithmetic operations. Instructions are provided for packing and unpacking decimal numbers so that they may be changed from the zoned to the packed format and vice versa. Logical Operations and the EBCDIC Character Set Figure 2-7. Zoned decimal number format. The decimal number +89,732 requires five bytes Logical information is handled as fixed- or variable-length data. It is subject to such operations as comparison, translation, editing, bit testing, and bit setting. When used as a fIXed-length operand, logical information can consist of either one, four, or eight bytes and is processed in the general registers (Figure 2-9). A large portion of logical information consists of alphabetic or numeric character codes, called alphameric data, and is used for communication with character-set sensitive I/O devices. This information has the variable-field-length format and can consist of up to 256 bytes (Figure 2-9). It is processed storage to storage, left to right, an eight-bit byte at a time. The CPU can handle arty eight-bit character set, although certain restrictions are assumed in the decimal arithmetic and editing operations. However, all character-set sensitive I/O equipment will assume either the extended binary coded decimal interchange code (EBCDIC) or the USA Standard Code for Information Interchange (USASCII) extended to eight bits. Use of EBCDIC is assumed throughou t this book. Floating-Point Arithmetic Fixed-Length Logical Operand (One , Four, or Eight Bytes) High-order Byte Low-order Byte ~I zone[] Di~t IZone IDigit ~I 8 9 7 3 + 2 11111 1000 1111 11001 1111 1 0111 11111 0011 1100J0010 Byte 1 Byte 2 Byte 3 Byte 4 Byte 5 Floating-point numbers occur in either of two fixed-length formats-short or long. These formats differ only in the length of the fractions (Figure 2-8). They are described in detail in the chapter on floating-point arithmetic. Floating-point operands are either 32 or 64 bits long. The short length permits a maximum number of operands to be placed in storage and gives the shortest execution times. The long length, used when higher precision is desired, more than doubles the number of digits in each operand. Four 64-bit floating-point registers are provided. Arithmetic operations are perfonned with one operand in a register and another either in a register or from storage. The result, developed in a register, is generally of the same length as the operands. The availability of several floating-point registers eliminates much storing and loading of intermediate resu~ts. Short Floating.Point Number (One Word) IS o I 1 I Characteristic Fraction 7 8 31 Long Floating-Point Number (Double Word) Is I o 1 Characteristic· I Fraction 7 8 Figure 2-8. Short and long floating-point number formats 63 Logical Data Variable-Length Logical Operand (Up to 256 Bytes) Character I Character I ~~ ~~ ~ Ir------..., Character 16 Figure 2-9. Fixed- and variable-length logical information EBCDIC does not have a printed symbol, or graphic, defined for all 256 eight-bit codes. When it is desirable to represent all possible bit patterns, a hexadecimal representation may be used instead of the preferred eight-bit code. The hexadecimal representation uses one graphic for a four-bit code, and therefore, two graphics for an eight-bit byte. The graphics 0-9 are used for codes 0000-1001; the graphics A-F are used for codes 1010-1111. EBCDIC eight-bit code for characters that can be represented by well-known symbols is shown in Table 2-1. The hexadecimal equivalents and punched card code are also shown. For other symbols, System/360 control characters, and unassigned codes~ see the complete 256-position EBCDIC chart in the Appendix. It may be observed from the table that the EBCDIC collating sequence for alphameric characters, from lower to higher binary values, is (1) special characters, (2) lower case letters, (3) capital letters, and ( 4) digits, with each group in its usual order. System/360 Review 21 Table 2-1. Extended Binary Coded Decimal Interchange Code (EBCDIC) for Graphic Characters Graphic character EBCDIC 8-bit code Bit Positions 0123 4567 blank ¢ ( + & ! $ * ) ; - , % ? : # @ , = " a b c d e f g h i j k I m n 0 p q r s t 22 0100 0100 0100 0100 0100 0101 0101 0101 0101 0101 0101 0110 0110 0110 0110 0111 0111 0111 0111 0111 0111 1000 1000 1000 1000 1000 1000 1000 1000 1000 1001 1001 1001 1001 1001 1001 1001 1001 1001 1010 1010 0000 1010 1011 1101 1110 0000 1010 1011 1100 1101 1110 0000 1011 1100 1111 1010 1011 1100 1101 1110 1111 0001 0010 0011 0100 0101 0110 0111 1000 1001 0001 0010 0011 0100" 0101 0110 0111 1000 1001 0010 0011 Hex equivalent Punched card code 40 4A 4B 4D 4E 50 SA 5B 5C 5D 5E 60 6B 6C 6F 7A 7B 7C 7D 7E 7F 81 82 83 84 85 86 87 88 89 91 92 93 94 95 96 97 98 99 A2 A3 no punches 12-8-2 12-8-3 12-8-5 12-8-6 12 11-8-2 11-8-3 11-8-4 11-8-5 11-8-6 11 0-8-3 0-8-4 0-8-7 8-2 8-3 8-4 8-5 8-6 8-7 12-0-1 12-0-2 12-0-3 12-0-4 12-0-5 12-0-6 12-0-7 12-0-8 12-0-9 12-11-1 12-11-2 12-11-3 12-11-4 12-11-5 12-11-6 12-11-7 12-11-8 12-11-9 11-0-2 11-0-3 Graphic character EBCDIC 8-bit code Bit Positions 0123 4567 u v w x y z A B C D E F G H I J K L M N 0 P Q R S T U V W X y Z 0 1 2 3 4 5 6 7 8 9 1010 1010 1010 1010 1010 1010 1100 1100 1100 1100 1100 1100 1100 1100 1100 1101 1101 1101 1101 1101 1101 1101 1101 1101 1110 1110 1110 1110 1110 1110 1110 1110 1111 1111 1111 1111 1111 1111 1111 1111 1111 1111 0100 0101 0110 0111 1000 1001 0001 0010 0011 0100 0101 0110 0111 1000 1001 0001 0010 0011 0100 0101 0110 0111 1000 1001 0010 0011 0100 0101 0110 0111 1000 1001 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 Hex equivalent A4 AS A6 A7 A8 A9 C1 C2 C3 C4 C5 C6 C7 C8 C9 Dl D2 D3 D4 D5 D6 "D7 D8 D9 E2 E3 E4 E5 E6 E7 E8 E9 FO F1 F2 F3 F4 F5 F6 F7 F8 F9 Punched card code 11-0-4 11-0-5 11-0-6 11-0-7 11-0-8 11-0-9 12-1 12-2 12-3 12-4 12-5 12-6 12-7 12-8 12-9 11-1 11-2 11-3 11-4 11-5 11-6 11-7 11-8 11-9 0-2 0-3 0-4 0-5 0-6 0-7 0-8 0-9 0 1 2 3 4 5 6 7 8 9 PROGRAM EXECUTION Interplay of equipment and program is an essential consideration in System/360. The system is designed to operate with a control program that coordinates and executes all I/O instructions, handles exceptional conditions, and supervises scheduling and execution of multiple programs. System/360 provides for efficient switching from one program to another, as well as for the relocation of programs in storage. To the problem programmer, the control program and the equipment are indistinguishable. The CPU program consists of instructions, index words, and control words that specify the operations to be performed. Some of its functions will be discussed here. The format of the machine instructions is basic to an understanding of how the CPU executes them and how it forms addresses of operands in main storage. A double word called the program status word (PSW) contains detailed information required by the CPU for proper program execution: the instruction address, the condition code setting, etc. It is stored at a fixed location. If a problem program aborts and the contents of storage are printed out, the PSW can be inspected by the programmer. He will find much information to help him analyze the trouble, induding a code that identifies the cause of the interruption. The interruption system permits the CPU to respond automatically to conditions arising outside of the system, in I/O units, or in the CPU itself. Interruption switches the CPU from one program to another by changing not only the instruction address but all essential machine-status information. Programs are checked for correctness of instructions and data as the instructions are executed. (The types of errors involved are not detectable during assembly.) This policing action distinguishes and identifies program errors and machine errors. Thus, program errors cannot cause machine checks: each of these types of error causes a different type of interruption. Sequential I nstruction Execution Normally, the operation of the CPU is controlled by instructions taken in sequence. An instruction is fetched from a location specified by the instruction address in the current PSW. The instruction address is then increased by the number of bytes in the instruction fetched to address the next instruction in sequence. The instruction is then execu ted and the same steps are repeated using the new value of the instruction address. A change from sequential operation may be caused by branching, interruptions, etc. Branching The normal sequential execution of instructions is changed when reference is made to a subroutine, when a two-way choice is encountered, or when a segment of coding, such as a loop, is to be repeated. All these tasks can be accomplished with branching instructions. Provision is made for subroutine linkage, permitting not only the introduction of a new instruction address but also the preservation of the return address and associated information. Decision-making is generally and symmetrically provided by the Branch on Condition instruction. This instruction inspects a two-bit condition code in the PSW, that reflects the result of a majority of the arithmetic, logical, and I/O operations. Each of these operations can set the code in any one of four ways, and the conditional branch can specify any of these four settings, or any combination of them, as the criterion for branching. Loop control can be performed by the conditional branch when it tests the outcome of address arithmetic and counting operations. For some particularly frequent combinations of arithmetic and tests, the instructions Branch on Count and Branch on Index are provided. These branches, being specialized, provide increased performance for these tasks. Instruction Format The length of an instruction fonnat can be one, two, or three halfwords. It is related to the number of storage addresses necessary to specify the location of all operands in the operation. Operands may be located in registers or in main storage, or may be a part of an instruction. An instruction consisting of only one halfword causes no reference to main storage. A two-halfword instruction provides one storage-address specification; a three-halfword instruction provides two storage-address specifications. All instructions must be located in storage on integral boundaries for halfwords. Figure 2-10 shows the five basic instruction formats, called RR, RX, RS, SI, and SS. These format codes express, in general terms, the operation to be perfonned. RR denotes a register-to-register operation; RX, a register-and-indexed-storage operation; RS, a register-and-storage operation; SI, a storage and immediate-operand operation; and SS, a storage-to-storage operation. An immediate operand is one contained within the instruction. F or purposes of describing the execution of instructions in the SRL manual IBM System/360 Principles of Operation (A22-6821), operands are designated as first and second operands and, in the case of branch-on-index instructions, third operands. These names refer to the manner in which the operands participate. The operand to which a field in an instruction format applies is generally denoted by the number following the code name of the field, for example, R 1, Bv L 2 , D2 · In each format, the first instruction halfword consists of two parts. The first byte contains the operation code. The length and format of an instruction are specified by the System/360 Review 23 fIrst two bits of the operation code: Bit Positions (0-1) 00 01 10 11 Instruction Length One halfword Two halfwords Two halfwords Three halfwords Instruction Format RR RX RS or SI SS The second byte is used either as two 4-bit fields or as a single 8-bit field. As shown in Figure 2-10, this byte can contain the following information: Four-bit operand register specification (Rb R2, or R3 ) Four-bit index register specification (X2 ) Four-bit operand length specification (Ll or L2 ) Eight-bit operand length specification (L) Eight-bit byte of immediate data (I2) In some instructions a four-bit field or the whole second byte of the first halfword is ignored. In the Branch on Condition instruction, which may be used in either the RR or RX format, the first four bits of the second byte are used as a 4-bit mask field (M 1 in the following diagram). This mask tests the four settings of the condition code and is used to determine whether a branch will or will not be made. RR I 07 (BCR) M1 Ax I 47 (BC) j Op Code :0 M1 X2 1112 1=;r;J 7 8 11 12 15 Re'gister Operand 1 Address of Operand 2 I 78 :0 11 12 15 16 , 51 Address of Operand 1 I~~ I I I I 1 I I 1 I I Register Register Address of I Operand 1 Operand 3 Operand 2 1 ~I Op Code :0 1 R1 I I R3 11 12 7 8 , B2 15 16 I : Length: Operand 1iOperand 2 Op Code :0 I I L1 7 8 L2 11 12 I B2 1516 1920 I Generation of Main Storage Addresses To permit the ready relocation of program segments and to provide for the flexible specifications of input, output, and working areas, all instructions referring to main storage have been given the capacity of employing a full address. The address used to refer to main storage is generated from the following numbers, all binary: Base Address (B) is a 24-bit number contained in a general register specified by the program in the B field of the instruction. (One way to insert a base address into a register is to specify a BALR operation at the beginning of a program. The BALR operation does just that, getting the address of the next sequential instruction from the current program status word, no matter where the program may have been relocated.) The B field is included in every address specification. The base address can be used as a means of relocation of programs and data. It provides for addressing the entire main storage. The base address may also be used for indexing purposes. Index (X) is a 24-bit binary number contained in a general register specified by the program in the X field of 1 Ar..1ress of O..,erand 2 A I 19 20 31 32 35 36 47' I I Length Address of Operand 1 I i=cJ 78 31 31 Address of Operand 1 B1 15 16 I 02 19 20 , ~: j S5 'I Op Code 12 B1 01 :~0------~7-8------15LI-6-1~9-20---------3~I' 1 R5 31 1920 I : Immediate : I Operand I Op Code 15 I : Rx~i-O-Pc-o-de~j};l~R1~=X=2~I=B2~1~A=0=2==~1 R2 1112 In all instructions, the second and third halfwords always have the same format: four-bit base register designation (Bl or B2), followed by a 12-bit displacement (D! or D2). 24 RR Register Register Operand 1 Operand 2 1 I I I I 78 : Third Halfword Byte 2 I I I I 78 Second Halfword First Halfword Byte 1 1112 1516 B 11 1920 1 Address of Operand 2 31 32 35 36 i 47 Figure 2-10. Machine instruction formats the instruction. It is included only in the address specified by the RX instruction format; or it may simply be omitted in an RX instruction. The RX format instructions permit double indexing. Displacement (D) is a 12-bit binary number contained in the instruction format. It is included in every address computation. The displacement provides for relative addressing up to 4095 bytes beyond the base address, which is the limit that can be expressed by 12 binary bits. In Chapter 1 we saw how the displacements were calculated by the assembler from symbolic addresses written by the programmer. We also saw that the three binary numbers are added together to form the actual address. This sum is a 24-bit number, which can be represented by six hexadecimal digits. The program may have zeros in the base address, index, or displacement fields. A zero is used to indicate the absence of the corresponding address component. A base or index of zero implies that a zero quantity is to be used in forming the address, regardless of the contents of general register O. Initialization, modification, and testing of base addresses and indexes can be carried out by fIXed-point instructions, or by Branch and Link, Branch on Count, or Branch on Index instructions. Interruptions and the Program Status Word To make maximum use of a modern data processing system, some automatic procedure must be made available to alert the system to an exceptional condition, the end of an I/O operation, program errors, machine errors, etc., and send the system to the appropriate routine following the detection of such an event. The system must have, in effect, the ability to pause to answer the telephone and then to resume the interrupted work. This automatic procedure is called an interruption system. It makes possible the operation of a system in a non-stop environment and greatly aids the efficient use of I/O equipment. The desire to make the interruption procedure as short and simple as possible means that the metho{L ·)f switching between the interrupted program and the program that services the interruption must be quite efficient. It operates as follows: The complete status of the System/360 is held in eight bytes of information. This status information, which consists of the instruction address, condition code, storage protection key, etc., is saved when an interruption occurs, and is restored when the interruption has been serviced. As soon as the interruption occurs, all the status information, together with an identification of the cause of the interruption, is combined into a doubleword called the program status word (PSW). The PSW is stored at a fixed location, the address of which depends on the type of interruption. The system then automatically fetches a new PSW from a different fIXed location, the address of which is also dependent on the type of interruption. Each class of interruption has two fIXed locations in main storage: one to receive the old PSW when the interruption occurs, and the other to supply the new PSW that governs the servicing of that class of interruption. After the interruption has been serviced, a single instruction uses the stored PSW to reset the processing unit to the status it had before the interruption. Types of Interruptions The interruption system separates interruptions into five classes: Supervisor Call interruptions are caused when the processing program issues an instruction to turn over control to the supervisor in the control program. The exact reason for the call is shown in the old PSW. External interruptions are caused by either an external device requiring attention or by the system timer going past zero. Machine Check interruptions are caused by the machinechecking circuits detecting a machine error. I/O interruptions are caused by an I/O unit ending an operation or otherwise needing attention. Identification of the device and channel causing the interruption is stored in the old PSW; in addition, the status of the device and channel is stored in a fIXed location. Program interruptions are caused by various kinds of programming errors or unusual conditions resulting from improper specification or use of instructions or data. The exact type of error is shown in an interruption code in the PSW. Finding the Source of a Program Interruption When a program interruption occurs, provision is always made to locate the instruction that was being interpreted and to identify the exact type of error involved, so that the programmer can make the necessary corrections. For this information he must go to the PSW in a printout of storage contents. Fifteen interruption codes are used for the different types of program interruptions, as follows. Interruption Code 1 00000001 2 00000010 3 00000011 4 00000100 5 00000101 6 00000110 7 00000111 8 00001000 9 00001001 10 00001010 11 00001011 12 00001100 13 00001101 14 00001110 15 00001111 Program Interruption Cause Operation Privileged operation Execute Protection Addressing Specification Data Fixed-point overflow Fixed-point divide Decimal overflow Decimal divide Exponent overflow Exponent underflow Significance Floating-point divide To take an example, one of the conditions that causes a "data exception" to be recognized is an incorrect sign or digit code in an operand used in decimal arithmetic. In this case, the operation would be terminated, and all, part, or none of the arithmetic result would be stored. Since the result is unpredictable, it should not be used for further computation. The interruption code, binary 0000 0111, or hexadecimal 07, for a data exception would be recorded in bit positions 24-31 of the program old PSW (always at main storage location 4010)' The location of the instruction that was being interpreted when the interrupt occurred can also be determined from an inspection of the old PSW. The instruction address, which is found in bit positions 40-63 of the PSW, is for the instruction to be executed next. To locate the preceding instruction, all that is needed is to subtract its length in bytes. This instruction length can be found in bit positions 32 and 33 of the PSW, recorded there in binary as 1, 2, or 3 halfwords. System/360 Review 25 HEXADECIMAL NUMBERS Hexadecimal Code Hexadecimal numbers have been mentioned a number of times. In Chapter 1 we used them to represent machine language instructions, and we saw that the assembler listed object code, location counter settings, and addresses in hexadecimal numbers. In System/360 hexadecimal code is a shorthand method of representing the internal binary zeros and ones, one hex digit for each four binary bits. Hex numbers are a convenient way for the assembler language programmer to specify masks in testing and branching operations, and to specify hexadecimal constants (type X). Principally, he uses hexadecimal code to locate and interpret the contents of storage, which may be printed out when a program must be analyzed and debugged. In a later chapter, we shall see some "dumps" of storage and attempt to locate information in them. Converting from binary to hex, or from hex to binary, is simple. There are only 16 hex symbols, and their value is based on the numerical value of four bits. We recall that foucbits in the binary number system can express all values from zero to 15 10 • We also recall that the position of each bit determines its value: Decimal 1 2 4 8 Binary 0001 0010 0100 1000 graphic hex symbols per byte. The same hex coding system is used regardless of the code in which the information is recorded internally. The internal information may be EBCDIC characters, zoned decimal numbers, signed binary numbers, the eight-bit code used for System/360 operation codes, or any of the other codes and formats in use. All are coded in some form of binary coding, and, since the eight-bit byte is the basic unit of System/360, they can readily be taken four bits at a time. Let's look at some examples. Each "box" represents a byte. Binary bits are shown in groups of four for convenience. 1. EBCDIC characters I B M 3 1100 1001 1100 0010 1101 0100 1111 0011 Characters I nternal form Hex code C 9 C 2 D F 4 3 2. Zoned decimal number Decimal I 8 9 7 3 I nternal form [ 1111 1000 1111 1001 1111 0111 1111 0011 Hex code I F 8 F 9 F 7 2 + F + 3 2 1100 0010 C 2 3. Packed decimal number Decimal Internal form Hex code Some people fmd it easier to remember these binary positional values this way: 8 9 1000 1001 8 9 7 3 0111 0011 7 3 0010 1100 2 C 4. Signed binary number (This fixed-point fullword is equivalent to decimal +89,732) I nternal form Hex code If we try the four bit values in various combinations, we fmd that we can rather quickly discover how to count from zero to the equivalent of decimal 15 in sequence. In order to be able to represent these 16 values by a single symbol, the letters A, B, C, D, E, and F are used for 10, 11, 12, 13, 14, and 15, respectively. The numbers 0-9 stand for themselves. The entire four bit code is shown in Table 2-2. Table 2-2. Hexadecimal Code Binary Hexadecimal Decimal 0000 0001 0010 0011 0100 0101 0110 0111 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 Binary Hexadecimal Decimal 1000 1001 1010 1011 1100 1101 1110 1111 8 9 A B C D E F 8 9 10 11 12 13 14 15 All kinds of information, data, instructions, etc., in System/360 can be represented in hexadecimal code, two 26 The reader may wonder how, when he sees a hexadecimal printout of storage contents, he will be able to interpret the different formats correctly. This is not a problem, but does require care. The programmer can refer to the assembly listing of the program. By tracing the assembler addresses, he can calculate just where in main storage each instruction or data item is. In some cases, the format will have been specified explicitly. In others, he must know which format is implied by use of particular instructions or types of data. Hexadecimal Number System Turning back to the examples, we notice that internally the characters and decimal numbers are, generally speaking, coded separately in either four- or eight-bit binary codes. The binary number in example 4, however, is recorded internally in its actual value as an integer in a number system with a base of 2. The O's and 1's are the only digits in this number system. Similarly, the hexadecimal equivalent 15E84 is an integer in a valid number sys~em with a base of 16. Considering the binary and hex numbers in this example in their entirety, they have exactly the same total value. Each hex digit also equals the value of the four bits it represents. We see from this that hex numbers can be used in two different ways: (1) simply as a four-bit code into which each internal half-byte is translated, and (2) both as a four-bit code and as a valid number system with a base related in a definite way to the base of the binary number system. In the familiar decimal number system, the base is 10, and there are ten digits, 0-9. In the decimal number 234, we know that the 2, because of its position, equals 2 x 100, or 200; the 3 equals 3 x 10, or 30; and the 4 equals 4 xl, or 4. The three values are in effect added together. We may represent the place value of each digit in a whole number (not a fractional or mixed number) in this way: In the same way, the binary number system has a base of 2 and has two digits, 0 and 1. Its place values are: Power of base 2 Value in decimal It is this relationship that makes one hex digit equal arithmetically to four binary bits, two hex digits equal to two groups of four bits each, etc. All hex and binary digits must of course be kept in correct place order. Now we are ready to figure out some actual hexadecimal values. Hex numbers are especially useful for calculating main storage addresses and displacements. A storage address, we may remember, is a 24-bit true binary number internally, always represented externally by the machine as six hexadecimal digits. We shall use Table 2-3 for converting hex numbers to decimal, and decimal to hex. It is for integers only. The table shows eight places, each place being the position of a hex digit, starting from the right. The table shows the equivalent decimal value of each hexadecimal digit in each hex position from 1 to 8. To convert a hex number to decimal, it is necessary only to find the value of each hex digit in the column corresponding to its position, and to add them together. To convert D3416 to decimal, we start in column 3 because this is a three-digit number. We find (1) D0016 = 332810 in column 3, (2) 3016 = 48 10 in column 2, and (3) 416 = 410 in column 1 ; then (4) summing the decimal values, we get The hexadecimal number system has a base of 16 and has 16 digits, 0-9 and A-F. Its place values are: We may notice that there is a relationship between binary and hexadecimal place values. Beyond the zeroth power (this always equals 1), hex place values are exactly four times greater than binary. This becomes clear when we compare them up to 212 =16 3 : Power of base 2 212 211 121°129 28 271 26 125 24 3 Power of base 16 Value in decimal 4096 2048110241512 256 128164132 16 160 1 8 14 1 2 03416 = To convert the five-digit number B60A616 to decimal, we follow the same procedure, beginning in column 5: Hex BOOOO 6000 00'0 AO 6 B60A6 20 2 1 22121 16 1 162 163 3328 48 4 338010 Decimal 720 896 24 576 0 160 6 745 638 Using the same table to convert a decimal number to hexadecimal requires a rather different procedure. Let's Table 2-3. Hexadecimal and Decimal Integer Conversion Table HALF WORD HALF WORD BYTE BYTE BITS 0123 BITS 4567 Hex Decimal Hex 0 1 2 3 4 5 6 7 8 9 A B 0 268,435,456 536,870,912 805 306,368 1 073,741 824 1, 342, 177 280 1 610,612 736 1 879 048 192 2 147,483 648 2 415 919 104 2,684 354 560 2,952,790,016 3 221 225 472 3,489,660,928 3,758,096,384 4,026,531,840 0 1 2 3 4 5 6 7 8 9 A B C D E F 8 C D E F Decimal 0 16,777,216 33,554,432 50 331,648 67 108 864 83 886,080 100 663 296 117 440,512 134 217,728 150 994 944 167 772 160 184,549,376 201J 326,592 218,103,808 234,881,024 251,658,240 7 BYTE BYTE BITS 0123 BITS 0123 BITS 4567 BITS 4567 Hex Decimal Hex Decimal Hex Decimal Hex 0 1 2 3 4 5 6 7 8 9 A B C 0 1,048,576 2,097,152 3,145,728 4 194 304 5,242,880 6 291 456 7,340 032 8,388,608 9 437 184 10 485 760 11,534,336 12,582 912 13,631,488 14,680,064 15,728,640 0 1 2 3 4 5 6 7 8 9 A B 0 65 536 131,072 196 608 262 144 327 680 393 216 458 752 524 288 589 824 655 360 720,896 786 432 851,968 917,504 983,040 0 1 2 3 4 5 6 7 8 9 A B 0 4,096 8,192 12,288 16 384 20,480 24,576 28 672 32 768 36 864 40,960 45,056 49,152 53,248 57,344 61,440 0 1 2 3 4 5 6 7 8 9 A B D E F 6 C D E F 5 C D E F 4 C D E F Decimal Hex 0 256 512 768 1 024 1 280 1 536 1 792 2 048 2 304 2,560 2 816 3072 3 328 3,584 3,840 0 1 2 3 4 5 6 7 8 9 A B 3 BITS 4567 BITS 0123 Decimal 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 C D E F 2 Hex Decimal 0 1 2 3 4 5 6 7 8 9 A 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 B C D E F 1 X2Q-8C47 System/360 Review 27 take 338010 as an example. We look for the highest decimal value in the table that will fit into 3380. The closest is 332810 in column 3, equal to the D. We make a note that this corresponds to D0016 and subtract, as shown below. The closest value below the remainder (5210) is 4810 in column 2, and we note it is equal to 3016 , Subtracting again, we look for the best fit into the remainder of 410 , ana find 410 m column 1, equal to 416 , Adding the hex values, we get the result 3380 = D34, which we know frorr our first conversion example is correct. (The best way to check the result of a conversion is to reconvert. Any lost zeros are likely to be found in the process.) Decimal 3380 3328 52 48 4 000 999 +1 1000 In hex, when 1 is added to the highest digit F, the result is also 0 and a carry of 1: 30 4 D34 F Hex Decimal 745 638 720 896 24 742 24 576 166 160 6 99 +1 100 9 +1 10 Hex Without looking back, let's convert 745,638 1O to hexadecimal: BOOOO 6000 AO 6 B60A6 The easiest way to fmd the decimal value of a long binary number is to convert it to hex, and from hex to decimal. Similarly, to find the binary value of a decimal number, the decimal number should be converted to hex, and from hex to binary. To get the binary equivalent of 745,63810 , we would convert it to hex as in the last example and merely substitute the four-bit code for each hex digit in the result: o B 6 0 A 6 0000 1011 0110 0000 1010 0110 It is entirely feasible to perform all kinds of arithmetic calculations in hexadecimal arithmetic. The rules are the same as in decimal arithmetic. Most programmers prefer to 28 convert hexadecimal values to decimal, however, do their calculations in decimal, and then convert back to hex. This can be done easily and quickly with the use of a conversion table. On the other hand, computer personnel often find it useful to be able to do simple addition in hexadecimal. Until they become proficient, they can simply count on their fingers. The rules for carrying are the same as in decimal addition. In decimal, the highest digit value is 9. When 1 is added to 9, the result is 0 and a carry of 1. Or, as we usually see it: +1 10(= 16 10 ) FF +1 100 (= 256 10) FFF +1 1000 (= 4096 10 ) The following list of equivalent values may help to crystallize the concepts of hexadecimal notation. Hex numbers that end in zero are always multiples of 16. To avoid confusion hex numbers like 10, 11, 12, etc., should be read as "one zero, one one, one two," and not as "ten, eleven, twelve." Dec. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 Hex 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12 13 14 15 Dec. 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 Hex 16 17 18 19 1A 1B 1C 1D IE IF 20 21 22 23 24 25 26 27 28 29 2A Dec. 43 44 45 46 47 48 49 50 51 52 Hex 2B 2C 2D 2E 2F 30 31 32 33 34 62 63 64 65 78 79 Dec. Hex 80 50 81 51 94 95 96 97 98 99 100 5E 5F 60 61 62 63 64 3E 3F 40 41 240 FO 4E 4F 254 255 256 FE FF 100 Chapter 3: Fixed-Point Arithmetic This chapter introduces and' discusses some of the fIXedpoint operations of the standard instruction set in the System/360. These include the arithmetic and shifting instructions as the central topic, with important consideration also of certain logical operations (comparison, branching), .and loop methods. Fixed-point instructions perform binary arithmetic on fIXed-length data of either a fullword or a halfword. The use of registers for arithmetic and other operations is thus most convenient. As might be expected, the fIXed-point instruction set uses only these three instruction formats: RR, RX, and RS. In the course of presenting the instructions and considering programming methods used with the System/360, we shall review the basic ideas of the machine organization and operation. The presentation will be almost entirely through the medium of eight examples and a final extended case study. ADDITION AND SUBTRACTION For a first example we shall consider a simple inventory calculation. We begin the calculation with an on-hand quantity, a receipt quantity, and an issue quantity. We are required to compute the new on-hand, according to the formula: new on-hand = old on-hand + receipts - issues Using fairly obvious symbols for the four quantities, this becomes: NEWOH = OLooH + RECPT - ISSUE A program to carry out this calculation is shown in Figure 3-1. We shall be concentrating on the four actual processing instructions, but at the outset we shall display all programs in logically complete form. The assembler instruction PRINT NOGEN is used simply to suppress printing of statements generated by macro instructions such as the EOJ macro. These statements and their storage locations and displacements will still be part of the object program; they will be omitted only from the printed listing. The next three lines of coding are rather standard preliminaries; instructions of this character will appear at the beginning of all but highly specialized programs. To review briefly, the START establishes a reference point for the assembly: the assembly listing (shown later) will assume that the frrst byte is to be loaded into 256 as shown. The BALR (Branch and Link Register) and the USING, as written here, together direct that register 11 shall be used as a base register wherever one is needed, and inform the assembler that the base register at execution time will contain the location of the frrst byte after the USING. ~ I S~CK PROGRAM ..T. .J: JONES PROGRAMMER Na.,. 1 ~fr4~I(:.~ &~~I~ ; ! I , i ! I ! I ~I 2-" L ~4 ()S~k; j I Operand Operation 8 1 .' A , ! ! 1 I S !! slrt I(;L 1)011 ~e (:ID 7i e 1S Sv NE. .~iii J It I _..tLL..k ! III ....... ~ If l'sZ i ~~!tL I I I Ii i ! i OiL ])oi# i 13. ~ ~P:T l.3c lzl$ ~tIla j. 1 "I, I p'tA': i ····11 P" ' J;! ~AJ i: .-0.' ,< i ! , J !I 1 !I I j I : , "1 I i I i 1 1: ~ 1 I I I , i I i I l' ! ; .1 i i 1 I,l:,I'9il I I re~ . i. t 1:iII J),C ll>iC ~I j , j eldrl '/)c 2.5 20 16 I I ,, it, I ...:J Figure 3-1. A program, written in assembler language, to perform a simple computation in binary arithmetic Now we reach the first processing instruction, where we wish to concentrate our attention. The Load instruction is classified as an RX format instruction, which implies a number of facts about it: 1. The instruction itself takes up four bytes of storage. 2. The fields within the instruction are, from left to right: th~ operation code (eight bits), the number of the register -'to be loaded from storage (four bits), the number of the register 'used as an index register (four bits), the Fixed-Point Arithmetic 29 number of the register used as a base register (four bits), and the displacement (twelve bits). 3. The instruction involves a transfer of information between storage and a general register. 4. The effective address of a byte in storage is formed by adding the con tents of the base register, the contents of the index register, and the displacement. If register zero is specified for an index register or a base register, a zero value is used in the address computation, rather than whatever register zero may contain. The operation of the Load instruction is straightforward: obtain a fullword (four bytes) from storage at the effective address specified, and place the word in the general register indicated. The effective address must refer to a fullword boundary, which means that the address must be a multiple of 4. Let us consider the complete line of coding for the Load instruction to see what each part does. The letter L is the mnemonic operation code for Load; this is converted by the assembler into the actual machine operation code for Load, 58. The 3 is the number of the general register we wish loaded with a word from storage. OLDOH is the symbolic address of the word in storage to be copied into general register 3. By writing the address in this fashion, we have indicated that the assembler should supply the base register and the displacement, and that we do not wish indexing. The assembly listing for this program is shown in Figure 3-2. Looking at the machine instruction assembled from this symbolic instruction, and remembering that all numbers are shown in hexadecimal, we see that the operation code is 58, the general register is 3, the index register is zero, the base register is B (= 11 10), and the displacement is 01216 • Since the base register contains 102, the effective address is 114, which is shown in the assembly listing under ADDR2 as the address of the second operand and which we see is the location of OLDOH. The Add instruction is also of the RX format. The operation is to add the fullword at the storage address specified, to the general register named. In our case, we LOC 000100 000100 000102 000102 000106 00010A 00010E OBJECT CODE ADDRI ADDR2 SOURCE STATEMENT 1 2 STOCK 3 BEGIN 05BO 5830 5A30 5830 5030 STMT B012 8016 BOlA BOlE 00114 00118 OOllC 00120 4 5 6 7 8 9 000114 00000009 000118 00000004 000 llC 00000006 000120 000100 12 OLDOH 13 REC PT 14 ISSUE 15 NEWOH 16 Figure 3-2. The assembly listing of the program in Figure 3-1 30 have, of course, named the same general register as in the Load instruction, since the intent is to add OLDOH and RECPT together. Looking at the assembled instruction, we see that things have been handled much as they were with the Load. Base register 11 has been assigned, there is no index register, and the displacement has been computed to give the effective address of the storage location associated with RECPT (l18). After the execution of this instruction, register 3 will contain the sum of the storage quantities identified in our program by OLDOH and RECPT. The Subtract instruction (S) in the next line subtracts the quantity identified by the symbol ISSUE from the quantity now standing in register 3. The format and general operation of the instruction are very similar to Add. Now we have the desired result in register 3. The problem statement required the result to be placed in another location in storage, that is identified by the symbol NEWOH. Placing the contents of a general register in storage is the function of the Store instruction (operation code ST). The general register contents are unchanged by the operation. The format is again RX, so address formation is as before. This completes the actions required by the problem statement, but we must now indicate what we want done next. The System/360 forces a program organization that keeps the machine in operation as much of the time as possible. What we have shown here is an End of Job macro instruction, which is used in the Disk Operating System environment. As we saw in the preceding chapter, the EOJ macro generates a SupeIVisor Call instruction, SVC 14. The use of -this instruction assumes that there is in storage, at the time of execution of this program, a control program that runs the machine between jobs. We here indicate to the supervisor that this program has no further need for the machine. The program in Figure 3-2 does not include any instructions for reading in data from an input device such as a card reader or magnetic tape unit, or for printing out or punching out the results of our calculations. Input and PRINT START BALR USING L A S ST EOJ DC DC DC OS END NOGEN 256 11 ,0 *,11 3,OLDOH 3,RECPT 3,ISSUE 3, NEWOH F '9' F'4' F '6 t F BEGIN output instructions vary considerably in different systems, depending upon the operating (or programming support) system in use and the particular pieces of input/output equipment available at an installation. In normal commercial practice, a computer program would be used, not for calculations on just one set of data, but on large series of data that require similar treatment. An example would be a program to calculate weekly pay for several hundred employees' of a company, and the data would include the hours worked, pay rate, withholding amounts, etc., for each of them. In our program examples, our principal interest lies in how the assembler language instructions work, and so we will generally use only one set of specific values for the purpose of illustrating what happens in each step. We have simply entered the illustrative values for the input data with DC instructions, and reserved space for the output with a DS. The F's in the DC's and the DS specify fullwords of four bytes. The Load, Add, Subtract, and Store instructions all operate on fullwords. As we shall see in later examples, there are corresponding halfword instructions. The END instruction informs the assembler that the termination of the program has been reached and specifies in this case that the first instruction to be executed after the program is loaded is the one with the name BEGIN, that is, the BALR instruction. By using either a suitable assembler language routine or macro instruction, it is possible to get a "dump" of the contents of the registers and selected areas of storage, and get our data and results out of the machine. Such a routine produced the numbers, converted to decimal, that are shown in Figure 3-3. The four items, in sequence, are OLDOH, RECPT, ISSUE, and NEWOH. It might be interesting to run this program again with a value of, say, 16 for ISSUE. We know that negative fixed-point numbers are represented in two's complement form. Our output routine will make a conversion to true 0000009+ 0000004+ 0000006+ 0000007+ Figure 3-3. Output of the program of Figure 3-2. The four numbers are OLDOH, RECPT, ISSUE, and NEWOH, in that order. numbers and sign, as shown in the first line of Figure 3-4. In the second line, the same numbers are shown in hexadecimal as they normally appear in a dump. We see that the first three numbers, which are positive, have zeros before the Significant digits. The last number, which is negative, has 1's to the left of the significant digit (hexadecimal F equals binary 1111). If we were to write out this hexadecimal number, FFFFFFFD, in the binary form actually in storage, we would have thirty 1's followed by 01. Recalling how two's complement numbers are formed, we see that the complement of this nu~ber is binary 11, which equals decimal 3. Checking with the given data and the formula, we see that this is the correct answer, and, of course, the decimal value was printed out as a minus 3. Naturally, if a negative result were actually obtained in an inventory control program, it would indicate some kind of trouble, probably bad data; it is not possible to issue more than there are on hand plus what was received. A realistic program would include a test for the possibility of a negative result and the corrective action to be taken. 0000009+ 0000004+ 0000016+ 0000003- 00000009 00000004 00000010 FFFFFFFD Figure 3-4. Output of the same program with a value for ISSUE that causes NEWOH to be negative. Values are shown in decimal in the fIrst line, hexadecimal in the second; the value for NEWOH is in complement form. Fixed-Point Arithmetic 31 MULTIPLICATION For a simple example of fixed-point multiplication in the System/360, consider the following problem. We are to multiply an ISSUE quantity by a PRICE to get TOTAL. We shall assume that PRICE is an integer, expressed in pennies. The product will therefore also be in pennies. For instance, an ISSUE of 5 and a PRICE of 2S would give a TOTAL of 125. The program to do this multiplication is shown in Figure 3-5. The first four lines are the same as before. The Load places the multiplicand in general register 5. The Multiply (M) forms the product of what is in 5 and what is in the full word identified by PRICE, and places the result, which could of course be much longer than either of the factors, iii registers 4 and 5 combined. It is required that the general register named in the Multiply be even numbered; if it is not, a specification exception and an interrupt occur. The multplicand must always be in the odd-numbered register of an even-odd pair, such as 4 and 5 here. The multiplicand in the odd register, and whatever may have been in the even register, are both destroyed by the operation of the Multiply. After the product has been formed, we store it in TOTAL on the assumption that the result does not exceed the length of one register. The validity of such an assumption, of course, is the responsibility of the programmer; if in fact the product extended over into register 4, there would be no automatic signal of the fact that the result in TOTAL is not the complete product. If a product extending into the even register could be a legitimate outcome, we would naturally have to arrange to store both parts of the product. Let us try this program with several sets of sample factors in order to see precisely how the operation works. Figure 3-6 shows the values of ISSUE, PRICE, TOTAL, and the contents of register 4 and 5 after the completion of the program. These were obtained by a dump routine and converted to decimal. We see that the product of 7 and 23 is indeed 161, as we might expect. This number is shown as the contents of register 5, while register 4 is zero; the LOC 000100 000100 000102 000102 000106 00010A OBJECT CODE ADDR 1 ADDR2 05BO 5850 BOOE 5C40 B012 5050 B016 000110 00000007 000114 00000017 000118 000100 00110 00114 00118 STMT 1 2 3 4 5 6 7 8 11 12 13 14 ISSUE 0000007+ PRICE 0000023+ REG 4 0000000+ REG 5 0000161+ Figure 3-6. Output of the program of Figure 3-5 product was not long enough to extend into 4. In Figure 3-7 the numbers are the same except that the 7 is negative. (This makes no sense in terms of the problem, of course.) We see that TOTAL and register 5 are negative, as expected, but what has happened to register 4? The answer is that the product is a full 64 bits long; a negative number has l's to the left ·of the leftmost Significant digits. Register 4 properly contained all l's which, considered as part of the 64-bit product, are merely sign bits. But printed as a separate number (which is pointless, in reality), a word of all 1's represents -1 as shown. A printout not reproduced here substantiates what we have said: register 4 printed in hexadecimal form appeal'S as eight F's. ISSUE 0000007- PRICE 0000023+ REG 4 0000001- REG 5 0000161- In Figure 3-8 we see an example of what can happen when the numbers entering the machine do not conform to the assumptions made in setting up the program (that is, the product would never extend into register 4). With both factors being 87654, the product, in decimal, should be 7,683,223,716. This is too long to fit into register 5, so we would expect TOTAL to contain only the equivalent of the part of the product that appeared there. But we would hardly have expected it to be negative! What happened? PRINT START BAlR USING l M ISSUE PRICE TOTAL ST EOJ DC DC OS END NOGEN 256 11 ,0 *,11 5,ISSUE 4,PRICE 5,TOTAl F'7' F'23' F BEGIN Figure 3-5. Assembly listing of a program to perform binary (ftxed-poinO multiplication 32 TOTAL 0000161- Figure 3-7. Output of the program with a negative value for ISSUE SOURCE STATEMENT GROSS BEGIN TOTAL 0000161+ The answer becomes apparent if we look at the product as a hexadecimal number and note the part of it that would appear in register 5. The complete product is lC9F4BOA4, that is, nine hexadecimal digits - a register can hold eight. So the 1, preceded by seven hexadecimal zeros, would be the contents of register 4, as shown. The part in register 5 begins with the hexadecimal digit C, which is 1100 in binary. This means that the leftmost bit is 1, which, when C9F4BOA4 is taken as a number by itself, indicates a negative number that is in two's-complement notation! Thus, in converting to decimal for Figure 3-8, System/360 performed as it was designed to do, recomplemented (to hexadecimal 360B4F5C), and came up with the decimal equivalent of that amount. This recitation of troubles is not meant to suggest any difficulty in using the System/360. Any programmer appreciates the necessity of knowing a good deal about his data and for testing it for validity if he is not sure of it. The purpose in showing these slightly surprising results is simply to clarify how the machine operates, especially since many programmers will not have had previous contact with complement representation of negative numbers. ISSUE PRICE TOTAL 0087654+ 0087654+ 906710876- REG 4 REG 5 0000001+ 906710876- Figure 3-8. Output of the program with values for ISSUE and PRICE that lead to a TOTAL too large to fit in a fullword Fixed-Point Arithmetic 33 MULTIPLICATION AND DIVISION WITH DECIMAL POINTS bering that the computer knows nothing of our behind-thescenes understanding about decimal points, all we have to do to round off is to add 50 to the integer product. We will think of the 50 as $0.0050, but to the computer it is 50. Having done this, we need finally to divide by 100 to correct for using 103 in place of 1.03. This requires the Divide instruction, which as we might expect is a close relative to the Multiply instruction. The dividend must be in an even-odd pair of registers as a 64-bit number. This requirement is already met by the way the Multiply leaves the product in an even-odd pair (the machine was designed to make it simple to follow a Multiply with a Divide). The remainder is placed in the even register and the quotient in the odd. Our quotient will be 2564 (we read: $ 25 .64) and the remainder will be 17 (we don't care about this). The quotient can now be stored back in the location for PRINC, as required in the problem statement. The question will occur to many: why was it necessary to divide? Why not simply shift two places right to drop the right two digits? The answer is, of course, that we could do precisely that in decimal, but this is binary. Shifting one place to the right in decimal divides the number by 10; shifting one place to the right in binary divides the number by 2. There is no number of binary shifts that divides a number by a factor of decimal 100. Six places divides it by 64, and seven places by 128. With this way of approaching the problem, we have no choice but to divide. It should be kept clearly in mind that in all examples so far we have explicitly stated that all quantities were to be viewed for programming purposes as integers, whatever we on the outside might understand by the digits. This was by agreement, not necessity. We can work with binary numbers that are taken to have "binary points" elsewhere than at the extreme right. Let us, for instance, attempt to express the factor 1.03 as a binary number. The next example involves a little further practice with multiplication, an application of the Diviqe instruction, and a rather basic question of decimal point handling in binary. The task is to increase a principal amount named PRINC by an interest rate of 3%. The principal is stored in pennies as in the previous example; for instance, 24.89 would be stored simply as the integer 2489. Later program segments would have to insert any "graphic" decimal point that might be desired for printing; at this point we make a mental note of the true situation, while pretending for programming purposes at the moment that the unit of currency is the penny. One possible program is shown in Figure 3-9. (There are other ways, as we shall see.) After the usual preliminaries we load the principal into an odd-numbered register preparatory to multiplying. The interest rate is shown as 103, which should be read as 1.03. This is a shortcut: instead of multiplying the principal by 0.03 and adding the product to the principal, we multiply the principal by 1.03. The result is the same either way; our way saves an addition. The absence of the decimal point is another matter. We are saying here that instead of multiplying by 1.03, we will multiply by 103; the product will be 100 times too large as a result. It will be necessary in a subsequent step to divide by 100 to correct for this. The reason for this is that there is a question of how to represent a decimal fraction in binary form. The question can be answered, as we shall see, leading to a different program. For now, let us take what seems at first to be the easy way out and stay with integers. Using the sample principal mentioned above, 24.89, the product after multiplication is 256367. We shall assume that the product in all cases is short enough to be held in register 5 alone. We now wish to round off. We think of the product as $25.6367; the desired rounded value is $25.64. Remem- LaC 000100 000100 000102 000102 000106 00010A 00010E 000112 000118 00011 C 000120 000124 000100 OBJECT CODE ADDRI ADDR2 05BO 5850 5C40 5A50 5040 5050 B016 BOlA BOlE B022 B016 000009B9 00000067 00000032 00000064 00118 0011C 00120 00124 00118 5TMT 1 2 3 4 5 6 7 8 9 10 13 14 15 16 17 SOURCE STATEMENT INTA BEGIN PRINT START BALR USING L M A PR I NC INT C50 CI00 D 5T EOJ DC DC DC JC END NOGEN 256 II ,0 *,11 5,PRINC 4, INT 5, C50 4,CI00 5,PRINC F'2489' F'103' F'50' F'lOO' BEGIN Figure 3-9. Assembly listing of a program involving binary multiplication and division with the result rounded off 34 It may be recalled from a study of the conversion rules that there will be in general no exact binary equivalent for a decimal fraction. If we try 1.03 we get an infinitely repeating binary fraction. The first twelve bits are of our product. The Scale factor method shown gives this. (In fact, the constant con4)ists of a 1 followed by ten zeros.) After rounding off we are left with eleven superfluous bits at the right end of the product. These can be shifted off the end of the register with a suitable shift instruction. "Suitable" in this case means that the shift should be to the right, it should involve a single register, and it should be an algebraic shift so that if the number were negative, proper sign bits would be shifted into the register. The instruction is called Shift Right Single (SRA), in which we specify the register first and then the number of pOSitions of shift desired. Bits shifted off the right end of the register are lost. After the shift we are ready to store the result. The point of doing all this is that we have replaced a Divide with a Shift, and the latter is conSiderably faster than the former. In some applications the difference in time could be significant. If we print the result, we get a surprise: the answer is 2563 ($25.63); rounding seems not to have taken place The trouble is that the binary "equivalent" of the decimal number 1.03 was not exactly equivalent. To prove the point, let us ask for 15 binary places in the fractional part of the constant created for 1.03. We change the rounding constant likewise, and make the shift 15 places. This time, the printout shows 2564 ($ 25 .64) as before. The moral of this story is that decimal fractions do not usually have exact binary equivalents. Computations that are required to be exact to the penny should be done in integer form, as in the frrst version of the program. (Even though a larger number of bits led to a correct answer this time, it would not always do so, particularly for larger principal amounts.) This means, in most situations, that it would be most unwise to go the further possible step of representing penny amounts as binary fractions. Unless approximate results are acceptable, which they sometimes are, of course, the use of anything but integer arithmetic leads to problems more severe than they are worth. 1.00000111101 The binary point is, of course, understood (by us). If we enter such a number as the constant (which we shall see how to do in a moment), we can multiply by it. The machine cares nothing for our understood binary points, and carries out the multiplication. We must then take into account the understood binary point in the product, according to a literal translation of familiar rules: the binary point in the product will have as many places to the right as the sum of the number of places to the right of the binary points in the multiplier and in the multiplicand. If the constant has eleven places to the right, as written above, and the principal is still understood to be an integer (zero places), then the product will have eleven places to the right. Let us turn to Figure 3-10 to see how this much of the revised program looks. The Load is the same as before, as is the Multiply. The constant used for multiplication is different, however. Down at INT we see that the DC is FSll'1.03' The F stands for fullword, as before. The S stands for Scale factor and is the number of binary places that are to be reserved for the fractional part of the constant. We have indicated eleven places as the number of bits to the right of the binary point in the factor as we write it before. The Add to round off is the same as before, but once again the constant is different. What we have after the multiplication this time is not an integer but a binary fraction. To the left of the assumed binary point we have a whole number of pennies; to the right a fractional part of a penny. This time, to round off we need a constant that is 0.5 cent expressed in the same form as the fractional part LaC 000100 000100 000102 000102 000106 00010A OOOlOE 000112 OBJECT CODE AODRI AODR2 STMT SOURCE STATEMENT 1 2 INTB 3 BEGIN 05BO 4 5850 5C40 5A50 8A50 5050 B016 BOlA BOlE OOOB 8016 00118 001lC 00120 OOOOB 00118 5 6 7 8 9 10 000118 000009B9 OOOl1C 00000830 000120 00000400 000100 13 PRI NC 14 INT 15 HALF 16 PRINT START BALR USING L M A 5RA 5T EOJ DC DC DC END NOGEN 256 11 ,0 *,11 5,PRINC 4, I NT 5,HALF 5,11 5,PRINC F'2489' FS11'l.03' FSll'O.5 ' BEGIN Figure 3-10. A different version of the program of Figure 3-9, using a scale modifier for constants Fixed-Point Arithmetic 35 Some readers may be wondering whether binary arithmetic is worth the trouble. The answer is yes, of course. Many applications of binary arithmetic raise none of the questions suggested here and do not involve the possible complications with complement form either. For the straightforward cases, it is barely necessary to know anything about the binary and complement matters. We present examples like these to warn the unwary and to lay a 36 foundation of understanding for those with problems where the advantages of binary arithmetic are worth the care that must be exercised in using it. It is true that many applications will suggest staying with decimal arithmetic, for users having the decimal instruction set, but even then there will be more than a few occasions where binary operations are the only ones that make sense from a standpoint of time. SHIFTING AND DATA MANIPULATION Having introduced the shifting operation briefly in the previous example, let us now turn to an application that will involve considerably more shifting. We begin with a fullword, supplied by some other program, in which three data items are stored in binary form: Bits 0-11 12 - 23 24 - 31 Item name A B C We are required to separate the three data items and store each in a separate halfword storage location, with names for the latter as shown. All three numbers are positive. The program shown in Figure 3-11 is a more or less straightforward matter of shifting and storing, but a few notes are necessary to make clear what is happening at certain points. The numbers in the Comments field are sample contents of registers 6 and 7 as they would appear during execution of the program if the original word were hexadecimal 78ABCDEF. These sample values, of course, were entered when the source program was punched; it is quite impossible for the object program to print anything on the assembly listing. We begin by loading the fullword into an even-numbered general register. This permits us to continue with a doublelength shift that moves bits from the named even-numbered register into the adjacent odd-numbered register, which we think of as being to the right. This is what "double" means in Shift Right Double Logical (SRDL). The "logical" refers to the handling of sign bits and means that zeros are entered at the left of register 6. This is in contrast to the "algebraic" shifts, in which the bits entered at the left are LOC 000100 000100 000102 000102 000106 a0010A 00010E 000112 000116 00011A 000 11 E OBJECT CODE ADDR 1 ADDR2 STMT 4 5860 8C60 8870 4070 8C60 8870 4070 4060 B022 0008 0018 B02A aooe 0014 B028 B026 00124 00008 00018 0012C OOOOC 00014 0012A 00128 5 6 7 8 9 10 11 12 13 000124 000128 00012A OOO12e 000100 16 FWORD 17 A 18 B 19 C 20 Actually, the restriction to positive numbers is net too difficult to remove. It would have to be agreed that the leftmost bit of each item was its sign bit, that is, that in generating the fullword each negative item was in cemplement form and of such length as to fit in the item size allotted. With this assumption, the program ef Figure 3-12 properly expands the sign bits of the items and stores any negative items in halfwords in complement form. The "expansion" of the sign bit is one of the functiens of an algebraic shift, as noted above. The program must also be SOURCE STATEMENT 1 2 SHIFTA 3 BEGIN 05BO made to be the same as the original "sign bit", that is, the original leftmost bit. Here, we were guaranteed in the problem statement that all three numbers are positive, so we can ignore any question of what the leftmest bit in each item might be. Whether it is zero or one, the number represented is positive. The SRDL moves the rightmost eight bits into register 7; from there we move them to the right-hand end of the same register, using a single-length logical shift that does not affect register 6. What were originally the rightmest eight bits of the fullword are now properly positioned in register 7 to be stored in a halfword lecation with the Store Halfword (STH) instruction. The action here is to stere the rightmost 16 bits of the register named, in the two bytes identified by the effective address. The register is not disturbed by the operatien of the instruction. This is an RX format instruction; it could be indexed if we had eccasien to do so. Now we again shift the two registers together to' get the twelve-bit B item into register 7. From there we meve it on over to' the right-hand end of 7 and store it. A further shift of what was originally the leftmost twelve bits is not needed, since they are now in the right-hand end of 6, from whence they may be stored. PRINT START BALR USING L SROL SRL STH SRDL SRL STH STH EOJ OS OS OS OS END NOGEN 256 11 ,0 *,11 6,FWORO 6,8 7,24 7,C 6,12 7,20 7,B 6,A 78ABCDEF 0078ABCO 0078ABCD 0078ABCD 0000078A 0000078A 0000078A 0000078A 00000000 EFOOOOOO OOOOOOEF OOOOOOEF Beoooooo OOOOOf3CO 000008CO OOOOOBC[) F H H H BEGIN Figure 3-11. Assembly listing of a program to separate three quantities stored in one fullword Fixed-Point Arithmetic 37 lOC 000100 000100 000102 000102 000106 00010A 000 IDE 000112 000116 OOOllA 00011 E 000122 000126 00012C 000 130 000132 000 134 000100 OBJECT CODE ADOR 1 AODR2 05BO 5860 8C60 8A70 4070 8C60 8A70 4070 8C60 8A70 4070 B02A 0008 0018 B032 OOOC 0014 B030 0012C 00008 00018 00134 OOOOC 00014 00132 oooe ooooe 0014 B02E 00014 00130 STMT SOURCE STATEMENT 1 2 SH IF TB 3 BEGIN 4 5 6 7 8 9 10 11 12 13 14 15 18 19 20 21 22 FWORD A B C PRINT START BALR USING L SRDL SRA STH SRDL SRA STH SRDL SRA STH EOJ DS DS DS DS END NOGEN 256 11,0 *,11 6,FWDRD 6,8 7,24 7,C 6,12 7,20 7,B 6,12 7,20 7,A 78ABCDEF 0078ABCO 0078ABCO 0078ABC 0 0000078A 0000078A 0000078A 00000000 00000000 00000000 00000000 EFOOOOOO FFFFFFEF FFFFFFEF BCDFFFFF FFFFFBCD FFFFFBCO 78AFFFFF 0000078A 0000078A F H H H BEGIN Figure 3-12. Modified version of the program of Figure 3-11, making it operate correctly with negative quantities changed to expand the sign of item A. The final two shifts are added to do this. Actually, it could be done more efficiently and these extra steps avoided simply by changing the SRDL in statements 6 and 9 to the algebraic SRDA. Figure 3-13 shows the output of the two programs for the sample input word of 78ABCDEF. The three parts of the combined word, in hexadecimal, were therefore 78A, BCD, and EF. In the first line of Figure 3-13 we see that these have been put into halfwords by the first program as 078A, OBCD and OOEF, that is, as three positive numbers. In the second line we see that the second program, on the 38 other hand, interpreted the second and third numbers as negative because their leftmost bits were 1'so The three output halfwords are 078A, FBCD, and FFEF, showing that the sign bits of the numbers were properly expanded. PROG SHIFTA 078A OBCD OOEF PROG SHIFTB 078A FBCD FFEF Figure 3-13. Output of the two programs executed with hexadecimal 78ABCDEF for the fullword BRANCHES AND DECISION CODES The Condition Code Decisions and branching are important parts of data processing, and the programming methods by which these operations are carried out are important aspects of the programming task. The facilities offered by the System/360 are particularly powerful and flexible. The basic action is the setting of the condition code by any of a large number of instructions and the subsequent testing of the condition code by a Branch on Condition instruction. Many arithmetic, shift, and logical instructions have as ~ part of their action the setting of the condition code to indicate something about the result of the instruction's execution. For instance, after an Add instruction, the condition code indicates whether the sum was zero, positive, negative, or too large for the register. After a Compare instruction the condition code indicates whether the first operand was greater than, equal to, or less than the second operand. The meaning of each of the different states or values of the condition code is specified in the descriptiop. of each instruction that affects the code. These descriptions may be found in the System/360 Principles of Operation, which also contains a complete tabulation of the instructions involved and the meaning of the condition codes. The condition code occupies two bits (in the control program area of storage). Two bits can, of course, be set in just four ways: 00, 01, 10, and 11; and these four binary settings are equal to decimal values 0, 1, 2, and 3, respectively. At any time after the condition code has been set by the action of an instruction, it may be tested by using a Branch on Condition (BC) instruction. In this instruction, which is in the RX format, the four bits that in other instructions designate a general register are here used for a mask that designates in which states of the condition code we wish a certain branch to occur. The leftmost bit of the mask checks for a condition code of zero, the next bit for code 1, the next for code 2, and the rightmost for code 3. If the condition code is equal to any of the values selected by the mask bit(s), the Branch is taken. The correspondences between condition codes and mask are summarized in Table 3-1. Note that the mask bits correspond from left to right with the four condition codes. Another way, perhaps easier to remember, of summarizing this correspondence is as follows: Condition code Mask used to test code 023 842 1 A BC instruction with a decimal mask of 12 (8+4) specifies that a branch is to be made if the condition code is a or 1, and is not to be made if the condition code is 2 or 3. A mask of 7 (4+2+1) will cause a branch only if the condition code is 1, 2, or 3. A decimal mask value of zero makes the instruction test for no condition codes; it thus becomes a no-operation instruction. A mask of 15 tests for any condition code; it is thus an unconditional branch. Table 3-1. Masks for testing various states of the condition code Mask bits 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Decimal value a 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Condition codes tested None 3 2 20r 3 1 1 or 3 1 or 2 1,2, or 3 0 o or 3 o or 2 0,2, or 3 o or 1 0,1, or 3 0,1, or 2 0,1,2, or 3 A Sorting Procedure To see how some of these ideas are applied, consider a simple example. We are given three fullword data items named A, B, and C. They may be positive or negative. We are required to change any negative values to positive, and then to rearrange the three values in storage to make the number in A the largest, the number in B the next largest, and the number in C the smallest of the three. Figure 3-14 expresses the logic of the method that will be used here to perform the sort; other ways are possible. We first make all three numbers positive. A comparison is then made between A and B; if A is the smaller, we interchange the two values. Now we know that the value in A is the larger of the two, whether it originally was or not. A similar process compares A and C and interchanges if A is smaller. Having done this, we know that what is in A is the largest of the three. A final comparison of the numbers now Fixed-Point Arithmetic 39 Make A, B,C positive A< B Interchange A and B A 301C 4080 30911 1288 4770 3040 40S0 3098 00156 0012E C0156 C0194 COllE C019C 000142 000146 000148 00014C OC0150 8870 1068 4A70 8A 70 4070 0001 COCOI 3092 0001 3098 00194 ooeo I C01911 000156 oe0158 00015A ,.QOJU.5C 0001 0002 0003 ..Q0Q.i.. oe018E 000190 000192 000194 000196 000198 COO19A 0OO19C 000100 0010 ODIE OOlF 0001 FFFF 00142 001911 -- 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 26 27 28 29 30 31 34 35 36 Register 3 4 5 6 7 8 10 11 Usage Base register Index register Word of l's Left half of dividend Sum of temperatures-right' half of dividend Count of nonzero temperatures Increment for BXLE Limit for BXLE The initialization consists of setting up the contents of the seven registers used by the program. The fust one to be set to zero (6) is cleared by a Subtract Register, the others by Load Registers from 6. The Load Halfword to get the SOURCE STA TfMENT 1 2 AVGTEMP 3 BEGIN 4 0530 4850 IB66 1876 1886 41AO 48BO 4eeo 8BBO 1846 4954 4780 4A74 4A80 SHn variations of a number of instructions that should be quite familiar in their fullword forms. Before analyzing the operation of the program, it may be helpful to summarize the functions of the registers used, which will often be a valuable thing for the programmer to do. lOOP ZERO NOT TEMP PRINT START BAlR USING LH SR lR LR LA lH SH SLA lR CH BE AH AH BXlE STH lTR 6NZ STH EOJ SlA DR AH SRA STH EOJ DC DC DC NOGEN 256 3,0 .,3 5,ONES 6,6 7,6 8,6 10,2 11 ,DAYS 11 ,ONE 11,1 4,6 5,TEMP(4) ZERO 7,TEMP(4) 8,ONE 4,10,lOOP 8,NGCOO 8,8 NOT 5,AVER 7,1 6,8 7,ONE 7,1 7,AVER H'l' H'''H'3- - --- - ..ll o.c. .w.4 - 61 62 63 64 65 66 67 68 69 DC DC DC DC DC OS OS DS END H'29' H'30' H"3l' H'lX'FFFF' ONE ONES CAYS AVER NGOOD EXTENDED MNEMONIC FOR Be 8 EXTENDED MNE~ONIC FOR 8C 1 STORE ONES IF NO GOOD DATA STOP TO GET EXTRA BINARY PLACE IN QUOTIENT DIVIDE REGISTER ROUND OFF DROP THE EXTRA BIT FINAL RESULT END OF Joe - - H H H BEG I N Figure 3-23. A program to compute average monthly temperature, which takes into account the possibility of omitted readings 48 number of days into register 11 automatically expands the halfword into a fullword, which would mean that the sign bit of a negative number would be filled out. With correct data, the word here cannot be negative, of course. The number of days is to be used to terminate the summing loop that adds up the temperatures. The loop should be executed as many times as the number of days; it should be repeated (after the first time) one less time than the number of days. We accordingly subtract 1 from register 11 after loading it. Since the table of data consists of halfwords, the index register will have to be incremented by 2 between loop repetitions, and the proper limit value is two less than double the number of days. We can double a number quite simply by shifting left one place in a binary machine. (If the table had consisted of fullwords, requiring an increment of 4, a left shift of two places would mUltiply the number of days by 4.) In the working part of the loop we first check to see whether the particular temperature is valid, by comparing with the word of all 1's that had been set up in register S. The Compare Halfword expands the halfword from storage to a fullword by propagating the sign bit. This is necessary to us, since the load halfword that put the word of all I 's in register 5 did the same thing. We next branch on equal to the instruction at ZERO, which would happen if the reading was bad. If it was good, the branch is not taken; we add in the temperature, add one to the count of good readings, and then reach the BXLE. The BXLE increments the index register (4) by 2 (which is in 10) and checks whether the index is now the same as what we put in 11. If the index is low or equal, meaning that the list has not been exhausted, we branch back to LOOP to go around again. When the loop is finished, we reach the Store Halfword after the BXLE. Here we store the count of good readings at NGOOD; this conceivably could be zero. Next we check whether it was zero, using the Load and Test Register instruction (LTR). With the two register designations being the same, as they are here, the effect of this instruction is to set the condition code according to the sign and magnitude of the count in register 8. The Branch on Condition instruction then asks whether the count was either positive or negative and branches if so. If it was neither of these it must have been zero, in which case we store the word of all 1's for the average in AVER, and stop. If there was at least one good reading, we are ready now to compute the average. In order to be able to round off to the nearest degree, it is necessary to arrange the division so that the quotient has one binary place in it; this can be done by shifting the dividend to the left one place before dividing. The division is done this time with the Divide Register instruction, since the desired divisor (the count) is already in a register. Following the Divide Register we add 1 to the rightmost bit position of the quotient register to round off. Having done so, we shift the quotient back to the right to get rid of the extra bit and store the result. Fixed-Point Arithmetic 49 QUESTIONS AND EXERCISES 1. The L, A, S, and ST instructions all operate on a (fullword, halfword). 2. The first operand of an instruction usually specifies the operand that (sends, receives) information. 3. In a ST instruction the first operand specifies the operand that (sends, receives). Does the ST instruction, in this respect, follow the general rule, or is it an exception to the general rule? 4. Is the instruction M 7,QTY a legitimate instruction? If not, why not? 5. The D instruction specifies _ _ _ _ _ _ _ _ _ __ as the first operand, and the _'_ _ _ _ _ _ _ _ _ __ as the second operand. After completion of the divide operation, where is the quotient located? Where is the remainder located? 6. Assume that a fullword area of storage (reserved by a DS), to be addressed as XANDY, contains two positive items as below: x XANDY-O 50 y 19 20 31 Write the program to store X in a fullword area in storage called X, and Y in a halfword area in storage called Y. 7. The instruction BC 5,ROUT3 would branch to ROUT3 if the: a. Condition code is 5. b. Condition code is 1, 2, or 3. c. Condition code is 1 or 3. 8. Write an instruction to branch unconditionally to an instruction called NEWONE. 9. There are four fullwords named Xl, X2, X3, and X4 sequentially located in storage. Write one instruction that loads these four fullwords into registers 2, 3, 4, and 5 respectively. 10. Write an instruction that clears register 5 to zero. 11. Consider the instruction named LOOP in Figure 3-20. How will the effective address of TABLE(ll) be formed? 12. Write a single instruction that adds the contents of register 6 to register 5, tests to see if the sum now in register 5 is equal to or less than the contents of register 7, and then branches to an instruction called NEWONE if the answer is yes. Chapter 4: Programming with Base Registers and the USING Instruction A major programming feature of System/360 is the use of base registers for addressing main storage. One advantage is that compatibility is maintained between the small system with its short addresses and the large system with its longer addresses. The same instruction size and format accommodates both. Also, through appropriate use of base registers it is possible to relocate assembled programs almost at will. Great flexibility in program organization is thus achieved, since storage locations can be reassigned as dictated by the needs of the particular "mixture" of programs or program segments. Base registers are thus deeply involved in programming and in program execution. However, as we shall see, it is possible to delegate to the assembler almost all the clerical work of keeping track of base registers and computing displacements. With a full understanding of these techniques, the programmer is able to leave the housekeeping to the assembler where appropriate, and to employ more sophisticated methods where needed. In this chapter we shall see how the automatic techniques are called into operation and how the assembler implements them, and we will explore a few sHghtly more advanced techniques. As in so many other aspects of programming, particular emphasis mu st be placed on the question of when various actions occur: during assembly, linkage editing, or program execution. THE USING INSTRUCTION Automatic computation of the addresses of all operands in main storage requires the programmer to supply two items of information to the assembler and one to the object program. With the USING instruction, the programmer tells the assembler: 1. Which general registers may be used as base registers 2. What each one will contain at the time the object program is executed With this information the assembler can do its work of designating base registers and computing displacements. It still remains to place in the base registers the values we have promised the assembler will be there. This can in principle be done in many ways, but the most common is to use the Branch and Link Register instruction (BALR). The general format of this instruction is: BALRR1,R2 Rl receives the address of the next byte after the BALR; R2 supplies a branch address unless it is zero, in which case the next instruction in sequence is taken as usual. For OUI purposes here, the second operand (R2) is always zero. For instance, in the illustrative program we shall be considering shortly, we have an instruction: BALR 11,0 This places in register 11 the address of the next byte after the BALR, and there is no branch. Register 11 was arbitrarily chosen as the base register for this program. It is used as a base register for most of the programs in this text. In actual practice, the choice of a base register cannot be a completely arbitrary one. As mentioned earlier, most installations find it necessary to establish rather rigid conventions for register usage. In addition, the various operating systems for System/360 make use of certain general registers for supervisor routines, linkages between separate programs, and other purposes. Under most operating systems, registers 2 through 11 are freely available to the programmer and should be used to avoid any complication. Programming with Base Registers and the USING Instruction 51 AN EXAMPLE These ideas may be made more concrete by considering an example. Figure 4-1 is an assembly listing of a program the processing details of which do not concern us. The START instruction specifies that the assembled first byte of the program is location 25610 = 10016 • We see that the BALR instruction has in fact been placed at 100. (All numbers in the object program area of the assembly listing - on the left-hand side - are hexadecimal.) The BALR instruction specifies that general purpose register 11 is to be loaded with the address of the next machine instruction. This, of course, is done at execution time by the machine. The USING instruction, which is an assembler instruction and takes no space in the object program, informs the assembler that general purpose register 11 is available for use as a base register and will contain the address of the next machine instruction, as signified by the asterisk. The BALR is a two-byte instruction so the next instruction, the Load, is placed at 102. This number, shown in the location counter column in the USING statement, indicates what the assembler assumed would be the contents of base register 11. Let us look at the Load instruction to see how the assembler handled it. Reading from left to right the operation code is 58, the register loaded with a word from storage is number 2, no index register is specified, the base register is B16 = 11 10 , and the displacement is 02216 • With base register 11 containing 102 and with a displacement of 22, we get an actual address of 12416 , as listed under ADDR2. Looking down the listing we see that 124 is in fact the address corresponding to the symbol DATA, as it should be. The Add instruction is similar. With base register 11 again automatically designated, we have a base address of LOC 000100 000100 000102 000102 000106 00010A 00010E 000112 000116 OOOllA OOOllE 000124 000128 00012C 000130 000134 000138 000140 000100 OBJECT CODE ADDRI ADDR2 0580 5820 5A20 8B20 5820 5020 5860 5A60 4E60 B022 B02A 0001 B026 B02E 8032 B036 B03E 00000019 OOOOOOOF OOOOOOOA OOOOOOOC 0OOOO04E 00124 0012C 00001 00128 00130 00134 00138 00140 STMT 1 2 3 4 5 6 7 8 9 10 11 12 13 16 17 18 19 20 102 and a displacement of 2A for an effective address of 12C, which is the address of the symbol TEN. The Shift Left Algebraic instruction is a little different. All shift instructions have the RS format, with the index portion unused, but they still must specify a base register. Even though the effective "address" is never used for a storage reference, it is possible to make effective use of a variable number or positions of shift by varying the contents of the base register. In this program, however, such is not the case and we need a base register designation of zero. We see that this was done. The effective address is therefore just the displacement of 1. The remainder of the program presents no new base register concepts. As always, it is most important to distinguish between what is done at assembly time and what is done at execution time. The assembler, in the example at hand, has filled in base register numbers where needed and has computed displacements. These base register numbers and displacements become part of the actual instructions, as listed down the left side of the assembly listing. In carrying out the assembly operations, the assembler had to know what base register we wished to use and what we planned to put in it; this information we provided with the USING. The assembler cannot load the base register for the execution of our program, since that can be done only when the program is executed. We therefore provided the BALR instruction, which, at execution time, places the address of the next instruction into the specified register. The remainder of the program can now be carried out, with effective addresses being developed as intended. The assembler program is actually processed in several separate phases. One of its functions is to determine the length· and location of each instruction, area, and constant. SOURCE STATEMENT PROGE BEGIN DATA TEN RE SUL T BINI 21 B I N2 22 DEC 23 PRINT START BALR USING L A SLA S ST L A CVD EOJ DC DC DC OS DC DC NOGEN 256 11,0 *,11 2,DATA 2,TEN 2,1 2,DATA+4 2,RESULT 6,BINl 6, BI N2 6,DEC F'25' F' 15' F'10' F F' 12' F'78' os 0 END BEGIN Figure 4-l. Listing of a program to show how the assembler calculates and supplies addresses of all storage operands. The processing performed is not intended to be realistic. 52 While doing this, the assembler constructs a symbol table. As shown in Figure 4-2, this lists for each symbol used in the program: its length in bytes, either its value or location (VALUE), the number of the statement in which it is defined (DEFN), and all statements in which it is referenced. With the length and location of each instruction and the base register information provided by the programmer in the USING instruction, the assembler is able, in a later phase, to calculate the base register and displacement and to list these and the actual assembled addresses of all operands as they appear in Figure 4-1. In our program, we said with the START instruction that the first byte of the program should be assembled at location 25610 = 10016 . Everything said so far has assumed that the program will actually be loaded at 10016 . This is not so. In the first place, this location is within the low area of main storage that is occupied by the supervisor and other parts of the control program, and could not be used for program execution. Parenthetically, it should be explained that START 256 is not a standard programming practice. We have chosen it for the examples in the first few chapters of this book to cause our assemblies to begin at some positive value, simply for illustrative purposes. The usual practice is to specify a zero START, which greatly simplifies the programmer's chore of calculating addresses, a necessity when debugging a program. The second reason that our program will not be loaded at location 10016 is that, regardless of the location we give in the START statement, our assembled object program is in relocatable form and it is not executable until processed by the linkage editor. The linkage editor is an IBM service program that is part of the operating or programming support system. The linkage editor assigns the actual starting address in main storage for each object program in a job input stream, and edits these into executable programs. It uses information supplied by the assembler regarding the length of the program, its name (given in the START statement, PROGE in this case), the assembled locations of any relocatable address constants, and other details necessary to perform the relocation. When the program is in executable form, all statements, constants, reserved storage spaces, etc., remain in the same relative positions as in the assembly listing. Nothing needs to be changed to make the object program operate correctly from the new location or, at a later time, from still another location. All that is involved is the relocation factor. Suppose the linkage editor assigns location 320016 as the starting address. When the program has been loaded, it begins with execution of the BALR instruction. Now, what is the address of the next instruction after the BALR? The answer is 3202. This value goes into register 11 and becomes the base address. The displacements in the assembled instructions have not changed, of course. The effective address in the Load instruction is now 3202 + 22 = 3224. With the new starting location, 3224 is exactly where DATA appears. All other addresses are correctly computed as well, including the "address" in the Shift, which is completely unchanged since no base register is used. I t is also possible for the programmer by use of a control card to i~ll the linkage editor which starting location to assign. A ·complete relocation of the program after assembly is thus a simple matter. In more complex program structures, the linkage editor has more work to do than this example'might suggest, but it is nevertheless feasible to execute programs from whatever storage locations may be convenient and available under any particular set of circumstances. As we have noted, this simplicity of program relocation was one of the reasons for providing base registers in System/360. The techniques of program relocation and the functions of the linkage editor will be discussed in more detail in the chapter on· subroutines and program relocation. CROSS-REFERENCE SYMBOL LEN BEGIN BINl BIN2 DAT A DEC PROGE RESULT TEN 00002 00004 00004 00004 00008 00001 00004 00004 VALUE DEFN 000100 000134 000138 000124 000140 000100 000130 00012C 00003 00020 00021 00016 00022 00002 00019 00018 0023 0010 0011 0005 0012 0008 0009 0006 NO STATEMENTS FLAGGED IN THIS ASSEMBLY Figure 4-2. Symbol cross-reference table constructed and listed by the assembler for the assembly in Figure 4-1 Programming with Base Registers and the USING Instruction 53 MORE THAN ONE BASE REGISTER following instruction, we give that instruction a symbolic name (HERE) and use the symbol. This gives exactly the same effect with respect to register 11, and permits us to refer to the contents of 11 in terms of a symbol, which we shall need for loading register 9. (The choice of register 9 was arbitrary.) In loading the second base register, we cannot use a BALR; we want register 9 to contain not the address of the next instruction, but 4096 more than whatever went into 11. To accomplish this we use an address constant, named BASE in this case, which is written with the address HERE+4096. We see that the constant BASE has been assembled as we instructed: hexadecimal 1102 is 1000 greater than the value of the symbol HERE, and 100016 =409610 , Base register 9 will thus be loaded with 110216 at execution time. This information is given to the assembler with a USING that has the address HERE+4096. It is worth while noting which base register was used in the Load instruction that loaded base register 9: we see that the base register is 11 (which contains 102) and there is a displacement of A (+ 10 decimal). The effective address is thus 1OC, which we see is indeed the address of the constant BASE. It is important to realize that at the time register 9 is being loaded, the only base register available is 11; the effective address of the instruction that loads 9 therefore cannot be more than 4096 greater than the contents of 11. Thus the address constant BASE cannot be at the end of the entire program, which would be more than 4096 bytes away. We have chosen to place it at almost the beginning and branch around it. Other placements are possible, so long as they do not cause the assembler to try to use a displacement in the Load instruction at HERE that is negative or greater than 4095. The displacement in an instruction is limited to a positive number in the range 0-409510 = 0-FFF I6 , since this is the limit that can be expressed in an unsigned 12-bit number. This means that an effective address cannot be less than the base address or more than 4095 greater, when an index register is not being used. If a program must reference a range of addresses greater than 4095, the easiest and most common approach in routine programming is to use more than one base register. It should be noted, however, that it takes a rather large program segment to exhaust the range of displacements using one base register. With average length instructions, it takes a full pad of coding paper to use up 4096 bytes: It will usually be desirable to break a program this large into smaller segments anyway, so it will probably be extremely rare in practice to need more than one base register because of program length. Long sections of storage for data or results are another matter. Frequently it may be advantageous to assign one base register to the program and another to data. This is done in the last example in this chapter. For now, to establish some basic ideas, let us make up a program that does use more than 4096 bytes for combined data and program. We shall not actually write an illustrative program that large, but we can simulate the effect of such a size by using the ORG assembler instruction to advance the location counter. The partial program shown in Figure 4-3 was designed with the sole purpose of illustrating base register ideas; the "processing" is not intended to be meaningful. After the usual START, we have a BALR to load base register 11 with the address of the next instruction. The USING instruction is slightly different this time. Instead of using an asterisk to denote the address of the fust byte of the lOC 000100 000100 000102 000102 001102 000106 00010A OOOIOC 000110 000114 000118 001100 001100 001104 001108 OOllOC 001110 000100 OBJECT CODE ADDRI ADDR2 05BO 5890 BOOA OOIOC 47FO BOOE 0000 00001102 5820 BFFE 5A20 900E 47FO 9002 00110 0000001B 5830 BFFE 5A30 900E 41FO BOOE OOOOOOOA 01100 01110 01104 01100 01110 00110 STHT SOURCE ST ATEHENT 1 PROGF 2 BEGIN 3 4 HERE 5 6 7 8 9 10 11 12 13 14 15 16 17 BASE FIRST DATA SECOND TEN ST ART BALR USING L USING BC 256 11,0 HERE,11 9, BASE HERE+4096,9 15,FIRST DC A(HERE+4096) 2,DAT A 2,TEN 15,SEeOND *+4068 F I 123' 3, OAT A 3,TEN 15,FIRST f'10' BEGIN l A BC ORG DC l A Be DC END Figure 4-3. Listing of an incomplete program with an Origin (ORG) assembler instruction to simulate a length of over 4096 bytes, thus requiring two base registers 54 As an example of an attempt to use a negative displacement, suppose we were to put the address constant BASE at the very beginning of the program, between the START and the BALR: then the displacement in the Load would need to be -6, which is impossible. Following the constant BASE, we have two instructions that are meant to suggest the processing steps of the program, and then a branch to an instruction near the end. For the sake of illustration, we want the program to look as though it is more than 4096 bytes long. This we can simulate by an ORG that, in this case, advances the location counter by 4068. This arbitrary -appearing number was chosen to put DATA at the end of a 4096-byte segment controlled by base register 11, which means that the following instructions and data are referenced by base register 9. Let us now investigate how the assembler assigned base registers and computed displacements. The next instruction is a Branch on Condition with a mask of 15, which indicates a branch on any condition, or an unconditional branch. This branch to FIRST involves a location under the control of base register 11; if base register 9 were specified, the displacement would have to be negative. The Load at FIRST refers to DATA. The base is 11, with a large displacement of FFE16 = 409410 . The Add refers to a location that is more than 4096 bytes away from the beginning of the program, so base register 11 cannot be used. We see that 9 has been indicated, with a displacement of E16 = 1410 • The following Branch on Condition references a storage location 2 greater than what was placed in register 9, so register 9 is the base and the displacement is 002. Down at SECOND, the base registers and displacements for getting DATA and TEN are exactly as they were before; these matters are unaffected by the location of the instructions. The assembled Branch on Condition to FIRST is precisely the same as the assembled Branch on Condition that appeared earlier just before BASE. The essential concept is that the assembler assigns whatever base register is necessary to get a displacement less than 4096. If the program has been written so that two or more base registers have contents that satisfy this rule, the assembler chooses the one that leads to the smallest displacement. Later we shall see an instance in which this rule for choosing base registers is important. Programming with Base Registers and the USING Instruction 55 SEPARATE BASE REGISTERS FOR INSTRUCTIONS AND DATA We have suggested that it will be rare for a program segment to be so long as to require more than one base register. On the other hand, it may be fairly common to want separate base registers for instructions and data, even though the instructions take far fewer than 4096 bytes. How this can happen is illustrated in the following problem. Suppose we have six records in storage, each record consisting of 80 characters. The six records are in consecutive storage locations; the first of the 480 bytes has the symbolic address DATA. Within each record there are eight fields of ten characters each, named FIELD 1, FIELD2, etc. Each field is in packed decimal format. We are required to add FIELDI and FIELD2 and place the result in FIELD3. The other five fields are not used in this program. This processing is to be done for each of the six records, using a loop. Now the question is, how do we attack the loop? The arithmetic will use decimal instructions, which have the SS format and do not provide for use of an index register. We could write instructions to modify the displacement of every instruction that refers to the records, but this is very poor form if there is a better way available. The solution proposed here is to modify the base register contents to pick up the records in succession, which means that between loop repetitions we will add 80 to the base register. But now we have a new problem. If only one base register is used, how do we modify its contents and still get a correct base for Branch instructions and for references to program constants? The simplest answer is probably LOC 000100 000100 000102 000102 00012C 000106 00010C 000112 000116 OOOl1A OBJECT CODE ADDRI ADDR2 05BO 5880 BOlE D209 FA99 5A80 5980 4770 00120 8014 8000 00140 0012C 8014 800A 00140 00136 B022 00124 00128 B026 B004 00106 000120 0000012C 000124 00000050 000128 0000030C 00012C 00012e 000136 000140 00014A 000154 00015E 000168 000172 00017C 000100 STMT SOURCE STATEMENT 1 2 LOOPA 3 BEGIN 4 5 LOOPl 6 7 LOOP2 8 9 10 11 12 15 16 17 18 19 20 21 22 23 24 25 26 27 28 obvious: use two base registers, the second of which refers only to the data processed by the loop. A program is shown in Figure 4-4. The loading of base registers is much as it was in Figure 4-3, except that this time register 8 is loaded with the address corresponding to DATA, rather than with 4096 more than what register 11 contained. As a matter of fact, it turns out that register 11 contains 10216 , and register 8 contains 12C16 • This will mean that the first byte of the area named DATA could be obtained by adding a displacement of 2A to register 11, or by adding a displacement of zero to register 8. As we noted, the assembler picks the way that gives the smaller displacement. It is essential for us to be able to depend on this fact. We see also that in this program the address constant for loading register 8 has been placed at the end of the instructions rather than in the instruction stream. This is permissible as long as we are sure that it is not more than 4096 bytes away from the beginning of the program, which it obviously is not. It is assumed, for the purposes of this illustration of base register ideas, that the data is provided by another program segment and will be used later by still another prpgram. We therefore provide space for the data with DS instructions that allot space for the required number of characters but do not assemble constants to be entered. The DS for DATA, in fact, does even less than that: it provides a reference point for the symbol, but does not even reserve space since a zero is written for the duplication factor. Thus BASE EIGHTY TE ST DATA FIELDl FIELD2 FIE LD3 FIELD4 FIELD5 FIELD6 FIELD7 FIELD8 PRINT START BALR USING L USING MVC AP A C BNE EOJ DC DC DC OS OS OS OS OS OS OS OS OS OS END NOGEN 256 II ,0 *,11 A,BASE DATA,8 FIELD3,FIELOl FIELD3,FIEL02 8,EIGHTY 8,TEST LOOP2 A(OATA) F'80' A(OATA+480) OF CllO CLlO CLlO CllO CllO CLlO eLlO CllO 5CL80 BEGIN Figure 4-4. Program with separate base registers for processing and data, showing how a base register can be used to provide indexing for loop control 56 DATA and FIELDI b,?th refer to the same byte. The point of this approach is to have DATA for a name for the entire 480-character storage area, and still use names for the fields within the first record. An alternative approach would be to use DATA as the name of the first field, DATA + 10 for the second, DATA + 20 for the third, etc., but the loss of meaningful names would be a disadvantage. Another alternative would be to omit the entry for DATA and use FIELDI wherever DATA appears earlier. This would also be a little less meaningful, perhaps. The final DS reserves 400 bytes of storage for the remaining five records. The Move Characters instruction at LOOP2 moves the first field to the third field location. Reading across the assembled instruction, which we note is in the SS format, we see: the actual operation code is D2, the length code is 09, the base register for the first operand is 8, the displacement for the first operand is 014, the base register for the second operand is also 8, and the displacement for the second operand is zero. The length code of 9 is correct for a field oflength 10; the assembler picked up the implied length from the DS entry for FIELD3, and subtracted 1 from the length to get the length code. Checking the address calculations, we see that a base address of 12C plus a displacement of 014 give an effective address of 140, which is correct for FIELD3. A base address of 12C and a displacement of zero give the address of FIELD 1. The Add Decimal instruction that follows does the required addition. This instruction has two length codes, both 9 in this case, for two fields of length 10. The displacement of OOA, together with the base address of 12C, correctly lead to 136, the address of FIELD2. The addressing of FIELD3 is as before. Now we are ready to add 80 to the base register associated with DATA and go back to process more records if more remain. We add 80 to base register 8 and then compare with an address constant to test for completion of the loop. What should the test constant be? Since we modify before testing, and since there are 480 characters in the six records, we should stop repeating if at this point the base register contains a number 480 greater than what it was to start. It was originally the equivalent of the symbol DATA, so the test value ought to be DATA + 480, as shown. The ·Branch on Not Equal here is the extended mnemonic for BC 7. If the branch is not executed, we are finished and the next instruction ends the job. If the program were written to use only one base register, we would be in trouble with the address of the Branch instruction. The assembler would assume a certain value for the base register and compute a displacement accordingly. After modifying the base register contents, we would no longer have the desired branch address. It is of course true that we are modifying the contents of base register 8 also, but we have carefully arranged that it is not used as a base for anything besides DATA. No confusion is caused, therefore, because we have "cheated" by changing the contents of a base register from what we promised the assember would be there. What we told the assembler will lead correctly to the first record processed; by the time the contents are actually changed during execution, the assembler will no longer be on the scene to know that anything happened. In practice it would normally be necessary to process many blocks of six records, not just one. In that case we would have to get register 8 back to its starting value. This is done simply by re-executing the Load instruction at LOOPI. If a program like this were to be executed, it is perhaps obvious that something would have to be done during loading to take care of the address constants at BASE and TEST. It would clearly not be enough for the linkage editor just to assign the initial program loading location. This matter is properly handled by an automatic flagging of all address constants in the relocation dictionary produced by the assembler, and by suitable modifications performed by the linkage editor. In order to illustrate one last facet, suppose that there were some compelling reason to place additional instructions after DATA. This could be done by a Branch to them. Suppose that within these additional instructions there were Branches to locations within the new group. What would the base register situation be? With the size of program and data shown, either base register 11 or 8 could supply a displacement of acceptable size; the assembler could pick the one leading to the smaller displacement, register 8. But the contents of 8 change as the loop is executed; how can we tell the assembler that 11 is wanted, not 8? The answer is the DROP instruction, in which we would say DROP 8 at the beginning of the new group of instructions. This says to the assembler that general purpose register 8 may no longer be used as a base register. The only one left is then 11, so it is the one used, as desired. Programming with Base Registers and the USING Instruction 57 QUESTIONS AND EXERCISES Consider the following programs. Note that some of the program statements have been omitted from the listings. The locations assigned to each instruction, constant, and area are listed in hexadecimal, as in all program listings. The locations are such that you should have no difficulty with hexadecimal arithmetic. Questions 1 to 4 refer to Figure 4-5. 1. In the program in Figure 4-5, a. What instruction informs the assembler that register 11 is to be used as a base register, and tells the assembler what value it must assume to be in that base register? b. What instruction causes register 11 to be loaded with the base address at execution time? 2. In the spaces provided in the diagram, a. Write the value the assembler assumes to be in base register 11. b. Using the symbol table and answer 2a, write the base register and displacement appearing in the object instruction for each encircled operand. 3. Using the specified base register and displacement, write (in the spaces provided) the effective address developed during assembly for each encircled operand. 4. Assume that the program, when loaded for execution, is located starting at 320016 instead of 20016 • In the spaces provided, list: a. The locations into which each instruction, area, and constant (that is, each statement) is loaded. b. The value placed in register 11 at execution time. c. The effective address computed at execution time for each encircled operand. 5. Consider the program in Figure 4-6. In the spaces provided, list: . a. The symbol table prepared by the assembler (symbol and location only). b. The contents of base registers 9, 10, and 11 assumed by the assembler. c. The base register and displacement for each encircled operand. d. The values actually placed in registers 9, 10, and 11 at execution time, assuming the program is loaded at 1000 16 • e. The location of ea~h statement at execution time. f. The effective address computed at execution time for each encircled oper~nd. During assembly r -________ __________ ~A~ LOCATION During execution with program loaded at 320016 ~ STORAGE OPERAND OF STATEMENT PROGG START BALR 512 11,0 USING L *,11 A 2~ 000202 000206 S ST 2~ATA+Y 2 ([ESULY 000234 000238 L 6~ 000252 DATA DC DC F' 25 ' F'15' 000304 000308 TEN DC DS F'10' F 000328 BEGIN RESULT ~ Base Reg. Displacement Address ~--~I\,------, LOCATION ADDRESS OF dF STATEMENT STORAGE OPERAND· _------::::::::="'1 VALUE LOADED IN BASE REGISTER 11 --- - -- --- 000324 SYMBOL BIN1 DC END F'12' 000344 BEGIN ·Base and displacement remain the same as during assembly. Figure 4-5. Program for questions 1 to 4 58 LENGTH VALUE BEGIN 02 000200 BIN1 04 000344 DATA RESULT 04 04 000304 000328 TEN 04 000324 During execution with program loaded at 100016 ~______-JA~_______~ During assembly r -____________ LOCATION OF STATEMENT PROGH START o BEGIN BALR 11,0 USING FIRST,ll FIRST BC 15,SKIP ~A~ ____________ ~ STORAGE OPERAND Base Reg. Displacement Address OF VALUE LOADED INTO BASE REGISTERS During assembly (assumed) During execution (actual) II 000002 10 DC F'3472' BASEl DC ACFIRST+4096) 000024 BASE2 DC ACFIRST+8192) 000028 000008 000104 -- --- L 1~ USING FIRST+4096,10 L 9QASED FIRST+8192,9 000108 -- --- ------- USING BC 1~ 001504 -- --- LOOP A ~ 001898 -- --- ----- LOOPB S 5,DATA 002204 BC a<@D 002508 -- --- ---- BC 8cc1oopv 002904 -- --- ---- END BEGIN CK8 ADDRESS STORAGE OPERAND* 000000 DATA SKIP LOCATION OF STATEMENT ------- ------SYMBOL VALUE *Base and displacement remain the same as during assembly. Figure 4-6. Program for question 5 Programming with Base Registers and the USING Instruction 59 Chapter 5: Decimal Arithmetic The decimal instruction set is an optional feature of System/360, but one that most users elect. Besides making it possible to do arithmetic in the more familiar decimal system, the decimal instruction set includes instructions for editing data, that is, preparing data for printing by the insertion of characters such as commas, periods, and dollar signs. The decimal instruction set permits· operations on variable length data since the operations are performed in storage areas rather than in registers. It includes the following instructions: Add Decimal Compare Decimal Divide Decimal Edit Edit and Mark Multiply Decimal Subtract Decimal Zero and 'Add The student will find a detailed description of the basic operation of these instructions in the System/360 Principles of Operation. This chapter will provide examples of their use in various problem situations and will attempt to show how the programmer can make them a working part of his strategy. Data operated upon by instructions in the decimal set must be in one of two forms, packed or zoned, depending on the instruction. As a generalization, we can say that the packed format is required for arithmetic and the zoned for input/output. The two formats are shown in Figure 5-1. Byte I I I I I I I Zone I Digit I Zone 1__ _ I Digit Byte I Digit Izone I Byte I I I Digit I Sign I I Byte I Digit I Sign I I Digit I Figure 5-1. Formats of packed and zoned decimal numbers In the packed format, two decimal digits are placed in each byte except the rightmost of the field, which contains a digit and the sign of the entire number. Digits and sign occupy four bits each. The decimal digits 0-9 have the binary codes 0000-1001. The codes 1010-1111 are not valid as digits. In the sign position, the code combinations 60 Decimal instructions have precise requirements that operands be in packed or zoned format. The Pack and Unpack instructions, standard instructions of the system, are available for converting from one form to another. The Move with Offset instruction, another of the standard instructions, is often used for shifting factors used or developed in decimal arithmetic operations. Instructions for converting from binary to packed and from packed to binary are also part of the standard instruction set. We shall see examples of all of these operations later. Decimal instructions use the SS (Storage-to-Storage) format. The machine format is: lop Code \ Ll I I I I I I L2 Bl 01 B2 02 In assembler format, as written in the source program, the sequence of an SS instruction is: Op code Byte Ir-D-i-9i-t--'I-D-i9-i-t-r1-D-i9-it---'r~~ ~~] Digit I Digit Byte 1010, 1100, 1110, and 1111 are taken to mean plus, and 1011 and 1101 are recognized as minus. When a sign is generated as a part of an arithmetic result, a plus is 1100 and a minus is 1101. As mentioned before, all reference to binary codes in this book is to System/360 EBCDIC unless another is specified. In the zoned format the rightmost four bits of a byte are called the numeric portion of the byte and contain a digit. The leftmost four bits are called the zone and contain either a zone code or, in the case of the rightmost byte, the sign of the number. The codes for signs are treated as described for the packed format. The code for all zones is 1111. Dl (Ll ,Bd,D2(L2 ,B2) There are two addresses, both of course referring to core storage. Each address is formed from a base register contents and a displacement. The address always refers to the leftmost byte of an operand. For each operand there is a separate length in most cases. In the machine instruction, the length code may vary between 0000 and 1111, or zero and 15. These correspond to lengths of one to 16. In other words, the actual length is one greater than what appears in the length code of the object program. In assembler language programming, lengths will quite often be implicit in the data defmitions, but when we do write an explicit length, it is the actual length. The generation of the proper code in the machine instruction ( one less than whatever we write) is the function of the assembler. With these preliminaries in mind, let us turn to an example. ADDITION AND SUBTRACTION IN DECIMAL Let us take the first example used in the chapter on fixed-point arithmetic and write it with decimal arithmetic. The application is an inventory updating. We were given an old on-hand (OLDOH), a number received (RECPT), and a number issued (ISSUE); we were to compute the new on-hand (NEWOH). For this program we shall assume that all data entries are already in packed format and are four bytes long. Four bytes can contain, in packed format, seven decimal digits and the sign. In Figure 5-2 let us look first at the data definitions. The DC instructions for OLDOH, RECPT, and ISSUE and the DS for NEWOH all have operands that start with PL4. The P stands for packed format, and the L4 for a length of 4. Lengths are always in bytes, never digits. This is our first contact with a length modifier in a DC instruction. Here, we are specifying that the constants must be four bytes long. If we had omitted the length, the constant generated by the assembler would have been as long as needed to hold the data value we wrote, in this case one byte. (Length modifiers are permitted for other types of data, too.) Looking at the assembly listing in Figure 5-3, we see that the DC entries have resulted in four-byte constants. In each case, with the data shown, there are six zeros, followed by a digit, followed by a hexadecimal C (binary 1100), which signifies a plus sign in EBCDIC. Turning back to the instructions of the program, we see the familiar PRINT, START, BALR, and USING instructions. Note that the START instruction specifies zero. This is the usual programming practice at most computer installations, and we will follow it in this book from now on. The ST ART instruction simply tells the assembler where to begin the program during assembly. The linkage editor will assign the actual starting address later, that is, the address in core storage at which the program will be located during execution. The first processing instruction is a new one, Move Characters (MVC). This is an SS format instruction of a slightly different sort: it moves from storage to storage, but lOC 000000 000000 000002 000002 000008 OOOOOE OBJEC T CODE ADDRI ADDR2 05BO D203 B020 B014 00022 00016 FA33 B020 B018 00022 OOOlA FB33 B020 BOIC 00022 DOOlE STMT there is only one length, because the "sending" and "receiving" fields must be of the same length. That length may be from one to 256 bytes. Looking at the assembled instruction, we see that a length code of 3 has been supplied by the assembler; this is the correct code for a length of four bytes. The length of the operands was implied by the data defmitions. It is also possible, and frequently necessary, to write explicit lengths to override what the assembler would infer. The generation of an address from the base register contents and the displacement is as before: for instance, for OLDOH the base register contains 002, the displacement is 014; the sum of these is 016 which we see is the address for OLDOH. SOURCE STATEMENT 1 2 STOCKl 3 BEGI N 4 5 6 7 8 000016 0000009C OOOOlA 0000004C OOOOlE 0000006C 000022 000000 Figure 5-2. An assembler language program to perform a simple calculation in arithmetic, using the System/360 decimal instruction set I I OlOOH 12 RECPT 13 ISSUE 14 NEWOH 15 PRINT START BAlR USING MVC AP SP EOJ DC DC DC DS END NOGEN 0 1l,O *,11 NEWOH,OlDOH NEWOH,RECPT NEWOH, ISSUE Pl4' 9' PL4'4' PL4'6' PL4 BEGIN Figure 5-3. Assembly listing of the decimal arithmetic program in Figure 5-2 Decimal Arithmetic 61 The purpose of the Move Characters instruction is to get the old on-hand quantity into a location where we can perform arithmetic without disturbing the original quantity. The decimal instructions make no use of the general registers (except, of course, to specify the base), so we must provide storage locations for all data. We do not wish to destroy the old on-hand, so we must arrange for the arithmetic results to go somewhere else. In this case, the obvious place is NEWOH, where we want the eventual result anyway. In other problems, as we shall see, it is often necessary to provide temporary working storage. The Add Decimal (AP, for Add Packed) instruction adds the quantity received to the old on-hand, which by now is in NEWOH. Note that the result of an arithmetic operation is always stored in the first operand location. The two fields in an Add Decimal instruction need not be the same length, since there are two length codes in the instruction. Here, 62 they are the same, as it happens. The Subtract Decimal (SP) instruction deducts the quantity issued. There is no need for something equivalent to a Store instruction; every instruction already involves two storage addresses, one of which receives the result. The output in Figure 5-4 shows that the result has been correctly computed. 0000009C 0000004C 0000006C 0000007C Figure 5-4. Output of the program of Figure 5-3, showing OLDOH, RECPT, ISSUE, AND NEWOH, in that order DECIMAL MULTIPLICATION For a simple example of decimal multiplication, let us write a program for the computation of a new principal amount. We are given a principal (PRINC), here taken to be four bytes, and an interest factor (INT), two bytes; we are to compute the new prmcipal amount after adding in the year's interest. The interest rate of 3% is expressed as the factor 1.03, so that a single multiplication does the whole job. A program is showri in Figure 5-5. The Multiply Decimal (MP) instruction takes the second operand to be the multiplier; the first operand initially contains the multiplicand~ arid at the end of the operation contains the product. However, we cannot begin with a multiply instruction specifying PRINC as the multiplicand, as we might be inclined, because extra space is required. The first operand is required to have at least as many high-order zeros as the size of the multiplier field. We need, therefore, to move the principal to a working storage area having extra positions at the left. These extra positions must be cleared to zero before the multiplication starts. The Zero and Add (ZAP) does just what we need. The effect of the instruction is to clear the first operand (PROD, in this case) to zero, then add the second operand (PRINC) to it. PROD is two bytes longer than PRINC; these extra four digit positions will be cleared to zeros before PRINe is added in. This provides the zeros needed to satisfy the multiplication rule. Now we multiply. With the sample data shown, the result in PROD will be 0000025636 7C, as shown in the comments field. We were regarding 2489 as meaning $24.89, and 103 as meaning 1.03, so there are four places to the right of the understood decimal point in the product, which we therefore regard as 0000025.6367 +. We would now like to round this off to $25.64. This can be done in a number of ways. Here we simply add a constant (ROUND) properly set up to add a 5 into the second place from the lOC OBJECT CODE ADDRl ADDR2 SOURCE ST AT EMENT STHT 1 000000 000000 05BO 000002 000002 000008 OOOOOE 000014 F853 FC51 FA5l 0100 B026 B026 B026 B02A B020 6024 B02C B02B 00028 00028 00028 0002C 00022 00026 0002E 00020 OOOOlA 0203 B020 B021 00022 00029 000022 0002489C 000026 103C 000028 00002E 050C 000000 2 I NTC 3 BEGIN 4 5 * 6 7 B 9 10 * ** 11 12 13 14 15 16 19 20 21 22 23 right. The second operand in an Add Decimal instruction is permitted to be shorter than the first (which holds the result). When this is done, any carries that occur are properly propagated. We are now ready to discard the two digits at the right end of the product. But this is not quite as Simple as just not moving them to PRINe, because if we did that, PRINC would not be a legal operand in any subsequent arithmetic operation, since it would not have a sign. Before moving the result bac~ to PRINe, therefore, we must move the sign from where it is to the byte just to the left. This we can do with a Move Numeric (MVN) instruction, which transmits only the numeric portions of the bytes. The instruction says: Take the numeric portion of the byte at PROD+5 (which is the rightmost byte of the PROD, and contains the sign) and move it to the byte at PRODt4 (which is the byte to the left and will be the rightmost byte of PRINC after the next instruction); the field to be moved is one byte long. The length for this instruction cannot be left to the assembler; the implied length here would be 6 (the length of PROD), which would destroy the result. The Move Numeric instruction has only one length code, so we need give only one explicit length. Finally, we are ready to move the result to the field where it is required to be at the end of the program, PRINC. Remember that PROD is six bytes long. The leftmost byte contains two zeros, we assume, and the maximum size of the result is taken to be seven digits. The validity of such an assumption as always, is the responsibility of the programmer. The rightmost byte of PROD contains a digit and sign that we now wish to drop. To drop the leftmost byte, we write the address as PROD+ 1. To drop the rightmost, we need a length of 4, which happens to be the implied length of PRINC, so no explicit length is necessary . * * PRI NC I NT PROD ROUND PRINT ST ART BALR USING NOGEN 0 11,0 *.11 NUMBERS BELOW SHOW CONTENTS OF PROD AFTER INSTR IS EXECUTED C IS PLUS SIGN IN PACKED FORMAT ZAP MP AP MVN PROD,PRINC PROD,INT PROD,ROUND PROD+4(1),PROD+5 00 00 00 00 MVC PRINC,PROD+l CONTENTS OF PRINC WILL BE 00 02 56 4C EOJ DC DC OS DC END Pl4'2489' Pl2'103' Pl6 Pl2'50' BEGIN 00 00 00 00 00 02 02 02 02 56 56 56 48 36 41 4C 9C 7C 7C 7C Figure 5-5. Listing of a program that performs decimal multiplication. Step-by-step results to be expected during execution are shown in the comments field. Decimal Arithmetic 63 DECIMAL DIVISION Some of the operations in working with the decimal instruction set are different enough from similar operations in other machines that it may be well to pause and consider them in somewhat more detail than we have devoted to other topics. Division is one such operation; Move instructions, used as the equivalent of shifting and considered later in the section on shifting of decimal fields, is another. The Divide Decimal (DP) instruction is in the SS format. The first operand is the dividend (the number divided into), the second the divisor (the number divided by). After the operation is completed, the frrst operand field holds the quotient (at the left) and the remainder (at the right). The remainder is the same length as the divisor. Let l:S see how this description works out in an example. Suppose we begin with the symbolic locations DIVID and DIVIS as follows: DIVIDbefore DIVIS 0 0 0 0 0 4 2 4 6 + 0 3 1 + We have indicated DIVID as a "before" value, because after the division the same field will contain both the quotient and the remainder. All operands are in packed format, as with other decimal arithmetic operations. After executing the instruction: DP DIVID,DIVIS the contents of DIVIS would be unchanged; the contents of DIVID would be: DIVID after 0 0 1 3 6 + 0 3 0 + This means that 4246 divided by 31 in this way gives a quotient of 136 and a remainder of 30. The divisor was two bytes, so the remainder is two bytes. The quotient takes up the remaining space in the first operand field. The question of the lengths of the various fields can be answered with a useful rule: Number of bytes in dividend = number of bytes in divisor + number of bytes in quotient I t is perhaps most common to know the number of bytes in the divisor and the number desired in the quotient, the question being how much space to allow in the dividend in order to get the specified size of the quotient. If two of the three lengths are known, the formula can be used to get the length of the third. Note that the formula is stated in terms of the number of bytes, not the number of digits. The reason is that the frrst operand field contains only one sign at the beginning, when it is the dividend, but two afterward, when it contains both quotient and remainder. This change would invalidate 64 a rule stated in terms of digits. A very similar rule gives the relationship among decimal points. If we agree that by "decimal places" we mean the number of digits to the right of an assumed decimal point, the rule is: Number of places in dividend = number of places in divisor + number of places in quotient In the example given above, we assume that all quantities are integers, that is, they have no decimal places. The rule still holds, although in its most elementary form: 0=0+0 Let us see what the result would be if we were to arrange the dividend of the example so that it has one decimal place: DIVIDbefore 0 0 0 0 4 2 4 6 0 + In other words, we now view the dividend as 4246.0. The result is: DIVID after 0 1 3 6 9 + 0 2 1 + The rule says that the quotient should have one decimal place: the dividend has one and the divisor has zero. The quotient must therefore be interpreted as meaning 136.9. (And if anything has to be done with the remainder, it should be taken as meaning 2.1.) Suppose the dividend were shifted one more place to the left: DIVIDbefore 0 0 0 4 2 4 6 0 0 + DIVIDafter 1 3 6 9 6 + 0 2 4 + This result should be read as 136.96. What would happen if we tried to set up the dividend with yet one more shift to the left? There is room in the dividend - but there is no more space in the quotient field. This constitutes a divide exception, which occurs whenever the quotient is too large to fit in the field available to it. An interrupt occurs. I t is possible to check for the possibility of a divide exception, given sample numbers. To do this, the leftmost digit position of the divisor is aligned with the second digit position from the left of the dividend. When the divisor, so aligned, is less than or equal to the dividend, a divide exception will occur. Take the situation suggested: DIVIDbefore DIVIS 0 0 4 2 4 6 0 0 0 + 0 3 1 + This is the alignment described by the rule. As aligned, the divisor is smaller. We saw before that there would not be enough room for the quotient. This question does depend on the particular numbers involved, of course. Suppose the quantities were aligned the same way but that the dividend were 2246 instead of 4246: DIVIDbefore 0 0 2 2 4 6 0 0 0 + DIVIS 0 3 1 + This is entirely acceptable. To be completely confident that a divide exception cannot occur, we have to know the maximum possible size of the dividend and the minimum possible size of the divisor, or we must know the maximum size of the quotient. Further examples of decimal division will be given after we have studied shifting, which is often needed to arrange the dividend so as to give the necessary number of decimal places. Decimal Arithmetic 65 SHIFTING OF DECIMAL FIELDS Shifting as such is not provided in System/360 decimal operations. As in other variable-field-length computers, the equivalent of shifting is performed by appropriate combinations of data movement instructions. The matter is made somewhat more complex by the factor of packed formats, with two digits per byte and with the special status of the sign position. This is a small price to pay for the increased storage economy of the two-digitsper-byte arrangement. It is also necessary to exercise caution when overlapping fields are to be manipulated, in order to be sure that no data is destroyed. This is another occasion where it is absolutely essential to remember that all operands are addressed by the leftmost byte. Shifting to the Right Let us begin with the simplest type of shift: a decimal right shift of an even number of places. Suppose that we have a five-byte, nine-digit number in SOURCE; we are to move it to a five-byte field named DEST with the last two digits dropped and two zeros at the left. We can do this two ways: with or without disturbing the original contents of SOURCE. Let us do it first without disturbing them. Suppose that the two fields originally contain: 12 SOURCE 34 56 78 9S 55 55 DEST 55 55 55 The S stands for a ph.is or minus sign, whichever it might be. The instructions for accomplishing the shift could be as follows, where we have also shown the contents of the two fields after the execution of each instruction: SOURCE MVC DEST+l(4),SOURCE' 12 34 56 78 9S MVN DEST+4(1),SOURCE+4 23 34 56 78 9S MVC DEST(l),ZERO 12 34 56 78 9S DEST 55 12 34 56 78 55 12 34 56 7S 00 12 34 56 7S In the first Move Characters instruction, an explicit length of 4 is stated; this length applies to both fields. With the first operand address being DEST+ 1, the four bytes of the destination are the rightmost four. The second operand is given simply as SOURCE, so the four bytes there are the leftmost. The last two digits ( one byte) have been dropped. But the sign has been dropped too, in the process. We accordingly use a Move Numeric instruction to attach it to the shifted number. This must be done with an explicit length of one, to avoid disturbing any of the digits of DEST. Both addresses must be written with the "+4" to pick out the proper single character. Finally, we move one byte of a constant named ZERO (not shown), which contains zeros, to the first byte of DEST. This clears to zero whatever may have been there before. 66 If the contents of SOURCE are no longer needed in their original form, the following sequence is a bit shorter. SOURCE DEST MVN SOURCE+3(l),SOURCE+4 12 34 56 7S 9S 55 55 55 55 55 ZAP DEST,SOURCE(4) 12 34 56 7S 9S 00 12 34 56 7S The Move Numeric moves the sign to the byte which will contain the sign in the eventual result. The Zero and Add picks up four bytes of SOURCE and adds them to DEST after clearing DEST to zeros. The Zero and Add has two length codes. For DEST we use the implied length of 5; for SOURCE it is necessary to give an explicit length in order to drop the last two digits. Finally, suppose that for some reason it is necessary to leave the shifted result in SOURCE, without resorting to the expedient of simply moving the sign and appending zeros at the left. MVN SOURCE+3(1),SOURCE+4 ZAP SOURCE,SOURCE(4) SOURCE 12 34 56 7S 9S 00 12 34 56 7S The sign movement is as before. In the Zero and Add, the second operand is given as SOURCE(4), which means a four-byte field the leftmost byte of which has the address SOURCE; this is just 12 34 56 7S. The first operand is simply SOURCE, with its implied length of 5, which means the whole field. I t is important to know that this type of overlap is permitted when the first operand field is at least as long as the second operand, but not when it is too short to contain all significant digits of the second operand. A little study shows that a violation of this rule would result in destroying bytes of the second operand before they have been moved. Let us now turn to a slightly more complex shift, one that involves an odd number of places. This requires the use of a special instruction designed for the purpose, the Move with Offset. The action of this instruction can be described as follows. The sign of the first operand is not disturbed; the second operand is placed to the left and adjacent to the four low-order bits (the sign bits) of the first operand. Any unused high-order digit positions in the first operand are filled with zeros. Looking at an example, take the fields described in the previous' illustration, but suppose that the shift must be three positions instead of two. SOURCE MVO DEST,sOURCE(3) DEST 12 34 56 78 9S 00 01 23 45 65 MVN DEST+4(1),SOURCE+4 12 34 56 78 9S 00 01 23 45 6S In the Move with Offset, the second operand is given as SOURCE(3), which picks up a three-byte field starting at the left, namely, the bytes containing 12 34 56. The first operand is DEST, with its implied length of 5. The digits 12 34 56 are moved to DEST with an offset of four bits, or one digit, leaving 00 01 23 45 65 in DEST; the rightmost 5 is the one that was there to begin with. A fmal Move Numeric attaches the source sign to the destination field. If the shift is required to leave the result in SOURCE, only one instruction is needed, since the Move with Offset instruction has no effect on the sign of the frrst operand, and the left end of the receiving field is filled with zeros. MVO SOURCE,SOURCE(3) SOURCE 00 01 23 45 6S The overlapping fields here cause no trouble, since again the movement is to the right of the original contents. (Actually, overlap of any type is permitted; it is the programmer's responsibility to make sure that the result is meaningful.) Shifting to the Left A shift to the left presents slightly different problems. This time suppose that we have a source field of three bytes and a destination of five. Before SOURCE 12 34 5S DEST 99 99 99 99 99 Let us take our problem, to move the number at SOURCE to DEST, with four zeros to the right at DEST, and with DEST left ready to do arithmetic. An acceptable sequence of instructions is shown below. MVC MVC MVN MVN DEST(3),SOURCE DEST+3(2),ZEROS DEST+4(l),DEST+2 DEST+2(l),ZEROS SOURCE 12 34 5S 12 34 5S 12 34 5S 12 34 5S 12 34 12 34 12 34 12 34 DEST 5S 99 5S 00 5S 00 50 00 99 00 OS OS The first Move Characters needs an explicit length on DEST; otherwise,. the length would, improperly for our problem, be interpreted from DEST as 5. The last two bytes of DEST are unaffected by the frrst Move; a second clears them. A Move Numeric transfers the sign, and a second Move Numeric clears the now extraneous sign that went with the source data on the frrst Move Characters. Another way to clear the extraneous sign is available, using the And Immediate instruction. "Anding" two quantities gives a result that has a one bit wherever both operands had l's, and a zero elsewhere. For instance, if we "And" 1100 and 1010, the result is 1000; only in the frrst bit position did both operands have ones. In the And Immediate instruction (NI) , both operands are exactly eight bits long. One of them is given by the byte specified by the address; the other is contained in the instruction itself (which is the reason for the term "immediate"). The result replaces the byte specified in storage. In the example at hand, we wish to leave the first four bits of the byte at DEST+2 just as they were; this can be done by placing ones in the corresponding positions in the part of the instruction that will be "And-ed". (This is usually called the mask.) We wish to make the right four bits of DEST+2 zero, whatever they were before; this can be done by placing zeros in that part of the mask. The mask, in short, should be 11110000, expressed in binary. To write the instruction, we can either convert this to its decimal eqUivalent 240, or, better, write it in hexadecimal, X'FO'. In other words, we can replace the last instruction with either of the following: NI NI DEST+2,240 DEST+2,X'PO' Finally, consider a shift to the left of an odd number of places. For an example, take the data of the preceding illustration, but suppose there are to be three zeros at the right instead of four. MVC MVC MVN NI MVO Before DEST(3),SOURCE DEST+3(2),ZEROS DEST+4(1),DEST+1 DEST+2,X'PO' DEST(4),DEST(3) SOURCE 12 34 5S 12 34 5S 12 34 5S 12 34 5S 12 34 5S 12 34 5S 99 12 12 12 12 01 DEST 99 99 99 34 5S 99 34 5S 00 34 5S 00 34 50 00 23 45 00 99 99 00 OS OS OS The first four instructions are just the same as in the previous example, except that the And Immediate is substituted for the Move Numeric. The final instruction now is a Move with Offset that shifts one digit position to the right. Decimal Arithmetic 67 DECIMAL DIVISION WITH SHIFTING We are now prepared to approach a realistic problem in decimal division. Suppose that in a four-byte field named SUM we have the total of the number of hours worked by all the employees in a factory, given to tenths of an hour. In NUMBER we have the number of employees included in the sum; this is a two-byte number. We are to calculate the average workweek, to tenths of an hour, rounded, and place it in a two-byte location named AVERAG. We begin the analysis of the problem knowing that the dividend (SUM) has one decimal place to start, and the divisor (NUMBER) has none. If we set up the division this way, we would get a quotient having one place; this would not pennit rounding. Evidently we shall have to allow extra places to the right. One more would be sufficient, but this would involve a shift of an odd number of places; it would be simpler for us and faster in the machine to make a shift of two places and simply ignore the extra digit. The dividend therefore should be set up like this: XX XX XX XO 0+ The X's stand for any digits. Now we turn to the rule stating that the number of bytes in the dividend is equal to the number of bytes in the divisor plus the number of bytes in the quotient. We know that we. have two bytes in the divisor as it stands. The quotient need be only three: there can be no more than two digits before the decimal point, there will be three after the decimal point, and there will be a sign. (There will be LOC OBJECT CODE AOOR1 AoDR2 STMT SOUReE STATEMENT 1 000000 000000 05BO 000002 2 AVG 3 BEGIN 4 5 6 000002 000008 oooooe 000012 000018 00001E 0200 94FO F041 FA21 0100 0201 000026 00002A 00002B 000020 00002F 000031 000000 0193648C 487e 050e oe B028 B027 B024 B024 B025 B02B B02F 0002A 00029 B029 00026 B020 00026 8026 00027 B024 00020 00031 0002B 0002F 00028 00026 7 8 9 * * * 10 11 12 13 14 17 18 19 20 21 22 23 three decimal places in the quotient because there are three in the dividend and none in the divisor.) The dividend evidently should be five bytes. As it happens - which will by no means always be the case - that is just how long it will be as the result of the shifting we decided upon. With this much background, let us now look at the program shown in Figure 5-6. We assume that it is pennissible to destroy the original contents of SUM; if this were not so, it would be a matter of one extra instruction to move the contents of SUM to a working storage location. Notice in the list of constants at the end of the program that a one-byte constant named PAD has been established just after, and therefore to the right of, SUM. Now, instead of actually moving the contents of SUM in order to accomplish a shift, we simply extend the field by one byte. This is the function of the first two instructions. We have assumed, reasonably enough, that the sum is always positive, so a plus sign is moved with the fust Move Characters, and the original sign is simply erased with the And Immediate. The Divide Decimal might seem to carry the possibility of a divide exception. We must fall back on a knowledge of the data, which is the eventual foundation of any intelligent programming. We simply observe that the average hours worked would not be as great as 100 hours - and anything less can be contained in the space provided. Rounding is accomplished by adding 5 in the proper position. We move the sign to where it is needed, and finally transfer the result to the specified location in storage. SUM PAD NUMBER AVERAG ROUND ZERO PRINT START BAlR USING NOGEN 0 11 ,0 *.11 NUMBERS BELOW SHOW CONTENTS OF SUM AFTER INSTR IS EXECUTED MVC NI OP AP MVN Mve EOJ De OS DC OS DC DC END SUM+4(1),ZERO SUM+3,X'FO' SUM(5),NUMBER SUM(3),ROUNO SUM+1(1),SUM+2 AVERAG, SUM 01 93 64 BC OC 01 93 64 BO OC 39 76 3C 21 9C 39 81 3C 21 9C 39 8C 3C 21 9C AVERAG WIll BE 39 BC Pl4'0193648' PL1 Pl2"487' Pl2 PL2' 50' PL1' 0' BEGIN Figure 5-6. Assembled program showing decimal division and "shifting". Step-by-step results to be expected during execution are included in the comments field. 68 FORMAT AND BASE CONVERSIONS It is often necessary to convert from zoned to packed format and vice versa, and'also to convert from binary to decimal and vice versa. In this section, we shall examine a program that has been constructed as an exercise in manipulating the form of data. For practice purposes, some new instructions are introduced for these maneuvers, which might be accomplished more simply in a realistic situation. We are given a fullword named REG, in binary format. Actual data for the three-byte field named PREM is read in directly from an input card on which the sign is in the high-order position, instead of the low-order. That is, a positive number was punched with a 12 zone over the leftmost digit, and a minus number was punched with an 11 zone over the leftmost digit. We are required to place the sum of REG and PREM in ANS, as a decimal number in the normal zoned format, that is, With the sign in the zone of the low-order byte. The zone bits that result in a byte in storage from a 12 zone on the card, are the zone bits required for a plus sign in the EBCDIC zoned format in storage. An 11 zone likewise is translated into the correct zone bits for a minus sign. Our problem, then, is simply to move the zone bits of the high-order byte to the zone bits of the low-order byte. In the program of Figure 5-7 we have shown at the right of the frrst half-dozen instructions the contents of the last eight bit positions of registers 5 and 6, to aid in understanding how the instructions operate on sample data consisting of the three bytes: 1101 0011 LOC OBJECT CODE 1111 0111 ADDR 1 ADDR2 000000 000000 05BO 000002 000002 000006 OOOOOA 00000 E 000012 000014 000018 OOOOlE 000022 000026 00002C 4350 5450 4360 5460 1656 4250 F212 5860 4E60 FA71 F357 B03A B032 B03C B036 0003C 00034 0003E 00038 B03C 0003E B03D B03A 0003F 0003C B042 00044 B046 00048 B046 B03D 00048 0003F B04E B046 00050 00048 000034 000034 OOOOOOFO 000038 OOOOOOOF 00003C 00003F 000044 000048 000050 000000 1111 1001 STMT 1 2 3 4 5 6 7 8 9 10 SOURCE STATEMENT CONVERT BEGIN PRINT START BALR USING NOGEN 0 11 ,0 *,11 LAST BYTE (BITS 24 TO :Ill OF REGS 5 AND 6 AFTER EXECUTION OF EACH It\lSTR IS SHOWN BELOW * * * * * IC N IC 11 12 13 14 15 16 17 18 19 20 21 24 25 26 27 28 29 30 31 32 With the card column assignments we have described" this is the EBCDIC representation of -379. The program begins with a new instruction: Insert Character (IC). This is an RX format instruction that gets one character (byte) from the specified storage location and places it in the rightmost byte position of the register named. The other bit positions of ,the register are not disturbed. We do not know what might be in them, but it will not matter, as it happens, since the following instruction clears them. This is an And to erase the numeric bits of the high-order character of our sample data. Next we perform the similar operations on the low-order byte, using register 6, except that this time we erase the zone bits. Now we have in register 6 the numeric bits of the low-order byte, and in register 5 the zone bits that are to be attached to that byte. They can be combined with an Or Register (OR) instruction. "Or-ing" two operands is a bit-by-bit operation that results in a 1 wherever either operand had aI, and zero where both had zero. The result of this instruction is to combine the two groups of bits, leaving the result in register 5. This now is the byte that we want in the low-order position, so we use a Store Character instruction (STC) to place it there. Insert Character and Store Character do not require the character to be on any sort of integral boundary. They are the only indexable instructions for which this is true. The various decimal instructions do not require boundary alignment either, of course, but they are not indexable. The two N MASKI MASK2 PREM WORK REG DOUBLE ANS OR STC PACK L CVD AP UNPK EOJ DS DC DC DS DS DS DS DS END 5, PREM 5,MASK1 6, PREM+2 6,MASK2 5,6 5, PREM+2 WORK,PREM 6,REG 6, DOUBLE DOUBLE,WORK ANS,OOUBLE REG 5 1101 0011 1101 0000 1101 0000 1101 0000 1101 1001 110 1 1001 REG 6 1111 0000 01)00 0000 1001 1001 1001 1001 OF X'OOOOOOFO' X'OOOOOOOF' ZL3 PL2 F D ZL6 BEGIN Figure 5-7. Assembled program showing various instructions for changing the format of data. Contents of registers 5 and 6 to be expected during execution are given in the comments field. Decimal Arithmetic 69 And (N) instructions, however, do require their operands to be on full word boundaries. This is the purpose of the DS OF before the DC's for the masks. At this point we have merely got the sign where it is expected to be in the zoned format of a decimal number. Now we must convert from zoned to packed format, which is the function of the PACK instruction. The second operand names a field in zoned format; the flISt names the field where the packed format should be stored. Both fields carry length codes. Here, we are able to leave the lengths implied: three bytes for PREM and two for WORK (two bytes allow space enough for three digits and sign in packed format). The PACK instruction ignores all zones except the rightmost, which is taken to carry the sign. Therefore we can leave the zone of the high-order byte as it was without disturbing the operation. 70 With the PREM amount finally in packed format, we are almost ready to do the addition - but not quite, because the REG amount is still in binary. The next instruction, accordingly, is a Load followed by a Convert to Decimal (CVD). Convert to Decimal takes the binary number in the specified register and converts it to packed format decimal in the location given, which must be aligned on a doubleword boundary. At last it is possible to do the addition, which is done in decimal. A final instruction, Unpack (UNPK) , converts back from packed to zoned, as required in the problem statement. This will leave the final answer with the sign in the zone bits of the low-order byte, which was stated to be the desired position for whatever processing might follow. If it were necessary to get the result into the same format as PREM originally was, we could of course do so. DECIMAL COMPARISON: OVERTIME PAY Logical tests and decisions are as necessary in decimal operations as elsewhere. System/360 provides a Compare Decimal instruction, and the condition code is set as a result of this and three decimal arithmetic instructions. F or an example we take the familiar calculation of gross pay, with time-and-a-half for hours over 40. We have a RATE, given in dollars and cents, and an HOURS, to tenths of an hour. We are to place the total wages in GROSS. There are several ways to approach the overtime computation. We choose here to begin by figuring the pay at the straight-time rate, on the full amount in HOURS. We then inspect the hours worked, and if it was not over 40 the job is finished. If there was overtime, we multiply the hours over 40 by the pay rate, and multiply this product by one-half to get the premium, which is then added to the previous figure. Several other ways to arrange the sequence of decisions and multiplications are obviously possible. This one probably minimizes the computation time if most employees do not work overtime; if most did work overtime, a different sequence might be a little better. The program in Figure 5-8 begins with a three-instruction sequence to set up the multiplicand in a work area, multiply, and round. The Move with Offset instruction drops one digit in the move; this is the extra digit that was rounded off. The Move with Offset instruction does not transmit the sign; we have shown GROSS as a DC to get a plus sign there from the outset. Since the pay can never properly be negative, the plus sign will simply remain there throughout the operation of the program. LaC OBJECT CODE ADDR1 ADDR2 4 000002 000008 OOOOOE 000014 OOOOlA 000020 000024 00002A 000030 000036 00003e 000042 000048 Fa31 Fe31 FA30 F132 F911 47CO F83l FB3l Fe31 FC 30 FA31 0100 FA32 000050 000052 000054 000058 00005C 000050 00005F 000000 175C 446e oooooooe 5C 050C 400C SOURCE ST AT EMENT STMT 1 2 OTPAY 3 BEGIN 000000 000000 0580 000002 B056 B056 B056 B052 B050 B04C B056 8056 B056 B056 B056 B058 B052 B050 804E a05A 8056 B05D 00058 00d58 00058 00054 00052 8050 8050 804E 805A B05B 8059 B056 00058 00058 00058 00058 00058 0005A 00054 00052 00050 0005e 00058 0005F 0004E 00052 0005F 00050 0005C 00050 00058 00058 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 25 26 27 28 29 30 31 32 The Compare Decimal (CP) instruct.ion is not greatly different in concept from Compare instructions we have seen previously. The two operands ¥e compared algebraically; the condition code is set depending on the relative sizes of the two; neither operand is changed. The mask of 12 on the Branch on Condition will cause a branch if the contents of HOURS are less than or equal to FORTY, in which case there is no overtime to compute, and we simply branch out to whatever follows. If the man did work more than 40 hours, we compute his pay on the amount over 40, then multiply by 5, which we view as having a decimal point, that is, as being one-half. This is done because we have already computed the straighttime pay on the amount over 40; now we need only to compute the extra premium. After the multiplication by 5 we round off, using a different rounding constant this time because the multiplication by 0.5 has added another decimal place. (It is necessary to check that WORK is long enough to satisfy the rule about at least as many zeros as the size of the multiplier. Assuming that no employee could make $1000 in one week, the rule is satisfied.) After a Move Numerics to move the sign, we can add the rounded amount to GROSS to get the total pay. In the Add Decimal, note the length of 3 to drop the last byte, which after rounding is extraneous. We now reach the termination of the program, the same point to which we transferred if there was no overtime. In other words, both paths would lead, in a real program, to the same continuation point. * * * * OUT lUTE HOURS GROSS WORK FIVE FIFTY FORTY PRINT NOGEN START a BALR 11,0 USING *,11 NUM8ERS 8ELOW SHOW CONTENTS OF FIRST OPERAND (WORK OR GROSS) AFTER IN STRUCT ION I S EXEC UTED ZAP MP AP MVO CP 8C ZAP SP MP MP AP MVN Ai> EOJ DC DC DC OS De DC DC END WORK,HOURS WORK, RAT E WORK,FIVE GROS S, WORK 13 ) HOURS, FORTY 12,OUT WORK,HOURS WORK,FORTY WORK, RATE WORK,FIVE WORK,FIFTY WORK+2(1),WORK+3 GROSS,WORK(3) (JC OC 5C 5C 00 00 00 00 00 78 78 07 44 05 05 80 do 00 00 00 00 00 00 00 00 08 40 40 40 08 44 6C 04 6C 05 oe 25 DC 30 OC 3C oe 20 8C PL2'l.15' PL2'44.6' PL4'0' PL4 Pll' 5' PL2'50' PL2·40.0· BEGIN Figure 5-8. Assembled program that computes a man's gross pay, including any overtime pay, in decimal arithmetic. Results expected during execution are shown in the comments field. Decimal Arithmetic 71 THE SOCIAL SECURITY PROBLEM IN DECIMAL For a little further practice in applying decimal operations, we may rewrite the Social Security calculation of Figure 3-19 in the chapter on flXed-point operations. The logic of the decimal program shown in Figure 5-9 is the same as that of the earlier one. No new instructions are introduced, so a few notes should be all that is required to explain the program. We begin by moving the old year-to-date to the new year-to-date location. The purpose is simply to get one of the two operands in the following addition where we want the result to be. Following is a Zero and Add to get the new year-to-date into 'Yorking location where we can continue the processing without disturbing the NEWYTD location. From here on, the right side of Figure 5-9 shows the contents of the WORK field for sample data as shown in the DC instructions. LaC OBJECT CODE ADDR1 ADDR2 000000 000000 05BO 000002 000002 0203 B04F B04B 00051 0004D 000008 FA32 B04F B04B 00051 0004A 5TMT In the Multiply Decimal instruction that computes the Social Security tax on the new year-to-date figure, we use a constant for the 4.4% that has been set up with an extra zero at the right. This was done to put the product in a position where a Move with Offset would not be necessary. As it has been done, after rounding and moving the sign, we can carry out all following operations on the Social Security amount on the second, third and fourth bytes of WORK. Since the implied length from the DS is 6, an explicit length must be given. The explicit length specifications in the two Move Characters (statements 17 and 19) are unnecessary, however, because NEWFICA and TAX are defmed as 3 bytes, and the assembler already has that information. Except for the points discussed here, the operations closely parallel the program in the earlier version. SOURCE STATEMENT 1 2 FICA3 3 BEGIN 4 5 6 7 8 * 9 * 10 11 12 PRINT START BALR USING MVC AP NOGEN a 11,0 *,11 NEWYTD,OLDYTD NEWYTD,EARN CONTENTS OF WORK AFTER EXECUTION OF EACH INSTR ARE SHOWN BELOW '" OOOOOE 000014 00001A 000020 000026 00002C 000030 000036 00003C 000042 F853 FC51 FA52 0100 F932 4740 0202 0202 FB22 D202 B064 B064 8064 8067 8064 B034 B065 B056 8065 8059 00004A 000040 000051 000055 000058 000058 00005E 000061 000063 000066 000000 16400C 0770000C 33800C 34320C 440C 05000C 804F 805F 8061 8069 B05C 00066 00066 00066 00069 00066 B05C B065 8053 8065 00067 00058 00067 00058 00051 00061 00063 00068 0005E 00036 0005E 00067 00055 00067 13 14 15 16 17 18 19 20 23 24 25 26 27 28 29 30 31 32 33 UNDER EARN OloYTD NEWYTD OloFICA NEWFICA TAX MAX C44 HALF WORK ZAP MP AP MVN CP BC MVC MVC SP MVC EOJ DC DC OS DC OS DS DC DC DC DS END WORK,NEWYTO WORK,C44 WORK,HALF WORK+3(1),WORK+5 WORK(4),MAX 4,UNoER WORK+1(3).MAX NEWFICA(3),WORK+1 WORK+1(3),OlDFICA TAX(3),WORK+1 00 00 00 00 00 00 00 00 00 00 00 34 34 34 34 34 34 34 00 00 07 60 60 60 60 60 32 32 52 52 86 16 21 2C 2C 2C OC OC OC OC 40 00 00 00 00 00 00 00 00 00 OC OC OC OC OC OC OC OC oc OC PL3'16400' Pl4'170000' PL4 Pl3'33800' Pl3 PL3 Pl3'34320' Pl2'440' Pl3'5000' PL6 BEGIN Figure 5-9. Assembled program to calculate Social Security tax in decimal arithmetic. Results expected during execution are shown in the comments field. 72 THE "INDIAN" PROBLEM A certain programming exercise has been done by so many generations of IBM students that it is a classic. We present it here, worked out with the calculation in decimal and the counting in binary. The Indians sold Manhattan Island in 1627 for $24. If the Indians had banked their $24 in 1627, what would their bank balance be in 1965 at a 3% interest rate compounded annually? To make the problem a little more interesting, let us assume that the principal, $24, the interest rate factor, 1.03, and the number of years, 338, are all initially in zoned format. The program of Figure 5-10 accordingly begins with three PACK instructions to get from zoned to packed format. The general scheme of the program will be to multiply the principal by 1.03 as many times as there are years. In other words, we shall go around a loop repeatedly, each time performing a multiplication and subtracting 1 from a count. When the count has been reduced to zero, the computation of the balance is completed. This counting down from 338 to zero could, of course, be done in decimal, testing for zero with a Compare Decimal instruction. It is better programming practice, however, to remove time-consuming operations from the repeated part of the loop wherever possible. Doing the repeated combination of an Add Decimal, a Compare Decimal, and a Branch on Condition is much more time-consuming than another approach that is available to us. This other way is to convert the years to binary once, before entering the loop, then use a Branch on Count (BCT) in the loop, a single instruction that will subtract 1, test, and conditionally branch. The fourth instruction of the program is therefore a Convert to Binary (CVB) instruction, which in our program LOC 000000 000000 000002 000002 000008 OOOOOE 000014 000018 00001E 000024 00002A 000030 000036 00003A 000042 000046 000049 00004C 000053 000058 000060 000062 000068 000000 OBJ ECT CODE ADDR1 ADDR2 05BO F263 F212 F272 4F40 FC61 FA61 0100 0205 F865 4640 F386 B04A B051 B056 B056 B04A B04A B04F B060 B04A B016 B066 F2F4FOCO FIFOC3 F3F3C8 OSOC 8040 0004C 00042 B044 00053 00046 B047 00058 00049 00058 B051 0004C 00053 BOSE 0004C 00060 B050 00051 00052 B04A 00062 0004C B060 0004C 00062 00018 B04A 00068 0004C STMT 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 19 20 21 22 23 24 25 26 27 28 takes the doubleword at YEARSP and converts to a binary number in register 4. The Convert to Binary instruction requires an aligned doubleword operand, which is why the DS for YEARSP was set up as it was instead of with a CL8. The repeated part of the loop starts with a Multiply Decimal that should by now be moderately familiar. PRINCP was set up to be long enough to hold the size of number that previous runnings of the program have shown will be necessary. The programmer facing this problem completely fresh would have to make some preliminary calculations as to the possible size. Now comes a familiar sequence of decimal instructions to round, move the sign, and shift right two digits (one byte). One might be tempted to replace the Move Characters and Zero and Add instructions with a single one of the sort: MVC PRINCP+l(6),PRINCP thinking that a right-to-Ieft operation would permit this sort of overlap. A check of the Principles of Operation manual, however, discloses that Move Characters works from left to right! The instruction suggested would therefore propagate the leftmost character through the entire field! This can be quite useful on occasion, and is permitted, but it is hardly what we want here. Overlapping fields must be treated with caution. The Branch on Count subtracts 1 from register 4; if the result is not zero, a branch occurs. If the result is zero, the next instruction in sequence is taken. The loop will be carried out 338 times, as required. A fmal Unpack instruction puts the result into a location named BALANCE in zoned format. The answer obtained by execution of our program is $523,998.22. Carrying the calculations out to more decimal places would of course give a more precise result. SOURCE STATEMENT I NOlAN BEGIN LOOP PRINCZ INTZ YEARZ PRINCP INTP YEAR SP ROUND TEMP BALANCE PRINT START BALR USING PACK PACK PACK CVB MP AP MVN MVC ZAP BCT UNPK EOJ DC DC DC DS OS OS [)C OS OS END NOGEf',1 0 11,0 *,11 PRINCP,PRINCZ I NTP, I NT Z YEA RS P , YEAR Z 4,YEARSP PRINCP,INTP PRINCP,ROUNO PRINCP+5111,PRINCP+6 TEMP,PRINCP PRINCP,TEMP 4,LOOP BAlANCE,PRINCP MOVE SIGN DROP LOW-ORDER BYTE SUBTRACT 1 FROM REG 4 ZL4'24.00' ZL3'l.03' ZL3'338' PL7 Pl2 D PL2'50' PL6 ZL9 t3EGIN Figure 5-10. Assembled program to compute compound interest (the "Indian" problem), with counting in binary and calculations in decimal arithmetic Decimal Arithmetic 73 QUESTIONS AND EXERCISES la. Write the assembler instruction to define a packed decimal constant of 3 to be named CON3 and to occupy 5 bytes of stotage. b. Show how this constant appears on the assembly listing. 2. A length code in an instruction is called implied jf it is supplied by the on the basis of _ _ _ _ _ _ _ _ . An explicit length code is supplied by the _ _ _ _ _~__ 3. An explicit length code is (equal to, one more than, one less than) the actual number of bytes to be dealt with. 4. The length code in the object instruction is (equal to, one more than, .one less than) the actual number of bytes to be dealt with. Sa. In an MP instruction, the first operand specifies the location of a storage area containing _ _ _--..,_ _ __ b. Where is the product at the end of the multiplication? 6. If there were two successive DC statementS of: PRINC DC PL4'2489' INT DC PL2'103' and PRINC were assigned a location of 158: a. Byte by byte, what would be in the storage locations assigned to these constants? b. To what storage location would the operand INT-2 refer? 7. A DP instruction specifies in its first operand the location of the , and in its second operand the location of the . Where will the quotient and remainder be after the completion of a DP instruction? 8. Assume two fields: SOURCE cN_O_--I~!( End of Jab) Convert Zones of COMB to Binary Store Binary Code in CODES Make Sign of COMB a Plus Pack COMB in NUMERC 00000000000000000000000000000000000000000000 , 1 J • 5 6 7 8 9 10 11 11 'J 14 15 >6 17 18 19 10 I' II 2J 1<25 16 21 18 19 JO J' J? J3 3< J5 3€J7 38 39 40 '''1 '3" 45 1111111111111111111111111111111111 , 11111' , 1 , , 222222222222122222222222222222222222222222222 333333333333313333333333333333333333333333333 444 4 4 4 4 44 4 4 4 4 414 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 Figure 6-5. A flowchart of the steps required to solve the problem 55555555555555515555555555555555555555555555 6666666666666666166b6666666666666666666666666 777777777777777771777777777777777777777777777 88888888888888388818888888888888888888888888 99999999999999999399999999999999999999999999 1 2 3 456 78 91011 12 13141516 17 18192011112324251617 2829JO JI J?3J34 35J6373839104l424344 taM 5081 Figure 64. Alphabetic input for COMB that can be viewed as two numbers: 12345678 and binary 11001010 82 from LOOP to OK pick up the digits in turn, strip off the zone bits with a suitable And, and compare the numeric portions with 10. The instruction after OK picks up the rightmost byte of NUMBER; this should have either a plus sign or a minus sign. Another And, but with a different mask, strips off the numeric portion and the rightmost bit of the sign; we do not care whether the sign is plus or minus, a distinction which is made in the rightmost bit of the sign. A comparison then establishes whether the left three bits of the sign are 110, which they should be for an EBCDIC sign. At OK2 we are ready to go to work on the combined digits and zones at COMB. In preparation for what follows, we clear registers 8, 9, and 10. At LOOP2 there is a shiftbefore anything has been placed in the register shifted. The idea is that we want to shift the contents of this register seven times for eight bits. One way to accomplish this is to place the shift instruction so that it has no net effect the frrst time around. The Insert Character is indexed with register 10, which initially contains zero. We will therefore pick up the digits from·left to right this time. For each digit we use an And to drop the numeric bits, then test against constants so as to determine whether the sign is plus or minus. If it is neither, we get out; there should be one or the other. If the sign is plus, we branch to YES, where a 1 is added into register 9 the one that we shifted at the beginning of the loop. LOC OBJ ECT CODE ADDRI ADDR2 000000 000000 000002 000002 000006 000008 OOOOOC 000010 000014 4lAO IB99 439A 5490 5990 4140 0001 00001 B015 B08E BOA2 B018 00011 00090 000A4 0001A 00001A 00001E 000022 000026 00002A 46AO 4380 5480 5980 4180 B006 B01C B092 B09A B02E 00008 0001E 00094 0009C 00030 000030 000032 000034 000036 00003A 00003E 000042 000046 00004A 00004E 1B88 1898 18A8 8B90 438A 5480 5980 4180 5980 4180 0001 8070 B096 B09A B052 B09E B056 000054 000058 00005C 000060 000064 000068 00006C 000010 5A90 5AAO 59AO 4170 4290 940F 96CO F241 BOA6 OOOAB BOA6 OOOAS BOAA OOOAC B034 00036 00081 BOB5 B084 00086 B084 00086 6086 B070 00088 0001F 05BO STMT SOURCE ST ATEMENT 1 2 FORMAT 3 BEGIN 4 5 6 1 LOOP 8 9 10 11 000078 00001F 000081 000088 000090 000090 000094· 000098 00009C OOOOAO 0000A4 0000A8 OOOOAC 000000 OOOOOOOF OOOOOOEO OOOOOOFO OOOOOOCO 00000000 OOOOOOOA 00000001 00000008 00001 0001F 0009B 0009C 00054 OOOAO 00058 14 OK 15 16 11 18 19 22 23 24 25 26 27 28 29 30 31 32 35 36 OK2 LOOP2 YES NO 37 38 39 40 41 42 43 46 41 48 49 50 51 52 53 54 55 56 51 58 59 Whether the sign is plus or minus, we now reach NO, where we add 1 to the index register and branch back to LOOP2 if the contents are less than eight. Now, when we branch back, we again shift the contents of register 9 one position to the left. This means that each time we again reach the beginning of this loop, whatever has been assembled in register 9 so far is srufted left one place, thereby making room for another bit at the rightmost position of the register. Thus, when we finally get out of the loop and arrive at the Store Character, the 1ast byte of register 9 will contain a 1 in positions corresponding to plus signs in COMB, and zeros in positions corresponding to minus signs. The byte stored at CODES is just what the problem statement required. An And Immediate now erases the zone positions of the rightmost byte of COMB, and an Or Immediate places a plus sign there. The Pack instruction does not check zones, except in the rightmost byte, so we can proceed to it immediately, with no concern for the other zone positions. PRINT ST ART BALR USING LA SR IC N C BL EOJ BCT IC N C BE EOJ SR LR LR SLA IC N C BE C BE EOJ A A C BNE STC NI OI NUMBER COMB CODES NUMERC MASK1 MASK2 MASK3 PLUS MINUS TEN ONE TEST PACK EOJ OS OS OS OS OS DC DC DC DC DC DC DC DC END NOGEN a 11.0 *,11 10.7 9,9 9 ,NUMBER-l( 10) 9,MASKl 9,TEN OK 10,LOOP B,NUMBER+6 8,MASK2 8,PlUS OK2 8,8 9,8 10,8 9,1 8,COMB(10) 8,MASK3 8,PLUS YES 8,MINUS NO 9,ONE 10,ONE 10, TEST LOOP2 9,CODES COMB+1, X' OF' COMB+1,X'CO' NUMERC,COMB REG 10 IS USED AS AN INDEX CL EAR REG 9 INSERT 1 DIGIT IN REG 9--INDEXED STRIP OFF SIGN IS NUMBER LESS THAN 10 BRANCH AROUND EOJ IF OK NOT A DIGIT REDUCE CONTENTS OF REG 10 BY 1 & BRANCH IF HERE, ALL OIGITS CHECKED OK STRIP OFF LAST DIGIT & FINAL SIGN BIT COMPARE 3 REMAINING BITS WITH SIGN BRANCH IF OK NOT AN EBCDIC SIGN CLEAR REG 8 CLEAR REG 9 BY LOADING FROM REG 8 CLEAR REG 10 BY LOADING FROM REG 8 SHIFT REG 9 LEFT 1 BIT INSERT 1 BYTE IN REG 8--INDEXED STRIP OFF DIGIT PART COMPARE WITH CODING FOR PLUS BRANCH IF PlUS COMPARE WITH CODING FOR MINUS BRANCH IF MINUS NEITHER PLUS NOR MINUS IF PLUS ADD 1 TO CONTENTS OF REG 9 ADD 1 TO REG 10 FOR LOOP TEST COMPARE BRANCH BACK IF NOT FINISHED STORE LAST 8YTE OF REG 9 STRIP OFF OLD ZONE ATTACH ZONED PLUS SIGN CONVERT TO PACKED FORMAT PROGRAM TERMINATION CL1 CL8 Cll CL5 OF X'OOOOOOOF' X'OOOOOOEO' X'OOOOOOFO' X'OOOOOOCO' X'OOOOOODO' F'lO' F'l' F'8' BEGIN Figure 6-6. A program that checks a decimal field at NUMBER for validity and converts a composite field at COMB into separate binary and packed decimal quantities. The flowchart in Figure 6-5 was used as a guide for the programming Logical Operations on Characters and Bits 83 QUESTIONS AND EXERCISES 1. The byte at location KEY in main storage contains four program switches in bit positions 4-7. Each of these bit positions may be 1 (on) or 0 (off). Write an instruction that will reverse the setting of the program switches and leave bits 0-3 unchanged. 2. In the following byte, located at ADDR in main storage, a 1 in a particular position shows the presence of a characteristic and a zero its absence. Write instructions that will branch to ANIMAL for owners of dogs or cats or both, and proceed sequentially for all others. XXOOOOOO (not USed)~ ~L-PigeOn fancier . canary owner . . cat owner tropIcal fish raIser dog owner parrot owner ----=r I 3. Using the preceding, write instructions to branch to LIST2 for owners of fish but not canaries, or canaries but not fish. 4. Suppose location SUM contains 05432+ in packed decimal format, and suppose that general register 2 initially contains zero. Show what register 2 will contain (in hexadecimal or binary) after: a.IC 2,SUM b. IC 2,SUM+2 c. IC 2,SUM+ 1 5. At most, the TM (Test Under Mask) instruction can test _ _ _ _ _ _ _ _ bit(s) or _ _ _ _ _ _ _ __ byte(s) with one instruction. 6. At most, the CLC (Compare Logical Character) instruction can compare bites) or _ _ _ _ _ _ _ _ _ byte(s) with one instruction. 7. The CLC instruction will successfully compare two operands in only one of the following forms. Which is it? a. Packed decimal numbers b. Alphameric characters c. Zoned decimal numbers 8. In the CLC instruction, comparison proceeds from left to right, byte by byte, but ceases before the end of the operand is reached, as soon as one of the following is encountered ( select one): a. The EBCDIC sign code b. A special character c. An inequality d. An improper zone code 9. Neglecting leading zeros, give in decimal the contents of general register 5 after execution of each of the following: a. LA 5,5 84 b. LA LA LA c. d. 5,2 5,3(0,1) 5,FIELD FIELD DS F 10. Write instructions to determine whether or not the byte at main storage location FIELD contains a 5 (0000 0101 in binary). 11. In the following hypothetical program, the rows of dots represent straightforward instruction sequences of any reasonable length, whose nature need not concern us. LA 2,10 LOOP INST BC 01 O,ADDR INST+ 1,X'FO' ADDR . . BCT 2, LOOP Which part of the BC instruction is addressed by the relative address INST+ 1? 12. Bearing in mind that in question 11 the hexadecimal immediate data X'FO' is simply a convenient way of specifying binary 11110000 (or decimal 240), can you say that the 01 (Or Immediate) instruction: a. '~1:!! be executed once and only once? b. Causes certain instructions within the BCT loop to be skipped on all but the fust execution of the loop? c. Alters the bit structure of a mask field? d. Does all of the above? 13. Assume that the overall loop of the following sequence will be executed a number of times. What will be the effect of the XI (Exclusive Or) instruction? LOOP XI INST+I,X'PO' INST BC O,ADDR ADDR BCT 5,LOOP 14. Suppose that general register 5 contains a number of which only the high-order (leftmost) byte is of interest. Write a logical instruction to zero the three low-order bytes, together with any instructions necessary to defme masks, load other registers, etc., as required. Chapter 7: Edit, Translate, and Execute Instructions This chapter will be devoted to several highly specialized and useful instructions that are part of the assembler language. They call into play some new concepts, and their functions and machine actions are different in many ways from any of the instructions we have encountered so far. Since they may be regarded as irregular verbs, so to speak, of System/ 360 Assembler Language, we will subject each of them to careful scrutiny. The Execute (EX) instruction is a special type of branching instruction that causes one other instruction in main storage to be executed out of sequence without actually branching to its location. Since Execute can also modify the remote instruction before it is executed, it offers considerable economy in the number of instructions needed to achieve certain results. The other instructions covered in this chapter are Edit, Edit and Mark, Translate, and Translate and Test. These are part of the System/360 logical operations discussed in the preceding chapter. We begin with a detailed demonstration of how the Edit, and the almost identical Edit and Mark, instructions work. These two instructions are invaluable aids to any programmer concerned with decimal arithmetic. Translate can be used for code conversion or to provide a control function. The description of the Translate instruction is necessary for an understanding of the Translate and Test (TRT), which follows it. Detailed program examples are included, with special emphasis on the use of the powerful combination of TRT and EX in various applications. The programmer will find many additional applications for the techniques demonstrated in this chapter. Edit, Translate, and Execute Instructions 85 THE EDIT INSTRUCTION The Edit instruction is one of the most powerful in the repertoire of the System/360. It is used in the preparation of printed reports to give them a high degree of legibility and therefore greater usefulness. It makes it possible, as we shall see, to suppress nonsignificant zeros, insert commas and decimal points, insert minus signs or credit symbols, and specify where suppression of leading zeros should stop for small numbers. All of these actions are done by the machine in one left-to-right pass. The condition code can be used to blank all-zero fields with two simple instructions. A variation of the instruction, Edit and Mark, makes possible the easy insertion of floating currency symbols. We shall study the application and results of this highly flexible instruction by applying it to successively more complex situations. We begin with a simple requirement to suppress leading zeros; no punctuation is to be inserted. We have a field to be edited, called DATA. It is four bytes long, and the decimal data is in packed format. The packed format for data to be edited is a requirement of the Edit (ED) instruction, which is a decimal instruction. As we saw in an earlier chapter, data used in decimal arithmetic operations is always in packed format. If we happened to have source data in some other form, we would have to pack it before editing. The data to be edited is named as the second operand of the Edit. The first operand must name a field containing a "pattern" of characters that controls the editmg; after execution of the instruction, the location specified by the first operand contains the edited result. (The original pattern is destroyed by the editing process.) The pattern is in zoned fonnat, as is the result; the Edit instruction causes the conversion of the data to be edited from packed to zoned format, since zoned format is what is needed for most output operations_ We said that in our example the data field to be edited was four bytes long, that is, seven decimal digits and sign, which we shall assume to be plus. The pattern must accordingly be at least eight bytes long: seven for the digits and one at the left to designate the "fill character". The fill character is of our choosing, but is usually a blank. This is the character that is substituted for nonsignificant zeros. The leftmost character of the pattern in our case will be the character blank (hexadecimal 40 in System/360 EBCDIC coding). The other seven characters will contain hexadecimal 20, a control character called a digit selector, which is used to indicate to the Edit instruction that a digit from the source data may go into the corresponding position. Let us see how all this works out in our example. Suppose we set up an eight-byte working storage field named WORK into which we move the pattern (located in an area called PATTRN). Then we will perform our edit using WORK and DATA as the two operands. The two 86 instructions necessary to do the job are: MVC ED WORK,PATTRN WORK,DATA After execution of the two instructions, WORK contains our edited result. PATTRN still contains the original pattern and can transmit that original pattern to WORK for the editing of any new vallie in DATA. At PATTRN there should be the following characters, written here in hexadecimal: 40 20 20 20 20 20 20 20 or as they would appear in an actual program, defined as a hexadecimal constant: PATTRN DC X' 4020202020202020' In EBCDIC, 40 is the hexadecimal code for a blank and 20 for the digit selector control character. Hex is used to specify control characters, since there are no written or printed symbols to represent them. In this section, all patterns are shown exactly as they would appear in constants, except of course that the spaces would be closed up. In our example, suppose that at DATA there is 00 01 00 0+ The edited result would be b b b b 1 000 where the b's stand for blanks. All zeros to the· left of the frrst nonzero digit have been replaced by blanks; but zeros to the right of the frrst nonzero digit have been moved to WORK without change. This is the desired action. Figure 7-1 shows a series of values for DATA and the resultant edited results in WORK, using the pattern stated. Note that the h~gh-order position of WORK contains the fill character, a blank. The values of DATA are packed decimal; the edited results are changed during execution of the Edit instruction to zoned decimal fonnat. BDDDDDDD 40 20 20 20 20 20 20 20 1234567 0120406 0012345 0001000 0000123 0000012 0000001 0000000 1234567 120406 12345 1000 123 12 1 Figure 7-1. Results of Editing source data in left-hand column. Two lines at top give editing pattern in symbolic form (B represents a blank, D a digit selector) and in hexadecimal coding. The fill character that we supply as the leftmost character of the pattern may be any character that we wish. It is fairly common practice to print dollar amounts with asterisks to the left of the first significant digit in order to protect against fraudulent alteration. This is usually called asterisk protection. To do this, we need only change the leftmost character of the pattern of the previous example. The hexadecimal code for an asterisk is 5C; hence the new pattern is 5C 20 20 20 20 20 20 20 Figure 7-2 shows the edited results for the same DATA values that we used in Figure 7-1. *nnnnnnn 5C 20 20 20 20 20 20 20 1234567 0120406 0012345 0001000 0000123 0000012 0000001 0000000 *1234567 **120406 ***12345 ****1000 *****123 ******12 *******1 ******** Figure 7-2. Editing results with an asterisk as the fill character Any characters in the pattern other than the digit selector and two other control characters that we shall study later are called message characters. They are not replaced by digits from the data. Instead, they are either replaced by the fill character (if a significant digit has not been encountered yet), or left as they are (if a significant digit has been found). Suppose, for instance, that we set up a PATTRN as follows: The message characters inserted are, naturally, not limited to commas. A frequent application is to insert a decimal point as well as commas. Let us assume that the data values we have been using are now to be interpreted as dollars-and-cents amounts. We need to arrange fOT a comma to set off the thousands qf dollars, and a decimal point to designate cents. The characters in PATTRN,"where 6B is a comma and 4B is a decimal point, should be as follows: 40 20 20 6B 20 29 20 4B 20 20 The e~ited results this time are in Figure 74. see here something that wol.dd normally not be desired: amounts under one dollar have been edited with the d~cima1 point suppressed. We. would ordinarily prefer to have the decimal point. This can be done by placing a significance starter in the pattern. This control character, which has the hexadecimal code 21, is either replaced by a digit from the data or replaced by the fill character, just as a digit selector is. The difference is that the operation proceeds as though a significant digit had been foupd in the positiop occupied by the significance starter. ~n other words, succeeding char~cters to the right will not be suppressed. (An exception. to. this generalization may occur when we want to print sign indicators, a subject that will be explored later.), . We BDD,nDD.DD 40 20 20'~B 20 20 20 4B 20 20 1234567 0120406 0012345 0001000 0000123 0000012 ooooooi 12,345.67 i,204.06 123.45 10.00 1.23 12 1 0000000 40 20 6B 20 20 20 6B 20 20 20 The 6B is hexadecimal coding for a comma, and it is a message character. The edited result will contain commas in the two positions shown, unless they are to the left of the frrst nonzero digit, in which case they are suppressed. Figure 7-3 shows the results for the same data values. BD,nnn,DnD 40 20 6B 20 20 20 6B 20 20 20 1234567 0120406 0012345 00Q1000 0000123 0000012 0000001 0000000 1,234,567 120,406 12,345 1,000 123 12 1 Figure 7-3. Editing results with blank fill and the insertion of commas Figure 7-4. Editing results with blank rill and the insertion of comma and decimal point The pattern for this action, assuming we still want the comma and decimal point as before, should be 40 20 20 6B 20 20 21 4B 20 20 The effeGt is this: if nothing but zeros has b~en found by the time we reach the Significance starter (hex 21) in a left-to-right scan, the significance starter will turn on the significance indicator. This indicator will cause succeeding characters to be treateq as though a nonzero digit had been found. The result is that the decimal point will always be left in the result, as will zeros to the right of the decim!ll point. The edited results this tim~ are shown in Figure 7.;5. One useful point to remember is that the total number of digit selectors plus significance starters in the pattern must equal the number of digits in the field to be edited. Note that this is the case in all our examples. Edit, Translate, and Execute Instructions 87 BDD,DDS.DD 40 20 20 6B 20 20 21 4B 20 20 1234567 0120406 0012345 0001000 0000123 0000012 0000001 0000000 12,345.67 1,204.06 123.45 10.00 1.23 .12 .01 .00 Figure 7-5. Editing results with blank rill, comma and decimal point insertion, and significance starter. In the symbolic pattern, S stands for significance starter. We can begin to get a little idea of how the machine does its work on this instruction by noting that the significance indicator is initially in the off state before the scan begins. Scanning proceeds source digit by source digit. The significance indicator stays off until a nonzero data digit is found, or until the significance starter is encountered; either event causes the indicator to be turned on. Source digits 1-9 always replace a digit selector or significance starter, but whether a zero source digit will do so depends upon the state of the significance indicator. If the significance indicator is on, then we know that either a significant digit was found at some previous character position, or a significance starter has been encountered; in either case, a zero from the source data is inserted. If the significance indicator is off, we know that no significant digit has been found so far during the scan; therefore, the fill character appears in the result, rather than a zero from the data. It may be useful to refer to Table 7-1, which includes a summary of how the state of the significance indicator affects the editing operation under all conditions of consequence that you may encounter. The table also shows how the significance indicator itself is affected. In the table, the four columns at the left list all the significant combinations of the four conditions that can be encountered in the execution of the editing operation. The two columns at the right under Results show the action taken for each case - that is, the type of character placed in the result field and the new setting of the significance indicator. Use of the field separator will be discussed in a later paragraph. We have so far ignored the sign portion of the source data, which (in the packed decimal format required for the Edit instruction) is in the four low-order bits of the rightmost byte. These bits are examined each time the Edit instruction is executed. If the sign is plus, the significance indicator will then be turned off, as shown in the table; if the sign is minus, the significance indicator will be left on. The information will not appear in the result, however, if there are no further pattern characters to be scanned. As a matter of fact, if any of the source fields in the examples above had been negative, the results shown would have been exactly the same. Suppose, however, that pattern characters remain after the sign position has been examined. The action of the significance indicator in controlling the instruction continues just as before, although the setting of the significance indicator was accomplished by a different condition. There are, of course, no more digits to move. Hence we will not want to place digit selectors in the pattern in this position, Table 7-1. Summary of Editing Functions CONDITIONS Pattern Character Digit selector Significance starter Field separator Message character RESULTS Previous State of Significance Indicator Source Digit Low-Order Source Digit is a Plus Sign off off off on on off off off off on on * off on 0 1-9 1-9 0-9 0-9 0 0 1-9 1-9 0-9 0-9 ** ** ** * no yes no yes no yes no yes no yes ** ** ** *No effect on result character and new state of significance indicator. **Not applicable because source digit is not examined. 88 Result Character fill character source digit source digit source digit source digit fIll character fill character source digit source digit source digit source digit fill character fill character message character State of Significance Indicator at End of Digit Examination off on off on off on off on off on off off off on but, rather, sign indicators, such as a minus sign or CR for credit. The action taken with the characters in the pattern is the same now as it was before: they remain unchanged if the significance indicator is on, but are replaced by the fill character if the significance indicator is off. Let us set up a suitable pattern for the example data. Let us print the letters CR for negative numbers, with one blank between the rightmost digit and the C. In hexadecimal, CR is C3 D9, so the pattern becomes *DD,DDS.DDBCR 5C 20 20 6B 20 20 21 4B 20 20 40 C3 D9 1234567 0120406 0012345 0001000 0000123 0000012 0000001 0000000 -0098765 -0000000 *12,345.67*** **1,204.06*** ****123.45*** *****10.00*** ******1.23*** *******.12*** *******.01*** *******.00*** ****987.65 CR *******.00 CR 40 20 20 6B 20 20 21 4B 20 20 40 C3 D9 Figure 7-6 shows the results for sample data values as before, together with two negative values. BDD,DDS.DDBCR 40 20 20 6B 20 20 21 4B 20 20 40 C3 D9 1234567 0120406 0012345 0001000 0000123 0000012 0000001 0000000 -0098765 -0000000 12,345.67 1,204.06 123.45 10.00 1.23 • 12 .01 .00 987.65 CR .00 CR Figure 7-6. Editing results with blank fIll, comma and decimal point insertion, significance starter, and CR symbol for negative numbers If we use an asterisk now as the fill character, positive quantities will have three asterisks following the cents, as shown in Figure 7-7. This mayor may not be desired. There are other ways to handle the signs, as we shall see next. We have seen above that an amount of zero prints in the general form .00 when a significance starter is used. It may in some cases be desirable to make such an amount print as all blanks or all asterisks. This is very easily done by making use of the way the condition code is set by execution of the Edit instruction: Code o 1 2 Instruction Resul t field is zero Result field is less than zero Result field is greater than zero This means that after completion of the Edit we can make a simple Branch on Condition test of the condition code and move blanks or asterisks to the result field if it is zero. The movement is particularly simple because the fill character is still there in the field and an overlapped Move Characters instruction can be used as follows: BC MVC SKIP 6,sKIP WORK+l(12),WORK Figure 7-7. Same with asterisk rill The explicit length of 12 is based on the most recent pattern, which has a total of 13 characters. The MVC, as written, picks up the leftmost character and moves it to the leftmost-plus-one position. It then picks up the leftmostplus-one character and moves it to the leftmost-plus-two position, etc., effect propagating the leftmost character through the field. This is precisely what we want if the fill character is the one to be substituted . Figure 7-8 shows our familiar data values with zero fields blanked, and Figure 7-9 shows them with zero fields filled with asterisks. Only the fill character differs in the two programs that would produce the results shown in Figures 7-8 and 7-9; the Edit, the Branch on Condition, and the Move Characters are the same in both cases. BDD ,DDS .DDBCR 40 20 20 6B 20 20 21 4B 20 20 40 C3 D9 1234567 0120406 0012345 0001000 0000123 0000012 0000001 0000000 -0098765 -0000000 12,345.67 1,204.06 123.45 10.00 1.23 .12 .01 987.65 CR Figure 7-8. Editing results showing the blanking of zero fields by the use of two additional instructions *DD,DDS.DDBCR 5C 20 20 6B 20 20 21 4B 20 20 40 C3 D9 1234567 0120406 0012345 0001000 0000123 0000012 0000001 0000000 -0098765 -0000000 *12,345.67*** **1,204.06*** ****123.45*** *****10.00*** ******1.23*** *******.12*** *******.01*** ************* ****987.65 CR ************* Figure 7-9. Same with zero fields filled with asterisks Edit, Translate, and Execute Instructions 89 The condition code can also be used to distinguish between positive and negative numbers when it is necessary to present the sign in some manner that is not possible by using the automatic features of the Edit. We might, for instance, wish to test the condition code and use the results of the test to place a plus sign or minus sign to the left of the edited result. The Edit instruction can be used to edit several fields with one instruction. Doing so uses a fmal control character, the field separator (hexadecimal 22). This character is replaced in the pattern by the fill character, and causes the significance indicator to be set to the off state. The characters following, both in the pattern and in the source data, are handled as described for a single field. In other words, it is possible to set up a pattern to edit a whole series of quantities, even an entire line, with one instruction. The packed source fields must, of course, be contiguous in storage, but this is often no inconvenience. One limitation is that the condition code, upon completion of such an instruction, gives information only about the last field encountered after a field separator. Let us consider the example shown in Figure 7-10. Suppose that at DATA we pave a sequence of three fields. The leftmost of the fields' has four bytes, th~: hext has three, and the rightmost has five bytes. The first is to be printed with commas separating groups of three digits. The values are always positiVe and, therefore, no sign control is desired. Zero values will be blank since we shall 'not use a significance starter. The second field is to be printed with three digits to the right of the decimal point, with a significance starter to force amounts less than 1 to be' prInted with a zero before the decimal point. Positive quantities are to be printed without a sign, and negative quantities are to be printed with a minus sign immediately to the' right of the number. The third number is a dollar amount that could be as great as $9,999,999.99. Commas and decimal point are needed just as shown. Amounts less than $1 are to be 1234567C12345C123456789C 1,234,567 12.345 printed with the decimal point as the leftmost character. Zero amounts are to be blanked. Signs are not to be printed. There is to be at least one blank between the frrst and second edited result, and at least three between the second and third. Let us write out the necessary pattern in shorthand form, with b standing for a blank, d for digit selector, f for field separator, s for significance starter, and other characters for themselves: bd,ddd,dddfsd.ddd-fbbd,ddd,dds.dd The required blank between the Irrst and second edited result will be placed there by the replacement of the field separator with the fill character. The significance starter in the part of the pattern corresponding to the second field will give the required handling of quantities less than 1. The extra, two blanks between the second and third results are provided by the blanks in the part of the pattern corresponding to the third data item. (These are not treated as new fill 'characters; only the leftmost character in the entire pattern is so regarded.) Notice that the total of di~t selectors plus significance starters is equal to the number of digits in each field to be edited. Instructions to do the required actions are as follows: MVe ED Be Mve SKIP The choice of addresses in the final MVe that blanks a zero field is somewhat arbitrary. We reason that if the entire field is zero, the frrst three positions of it are surely bhink by now; hence a three-character MVe from there to the last three positions of the field will be correct. Figure 7-10 ',shows' initial source data values and edited results. The packe<.l source fields must be adjacent as shown; we address the leftinost character. ' 1,234,567.89 0123456C01234C012345678C 123,456 1.234 123,456.78 0010009COO123C001000000C 10,009 0.123 10,000.00 0004502C98007DOOOOO1210C 4,502 98.007- 0000800COOO12COOOOOOO06C 800 0000001COOO01DOOOOOOO01C 1 OOOOOOOCOOOOOCOOOOOOOOOC WORK,PATTRN WORK,DATA 6 ,SKIP WORK+30(3),WORK+18 12.10 0.012 .06 0.001- .01 0.000 Figure 7-10. Examples of multiple edits. On each line the first field is a combination of three items; all three were edited with one Edit, giving the three results shown to the right. The editing pattern is shown in the text. 90 THE EDIT AND MARK INSTRUCTION The Edit and Mark instruction (EDMK) makes possible the insertion of floating currency symbols. By this we mean the placement in the edited result of a dollar sign (or pound sterling symbol) in the character position immediately to the left of the fust significant digit. This serves as protection against alteration, since it leaves no blank spaces. It is a somewhat more attractive way to provide protection than the asterisk fill. The operation of the instruction is precisely the same as the Edit instruction, with one additional action. The execution of the Edit and Mark places in register 1 the address of the fust significant digit. The currency symbol is need~d one position to the left of the fust significant digit. Consequently, we subtract one from the contents of register 1 after the execution of the Edit and Mark and place a dollar sign in that position. There is one complication: if significance is forced by a significance starter in the pattern, nothing is done with register 1. Before going into the Edit and Mark, therefore, we place in register 1 the address of the significance starter plus one. Then, if nothing happens to register 1, we still get the dollar sign in the desired position by using the procedure described above. Let us suppose that we are again working with a four-byte source data field, which we are to edit with a comma, a decimal point, and CR for negative numbers. Accordingly, the pattern (in shorthand form) should be bdd,dds.ddbCR The significance starter here is six positions to the right of the leftmost character of the pattern. The complete program to give the required editing and the floating dollar sign is as follows: MVC LA EDMK BCTR MVI The Load Address instruction as written places in register 1 the address of the position one beyond the significance starter. If significance is forced, this address remains in register 1, but otherwise the address of the fust significant digit is placed in register 1 as part of the execution of the Edit and Mark. The Branch on Count Register instruction with a second operand of zero reduces the fust operand register contents by 1 and does not branch. There are, of course, other ways to subtract 1 from the contents of register 1, but this is the easiest and fastest. In the Move Immediate instruction we write an explicit displacement of zero and an explicit base register number of 1. The net effect is to move one byte of immediate data, a dollar sign, to the address specified by the base in register 1. This is the desired action. Figure 7-11 shows the effect on sample data values. Zero fields could be blanked by methods we have already discussed. BDD,DDS.DDBCR 40 20 20 6B 20 20 21 4B 20 20 40 C3 D9 1234567 0120406 0012345 0001000 0000123 0000012 0000001 0000000 -0098765 -0000000 $12,345.67 $1,204.06 $123.45 $10.00 $1.23 $.12 $.01 $.00 $987.65 CR $.00 CR Figure 7-11. Examples of the application of the Edit and Mark instruction to get a floating currency symbol WORK,PATTRN I,WORK+7 WORK,DATA 1,0 O(1),C'$' Edit, Translate, and Execute Instructions 91 THE TRANSLATE INSTRUCTION How It Works Another powerful programming feature of System/360 is the ability, through the Translate instruction, to convert very rapidly from one coding system of eight or fewer bits to another coding system. Using a conversion table, we can convert a string of characters from one form to another at speeds that compare favorably with that of decimal addition. Suppose that we have an input stream in which the data is in proper arrangement for processing, but is in Baudot teletypewriter code. Before System/360 can process the input, it must be converted to EBCDIC. The Baudot code is a five-bit code with shifting, which makes it the equivalent of a s~-bit code. For simplicity, we will omit control characters, punctuation marks, and fractions; for our purposes, they are "invalid". As shown in Figure 7-12, our transmission receiving equipment adds two zero bits at the beginning of each character, which do not change its binary value, and converts the code signals into the equivalent binary bit patterns shown in the illustration, so that our input stream is in the necessary eight-bit bytes. It remains for Us 'to translate this stream into the corresponding EBCDIC characters by programming. -The Translate instruction (TR) is in the SS format with two -storage operands. The frrst operand names the leftmost byte of a field to be translated; this field may be from one to 256 bytes in length. IIi programming parlance, it is called the a.rgument. The second operand names the start of a list, or table, that contains the characters of the code into which we' Wish to make the translation. The table may be 256 byt~~ in length or it may be shorter. It is called the function. . Our first step before using the Translate instruction is to construct a tablellke the one in Figure 7-13. Note that it is 64 byt~s ~ length, which is the maximum length of a six-bit code (2 6 = 64), and provides us with one byte for every binary value that we might receive. We give our table a name, TABLE, so that we will be able to refer to its symbolic storage address regardless of where it is. To create the table, a DC statement like the following might be used. In this case, we are arbitrarily filling the unused bytes with FF's. TABLE DC X'FFE3FFD64OC8D5D4FFD3D9C7C9D7C3E5C5E9 ..•........• F2FFFFF7F 1FFFF' Then, assuming each record is a maximum of 80 bytes in length, we set up a storage area for the Baudot data that is to be translated: RECORD DS CL80 After moving the frrst input item to RECORD, the only processing instruction that is necessary to convert it to EBCDIC characters is: TR RECORD,TABLE The operation of this instruction is byte by byte, from left to right, until the end of the frrst operand field. Like the other logical instructions- we have studied, TR treats all data as unstructured logical data, that is, as unsigned binary quantities. Say the frrst byte of RECORD is 0000 1010 (hex OA), which is R in Baudot code. The machine action will be to go to the address T ABLE+OA (that is, the byte at OA within the table) and to replace the OA in RECORD by the bit pattern it fmds in that byte of the table. This is hex D9 or 1101 1001, which is R in EBCDIC. If the next Baudot byte is 0010 1100 (hex 2C) for the numeral 8, it will be replaced by the contents of the byte at TABLE+2C: hex F8 or 11111000, which is 8 in EBCDIC. If the next Baudot byte is either hex 04 or 24,a space, it will be replaced by hex 40, the EBCDIC blank, which we placed in BIT POSITIONS 4, 5,6, 7 BIT POSITIONS 0,1,2,3 .. 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 HEX VALUE- t ,,- OOQO 0 0001 1 OQ1-0 2 0011 3 0 1 2 T E Z 5 3 0 3 4 5 6 7 0 sp H N M B S Y F X 9 sp 6 8 A 9 A B' e 0 E F L R G I P C V W J U .Q K 4 8 0 --, 7 1 2 Figure 7-12. Baudot teletypewriter code. This is a fIve-bit code that, with shifting, has the capacity of six bits, or 64 characters. Control characters, punctuation marks, and fractions have been omitted. 92 Sl/mhollc address 01' th,s ht/te IS ThIS 15 TABL£+07 TABLE I Address* Contentst Address Contents Address Contents Address Contents 00 / 01 02 03 04 05 06 07 08 09 OA OB OC 00 OE OF 7a1%!,.:.X' X ;Xl%.~ ~ 7rl/c'7v lX~170:X[5( ~ I~ :xX:x:l/( l/u';Yol/( ;X Va ;X X i;YsIX ;X LX ;X lX ~ I~ sp 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 10 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 20 2E 2F 30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F *Location of function byte within table, given in hex. tContents of function byte. The actual bit configuration is shown in hex at upper left; character at lower right is EBCDIC character represented by that bit pattern. Figure 7-13. Table for translation of Baudot code to EBCDIC. Unused bytes may be fIlled with FF's to test for invalid characters. the bytes at both TABLE+04 and TABLE+24. There are some important things to note about our translate table: 1. It contains the characters of the code into which we are translating, the function bytes. 2. It is in order, not by the binary sequence of the characters it contains, but by the binary sequence of the characters of the code from which we are translating, the argument bytes. 3. It is 64 bytes long, the length we determined was equal to the maximum number of bit combinations we might have to deal with in the Baudot code. If we were confronted by the reverse situation and needed to translate from EBCDIC to Baudot code, using the same letters, numbers, and blanks as before, we would have to construct a 256-byte table in order to have the required indexing or referencing capacity to 256 different addresses. (EBCDIC is an eight-bit code, and 'i = 256.) The table would contain the 38 characters of interest to us, but the contents of these function bytes would now have to be in the Baudot code bit configuration, and they would be in order by the sequence of the EBCDIC characters, which are now the argument bytes. We have not so far mentioned the unused function bytes of our table in Figure 7-13. We could store blanks or zeros as constants, but a better procedure is available to us, especially when a code without validity checks, like the Baudot, is transmitted. If we fill the spaces with a single unused character, such as hex FF (1111 1111), all invalid codes received would be translated to FF. After translation of each record, it would then be very simple to scan it for invalid characters by using a Compare Logical Immediate instruction. The Translate instruction may be used to convert any characters of no more than eight bits to any other characters, not necessarily from one standard code to another. It may be used to perform a control function, as in the program example in which we shall see the instruction at work. At frrst glance, this program may seem to be rather complicated, but it is simply a variation on an example of a sorting technique that we discussed earlier. An Example In the example we shall use the Translate instruction to accomplish a reversal of letters and digits in the collating sequence. When we compare a letter and a digit in normal EBCDIC coding the letter will always show as "smaller" than the digit. We shall assume that, for some special reason, it is necessary to arrange things so that letters sort as "larger". It should be realized that we need to reverse the ordering of letters and digits as complete groups. It is therefore not possible simply to reverse the paths taken on the comparisons in the program. Consider an example. With EBCDIC coding and the Compare Logical Character instruction, this is the binary sequence, and the machine's normal collating sequence, of the following five items: ADAMS JONES SMITH 12345 56789 Edit, Translate, and Execute Instructions 93 We want to modify the sorted order to: 12345 56789 ADAMS JONES SMITH If we were simply to reverse the paths taken after the comparison, the sorted order would be: 56789 12345 SMITH JONES ADAMS We shall see how, using the Translate instruction, we can rearrange the letters and 'digits so that digits sort ahead of letters, while retaining normal numerical and alphabetical order. The translated characters will be used only for the sorting operation; we are not required to translate the characters into anything that would be otherwise meaningful. The only thing we need to do in setting up the table, therefore, is to replace digits with something smaller than what we replace letters with. There are, of course, a great many ways to do this. In the program of Figure 7-14 we have chosen a scheme for its simplicity. The digits 0-9 are replaced by hexadecimal 01-10, A-I are replaced by 11-19, J-R by 21-29, and S-Z by 32-39. These replacements satisfy the one basic requirement, that digits sort earlier than letters. The scheme also preserves the ordering of the letters within the alphabet. The particular choices for the letters are not critical, but they will seem reasonable to someone familiar with punched cards. The program begins with an unfamiliar operand in the PRINT instruction, the assembler instruction that controls the content of the assembly listing. The operand DATA has the effect of causing constants to be printed out in full in the listing. If DATA is omitted or NODATA is specified, only the leftmost eight bytes of any constant will be printed, no matter how large it may be. In actual programming, it is generally considered good practice to include PRINT DATA routinely-and also to let generated macro statements appear in full (by the use of GEN or the omission of NOGEN in the PRINT statement). Then the programmer will always have a complete listing for program checking and debugging. A full explanation of the PRINT instruction may be found in the assembler language reference manual. We are assuming, for the purposes of this program, that the input stream contains nothing but letters and digits. There are only 36 of these. The other 220 positions of the table have been filled with blanks (represented by 40 in EBCDIC), which is not quite representative of what we might do in practice. In an actual application, if our data 94 had already been run and verified and we really knew that nothing else could appear, we would use relative addressing with a minus factor to reference the table, and would not store the blanks at the beginning of the table. If, as is more likely, we were concerned about the possibility of erroneous data, we might use the full 256-byte table to check validity. The task is to sort into the stated sequence three records of 13 characters each, using as the sort key the middle five characters of each record. In other words, the sorted records, which are named A, B, and C, are to be in sequence on their middle five characters after the execution of the program. In the program of Figure 7-14 we begin by moving the keys to locations in which they can be translated; we do not want to destroy the actual records. The working storage areas have been named KEYA, etc. We shall see shortly why these need to be 13 characters. The three Translate instructions make the conversions of coding on the keys that we have described in detail above. The original records are not disturbed. Now we load three general registers with the addresses of A, B, and C, that is with (A)A, (A)B, and (A)C. It is these addresses that will be moved during the bulk of the sorting, not the records themselves. The Compare Logical that comes next must be studied carefully. The instruction says that the fust operand begins 43 bytes after the address contained in register 2 and that the fust operand is five bytes long. Register 2 at this point contains the address of A because of the Load Multiple just before this instruction. Looking at the data layout, we see that 43 bytes past the beginning of A is the beginning of the translated key of A. Similarly, the second operand refers to the key of B. (Only one length is required on this instruction.) We are thus asking for a comparison between the translated key of A and the translated key of B. If the key of A is already equal to or smaller than the key of B, we Branch on Not High down to X where the next comparison is made. If the key of A is larger than the key of B, we proceed in sequence to the three instructions that interchange the contents of registers 2 and 3. This means that when we arrive at X, register 2 contains the address of the smaller of the keys of A and B, whether or not there was an interchange. In the addressing scheme described in the preceding paragraph, it is essential that there be a fixed relationship between the address of an item and the address of its translated key. In other words, the translated key of A in KEYA has to be the same distance beyond A as the translated key of B in KEYB is beyond B, and similarly with KEYC and C, so that the same displacement of 43 can be used for all three items. (This, in turn, is why KEYA, KEYB, and KEYC were made 13 characters long even though the keys are only fIVe.) This addressing scheme is necessary because on the second and third comparisons, we will not know which keys are being compared-A, B, or C. lOC 000000 000000 000002 000002 000008 OOOOOE 000014 00001A 000020 000026 00002A 000030 000034 000036 000038 00003A 000040 000044 000046 000048 00004A 000050 000054 000056 000058 00005A 000060 000066 00006E 00007B 000088 000095 0000A2 OOOOAF OOOOBC OOOOC 0 0000C4 0000C8 000005 0000E2 OOOOEF 0000F7 OOOOH 000107 00010F 000117 OOOllF 000127 00012F 000137 00013F 000147 00014F 000157 00015F 000167 00016F 000177 00017F 000187 00018F 000191 00019F 000 lA 1 0001AF 0001BO 0001B8 0001B9 0001CO 0001C8 0001C9 000101 000109 0001DF 0001E1 0001E9 000000 OBJEC T CODE ADDR1 ADDR2 05BO D204 D204 D204 DC04 DC04 DC04 9824 0504 4700 1862 1823 1836 0504 4700 1862 1824 1846 0504 4700 1863 1834 1846 D20C D20C 020C B097 BOA4 BOB 1 B097 BOA4 BOB 1 BOBA 202B B038 B070 B07D B08A BOED BOED BOED 00099 000A6 000B3 00099 000A6 000B3 00072 0007F 0008C OOOEF OOOEF OOOEF OOOBC 302B 0002B 0002B 0003A 202B 402B 0002B 0002B 0004A B048 302B 402B 0002B 0002B 0005A B058 BOC6 2000 000C8 00000 B003 3000 00005 00000 BOEO 4000 000E2 00000 STMT 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 33 0000006E 00000076 00000088 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4b40404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 4040404040404040 40 1112131415161118 19 404040404·04040 2122232425262128 29 4040404040404040 3233343536373839 404040404040 0102030405060708 0910 404040404040 34 35 36 37 38 39 40 41 42 43 44 45 SOURCE STATEMENT PRINT DATA,NOGEN SORTABC2 START 0 BEGIN BAlR 11,0 USING *,11 KEY A+4 (5) , A+4 MVC KEYB+4(5), B+4 MVC KEYC+4(5),C+4 MVC KEYA+4 I 5), TABL E TR TR KEYB+4(5), TABL E TR KEYC+4 I 5) ,T ABL E 2,4,ADDRA LM 4315.2),4313) CLC BNH X 6,2 LR 2,3 LR 3,6 LR X 4315,2),4314) CLC Y BNH 6,2 LR 2,4 LR 4,6 LR 43 15,3) ,43 141 Y CLC MOVE BNH 6,3 LR 3,4 LR 4,6 LR SMALL,0(2) MOVE MVC MEDIUM,O(3) MVC lARGE,0(4) MVC EOJ A C1l3 OS B OS C1l3 CLl3 C OS KEYA OS C1l3 KEYB OS C1l3 KEYC OS CLl3 ADORA AlA) DC AlB) ADORB DC AIC) ADORC DC SMALL C1l3 OS MEDIUM Cll3 OS LARGE OS CLl3 , TABLE DC CL193' MOVE KEYS TO POSITION FOR TRANSLATE TRANSLATE KEYS TO CHANGE COLLATE SEQ PUT ADDRESSES IN REGS 2, 3, 4 COMPARE KEYA WITH KEVB BRANCH IF ALREADY IN SEQUENCE INTERCHANGE COMPARE SMALLER OF A AND B WITH KEVC BRANCH IF ALREADY IN SEQUENCE INTERCHANGE COMPARE TWO LARGER KEYS BRANCH If ALREADY IN SEQUENCE INTERCHANGE MOVE USING ADDRESSES IN REG I STERS 46 DC X'l11213141516171819' 47 48 DC DC Cl7' t X'212223242526272829' 49 50 51 52 DC DC DC DC Cl8' , X'3233343536313839' CL6' , X'01020304050607080910' 53 54 DC END Cl6' BEGIN , Figure 7-14. A program to sort three fields named A. B, and C into ascending sequence on me-character keys in each field. The Translate instruction is used to make digits sort ahead of letters. Edit, Translate, and Execute Instructions 95 We now carry out the same actions using the addresses in registers 2 and 4, thus comparing the smaller of KEYA and KEYB with KEYC. The two addresses are interchanged if necessary, to make the address in register 2 that of the smaller. After this sequence of instructions, therefore, we can be positive that register 2 contains the address of the smallest of the translated keys. The- same set of actions on registers 3 and 4 gets them in proper sequence. Now we know that whatever rearrangements mayor may not have been carried out, register 2 contains the address of the smallest of the keys, register 3 the address of the middle-sized, and register 4 the address of the largest. We can therefore proceed to the three instructions that place the proper three records in SMALL, MEDIUM, and LARGE. For instance, the flIst of these instructions, the one at MOVE, says to move 13 characters from the address given in register 2, whatever it may be, to SMALL. The other two instructions do the same with registers 3 and 4. Figure 7-15 shows the contents of registers 2, 3, and 4 at four points during the execution of the program: at the 96 beginning~ at X~ at Y, and at MOVE. The three actual data items, in order, were: I111SMITHllll 2222ADAMS2222 3333567893333 In other words, the original items were in reverse order to the sequencing pattern we wanted. AFTER EXECUTION OF REG 2 REG 3 REG 4 STATEMENT 11 0000206E 0000207B 00002088 STATEMENT 16 0000207B 0000206E 00002088 STATEMENT 21 00002088 0000206E 0000207B STATEMENT 26 00002088 0000207B 0000206E Figure 7-15. The contents of registers 2, 3, and 4 during execution of the program in Figure 7-14, loaded at 2000 THE TRANSLATE AND TEST INSTRUCTION AND THE EXECUTE INSTRUCTION The Translate and Test instruction (TRT) adds great power to the processing capability of System/360. It is related to the Translate instruction and has the same format, but is very different in operation. It is used to scan a data field for characters with a special meaning. Since it merits our close attention, we shall study it in the three remaining programs in this chapter. As with Translate, we work with a table as the second operand that is accessed exactly the same way. That is, a frrst operand argument byte addresses a particular entry in the table by an address computation. Once again the table must be in order by the binary sequence of the code of the source material, which in this and the following sections will be standard EBCDIC input. This time, however, we must put zeros in the table to indicate characters without any special meaning and some nonzero value for each character with a special meaning. In further contrast to the Translate instruction, there is no change in the argument bytes as a result of the TRT operation, despite the "translate" in its name. Instead, the argument bytes are merely inspected, byte by byte, from left to right. If the first argument byte references a function byte that is zero, the next argument byte is inspected, and so forth. If all the function bytes that are referenced are zero, the condition code is set to zero and the operation is complete. However, if a nonzero function byte is referenced, the contents of that byte are placed by the machine in register 2 and the address of the argument byte is placed in register 1. The condition code is set to 1 or 2, and the operation is terminated. A condition code of 1 indicates that there are more function bytes t~ inspect, a condition code of 2 that the nonzero function byte is at the end of the field. The programmer may then make use of the information in the registers and in the condition code. This means that we can inspect a complete stream of argument bytes, looking for whatever interests us: error characters, end-of-message codes, blanks and commas that separate parts of a line, or whatever. The following problem shows one way to use the instruction. We are given the starting address of a string of characters of unknown length. The string contains an unknown number of names and addresses. Each name is of unknown length; each address component is of unknown length; there may be from one to four lines of address; we do not know how many names and addresses there are. All we do know is that after each "line" of information there is a dollar sign ($), after the last line of an address there are two dollar signs ($$), and at the end of the entire string there is a dollar sign followed by an asterisk ($*). We are required to set up each name and address in four lines named LINEl, LINE2, LINE3, and LINE4. Any unused lines must be blanked. When an address has been assembled in this manner, it is to be printed, after which we return to set up and print the next address. The table required for this application must be 256 bytes in length in order to reference the complete range of EBCDIC binary values. It will consist of 254 zeros, with entries only in positions 5B (91) and 5C (92), corresponding to dollar sign and asterisk respectively. For the dollar sign we have chosen to enter 01 and for asterisk 02. These choices are highly arbitrary; as we shall see, any other two numbers would be just as good. All we need to know about the input stream is where the doll-ar signs and asterisks appear; we care nothing about any other characters. The program in Figure 7-16 begins by placing in register 3 the address of the frrst character of the input stream that we shall break into names and addresses. On the assumption that there is only one such stream to process, this instruction is never repeated in this program. The next instruction is returned to each time another name and address is to be processed. It places a 4 in register 9 to be used as a guard against incorrect input streams; if ever a name and address would seem to require more than the four lines we have allotted, the program will stop. The next Load Address places in register 10 the address of th~ frrst line of the output. The next two instructions are ~ver1apping Move Characters that clear to blanks the o.Btput areas. With assumed line lengths of 120 characters, this makes 480 bytes to clear. Since the maximum length in a Move Characters is 256 bytes, two instructions are needed, each clearing two lines. The first MVC instruction clears to blanks the first two lines and the first position of the third line. The second MVC instruction uses the blank now in the frrst position of the third line to blank the remaining positions of the third line and all of the fourth line. Now we come to the Translate and Test. The fust operand starts at the address in register 3, which we set up with the starting address of the input stream; it is stated to be a maximum of 120 characters in length. The second operand address names the table. If the input stream is correct, a dollar sign will be found within 120 characters. If, because of an error, there is no end-of-line dollar sign, we will have a condition code of zero at the completion of the execution of the instruction. A Branch on Zero, accordingly, takes us to an error exit (this could also be written asBC 8,ERROR). In the normal case of finding a dollar sign to indicate the end of the first line, what do we have in the registers? Register 1 contains the address of the dollar sign that stopped the Translate and Test. We wish to do a little arithmetic on this address without destroying it, so we move it to register 4. Now we subtract from the address of the dollar sign the address of the first character of the line. The difference is the length of the line, in bytes. We are about Edit, Translate, and Execute Instructions 97 LOC OBJECT CODE AoDRl ADDR2 STMT SOURCE STATEMENT PRINT 2 MAILLIST START BUR 3 BEGIN USING 4 SR 5 1 000000 000000 000002 000002 OC0004 G00008 OOOOOC 000010 000016 00001C GOOOll OC0026 000028 00002A ce002E 000032 000036 00003A 00003E 000042 000046 05BO 1B22 4130 4190 41AO 02FO D2EE CC17 4780 1841 IB13 5B10 4740 4410 4130 41AA 4690 47FO 0700 0023F 8230 00004 0004 0005F 8050 B050 BOSC 0005F 0005E B14E B140 00150 0014F 3000 B30A 00000 0030C 00056 B054 B302 B044 B056 4001 0078 BOlA B054 000048 4130 4001 OC004C 5920 B306 OU0050 4170 B006 00304 00046 0005B 00C01 C0078 OOOlC 00056 00001 00308 00008 000058 0200 ACOO 3000 00000 OOCOO 00005E 40 00005F 000007 00014F 0001C7 ~ 00023F 000247 OC024F 000257 00025F 000267 00026F 000277 00027F 000287 00028F 000297 00029F 0002A7 E204C9E3C85BC4CS E3C906C9E35B58ol 4B40C3484001C1C3 o2E206D55BF1F2F3 F44004CIC90S40E2 E309C5C5E3SBC3CH C9C3CIC7D66B40C9 o3C3C9D5D6C9E2SB 5BC64B40C34B40C9 4B40CI05C4C509E2 o6C5SBF5F5F34004 CID7D3C540D7D3Cl C3C540CID7C109E3 C4C5D5E340 C002AC 000284 0002BC 0002C4 OC02CC 000204 0002CC 0002E4 0002EC OC02F4 0002FC 0(,0303 000304 00030B OC030C 000314 00031C 000324 00032C 000334 00033C 000344 OG034C OC03S4 0003SC 000364 000367 000369 OC0371 000379 000381 FSC3SBE6C8C9E3CS 400703ClC90SE26B 40D5C5E640E8D6D9 025BSBC44840C44B 40CIC4CID4E240Cl o5C440C6ClD4C9D3 E85BFSFOF540C7D9 CIE3C8E2D6CS5BCl D7E34840F3F15BD9 C5CIC4C9DSC76B40 C7CSD5CS4BS8SC 00 00000001 00000002 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 OOOOOOCOOOOOOOOO 000000 0102 OOOOOOCOOOOOOOOO oooooocoocoooooo 6 LA 7 AGAIN LA LA 8 9 10 11 12 13 14 1S 16 17 18 19 20 21 22 23 24 2S 26 27 28 29 32 35 36 KVC ~VC TRT LOOP Bl LR SR S BM EX LA LA BCT B NOPR OUT • • • •* *LA C ERROR ,"VCINS BLANK L1NEl L1NE2 LINE3 L1NE4 NAME BNE EOJ EOJ DATA,NOGEN o 11 ,0 *,11 2,2 3,NAflE 9,4 10 ,L1 NEI L1NE1I2411 ,BLANK LINE3+1(239),LINE3 0(120,3) ,TABLE ERROR 4,1 1,3 1.eNE OUT 1,MVCINS 3,110,4) 10,120(10) 9,LOOP ERROR ° 3.110,41 2,ENDCON AGAIN CLEAR REG FOR LATER COMPARE INSTR PUT STARTING AODR OF RECORD IN REG FOR ERROR CHECKING INITIALIZE TO START OF FIRST LINE BLANK LINES 1 & 2, 1ST POS LINE 3 BLANK BAL LINE 3 & LINE 4 SCAN RECORD FOR DELIMITER BRANCH IF NO UELIMITER IN 120 CHARS GET LENGTH CODE OF LINE BRANCH IF 2 DELIMITERS IN SEQUENCE MOVE LINE TO PRINTING POSITION SET UP NEXT TRT TO GET NEXT UNE BRANCH UNLESS FIFTH LINE MORE THAN 4 LINES THE PRINT ROUTINE WOULD START HERE SET UP FOR NEXT NAME & ADDRESS SEE IF DELIMITER WAS AN ASTERISK BRANCH IF NOT ALL FINISHED IF HERE ERROR STOP EX INSTR ADDS LENGTH FROM REG 4 DC OS OS OS OS DC 0(OtlOI,013) CLl' , CU20 CLl20 CU20 CLl20 C'SMITHSDETROITSSJ. C. JACKSONS1234 MAIN STREETSCHICAGO,X ILLINOISSSF. C. R. ANoERSONSS53 MAPLE PLACE APARTMENT ' 42 DC C'5CSWHITE PLAINS. NEW YORKSSD. D. ADAMS AND FAMILYS505 X GRATHSONSAPT. 3lSREADING, PENN.S*' 43 ONE 44 ENDCGN 45 TABLE DC DC DC F'l' F'2' 91X'00' 46 47 DC DC X'0102' 163X'OO' END BEGIN 37 38 39 40 41 "'VC OOOCOOOOOOOOOOOO 0000000000000000 CC03F9 OOOOOOOOOOOCOOOO 000401 0000000000000000 U00409 000000 0(;0000 48 Figure 7-16. A program to print names and addresses. The input stream contains an unknown number of names and addresses, each name and address contains a variable number of lines, and each line is of variable length. 98 ready to execute a Move Characters instruction in which we will use this computed address; but in the instruction itself the length code is always one less than the actual length. So we now subtract 1 from the difference residing in register 1. What would it mean if this difference were now negative? We shall see, in further analysis of the program, that it would indicate the double dollar sign that denotes the end of a name and address. We therefore Branch on Minus (or BC 4) to OUT, where we would normally process the completed name and address. Let us review the status of things. We have in register 3 the starting address of a group of characters that should be moved; in register 10 we have the address to which they should be moved; in register 1 we have the correct length code for a Move Characters instruction. We need either to place that length code in an instruction - or do something equivalent. "Something equivalent" is precisely what the Execute (EX) instruction provides. We say EX 1,MVCINS This means to execute the instruction at the second operand address named (MVCINS), after Or-ing together the last eight bits of register 1 and the length code portion of MVCINS. Looking down at MVCINS we see that a Move Characters instruction has been set up to do all the things just outlined as necessary, with the exception of the length. The instruction set up at MVCINS says to move a group:Of bytes starting at the address given in register 3 to another location given by the address in register 10. Both displacements are zero, because the base addresses are exactly what are wanted. The length code is zero in the instruction; the actual length is supplied by the last eight bits of register 1. One line of the complete name and address is thus moved to a printing position. The Execute instruction is a very serviceable tool in the hands of a resourceful programmer, especially when it is used in a loop that deals with varying conditions. It is an unusual branching instruction that causes one instruction anywhere in a program to be executed out of sequence. Then, unless the remote instruction itself happens to be a successful branch, the program continues with the next instruction after the Execute. As we have seen, Execute can actually modify the remote instruction before execution. It can specify length codes, immediate data, register operands, or whatever information goes into the second byte in the format of the remote instruction. It does this by Or-ing with the last eight bits of a register, which the programmer may use to store information, do arithmetic, or whatever. We will see further examples of the Execute instruction in the next two programs. We are now about ready to go back for another look at the input stream. To do that, register 3 must contain the address of the next valid data character in the stream. Register 4 contains almost what we need; it has the address of the dollar sign just prior to the next valid character. We accordingly use a Load Address instruction to get the desired address into register 3. The instruction operates as follows. The displacement of one is added to the contents of the base register to get an effective address. (If an index register had been specified, its contents would also have been added in.) This address is then placed in register 3, with no actual reference to storage. It would have been legitimate to place the sum back in register 4, if that had been desired. Load Address provides a fast and simple way to add a small positive amount to a register. In the next Load Address instruction we see register 10 being incremented by 120 by use of the method just described. The purpose is to set up the next line as the destination the next time around the loop. Finally we Branch on Count back to inspect the input stream again. If this .would mean trying for a fifth line, the branch is not taken and we reach the error exit. At OUT, which we reach on discovering either two dollar signs in sequence or a dollar sign followed by an asterisk, we would normally include a series of instructions to print the output. Since input/output operations are outside the scope of this book, we simply indicate by a No-Operation instruction that this action would occur here in the program. NOPR is an extended mnemonic for Branch on Condition with a mask of zero, which never causes a branch to occur. Following the output operations we are ready to go back for another name and address, unless this was the last one in the stream. Whether that was the case can be determined by looking at the function byte in register 2 to see whether it is that produced by a dollar sign or by an asterisk, that is, a lora 2 respectively. A comparison with ENDCON, which contains a 2 in proper form for a comparison with a fullword register, makes the determination. If the function byte is not that produced from an asterisk, we Branch on Not Equal back to AGAIN to repeat the whole process. Otherwise we reach the normal exit from the program. Figure 7-17 shows successive groups of output, based on the input stream assembled with the program. SMITH DETROIT J. c. JACKSON 1234 MAIN STREET CHICAGO, ILLINOIS F. c. R. ANDERSON 553 MAPLE PLACE APARTMENT 5C WHITE PLAINS, NEW YORK D. D. ADAMS AND FAMILY 505 GRATHSON APT. 31 READING, PENN. Figure 7-17. Four names and addresses produced by the program in Figure 7-16 Edit, Translate, and Execute Instructions 99 AN ASSEMBLER APPLICATION OF TRANSLATE AND TEST AND EXECUTE Another example of the powerful combination provided by the Translate and Test instruction with the Execute instruction is provided by a simplified version of part of the work an assembler must do. We are given an input stream consisting of one type of operand field in an assembler language program. The field that we shall process will always consist of two operands: the fIrst will be a general register, the second a symbolic address of not more than six letters. Relative addressing with either an increment or a decrement mayor may not be included in the second operand. Accordingly, our field will start with one or two decimal digits, a comma, and from one to six letters. After the fmal letter there will be either: (l) a blank, or (2) a plus or a minus sign followed by from one to four decimal digits and a blank. We are required to place the register number in REG as a binary number, to place the symbol in SYMBOL, and to place in INCDEC the increment or decrement as a properly signed binary number. We are, of course, defining away a great deal of the actual work of an assembler program, which must sort out many different kinds of instructions and operands, and errors too. The task of the Translate and Test Instruction this time will be to detect the "delimiters" that separate one part of the operand field from another. The delimiters in the job as we have defmed it are the comma, the plus sign or the minus sign, and the blank. These set off register from symbol, symbol from increment or decrement, and mark the end of the address. We will need a translate table with entries in the positions corresponding to these four delimiters. The input stream begins at symbolic location COLI6, a name chosen to suggest where the operand field might begin on a card, although we realize that, in the System/ 360 assembler language, it is not required to begin there. The program of Figure 7-18 begins by clearing to blanks the location set up for the symbol. This must be done because we do not know whether the symbols we shall find will always have six characters; therefore, any previous contents of SYMBOL must be erased. A similar consideration applies to INCDEC. There mayor may not be an increment or decrement, hence we are required to place zero there. It seems to be a little easier to clear INC DEC at the beginning and then to leave it zero, if nothing is placed there, rather than to clear it later if necessary. REG need not be cleared; we will always place something there. This time we construct the function table by entering a constant of 256 bytes of zeros in storage, and use the Move Immediate instruction to insert arbitrary values in the EBCDIC positions corresponding to the four delimiters. To fmd the correct positions, we need only read off the 100 hexadecimal values from an EBCDIC chart. For the delimiters, a value of 1 is used for a blank, 2 for a comma, 3 for a plus sign, and 4 for a minus sign. When the program is executed, these values will be moved into position in place of zeros in TABLE, which will then be in storage, and of course the values will be in the specified bytes before execution of the TRT instructions. Following a procedure somewhat similar to that used in the name and address program of the preceding section, we now place in register 3 the address of the leftmost character of the stream. A Translate and Test will stop after two or three characters, depending on whether the register number has one or two digits. We now compute in register 4 the proper length code, either zero or 1, and use an Execute to carry out a Pack instruction that is stored at PCKINS. This remote Pack takes its first operand from the address given in register 3, its length from register 4, and places the result in WORK. The latter was set up as a doubleword, so we may now do a Convert to Binary, placing the result in register 5 from whence we store it in REG. The frrst required action is complete. We are now ready to get the symbol, after some preliminaries. When we have found the delimiter after the symbol (a blank, a plus, or a minus), it will be necessary to compute the length of the symbol. In order to be able to do this later, we need now to put in register 3 the address of the first character of the symbol. This can be done with a Load Address instruction using register 1 as a base and a displacement of 1. The same scheme (base register 1 and displacement of 1) gives the correct starting address for the Translate and Test instruction also. Once again, after completing the Translate and Test, we compute the length of the symbol and use an Execute, this time to move the symbol from its position in the input stream to SYMBOL. When this has been done, we inspect the delimiter. If it is a blank, signified by a function byte of 1 in the TABLE, we are finished because there is no increment or decrement. If it is not a blank, then it must be either a plus or a minus, always assuming for this example that there are no errors. If it is a plus, we place a 2 in register 6; otherwise a zero. The purpose of this will become clear in a moment. At NEXT we once again place the address of the next character in the stream in 3, this time to be able to compute the length of the increment or decrement. The next six instructions are much as they were before, resulting in the value of the increment or decrement being placed in register 5 in binary. It will be positive; the sign was not included. Now we come to an Execute instruction used in a rather different way for a rather different purpose. We have specified register zero for the Or-ing, which means that the executed instruction is not modified. Then we have indexed the address of the instruction to be modified. We will therefore execute either the instruction at MININS, if register 6 contains a zero, or the instruction two bytes later, if register 6 contains 2. The net effect is to do nothing to register 5 if the sign is plus, and to make register 5 negative if the sign is minus. Having done this, we store the contents of register 5 at LaC OBJECT CODE ADDRI ADDR2 STMT SOURCE STATEMENT 1 000000 000000 000002 000002 QOOOOS OOOOOA OOOOOE 000012 000016 OOOOlA OOOOlE 000022 000028 00002A 00002C 000030 000034 00003S 00003C 000040 000046 Q00048 00004A 00004E 000052 000056 00005A 00005E 000062 000066 00006A 00006E 000072 00007S 00007A 00007C OOOOSO 0000S4 OOOOSS OOOOSC 000092 00009S 00009E OOOOAO 0000A2 OOOOAS OOOOAC 000060 00006S OOOOBE OOOOCO 0000C4 0000C6 OOOOCC OOOODO 0000D8 OOOOEO OOOOES OOOOFO 0001B8 0001CO 0001C8 0001DO 000108 000000 2 ASSMI3LR 3 BEGIN 05BO D205 1922 5020 920 1 9203 9204 9202 5830 DDOE 1841 1843 5840 4440 4F50 5050 4131 DD06 lS41 1843 5840 4440 5920 4780 5920 47S0 4160 47FO 4160 4131 0004 1841 IB43 5B40 4440 4F50 4406 5050 80AO 8086 000A2 000B8 BOAA BIOE BllC B12E B139 B08E BICE BOCE OOOAC 00110 OOllE 00130 0013B OOOCO 00100 00000 BOC2 B090 BOAE BOA6 0001 1001 BOCE 00001 000C4 00092 00080 000A8 00001 00000 BOC2 8096 BOC2 B08E BOCA B068 0000 B06C 0002 0001 1001 BOCE 00001 000C4 00098 000C4 00090 OOOCC 0006A 00000 0006E 00002 00001 00000 BOC2 B090 BOAE B09C BOAA 000C4 00092 000!30 0009E OOOAC F270 BOAE 3000 OOOBO 00000 0200 BOAO 3000 000A2 00000 1155 1055 404040404040 0000 00000100 00000001 00000002 00000003 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000600 0000000000000000 0000000000000000 F1F16BCIC2C3C4C5 Cb4EFIF2F3F440 INCDEC and our assigned task is completed; we have placed various parts of the operand in separate locations where they can be separately addressed. In the real world of an assembler, many more operations would have to be performed on this operand. Our small task of separating the various parts of the operand would facilitate these further operations. 4 5 6 7 8 9 10 11 12 PRINT START BALR USING MVC SR ST MVI MVI MVI MVI L TRT LR SR 13 14 15 16 S EX CVB ST LA TRT LR SR 17 OATA,NOGEN o 11,0 *,11 SYMBOL,BLANK 2,2 2,INCDEC TABLE+X'40',X'01' TABLE+X'4E',X'03' TABLE+X'60',X'04' TABLE+X'69',X'02' 3,ACOL16 (OLl6,TABLE 4,1 4,3 4,oNF 4,PCKINS 5,WORK 5,REG 3,1(1) 1(7,1I,TABLE 4,1 4,3 4,oNE 4,MVCINS 2,oNE OUT 2,THREE PLS 6,0 NEXT 6,2 3,1(ll 1I5,1),TABLE 4.1 4,3 4,ONE 4, PCK INS 5,WORK 0,MININS(61 5,INCDEC 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 45 46 47 48 49 50 51 52 53 SYMBOL REG INeOEC WORK BLANK EX eVB EX ST EoJ PACK MVC LNR LPR OS DS OS OS DC 54 55 56 57 58 AColl6 ONE TWO THREE TABLE DC DC DC DC DC ~9 CoLlb DC C'll,A8CDEf+1234 ' END BEGIN 60 S EX C BE e BE LA B PLS NEXT LA LA TRT LR SR S OUT PCKINS MVCINS MININS WORK,O(0,31 SYMBOL( 0) ,0 (3) 5,5 5,5 Cl6 CLEAR LOCATION FnR SYMBOL CLEAR REGISTER 2 CLEAR SPACE FO~ INCREMENT OR DECR INSERT NONlERn VALUES IN TABLE PUT STARTI~G ADDRESS I~ REG 3 LOOK FOR FIRST DELIMITER COMPUTE LENGTH cnOE OF REG NUM8ER PACK REG NUMBER ANn PLACE IN WORK CONVERT TO RIN~RY AND PUT IN REG 5 STORE REG NUMBER IN BINARY SET UP FOR NEXT TRT LOOK FOR NEXT ryFLIMITFR CO~PUTE LENGTH OF SYMBOL PLACE RESULT IN SYMBOL WAS DELIMITER A ALANK BRANCH IF SO WAS DELIMITER A PLUS SIGN BRANCH IF SO SET UP FOR LAT~R REMOTE INSTRUCTION SET UP FOR LATER REMOTE INSTRUCTION SET UP FOR NEXTTRT LOOK FOR NEXT OELIMITER COMPUTE LENGTH OF INCDEC THIS IS INCREMENT OR OECREMENT CONVERT TO BINA~Y AND PUT IN REG 5 COMPLEMENT IF SIGN WAS MINUS STORE RESULT PROGRAM TERMINATION EXECUTE INSTR AOOS LENGTH FROM REG 4 DITTO F F D CL6' , A(Co1l61 F' l ' F' 2' F' 3' 256X'OO' Figure 7-18. A program to break down the operands of an assembler language instruction into its constituent parts, using TRT and EX Edit, Translate, and Execute Instructions 101 PROCESSING VARIABLE-LENGTH BLOCKED RECORDS The following illustrative program applies techniques that are highly useful in certain commerical applications, and that the features of System/360 make particularly easy to accomplish. The task is the processing of blocked tape records (that is, many logical records in one physical block) with a variable number of records per block and with variable-length records. We shall take a record layout, furthermore, that places certain fIXed-length items after the variable-length portion of the record. Each record in a block to be processed by the program of this example will contain four fields, with characteristics as follows: Field DESC ACCT QOH DOLL Length variable, at most 60 characters 7 characters 4 bytes 4 bytes Type alphameric alphameric binary binary The first field is a variable-length description of a stock item; it is alphameric and at most 60 characters. The next field is an account number, of exactly seven alphameriC characters. The third field is four bytes long. It is a binary number giving the quantity on hand. The fourth and last field is also a four-byte binary number giving the year-todate sales of the stock item to the nearest dollar. However long the description may be, its final character is always an equal sign to serve as a sentinel marking the end of the variable-length portion of the record. There is an unknown number of records. Immediately following the last record is another equal sign, which is the last character in the block. We are required to process such a block, which we assume has already been read into core storage. We are to set up a line for printing that contains the account number, the quantity on hand, the sales, and the description, in that order. The numeric quantities are to be in zoned format. After printing a line for each record in the block, we are to print the total dollar sales from all records on a separate line. The program is shown in Figure 7-19. After the usual preliminaries we clear register 4 and store the resulting zero in TOTAL in order to be sure that the accumulator for total sales is zeroed. Register 7 is next loaded with the address of the first character of the block; register 7 will always contain the address of the first character of the next record as the loop is repeated. The MVI instruction inserts a one in the equal sign position of our translate table. This will occur during execution, of course. In the body of the loop we fIrst blank out the space assigned to the description because, in general, it will be possible for a long description to be followed by a short one; without a prior blanking, the end of the previous line would still be there. The MVC instruction used here will blank out the DESC area for its entire 60-byte implied 102 length provided that the first operand DESC in storage is one byte to the right of the second operand BLANK. Checking statements 50 and 51 of the assembly listing, we see that this is so. The Translate and Test instruction references a table in which the only nonzero entry corresponds to an equal sign. The effective address of the first operand in the Translate and T est is just the contents of rcgl:. ter 7 because the explicit displacement is zero. The length of 60 sets a limit on the search for an equal sign. If no equal sign is found within 60 bytes, the condition code will be zero; a Branch on Condition transfers to an error routine if this happens. We now are ready to move the description from its place in the block to the space from which it will be printed. This can be done readily enough once we have available the length code of the description. Register I after the Translate and Test contains the address of the equal sign. Subtracting from this address the address of the first byte of the description gives the length of the description in bytes; one less than this number is the length code of the description. With this number in register 3, we can Execute a remote Move Characters instruction that moves the description from the block storage area to a location from which it can be printed. Just before doing so, however, we have a Branch on Minus instruction to detect a negative number after the compu tat ion of the length code of the description; this would happen only if the first character of the· "description" were an equal sign, which would signal the end of the block. Getting the account number from the block area to the printing location is an easy matter. We know that the account number begins one byte beyond the address of the equal sign, which is contained in register 1. The effective address of the account number is therefore just register I as a base with a 1 for displacement. The address of the quantity on hand is just eight bytes beyond the address in register 1. Here we must be careful of word boundaries. The quantity on hand was said to be a four-byte binary number, but, because of the variable length of the description, it may not be aligned on a word boundary in the block storage area. We therefore use a Move Characters instruction to move it to a temporary storage area that is definitely aligned on a word boundary. TEMPI is on a word boundary because the DS says so. Now this binary quantity can be loaded into a register and converted to decimal in a doubleword. From here it is unpacked to the location from which it will be printed, named QOH. The same sequence of operations gets the year-to-date sales into DOLL. Because the sales are still in register 4 in binary, they can be added to the total for the block. LOC 000000 000000 000002 000002 000004 OOOOOB OOOOOC OOOOOE 000012 000018 OOOOIE 000022 000024 000026 00002A 00002E 000032 000038 00003E 000042 000046 00004C 000052 000056 00005A 000060 000064 000068 OBJECT CODE AOOR1 ADOR2 SOURCE STATEMENT 1 2 VARBLK 3 BEGIN 05BO IB44 5040 BOEE 5870 B07A IBII 9201 BIE6 023B BOA3 003B 7000 4780 B070 1831 1B31 5B30 B07E 4140 B06E 4430 B072 0206 Boa2 0203 BOE2 5840 BOE2 4E40 BOE6 F377 Boac 0203 BOE2 5840 BOE2 4E40 BOE6 F311 B097 5A40 BOEE 5040 BOEE 4111 0010 STMT OOOFO 0007C OOlEB BOA 2 000A5 000A4 B1b8 00000 0016A 00012 1001 00084 1008 000E4 BOE6 0008E 100C 000E4 BOE6 00099 00080 00070 00074 00001 ooooa 000E4 000E8 000E8 OOOOC 000E4 000E8 000E8 OOOFO OOOFO 00010 000074 00007A 00007C ooooao 000084 00008B 00008E 000096 000099 OOOOAI 0000A4 0000A5 0000E4 0000E8 OOOOfO 0000f4 OOOOFC 000104 000108 00010F 000113 000117 OOOllF 000127 00012F 000131 000138 00013C 000140 000148 000150 000158 00015A 000161 000165 000169 0001bA 000172 00011A 000182 00018A 000242 00024A 000252 00025A 000262 000000 00012 0200 BOA3 7000 000A5 00000 0000 000000F4 00000001 404040 404040 404040 40 C2C5E5C5036B40C2 03E4C56B40F64OC9 D5C3C87E FIF2F3C1C2C3F4 000001CA 000015CA CI05C1D3C56B40D9 C5C46B40F84OC9D5 C3C840C60609C1C5 C47E F2F3F4E7E8E9F7 00001F40 000125CO C603C lOse 7C 5&B40 F240C9D5C3C86B40 04C1C7D5C5E2C9E4 D47E F7F5F3C7C8D1F8 OOOOOOOC 00001EBO 7E 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 OAT A, NOGEN o 27 A 28 29 30 * 31 * 32 33 :0: 34 35 OUT 38 ERROR 41 MVC I NS ST LA 11,0 *,11 4,4 4, TOT AL 7,AFIRST 1,1 TABLE+X'7E' ,X' 01' DESC, BLANK o (60,7) ,T ABL E 8, ERROR 3,1 3,7 3,ONE OUT 3,MVCINS ACCT,lIl1 TEMP1,8(l) 4,TEMP1 4,TEMP2 QOH,TEMP2 TEMP1,12111 4,TEMPl 4,TEMP2 DOLL,TEMP2 4,TOTAL 4,TOTAL 7, 16 I II * * * *B AGAIN EOJ EOJ MVC DESCIO) ,0(7) 42 43 44 45 46 47 48 49 50 51 52 53 54 55 DC DC OS DC OS DC OS DC DC DS OS OS OS DC C'BEVEL, BLUE, 6INCH=' 56 57 58 59 DC DC DC DC C'123ABC4' FL4'458' FL4'5578' C'ANGLE, RED, 8 INCH FORGED=' 60 61 62 63 DC DC DC DC C'234XYZ1' FL4'aooo' FL4'75200' C'fLANGE, 2 INCH, MAGNESIUM=' 64 65 66 67 68 TABLE DC DC DC DC DC C'753GHJa' FL4'12' FL4'7a56' C'=' 256X'00' 4 5 6 7 8 9 L 10 AGAIN 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 * 00006C 47FO BOlO PRINT START BALR US I NG SR ST AFIRST ONE ACCT QOH DOLL BLANK DESC TEMPI TEMP2 TOTAL RECORD SR MV I MVC TRT BC LR SR S BM EX MVC MVC L CVD UNPK MVC L CVD UNPK CLEAR REGISTER 4 TO ZEROS PUT ZEROS IN TOTAL PUT ADDRESS OF 1ST RECORD IN REG 7 CL EAR REGI STER 1 PUT 1 IN EQ SIGN POSITION OF TABLE START OF RECORD LOOP LOOK FOR SENTINEL NO DELIMITER FOUND IN 60 CHARACTERS COMPUTE LENGTH CODE OF DESCRIPTION BRANCH IF EQ SIGN I S 1ST CHARACTER MOVE DESCRIPTION FOR PRINTING MOVE ACCOUNT NUMBER MOVE QOH TO TEMPORARY STORAGE AREA QOH TO REGISTER FOR PROCESSING CONVERT TO DECIMAL UNPACK AND MOVE FOR PRINTING SAME PROCESSING FOR DOLLARS ADD DOLLARS TO TOTAL PUT ADDRESS OF NEXT RECORD IN REG 7 PRINT ROUTINE WLD BE INCLUDED HERE GO BACK FOR NEXT RECORD NORMAL END OF JOB ERROR TERMINATION EXECUTE INSTR ADDS LENGTH FROM REG 3 AIRECORDI F'l ' CL7 CL3' , CL8 CL3' , CL8 CL3' , C' , CL60 F o F - 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000 69 END BEGIN Figure 7-19. A program to prepare for printing a series of variable-length blocked records, each consisting of four fields. Total dollar sales are computed at the same time. Edit, Translate, and Execute Instructions 103 This completes the actions needed to make our first line of information ready for printing, and we would normally include a printer output routine at this point. There may still be another record in the block, so we branch back to AGAIN to see whether there is. During execution, the program will continue to go through the loop each time there is another record. After the fmal record, the equal sign delimiter that follows it will produce a result of -1 for the length-code computation, and this will cause the program to branch (on the Branch on Minus instruction) to our EOJ macro at OUT. 104 The sample block that appears at RECORD involves a little bit of trickery. One of the essential aspects of the assignment is that the binary fields appear in the block not aligned on word boundaries. In real life such a block would have been set up by a previous program. Here, in attempting to set it up with DC entries, we run into the automatic boundary alignment that is normally performed on fullwords. This action can be overridden~ however, by specifying a length modifier. A length of 4 is, of course, the same as the implied length of a fullword; the whole purpose is to prevent boundary alignment. QUESTIONS AND EXERCISES For questions 1-6, show the contents of WORK after the execution of ED WORK,SOURCE. The characters in WORK have the following meanings: Character Meaning B S D C R Blank Significance starter Digit selector Comma Decimal C C3 R D9 * * F Field separator Hexadecimal Equivalent 40 21 20 6B 4B C3 D9 5C 22 BDDDDDDD 1. WORK SOURCE 0001540+ BDDDDDDDCR 2. WORK SOURCE 0005721+ 3. WORK BDD,DDSDDBCR SOURCE 0000014. WORK BDDD,DDCR SOURCE 00000+ 5. WORK BSD,DDD.DDCR SOURCE 0000010+ 6. WORK BDD,DDS.DDCRFDD,DDS.DDBCR SOURCE 0010143-00001077a. Write a DC named PATRN to set up the editing pattern for a 9-digit amount to be printed as follows: BX,XXX,xxx.XXBBB (for a positive amount) BX,XXX,xxX.XXBCR (for a negative amount) Insignificant zeros should print as blanks. However, amounts less than one dollar must be punctuated with a decimal point. b. If SOURCE contains 009250001-and we execute ED PATRN,sOURCE, what would PATRN then contain? c. What would PATRN contain if EDMK instead of ED were the operation? 8. PATRN DC X'4020206B2020214B20204OC3D9' EDMK PATRN,SOURCE Assume SOURCE contains 0123456-. Choose the address that would be in bits 8-31 of general register 1 after execution of the EDMK instruction: a. PATRN b. PATRN+1 c. PATRN+2 d. PATRN+3 9. Does the ED instruction affect general register I? 10. What would be in location AREA as a result of the following operations? AREA TABLE DC DC TR X' 00020103' C'ABCD' AREA,T ABLE a. ABCD b. DBCA c. DCBA d. ADBC e. ACBD 11. What would be in general registers 1 and 2 as a result of the following operations: AREA TABLE DC X' 00010203' DC X' 00000100' TRT AREA,TABLE a. Address of AREA+3 and X'03' respectively b. Address ofTABLE+2 and X'OI' respectively c. Address ofTABLE+3 and X'04' respectively d. Address of AREA+2 and X'Ol' respectively 12. Assume the following sequence: CONI DC F'10' WORK -DC CL16'1234567899123456' AREA DS CL20 L 2,CON1 MVI AREA,C' 0' MVC AREA+1(19),AREA EX 2,MOVE B ROU2 MOVE MVC AREA(O),WORK What will AREA contain after the instruction B ROU2 is executed? 13. What would AREA contain if the EX instruction were EXO,MOVE? Edit, Translate, and Execute Instructions 105 Chapter 8: Subroutine Linkages and Prosram Relocation Subroutines are an important element in programming. Storage space is conserved when a subroutine at one storage location is branched to from many points in a main section instead of being inserted each time it is needed. Programming, compilation, and debugging time are conserved when an existing subroutine can be incorporated into a new program. A subroutine is a set of instructions that performs a particular function. It may be used in more than one program or more than once within a .single program. Subroutines have been used in scientific programming for many years. Common subroutines used are the sine, cosine, and square root functions. Subroutines have now become equally important in commercial programming. In many cases, a main program may be little more than a sequence of branches to subroutines, some of which may be used many times, some only once. When a long and involved program is to be written, it is frequently divided into a number of separate subroutines to be written by different programmers. After the general plan is determined, each part may be relatively simple to program, and a considerable saving of time can be achieved. Each section can be assembled and debugged independently. Subroutines may be classified as either "open" or "closed". An open subroutine is included each time it is required in the main program. The open subroutine is not normally branched to but is inserted into the main program and as such has little or no difficulty communicating with the main program. The closed subroutine, which is the kind we shall investigate in this chapter, is included once in a program and in storage no matter how many times it is branched to. Since the subroutine may be entered from many points in the main program, communication of data to the subroutine and of results back to the main program can be a problem unless standards are set. In this chapter we shall be concerned primarily with the standards that have already been established for subroutine communication. By demonstrating the techniques in actual program examples, we shall answer questions like: How does the subroutine know where to return in the main program? How does the main program pass data to the subroutine? How does the subroutine pass results back to the main program? How can one program reference areas in another program that the assembler does not know about? Much of the above is accomplished through register addressing. For the rest we will look to functions of the assembler and the linkage editor. Subroutine Linkages and Program Relocation 107 SUBROUTINE LINKAGES The basic idea of a subroutine is to put it in storage at one place, then branch to it whenever its function is needed. If we are using a square root subroutine, for instance, we put it in one section of storage available for use as needed. Then, at any point in the main program that we need to take a square root, we branch to the square root subroutine, compute the square root, and branch back to the point in the main program where we left off. This. raises two questions: How does the subroutine know where to return when its work is finished? How does the main program provide the subroutine with information on the location of the number to be processed and where the result is to be left? The question of where to return is answered by a linkage that places in a register the address of the next instruction after the one that branches to the subroutine. In System/ 360 we do this with the Branch and Link Register (BALR) instruction that we have seen so frequently for loading a base register. But now we specify a second operand other than zero, so that it really is a branch. The technique is to place in a register, usually 15, the address of the fust instruction of the subroutine. Then, if we have chosen register 14 to hold the link, we write the instruction BALR 14,15. When executed, this instruction places in register 14 the address of the next byte after the BALR, and causes a branch to the address in register 15. At the end of· the subroutine it is merely necessary to specify an unconditional branch to the address in register 14. This is done with a Branch Register Unconditional (extended mnemonic BR). We can make these ideas much more clear by considering LaC 000000 000000 000002 000002 000006 oeOOOA oeoooc 000010 000014 OCOO18 0~001A OBJECT COCE ADDRI ACCR2 05BO 5830 58FO 05Ef 5030 5830 SBFO 05EF 5030 B022 BOlE oe024 OC020 B02A B026 BOLE CCG2C OC028 CC020 B02E 00030 STMT 000020 00000034 oe0024 000eOO01 OC0028 00000004 00002C OC0030 OC0034 05AO OC0036 000036 8B30 0001 0OOO3A 07fE 000000 ceeOl SOURCE STATEMENT 1 2 L1NK1 3 BEGIN 4 5 6 7 8 9 10 II 12 13 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 an example. It is not our purpose now to explore new ideas in information processing, so we chose an unrealistically simple job for the subroutine to do: to double a number by shifting it left one place. Communicating data and the location of results between the main routine and the subroutine is handled easily by placing the number to be doubled in a register, in this case register 3, before the branch to the subroutine, and leaving the doubled result in register 3 on the return to the main program. Figure 8-1 is a listing of a single program consisting of a main, or calling, routine and a subroutine. The START, BALR, and USING instructions in Figure 8-1 are still necessary; they are unchanged by the fact that a subroutine will be used. Next comes the fust processing instruction of the main routine, to load register 3 with a number that is to be doubled by the subroutine. Register 15 is then loaded with the address of the subroutine, using an address constant, in preparation for branching to the subroutine with the BALR. Address constants, a subject we have not so far encountered, provide a means of communicating between separate parts of a program or between separately assembled programs. We could have used other means in this single assembly, but since address constants will appear throughout the rest of this chapter, they are well worth some study. An address constant (adcon for short) is a storage address that is translated into a constant. Unlike other types of DC's, it is enclosed in parentheses. We are particularly interested here in two types of address constants: A and V. An A-type address constant may be absolute (its value does not change upon program relocation) or it may be PRINT STAR T BALR USING L L BALR ST L L BALR ST EOJ DC OC DC OS OS NOGEN 0 1l.0 *.11 3.FIRST 15.ADSRI 14,15 3,ANSl 3.SECOND 15.ADSRl 14,15 3.ANS2 FIRST NUMBER TO BE DOUBLEC SUBROUTINE ACDRESS LINKAGE RETURN ADDRESS GOES INTO 14 RETURN POINT fROM SUBROUTINE SECOND NUMBER TO BE DOUBLED SUBROUTI~E ACDRESS AGAIN LI NKAGE STORE SECOND RESULT END OF JOB SUBROUTINE ACDRESS ADSRI AISR11 fiRST F'l' SECOND F'4' ANSI F ANS2 F * TH IS IS THE END OF THE ~AIN PROGRAM * THE SUBROUTINE MAY USE ITS OWN BASE REGISTER * W~ICH ~UST BE LOADED AND IDENTIFIED * * SR 1 BALR 10.0 USING *,10 SLA THIS IS THE CNLY PROCESSING INSTRUCTION 3.1 BR 14 UNCONDITIONAL BRANCH TO MAIN ROUTINE END BEGI" Figure 8-1. Listing of a single program that consists of a main, or calling, routine and a subroutine. Standard linkage registers are used. 108 relocatable. The storage address is calculated by the assembler and is stored in binary integer form. If no length is specified, it is stored as a fullword, aligned to a fullword boundary. We note that in statement 16 the object code for the DC named ADSRI is 00000034. The operand is A(SRl); the A stands for address and the SRI in parentheses is the same as the label of the flIst machine instruction in the subroutine, which is at location 000034. A V-type address constant, which we shall see later, is similar to the A-type, but it must be relocatable. It is used to reserve storage for the address of a symbol that is defined in a program or program segment external to the program it appears in. During assembly the V-type constant is given a zero value, and it is placed in the assembler's external symbol dictionary, to be resolved later by the linkage editor. The BALR as written in statement 11 takes its branch address from register IS and places in register 14 the address of the next instruction. The Branch and Link (BAL) instruction can sometimes be used instead of BALR, thereby avoiding the loading of a register before branching. The restriction is that the address of the subroutine must be within the range of addresses of the current program base register. This will not always be true, and will never be true for separately assembled routines, as we shall discuss later. BALR is probably a good habit even when not strictly needed. We have now branched to the subroutine, which in this highly simplified example consists of just one processing instruction. The contents of register 3 are shifted left one LaC OCOOOO 000000 000002 000002 000006 0000011 oooooe 000010 000014 000018 OOOOIC OCOOlE 000022 00002A 00002e 000030 OCOO34 OCOOl8 00003C 000040 000042 000042 000046 0000411 OCOOOO OeJECT CODE ADDRI ADOR2 05BO 5830 58FO OSH 41FO 5030 5830 58FO 05EF 41FO 5030 B02E B02A C0030 CC02C B026 B036 B032 B02A 00028 OCC38 C0034 0002C B026 B03A CCC28 oeC3C 0000 00000040 COOOOOI0 1FFFFFFF 05AO 8B30 OCOI 4710 EOOO 47FO E004 00001 cecoo OCC04 STMT 00000001 00000004 00000002 00000008 Figure 8-2. Values of FIRST, SECOND, ANSl, and ANS2, respectively, after execution of program in Figure 8-1 In Figure 8-3 we add a feature to the program. Shifting a number left can, of course, result in loss of a bit from large numbers. Let us arrange things so that such a loss would be Signaled back to the main routine as an error. Our method of signaling may be as follows. If such a loss of information occurs, the subroutine would return to the instruction after SOURCE STATEMENT 1 2 UI'.K2 3 BEGIN 4 5 6 1 8 9 10 11 12 13 14 15 18 ERROR 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 place, which doubles the number, and the processing is fmished. We are now ready to return to the instruction following the BALR in the main routine. This address is precisely what is in register 14 now, so an unconditional branch to the address specified in register 14 is the correct return. The BR instruction is unconditional. .On returning to the main routine, we store the doubled number at ANS 1 and proceed to load another number into register 3 for doubling by the subroutine. We again go through the operations of loading register IS with the address of the subroutine and linking to it. Although it is true that register 15 still has the address of the subroutine in it from the last time, we prefer, even in this example, to load it again as a matter of good programming habit. Figure 8-2 shows the values of FIRST, SECOND, ANS 1, and ANS2, in that order, after the execution of the program in Figure 8-1. ACSRI FIRST SECOND ANSI ANS2 * * * * *SR 1 PRINT START BAlR L:SING L L BALR B ST L L BALR B ST EOJ E OJ NOGEN 0 11 ,0 *,11 3,FIRST 15,ADSRl 14,15 ERROR 3,ANSI 3,SECOND 15,ADSRI 14,15 ERRCR 3,ANS2 DC DC DC OS AI SR 1) F'16' X'7FFFFFFF' F F os FIRST NUMBER TO BE DOUBLED SUBROUTINE ADDRESS LINKAGE-RETURN ADDRESS ERROR RETURN RETUR~ POINT FROM SUBROUTINE SECOND NUMBER TO BE eOUBLED SUBROUTINE ADDRESS AGAIN LINKAGE ERROR RETURN STORE SECOND RESULT PROGRAM TERMINATION ERROR PROGRAM TERMINATION THI S ( S THE END OF THE ~AIN PROGRAM THE SUBROUTINE ~AY USE ITS OWN BASE REGISTER wHICh MUST BE LOADED At-.D IDENTIFIED BUR LlSING SLA Be B END 10,0 *,lC 3,1 010,14 ) 410,14) BEGIN THIS IS THE ONLY PROCESSING INSTRUCTION GO TO ERROR RETURN UNCONClTlONAl BRANCH TO MAIN PROGRAM Figure 8-3. The program of Figure 8-1 modified to give the subroutine a choice between two return points Subroutine Linkages and Program Relocation 109 the BALR; if there is no loss of information, the subroutine would return to an instruction that is four bytes after the BALR. In other words, the instruction after the BALR would be executed only in the error condition, and it would be cali~d the e"or return. The normal return would branch back heyol1u this point. We shall insert in the program, following each BALR to the subroutine, a branch to a routine labeled ERROR to discontinue the program if the error arises. (In practical applkatilltis, of course, we would take some corrective a\'lioli rather than give up completely.) The choice of whether to go back to the error return or the normal return will he made by the subroutine. Figure 8-3 shows the modificati\)fis required. After shifting left, we execute a Branch un Overflow (80, instruction that tests the condition code set by the Shift Left Single (SLA) instruction. If we have overnowcd, the branch is taken and the error return is 110 reached. If we have not overflowed, we go back to the normal return, which is four bytes beyond the address in register 14. This is done with a Branch Unconditional instruction (extended mnemonic B) that uses register 14 for a base register and has a displacement of 4. Figure 8-4 shows the information at the end of execution of the program, with the values for FIRST, SECOND, and ANS 1. A doubled value for SECOND has not been stored, since the error return was taken and the instruction (ST 3, ANS2) was never reached. 00000010 7FFFFFFF 00000020 Figure 8-4. Values of FIRST, SECOND, and ANSI, respectively, after execution of the program in Figure 8-3. ANS2 was not stored because the error return was taken during the doubling of SECOND. STANDARD LINKAGE REGISTERS So far we have seen a typical subroutine linkage in action, with a variation that allows a choice between two return points. Communication of data and results between the main program and the subroutine was made easily because the programmer knew which registers were used for what purpose in both. Supposing they had been written by different programmers? To ease the problem of assuring proper communications between program segments, which often are written by different programmers, standard register assignments and techniques have been defmed in each of the IBM operating or programming support systems. (They are similar, but not identical, in all System/360 operating systems.) Standard register assignments in the Disk Operating System (DOS) are shown in Table 8-1. The items in this chart will be explained in the following pages of text, as their use is discussed. These registers are used for the purposes shown, both by programmers and by the DOS macros. The DOS macros for subroutine linking are CALL, SAVE, and RETURN. Table 8-1. DOS Linkage Registers Register Number Register Function Contents 0 Parameter register Parameters to be passed to the su brou tine. 1 Parameter register Parameters to be passed to the su brou tine. or Parameter list register Address of a parameter list to be passed to either the control pnr gram or a user's subroutine. 13 Save area register Address of the register save area to be used by the subroutine. 14 Return register Address of the location in the main program to which control should be returned after execution of the subroutine. 15 Entry point register Address of the entry point in the subroutine. Let us examine the standard linkage registers. We have been using register 14 for the return register and register 15 for the entry point register. Data may be passed to a subroutine using registers 0 and 1. However, a more common practice is to use register 1 to hold the address of a parameter list, because we usually have more data than will fit in two registers. (The expression "parameter list" merely signifies a list of numbers of any desired value.) The parameter list may consist of either data or the addresses of data. Addresses are used more often so that data of varying lengths can be handled easily. One common technique is to write the data and/or data addresses in the instruction stream immediately following the BALR. This is a point from which the subroutine can readily obtain them. FigUre 8-5 illustrates the use of most of these techniques and includes a subroutine that averages a seri~s of numbers. The main program stores two series of numbers as lists in consecutive fullword locations. The average of each list is to be calculated and stored, so the subroutine will be used twice and each time will return to different points in the main program. To simplify the averaging routine, each list begins with the total number of entries in the list. Each list is identified by its starting address. This address is to be communicated to the subroutine, along with the address at which the subroutine is to store the average. There are several possible ways to give the necessary information to the subroutine. We chose one that is representative, using BALR and A-type address constants: BALR DC DC 1,15 A(LISTl) A (AVER 1) Link to subroutine Address of first parameter list Address of second parameter list The address of the first word of the list and the address at which the average should be stored will immediately follow the BALR that branches to the subroutine. The subroutine will be required to pick up the information it needs from this parameter list. It can find it because it will have the address of the first word after the BALR in register 1, loaded there by the BALR. Of course, prior to this we had to load the address of our subroutine entry point into register 15. This was done by use of the Load Address (LA) instruction. In addition the return address was loaded into register 14. The return address must be carefully calculated to assure that the proper return point is stored. This address is the current value of the assembler's location counter at the start of this instruction· plus the length of the LA (4 bytes) plus the length of the BALR (2 bytes) plus the length of the two address constants (8 bytes). This could also have been accomplished by labeling the return point and using that in a Load Address instruction. In any case, after we branch to the subroutine, register 1 contains the address of the fIrst DC in the parameter list. What about register 13, and what is a "save area"? Usually the subroutine will need to use the same registers that are used in the main program, but for different purposes. The main program may use the registers for base addresses, index addresses, intermediate results, or other data vital to the main program. To keep this data from being destroyed by the subroutine it has become conventional to store the contents of these registers in an area called a save area and defined by the main program. This area is 18 words in length in most System/360 systems and its address is stored in register 13 prior to branching to the subroutine. It is aligned on a doubleword boundary. In a standard save area, word 1 is used only by PL/I. Words 2 and 3 are used to trace subroutines that are branched to by Subroutine Linkages and Program Relocation 111 LOC OBJECT CODE AooR 1 ACDR2 000000 OCOOOO 05BO 00on02 000002 000006 000006 OOOOOA OOOOOE 000010 000014 000018 OOOOlC 000020 000024 OC0028 00002A 0OO02E 000032 000034 000038 4100 B086 C0088 4lFO BOCE 41EO B016 05lF 0OOOO04C 0000007C 5860 B03E 51\60 B042 5060 B046 41CO B086 0700 41FO BOCE 41EO B03A 05lF 00000060 00000080 00000 CC018 C0040 00044 00048 C0088 00000 0003C STMT SOURCE STA TEMENT 1 2 L1NK3 3 BEGIN 4 5 6 7 8 9 10 11 12 13 14 15 16 11 18 19 20 21 22 00003E 000040 000044 000048 00004C 000050 000054 000058 00005C 000060 000064 000068 00006C 000070 000074 000078 00007C 000080 000088 0000C4 000006 000006 OOOOOA OCOODE OUOOE2 OCOOE4 UOOOE8 OOOOEA OOOOH OOOOFO OOOOF2 OOOOF6 OOOOFA OOOOFe oeOl00 0000 00000038 00000040 25 26 27 28 29 30 31 32 00000004 OOOOOOOA OOOOOOOC 00000013 OOOOOOOF 00000006 OOOOOOOB 00000002 00000004 FFFFFFFD 00000005 FFFFFFFF 33 34 35 36 37 0590 5851 4160 5845 1874 8H70 11\15 5BI0 1822 lBB 5A35 8756 1024 5851 5035 0000 0004 0000 ooeoo 00e04 cocoa oe02 COO02 903A 00110 0004 901C 00e04 COOF2 0004 0000 00004 OCCOO 000000 000110 00000001 38 39 40 41 42 43 44 45 46 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 69 70 PRINT SlART BALR USING LA CNOP LA LA BALR DC DC L A ST LA CNOP LA LA BALR DC DC EOJ A B C L1STl NOGEN 0 11.0 * ,11 13.SAVEAREA 2.4 lS.AVER 14.*+14 1,15 A(LISTlI A(AIIERll 6.A 6.B 6,C 13.SAVEAREA 2,4 15,AVER 14,*+14 1,15 AILlST2) AIAVER2) DC F'56' DC F'77' OS F DC F'4' DC F'lO' DC F'12' DC F'19' DC F' 15' LI ST2 DC F'6' DC F'11' DC F'2' DC F' 4' DC F'-3' DC F' 5' DC F '-I' AVER1 OS F F AVER2 OS SAVEAREA OS 90 * THE EI'.D OF THE MAIN PRCGRAM * * ( 14,12) AVER SAVE BALR 9,0 USING *.9 5.0(1) L LA 6.4 4,0(5) L LR 7.4 5LA 7.2 AR 1.5 S 7.=F'1' SR 2.2 SR 3,3 3,4(5) A LOOP BXLE S.6,LCOP OR 2.4 L 5,4111 5T 3.0(5) RETURN (14.12) END BEGIN =F'l' ADDRESS OF SAVEAREA CONDITIONAL No-oP FOR ALIGNMENT BRANCH ADDRESS RETURN ADDRESS LINK TO SUBROUTINE ADDRESS OF FIRST PARAMETER LI ST ACCRESS OF RESULT eTHER PROCESSING X X ADDRESS OF SAVE AREA CONDITICNAL ND-OP FOR ALIGNMENT RETURN ADDRESS LINK TO SUBROUTINE ADDRESS OF SECOND PARAMETER LIST ADDRESS OF RESULT PROGRAM TERMINATION NUMBER OF ENTRIES IN LIST 1 NUMBER OF ENTRIES IN LIST 2 SAVE REGISTERS STARTING ADDRESS INCREMENT NUMBER OF ENTRIES NUMBER CF ENTRIES FOUR TIMES NUMBER OF ENTRIES LIMIT REDUCE BY 1 SO LOOP WILL NOT REPEAT CLEAR TO ZERO CLEAR TO ZERO ADD A VALUE FROM THE LIST DIVIDE BY NUMBER OF TERMS PICK UP ADDRESS OF RESULT STORE RESULT RETURN TO THE MAIN PROGRAM Figure 8-5. A program with a subroutine that averages a series of numbers. The subroutine will be used twice and will store the results at AVERI and AVER2. subroutines. Word 2 is the save area address of a preceding routine. Word 3 is the save area address of a succeeding routine. Words 4-18 are the contents of registers 14-12, respectively-that is, all the general registers except register 13. Note that early in the main program after the usual preliminaries, we load register 13 with the address of the save area. Then follows the CNOP (which we will discuss later on), and the loading of the entry point and return addresses. In the subroutine we begin by saving the contents 112 of the registers in the save area. This is accomplished through the use of the SAVE macro in DOS, or by regular assembler language Store instructions in systems where this macro is not available. Register 14 is specified as the first register to be stored, and all additional registers are stored simply by specifying the last register to be stored. Thus the registers are stored in the order 14, 15, then 0 through 12. The instruction generated by the SAVE macro is STM 14, 12, 12( 13). Therefore the registers are stored in the save area (its address is designated by the contents of register 13) starting at a point that is twelve bytes past the beginning of the area (we recall that the fIrst three words of the save area are reserved for other data). Now the subroutine can use the registers for its own purposes and, when its processing is fmished, can restore the registers to their status when the subroutine was entered. This entire procedure is normal practice; it can almost never be assumed that any registers are available to the subroutine unless their contents are first saved. Let's proceed with the work performed by the subroutine. Statement 51 gets the address of LIST 1 by a Load instruction in which the effective address is simply the contents of register 1. After execution of this LOAD, the address in register 1 (OOOO4C) is placed in register 5 for subsequent use. Stepping through the list will be done with a Branch on Index Low or Equal instruction (BXLE), so we proceed to set up the other parameters required. Register 6 is accordingly loaded with a 4, the increment between locations that must be added on each repetition of the loop. With register 6 containing the increment, register 7 must contain the fInal address, that is, the starting address plus four times the number of entries. We load register 4 with the number of entries, which is the first fullword in each list. It is to be left in register 4 for computing the average later. For purposes of controlling the loop, we move it to register 7, shift left two places (in effect multiplying by four), and add the starting address. Since the BXLE will repeat the loop on an equal, we must now reduce the value in register 7 by 1, using the literal term =F'I' to introduce the value one. After clearing registers 2 and 3, we are ready to go into the loop. A literal term in assembler language is a way of introducing data into a program in a machine instruction, and is (in that one sense only) like immediate data in an SI instruction. It is simply a constant preceded by an equal sign. It represents data rather than a reference to data. It can be used to enter a number for calculation, an address constant, or words or phrases for printing out a message. Unlike immediate data, a literal term is not assembled into the instruction in which it appears. The assembler generates the values of all literals in a program, collects them, and stores them in a "pool," usually at the end of the program. Their addresses, rather than their values, are assembled into the instructions in which they are used, and so literals are considered relocatable. The Add instruction at LOOP uses as its address the contents of register 5, which is the index register for the loop. The address in register 5 is adjusted by 4 so that we will address the fIrst number to be averaged rather than the number of entries. Between loop repetitions, register 5 is incremented by the contents of register 6, which we set at 4. Each time register 5 is lower than, or equal to, register 7 on comparison, we will branch back to LOOP. The looping stops when all entries in the list have been added to register 3. To compute the average, which is simply a matter of dividing the contents of registers 2 and 3 (the sum of all the numbers in the list) by the contents of register 4 (the number of entries in the list), a Divide Register instruction (DR) is used. The quotient is the average, which is in register 3. We are now ready to store the average; where does it go? The answer is to be found by looking at the fullword address that is four bytes beyond the address specified by the contents of register 1; the address of the average is placed in register 5 by the Load. A Store instruction using this address now completes the work of the subroutine. By use of the RETURN macro, we restore the registers that had been saved and branch back to the main routine. The RETURN macro is coded identically to the SAVE macro, that is, the operands are the starting and ending registers to be restored, RETURN 14,12. It restores the saved data to the specified registers and returns to the main program via the return address in register 14. The coding generated by the RETURN macro is a Load Multiple (LM) instruction and an unconditional branch. Back in the main routine, we do some simple processing using register 6. We can use the same registers as we used in the subroutine because the SAVE and RETURN macros allow each section of programming to view the registers as their own. We then wish to average another list, LIST2. At statement 15, note that we must again place the address of the save area in register 13 because we must enter the subroutine again. The execution of the subroutine follows the same lines as before, although this time it operates on different data and places the result in a different place. The CNOP assembler instruction, which we have not discussed so far, appears twice in the main program during the preparations for each branch to the subroutine. The function of the CNOP (Conditional No..Qperation) is to make sure that the two address constants appear in storage immediately after the BALR. If the instruction before the BALR ended on a fullword boundary, the BALR (a two-byte instruction) would then occupy the first two bytes of the next word. The assembler, automatically aligning on a fullword boundary an A-type constant for which no length is specifIed, would skip two bytes before locating the constant. Then, when the BALR is executed, register 1 would contain the address of the byte following the BALR instruction, but this address would not be correct for the parameter list. Before reaching the BALR we write the instruction CNOP 2,4. If the assembler's location counter is already set to a value that is two bytes greater than a fullword boundary, the CNOP is ignored, as is the case in statement 6. If not, as is the case in statement 16, the assembler inserts a Branch on Condition (BC) instruction with a mask of zero, which never causes a branch and therefore is equivalent to a "NoOperation". It occupies two bytes and thus causes the para- Subroutine Linkages and Program Relocation 113 meter list to be located immediately following the BALR. The CNOP in statement 6 has no effect; we see that the location counter is already located as described by the CNOP (that is, it is in the second half of a fullword000006). Therefore, no instruction is generated for the CNOP. Note that the DC will be on a fullword boundary without skipping any space between the BALR and the address constant. The CNOP'in statement 16 resulted in the creation of a No-Operation instruction because the assembler's location counter is at 000028, which is not two bytes beyond a fullword. If this had not been done, the assembler would have placed the BALR at 000030 and the A-type constant at 000034, thereby leaving a two-byte gap. The next byte after the BALR (000032) would go into register 1. The subroutine's ensuing attempt to call for a fullword from 114 000032 would have caused a specification exception and a program interruption. The CNOP could be used before or after the two LA instructions that load the return and entry point addresses into registers 14 and 15. We have used the CNOP ahead of the two LA instructions in this program. Figure 8-6 shows the two lists of num bers followed by the average of each, as computed by the program in Figure 8-5. 10 12 19 15 11 2 4 -3 14 5 -1 3 Figure 8-6. The data and results of the program in Figure 8-6. The last number in each line is the average. PROGRAM RELOCATION In an operating system environment, a program must be processed by the linkage editor program before it can be executed. During the process it is usually relocated, that is, assigned a starting location in main storage other than the locations assigned during assembly. Most programs are run more than once. A standard subroutine may be stored in a part of the system library in relocatable form and be used very frequently. A different core location may be assigned each time. We know that the storage location indicated by the START assembler instruction is tentative. The locations calculated by the assembler merely establish the relative storage locations of data and instructions within a program. Most programs are assembled relative to zero, but are never executed there because of restrictions on the use of lower core. System/360 was designed to run under a control program, and, under operating conditions, part of the control program is always resident in the low region of main storage for handling interruptions, error recovery routines, etc. In many systems this occupies several thousand bytes. Problem programs must be executed beyond this area. Relocation is necessary for a number of other reasons not of direct interest to the programmer. These are (1) the overall storage requirements of other programs that are to go into core at the same time and (2) various operating considerations dependent upon the type of installation, operating system environment, etc. The Linkage Editor When the capabilities of the linkage editor are added to program relocatability, great programming efficiency can be achieved by dividing a large program into separate sections for coding. Each section can be written by a different programmer and compiled and checked out separately. It is even possible to code some of the subroutines in a different programming language. Each part of the programming operation is greatly simplified. Time is saved by having several people work independently and simultaneously on the program. When all the routines have been compiled, they are in relocatable form and can be link edited in any sequence. The linkage editor will assign storage locations and match up all address references between the routines, so that the entire program can be executed correctly as one program. If it should be necessary to correct a routine, only that one routine would have to be reassembled or recompiled and then link edited again with the other routines. This facility also makes it relatively simple to maintain a large program that may have to be updated from time to time. The output of the assembler (or any language translator) is called an object module. It may consist of a single program or many. We should perhaps use the more exact term control section. A control section is the smallest separately relocatable unit of a program. It is an entity declared as such by the programmer by use of the START statement or another assembler instruction called CSECT. A program may consist of one control section or many control sections. In an operating system environment, an object module has two major characteristics: (1) It is relocatable. This means that a11 address constants are in a form that can be modified to compensate for a change in the starting location. (2) It is not executable. The object module may call for other object modules assembled at other times and stored in the system Hbrary in relocatable form. Programmers frequently indicate standard I/O or other object modules to be included as subroutines in a new program. This is perfectly feasible. The linkage editor, which is a service program, will find all required modules, process one after the other, and combine them into a single, executable load module (or program phase, as it is called in some systems). In Chapter 1 we described the output of the assembler program. The major items of input to the linkage editor program are the object code (or text), the external symbol dictionary (ESD) , and the relocation dictionary (RLD). (Linkage editor control cards are also included, but wfl1 not be covered in this book.) The text consists of the actual instructions and data fields of one or more control sections in the module. The dictionaries contain the information necessary for the linkage editor to resolve references between different modules. F or each control section in an assembly, the ESD contains its length in bytes, assembled address, and any name given in the START or CSECT statement. Also included is information about any V-type address constants, external references (a linkage symbol used in this control section but defmed in another), and entry points (a linkage symbol defmed in this control section but used in another). The relocation dictionary contains all address constants that appear in an assembly. RLD information includes (1) the control section in which the adcon is used as an operand, (2) the control section in which it is defmed, (3) whether it is a V-type or other type, (4) how long it is, and (5) the assembled address where it is stored. The linkage editor, working under control of the job control program, builds up composite dictionaries of the ESD and RLD data found in the object modules. It resolves all linkages between different control sections as if they had been assembled as one object module. It relocates each control section as necessary and assigns the entire load module (or program phase) to a contiguous area of main storage. It adds the relocation factor to the location given by the assembler's location counter at the start of each assembly. I t modifies all relocatable address constants to contain the relocated value of their symbols. (Except for adcons, no address values within the instructions and data fields are Subroutine Linkages and Program Relocation 115 In the last program, Figure 8-5, statements 6 through 11 and 16 through 21 were necessary to link to the subroutine, communicate data both ways, and get back to the right point in the main program. We had to be very careful about boundary alignment and using the correct standard linkage registers for the right functions. In DOS all of this can be taken care of very simply by use of the CALL macro, which generates instructions similar to the six statements in the last program. In the program in Figure 8-7 CALL appears in statements 7 and 18. The CALL macro instruction was designed primarily for use with separately assembled programs to pass control from one program to a specified entry point in another. It works equally well within a single assembly, however, because the assembler and linkage editor carry out their functions just changed during link editing. These remain in base and displacement form, as we shall see shortly in the dumps of the next program.) The load module is constructed by building the text in the form in which it will actually be loaded into core; it is then executable and nomelocatable. The CALL and PDUMP Macros Perhaps it would help to clarify just what happens during program relocation if we could see our program in main storage while it is being executed. We can do nearly that by getting a "dump" of storage during execution. In this section we shall see how a program appears, first relocated to one area of storage and then to another. We shall use a program almost identical to the last one, a main program assembled with a subroutine. LOC OBJECT COCE ACDRl ACDR2 STMT SOURCE STATEMENT 1 2 3 LI NK4 4 BEGIN 5 oeoooo 000000 0560 000002 0(10002 4100 B08E 00090 000018 OOOOlC oe0020 000024 5BoO B040 5AbO B04A 50bO B04E 4100 B08E 00048 CC04C CCCSO CCC90 000048 oe004C 000050 000054 000058 OC005C 000060 000064 OC0068 00006C OC0070 000074 000078 OC007C OC0080 OC0084 OC0088 000090 0000003!! 00000040 OOOOCC OOOODE OOOODE 0000E2 0000E6 OCOOEA OOOOEC OOOOFO OCOOF2 OOOOF6 0000F8 OOOOFA OOOOH OCOI02 000104 000108 0590 5851 4160 5845 1874 8B70 lA75 5B70 IB22 IB33 5A35 8756 lC24 5851 5035 OCOOOO 000118 000120 oe0128 00012C 5B5BC2C7C4E4D4D7 000000C000000200 00000000 00000001 00000004 OOOOOOOA OOOOOOOC 00000013 OOOOOOOF 00000006 OOOOOOOB 00000002 00000004 FFFFFFFO 00000005 FFFFFFFF 0000 0004 0000 OCOOO C0004 0002 oecoz 904E 0012C 0004 901C 00C04 CCCFA 0004 0000 00C04 ceeoo cecoo PRINT ENTRy START BALR l.;SING LA CALL NOGEN AVER o 11,0 *,11 13,SAVEAREA ADCRESS OF SAVEAREA AVER,ILISTl,AVERl) LINK TO SUBROUTINE 6,A 14 OTHER PROCESSING L 6,B 15 X A )( 6,C 16 ST ADDRESS OF SAVEAREA 17 LA 13. SAVEAREA 18 CALL AVER,ILISTZ,AVERZ) LINK TO SUBROUTINE PDUMP BEGIN,BEGIN+X'ZOO' 25 DUMP ROUTINE PROGRAM TERMINATION 30 EOJ 33A DC F'S6' 34 B DC F'77' 35 C OS F 36 LlSTl DC NUMBER OF ENTRIES IN LIST 1 F'4' 37 DC F '10' DC 38 F'12' 39 DC F'19' 40 DC F'15' 41 LI S T2 NUMBER OF ENTRIES IN LIST 2 DC F'o' 42 DC F'll' 43 DC F'Z' 44 DC F'4' 45 DC F'-3' 46 DC F'S' 47 DC F '-I' 48 AVERI F CS 49 AVER2 F OS 50 SAVEAREA OS 90 51 * THE END OF THE MAIN PRCGRAM 52 * 53 • 54 AVER SAVE 114.12) SAVE REGISTERS 57 BALR 9,0 58 USING *,9 L S,O(1) S T ARTI NG ADDRESS S9 LA 6,4 60 INCREMENT 61 L 4,OIS) NUMBER OF ENTRIES LR 7,4 62 NUMBER OF ENTRIES SLA 7,Z 41NUMBER OF ENTRIES) 63 AR 7,5 64 LIMIT REDUCE BY 1 SO LOOP ~ILL NOT REPEAT 65 S 7,=F'l' SR Z,2 66 CLEAR TO ZERO SR 3,3 67 ClEAR TO ZERO A 3,4(5) 68 LOOP ADO A VALUE FROM THE LIST 69 BXLE 5,6,LCOP DR Z,4 70 DIVIDE BY NUMBER OF TERMS L S,4(1) PICK UP ADDRESS OF RESULT 71 ST 3,0(5) 72 STORE RESULT RETURN (14.12) RETURN TO THE MAIN PROGRAM 73 71 END BEGIN 78 =C L8' UBPDUPlP' 79 =AIBEGIN,BEGIN+X'ZOO') 80 =VIAVER) 81 =F'l' 6 7 Figure 8-7. A slightly different version of the program in Figure 8-5, modified by use of two macro instructions, CALL and PDUMP 116 the same. As our program example is organized, the macro requires (1) the symbolic address of the entry point (AVER) as operand, which generates a V-type address constant, and (2) the separate ENTRY AVER statement. Since this is not a typical example of the ENTRY assembler instruction, it would be preferable to wait for the next program example before discussing it. The addresses of the parameter lists also appear in the operand field of the CALL macro, in parentheses, and will be in register 1 (the parameter list register) when the called subroutine is entered. The first three literals that appear at the end of the assembly appeared in the instructions generated by the PDUMP and CALL macros. As it was written, the CALL generates a V-type address constant rather than the A-type. We note that zeros and not the address of AVER are assembled in statement 80. This address will be supplied later by the linkage editor. Our plan for the program shown assembled in Figure 8-7 is to have it link edited, and, using linkage editor control cards, have it loaded and executed first at storage location 300016 , and then at 400016 • Re-assembly is not necessary. The linkage editor can override these cards, but will accept the instructions if they' do not create a problem.' Normally the programmer is not involved in the question of where a program is to be loaded. The storage area from which a program executes is pro~er1y an ope~ational, not a programming, decision. The DOS PDUMP macro causes the system, during program execution, to print out a hexadecimal dump of all the registers and any particular area of storage we are interested in. Such a dump is often used for program checkout, and learning to read one is a worthwhile exercise. We decide we want to see the state of affairs in storage at just the point when all calculations have been executed and the results stored, so we insert the PDUMP just before the EOJ. It will be reached just once, after the second execution of the subroutine. The programmer must supply two address expressions in the operand field of the PDUMP to show the beginning arid end of the storage area wanted. One or both of the addresses may be given in registers, but, since we want a dump from two different core locations, we use symbolic addresses. BEGIN will give us the beginning of the program, and BEGIN+200 is more than enough space to take us through to the end, as we know from the previous assembly of a similar program. Execution of the PDUMP macro will make no difference in execution of the program; processing will continue with the next sequential instruction. (Some other types of dumps result in termination of the program.) Reading a Dump Figure 8-8 shows the entire dump that was printed out (this is done by a line printer) during the first execution of the program in Figure 8-7. After assembly the program was link edited, loaded at 3000, and executed almost to the end. The point at which we see the dump is imffiediately after execution of the PDUMP macro. The EOJmacro instruction has not yet been reached. A hexadecimal format is used because it represents the binary contents exactly. The contents of all the registers are shown at the top of the printout. General registers 0-7 are in the first line,and 8-15 in the second, a fulIword each. The double word floating-point registers 0, 2, 4"and 6 are in the third line. Next comes the hex dump of main storage. The Six-digit column at the left shows the storage location of the first byte in each line. There are 2016 (or 3210 ) bytes to a line, divided into fullwords. Locating an address is simplified by the wide space' in the center of each' line; the next byte after this space is simply hex 10 beyond, the location in the lefthand column. For example, in the line that begins .at 003020, the BO in the fifth full word is at location 003030. The last two bytes in this line are at 003031:: and 00303F. The entire storage area shown is from 003000 to 0031 FF , since the boundaries given in the PDUMP, macro were BEGIN' and BEGIN+ X'200'. Printing of repeated lines of zeros at the end is suppressed. At the extreme right, any alphameric characters in a line are also represented in characters. In many cases this is very helpful in interpreting the hexadeciinal material and speeds up analysis of a dump. It is not particularly useful in our progra!n, however, so the characters will be omitted ,to let us get a closer look at ~he hex. The reader. may wish to arm GR 0-7 GR 8-F FP REG 00003120 00003118 OCOOFFFF 00002800 00004142 OA0407F1 OCOC2810 40003002 4431F800 8F5C28F5 4431F800 8F5C28F5 COOOFf84 FFfFFf7C 00000085 00002798 CC003698 OC003090 0000303C 00003008 4752F1E8 6828F5C1 02000000 BOOOOOOO 003000 003020 003040 003060 003080 0030AO 0030CO 0030EO 003100 003120 003140 003lEO 05B041DO 5060804E 4100BI1E 00C00013 OC003054 BC3A051f CC000085 OC000002 OCOOOOOO CC002800 40C03002 lA755870 07FEOOOO OCOOOOOO F~FFFFFF B08E58FO 4100B08E OA020AOE OOOOOOOF OOOOOOOE 00003000 00004142 00045845 58510004 00003200 812641EO 070C58FO OCCCC038 000C0006 0000C003 60003034 OA04C7f1 00C01874 50350000 00003008 B016051F B12641EO 00000040 OOCCOCOB 00000000 OOOOFFFF 00002810 8B700C02 98ECOCOC OOOOOCOI 00003008 00002798 00004160 901C1D24 00003000 00000000 --SAME-~ 00000000 00000000 00000000 00000000 00003084 00003068 00000004 00000004 00000000 OOOOFF84 00003698 904E1822 00000000 00000000 58608046 5A60804A 000b3088 41108116 O~COOOA OOOOOOOC FFfFFFFO 00000005 00000000 0000303C FfFFFF7C 00000085 90ECOOOC 05905851 18335A35 00048756 5858C207 C4E4D4D7 00000000 00000000 ....•.. 0........ •...••••• -•••- •• &- ••••••••• 0.... . .............. . .•.............. ............•... ••• Q•••• - . . . . . . . ..•••...••. 1.... • •••••••••• 1 •••• . •••••••••••••• ••• - . . . . . . . . . . . . ..$ ••••••••••••• •••••••• G....... • •••••••••8POUMP ...•..••••• Q.... •••••••••••••••• OCOOOOOO 00000000 00000000 00000000 Figure 8-8. Hex dump of registers and storage produced by execution of the PDUMP macro in the program in Figure 8-7. At right, EBCDIC characters are represented by characters. Subroutine Linkages and Program Relocation 117 himself with the card IBM System/360 Reference Data (see Preface), which is helpful for reading a dump. Figures 8-9 and 8-10 show the dumps with the program loaded first at 3000 and then at 4000. Key areas have been labelled to help the reader tie the dump listing to the assembly listing. In Figure 8-9 at location 3000 is OSBO, which is the object code for BALR 11,0. Next is 41DO B08E, which is for the instruction LA 13,SAVEAREA (see assembly listing). Next is the first CALL. Next is S860 B046 for the instruction L 6,A. In this way, the entire program was TlALI? 11. 0 AVERt BASE t AVER2 40~~~g~~y! 00003120100003118 OCOOFFFF 00004142 OA0401F1 OCC02810 4431F800 8F5C28F5 4431F8CO 8F5C28F5 003000 003020 001040 003060 003080 0030AO 0030CO OOlOEO 003100 003120 003140 0031EO 41DO 5060B04E 4100BIIE 00000013 FFFFFFFF 00003008 00002798 00004160 901C1024 00003000 00000000 00000000 2 AllCOAlS =A (BEGIN) =A {8EOIN + X '200 V ATJT)RcSS OF SAVE AREA AOOR~SS GR 0-1 GR 8-F FP REG B08 F 4100B08E OA020AOE OOOOOOOF OOOOOOOE 00003000 00004142 00045845 58510004 00003200 --SAME-00000000 entered into storage and the registers, instruction by instruction and DC by DC, just as it appears in the object code. This dump was printed after execution, however, and therefore the storage areas and registers affected by the instructions have been altered in accordance with the operations they specified. Let's see what happened when execution of this program began. The linkage editor supplied the starting address 3000, and the program was loaded starting there. BALR was executed; it put the current address from the PSW (by now 000COC08 00000000 OOOOFFFF OA04C7F1 00002810 00C01814 8B700002 50350000 98ECOOOC 0000300 OOOOOCOI I A DCOIJ LlSTZ ~~~~~~~=--/srC4LL OC003054 0000 0 4 B03A051F 00003068 CC000085 00C04 CCC00002 00000004 (oeoocooo 00000000 CCC02800 0000FF84 FFFFFF7C 4CC03002 00003698 90ECoOOC lA755B70 904E1B22jlB335A35 01FEOOOO OOOOCOOO 5B5BC207 oeoecooo OOOOCOOO 00000000 00000000 00000000 \ = V (AV€~) CCOOFF8' CC003698 4752F1E8 AOORESS OF $ (JBRO(JTlNE I pl ST CALL follow these events fairly clearly in the dumps, remembering that each dump is produced at just one particular point during processing. 1. The calling program MAIN 1 began with execution of the BALR 11,0 that is at 3000, then LA, then the LOAD macro. 2. The LOAD caused the subroutine load module to be entered into core beginning at 4000, and control to be returned to MAIN 1. 3. Next, in MAIN 1, execution of the CALL macro (see Figure 8-5 for the actions included) branched to and turned control over to the called program SUBR. It also informed SUBR where to find the parameter list and where to place the final result. We note that the last two fullwords in the CALL macro, as shown in the dump in Figure 8-15, are the addresses of LIST 1 and AVER 1. 4. SUBR was executed once, beginning with the SAVE macro at 4000, which stored the existing contents of all the general registers in the save area beginning at location 3098. Every instruction in SUBR was executed in turn, including repetitions of the loop, through the PDUMP macro. The reader may wish here to go back to the discussion about the subroutine in Figure 8-5 for a detailed review of the processing included. 5. The dump in Figure 8-13 was produced at this point. We note that the averaging calculations in SUBR used registers 2, 3, 4, 5, 6, and 7, and that its base address was in register 9. All this is reflected in the contents of these registers in Figure 8-13. 6. The final instruction in SUBR, the RETURN macro, restored the original contents of the registers from the save area and returned control to MAINI at location 3020 (L 6,A in statement 14). 7. MAIN 1 then did its processing in register 6, and stored the result (85) at C (the fullword at 3058). It again loaded the save area address in register 13 and executed the second CALL. 8. Beginning at the same location as before (4000), SUBR was executed again in its entirety. The contents of the registers were stored, the registers used, and the contents restored in the same way; and then control was returned to MAINI. The dump in Figure 8-14 was produced before the registers were restored. 9. This time control was returned to MAINI at location 3044, where the PDUMP macro was immediately executed, producing Figure 8-15. Next came the EOJ at location 304E, and, with the Supervisor Call instruction (OAOE), control went back to the control program. The remaining coding in the dump in Figure 8-15 is not executable, but consists of the constants and storage areas we set up in the original program and also those generated by the various macros. We note that the last item (at 30F8) is a value of 4000 for =V(AVER), the address constant for AVER. This was assembled as 00000000. The value was supplied by the linkage editor. Two observations can be made from this review of the programs' execution. The first is that program ·linkage is closely related to the speCification of base registers for each program. Throughout execution, the base-plus-displacement addreSSing system continues to work efficiently on the basis of the values originally assigned by the assembler. Second, communication between programs is easily maintained as long as the data and addresses needed by each is in a known . location. When routines are written by different programmers and assembled separately, communication is simplified by use of standard linkage registers for specific functions. Although details differ in certain respects, the necessary linkages can be e~tablished similarly in all the operating systems by use of either regular assembler language instructions or macro instructions. Subroutine Linkages and Program Relocation 123 QUESTIONS AND EXERCISES 1a. What functions does BALR 14,15 perform? b. What functions does BAL 14,SUB perform? c. What instruction is used to return to the main program after either a. or b. above? 2. Match register numbers with their conventional usage. REGISTER 1 a. return address 13 b. address of subroutine entry 14 c. save area address 15 d. address of parameter list 3. List 5 operations that are performed by the CALL macro. 4. The CNOP updates the value in the instruction counter during the ftrst phase of the assembly process. If the counter is now at a value of 000402, what will it be after 124 each of the following: a. CNOP 0,8 b. CNOP 0,4 c. CNOP 4,8 d. CNOP 6,8 e. CNOP 2,8 f. CNOP 2,4 Sa. What is generated by a SAVE (14, 12)? b. What is generated by a RETURN (14,12)? 6a. When a program is branching to an instruction not deftned within the confines of that program, what instruction is needed? b. When a program is to be branched to from another program, what may be used to identify the label of the instruction to be executed ftrst? Chapter 9: Floating-Point Arithmetic With the growing use of mathematical and statistical methods to solve business and industrial problems, floatingpoint arithmetic, long the province of scientists and engineers, is being used more and more by commercial programmers. Although FORTRAN and PL/I are far more efficient for the programmer who wants to solve a complex mathematical problem, floating-point arithmetic can simplify programming in assembler language when the values used in a computation cover a very wide range or are unpredictable. This is so because, in floating-point operations, the machine automatically keeps track of the decimal or binary point and the alignment of intermediate arithmetic results. The programmer need not expend the time and effort required to do this in involved decimal or binary calculations. Floating-point arithmetic may also save considerable storage space when the values used are either very: small or 75 very large. A value up to approximately 7 x 10 can be expressed in just four bytes. That number is equivalent to 7 followed by 7S zeros. Represented in packed decimal, it would use up over 30 bytes of storage. Since all floatingpoint numbers are exactly either four or eight bytes in length (at the option of the programmer), he reaps some additional benefits. He does not need to estimate the maximum possible sizes of his data, intermediate results, and final results for purposes of reserving sufficient space. Also, he does not run the risk of losing high-order digits from a register. He can, in fact, perform most calculations almost as directly as he would by hand. The System/360 floating-point feature performs the same arithmetic calculations as decimal and binary instructions: addition, subtraction, multiplication, and division. There are also similar instructions for comparing, loading, and storing. Just one different kind of instruction is included: the Halve instruction, which has the effect of dividing by two. The entire floating-point instruction set, although it may appear long and complicated (the list is presented later in this chapter), consists only of variations of these basic operations. These variations permit the programmer to choose between (1) long-precision and shortprecision numbers, (2) normalized and unnormalized addition or subtraction, and (3) register-to-register and storage-to-register operations. This brief chapter describes how floating-point numbers are represented in System/360, shows a few examples of floating-point instructions, and explains the new terms used in the preceding paragraph. It is a simplified introduction to the subject for the non-mathematician who may have some curiosity about floating-point operations or who may anticipate using the floating-point feature. Floating-Point Arithmetic 125 FLOATING-POINT NUMBERS Floating-point numbers are expressed in a form similar to that commonly used for scientific notation, which is a concise means of expressing very large or very small numbers. For example, the mean distance from the earth to the sun is roughly 93,000,000 miles. In scientific notation, we would give this number as 9.3 . 107• This expression consists of two factors, the significant digits multiplied by a power of 10. The exponent 7 indicates that the base lOis to be multiplied by itself seven times, and this will give the entire number the proper magnitude. The number base need not be 10, but it is the most common and the easiest for us to understand. The base might be 2 or 8 or 9 or 12 or whatever. In fact, in System/360 it is 16. Let's look at a couple of other examples of scientific notation in base 10. A light year, which is a common term for expressing large distances, represents a distance of 5,880,000,000,000 (or 5.88 . 10 12) miles. A unit that may be used for measuring the wavelength of light is 0.00000001 (or 0.1 . 10-7) centimeters. A number in this form of notation is generally, but not always, expressed with one integer to the left of the decimal point. Sometimes it is more convenient to place it elsewhere, either to make some numerical relationship clearer or to simplify computation. In such a case, the exponent is simply increased or decreased by the same number as the number of places the decimal point is moved. The following shows equivalent values for our three examples. 93,000,000 = 9.3 . 107 = 93.0 . 106 = .93 . 108 = .093 . 10 9 5,880,000,000,000 = 5.88 . 10 12 = 5880.0 . 109 = .588 . 1013 = .00588 . 1015 0.00000001 = .1 . 10-7 = 1.0. 10-8 = .01 . 10-6 = .000001 . 10-2 In System/360 a floating-point number, written as a decimal number by the programmer, is converted internally by the machine to a form very much like the underscored examples. In these, the part of each value to the left of the multiplication sign is a fractional quantity, without any whole numbers before the decimal point. Note that the first digit after the decimal point is a nonzero digit. A number in this form is known as a normalized number. The final example in each group of examples shows the form of an unnormalized number, in which the fraction has one or more high-order zeros. Floating-point numbers are fIxed in length, either a fullword (32 bits) for short precision or a doubleword (64 bits) 126 for long precision. The format of a short floating-point number is as follows: Characteristic (or Exponent + 64) o 1 Fraction 31 7 8 This format allows 24 bits for the fraction. A long floatingpoint number has the same arrangement, except that the fraction is 56 bits in length: Characteristic (or Fraction ~S~_E_xp_o_n_e_nt_+__ 64_)~~____________________- J o 1 7 8 CJ . 63 A value may be expressed in either short or long form; the short form will give greater speed and use less space, the long will give greater precision. In either format, the first bit is the sign of the fraction, for plus, 1 for minus, and indicates whether the entire number is positive or negative. The next seven bits are used for the exponent, which in System/360 is·called the characteristic by analogy with logarithms. The characteristic also includes a sign (but in an indirect way that will be explained shortly), giving us a plus exponent for large values (over 1) and a minus for small values (below 1). For example, 16 1 = 16 and 16- 1 = 1/16. Similarly, _16H = -16 and _16- 1 = -1/16. The characteristic is a power of 16, of course, not 10, and is a 7-bit binary number with a range of values from 0 to 12710 , The fraction is expressed in hexadecimal digits, 6 digits for short precision and 14 forlong, and in a normalized number its value is between 1/16 and 1. The fraction 1/16 is 0.1 in hexadecimal, with a bit pattern of 0001. Note that normaliZation applies to hexadecimal digits, not bits, and that the three high-order bits may be zero. The decimal point does not appear in storage, bu t is understood. The method devised for indicating the sign of the characteristic in System/360 floating-point numbers is to use what is called excess-64 notation. This avoids the complications of a second sign. As we mentioned, seven bits can represent a range of values from 0 to 127. If 6410 is always added to the actual exponent, a range from -64 through +63 can be represented without further indication of a sign. In this scheme, a characteristic of 65 is equivalent to an exponent of +1, 66 to +2, apd so on up to 127, which is equivalent to +63. In the low range, a characteristic of 63 is equivalent to an exponent of -1, 62 to -2, and zero to -64. Table 9-1 is given to help in understanding the actual value of some frequently used characteristics. ° Table 9-1. Equivalent Values of the Characteristics of Some Floating-Point Numbers Characteristic Dec. Hex 68 67 66 65 64 63 62 61 60 44 43 42 41 40 3F 3E 3D 3C Actual power of 16 +4 +3 +2 +1 0 -1 -2 -3 -4 Decimal value of characteristic 65,536.0 4,096.0 256.0 16.0 1.0 0.0625 (or 1/16) 0.00390625 (or 1/256) 0.000244140625 0.0000152587890625 Although the programmer does need to understand the internal form of floating-point numbers, he w.ill never have to do the calculations to break down a value into its hexadecimal exponent and fraction. The machine does that with the greatest of ease. To enter a value into storage in an instruction, the programmer need only define a constant, giving the value in decimal (with or without a decimal point) and specifying its type as E for a short floating-point number or D for a long number. Here are some examples: DC DC DC DC DC E'138.25' E'138' E'.00138' E'9.3E+7' D'9.3E+7' Each constant is specified by a decimal number, which we see may be an integer, a fraction, or a mixed number. A decimal point may be placed before, within, or after the number, or it may be omitted. A number without a decimal point is assumed by the machine to be an integer. The number may be signed or unsigned, and a number without a sign is assumed to be positive. The assembled object code for the floating-point numbers appears at the left in the listings (see Figure 9-1). This is a hexadecimal representation of the actual storage contents. The first two digits represent the sign plus the characteristic. The remaining digits represent the fraction. The decimal point is understood and does not appear in storage. The same numbers are shown in the comments column in Figure 9-2 in a form that is easy to read. A plus or minus sign is printed, depending upon whether the first bit is a zero or a one. The two digits following the sign give the characteristic, the first digit representing the value of the first three bits of the seven-bit characteristic. A decimal point (actually a hexadecimal point) is printed preceding the fraction. In the~e figures some of the decimal values specified are integers between 1 and 15 .. We see that they are represented in floating-point numbers by the corresponding hexadecimal digit in the fraction, with a characteristic of 41. To take the 9 as an example, +41.900000 should be considered as 16 1 9 • 16' Decimal 16 becomes +42.100000, which we consider as 1 16 2 .1"(; 7 The last two show how the expression 9.3 . 10 is specified as a constant. The E inside the quotation marks simply indicates an exponent. Figures 9-1 through 9-6, which follow, show various assembly listings of DC entries of floating-point numbers. 000148 00014C 000150 000154 000158 00015C 000160 000164 000168 00016C 000170 000174 000178 00017C 000180 000184 000188 00018C 41100000 41200000 41300000 41900000 41AOOOOO 41BOOOOO 41FOOOOO 42100000 42110000 421FOOOO 42200000 42210000 42FFOOOO 43100000 43101000 43FFFOOO 44100000 44100100 Decimal 32 becomes +42.200000, or 2 2 16 . 16' DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC E'l' E' 2' E'3' E'9' E'10' E'11' E '15 ' E '16' E '17' E '31' E'32' E'33' E' 255' E '256' E'257' E'4095' E'4096' E'4097' Figure 9-1. Assembly listing of decimal integers specified as short floating-point constants Floating-Point Arithmetic 127 The same constants are specified in Figure 9-3 as negative values. Looking at the actual storage values in the object code column, we see that the first digit in these cases is C. This represents the total of the fust four bits of our floating-point numbers. In other words, the value of the sign bit (decimal 8) is added to the value of the first three bits. of the characteristic. This is of no consequence when the s:gn is plus and is a zero bit. When it is negative, however, we get binary 1100 0001 (or hexadecimal C 1, equal to decimal 12 and 1) for a sign and characteristic of -41. There is still another interesting fact to observe in these representations of negative floating-point numbers. Note that the values are all in true notation, and not in two's complement form as in other types of System/360 arithmetic. In Figure 9-4 we have some decimal numbers that are fractional and mixed numbers, not integers. Decimal 0.5, for instance, becomes hexadecimal +40.800000, which we consider as 000190 000194 000198 00019C 0001AO 0001A4 0001A8 0001AC 0001BO 0001B4 0001B8 0001BC 0001CO 0001C4 0001C8 0001CC 0001DO 0001D4 41100000 41"200000 41300000 41900000 41AOOOOO 411100000 41FOOOOO 42100000 42110000 421FOOOO 42200000 42210000 42FFOOOO 43100000 43101000 43FFFOOO 44100000 44100100 The decimal number 1.5 becomes +41.180000, or 1 24 16 . 16 2 • It is interesting to note that the simple decimal number 0.1 is transformed into a nontermmating hexadecimal fraction' there is no exact hexadecimal representation for decimat' 0.1. On the other hand, complex-looking decimal fractions that happen to be negative powers of 16 are transformed into particularly simple hexadecimal numbers, as 0.00390625 =+3F.I00000. Figure 9-5 shows a few long floating-point numbers. The scheme is the same, the only difference being the presence of eight additional hexadecimal digits, which make the fraction a total of 14 digits. This permits more accurate representation of numbers that do not have an exact hexadecimal representation and naturally permits much greater precision when arithmetic is performed. F~gure :-6 shows some examples of short and long floatmg-pomt numbers specified by decimal numbers with exponents. The decimal numbers are all in the form of our examples of scientific notation at the beginning of this DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC "DC DC E'l' E '2' E '3' E '9' E'10' E'll' E'15' E'16' E'17' E'31' E'32' E'33' E'255' E'256' E'257' E'4095' E'4096' E'4097' +41.100000 +41.200000 +41.300000 +41.900000 +41.AOOOOO +41.BOOOOO +41.FOOOOO +42.100000 +42.110000 +42.1FOOOO +42.200000 +42.210000 +42.FFOOOO +43.100000 +43.101000 +43.FFFOOO +44.100000 +44.100100 Figure 9-2. A listing of the same examples as in Figure 9-1, showing them in the comments field in a form that is easy to read 000220 000224 000228 00022C 000230 000234 000238 00023C 000240 000244 000248 00024C 000250 000254 000258 00025C 000260 000264 C1100000 C1200000 C1300000 C1900000 C1AOOOOO C11100000 C1FOOOOO C2100000 C2110000 C21FOOOO C2200000 C2210000 C2FFOOOO C3100000 C3101000 C3FFFOOO C4100000 C4100100 Figure 9-3. The same values shown as negative numbers 128 DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC E'-l' E'-2' E'-3' E'-9' E'-10' E'-ll' E'-15' E'-16' E'-17' E'-31' E'-32' E'-33' E'-255' E'-256' E'-257' E'-4095' E'-4096' E'-4097' -41.100000 -41.200000 -41.300000 -41.900000 -41.AOOOOO -41.1100000 -41. FOOOOO -42.100000 -42.110000 -42.1FOOOO -42.200000 -42.210000 -42.FFOOOO -43.100000 -43.101000 -43.FFFOOO -44.100000 -44.100100 section. In E'12.78E+8' the decimal value is 12.78 . 108 , which we see becomes +48.4C2CBC in hexadecimal. In D'-0.OOOS7E-S' the decimal value is -0.00057' 10-5 . This like all the examples we have seen, is converted to a floating-point number in normalized form, that is, with no high-order zeros in the fraction. The fraction is always normalized unless the programmer specifies a decimal number with a scale factor. (Since scaling has not been discussed in this book and is not needed for our elementary compu- 000268 00026C 000270 000274 000278 00027C 000280 000284 000288 00028C 000290 000294 000298 00029C 0002AO 0002A4 0002A8 40800000 41180000 41140000 41120000 41110000 411COOOO 411EOOOO 411FOOOO 4019999A 3F28F5C3 3E418937 3D68DB8C 3CA7C5AC 4111999A 40400000 40100000 3F100000 tations, it is suggested that a student interested in the subject refer to his assembler specification manual.) In reviewing the hexadecimal values given by the assembler, we notice that in all the illustrations there are some fractions in which the fITst digit is 1. Hexadecimal 1, of course, is equivalent to binary 0001. It is important to realize that normalization refers to hexadecimal digits rather than to bits, and a normalized fraction may have as many as three leading zero bits. DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC DC +40.800000 +41.180000 +41.140000 +41.120000 +41.110000 +41.1COOOO +41.1EOOOO +41.1FOOOO +40.19999A +3F.28F5C3 +3E.418937 +3D.68DB8C +3C.A7C5AC +41.11999A -, 40.400000 +40.100000 +3F.100000 E'0.5' E '1.5' E'1.25' E'1.125' E'1.0625' E'1.75' E'1.875' E'1.9375' E'O.l' E'O.Ol' E'O.OOl' E'O.OOOl' E'O.OOOOl' E'1.1' E'0.25' E'0.0625' E'0.00390625' Figure 9-4. Some fractional and mixed decimal numbers expressed as short floating-point constants 0002B8 0002CO 0002C8 0002DO 0002D8 0002EO 0002E8 0002FO 0002F8 000300 4110000000000000 4120000000000000 4210000000000000 4980000000000000 4BB3A73CE5B59000 4080000000000000 401999999999999A 411199999999999A C11A86BD134658D5 3E10000000000000 DC DC DC DC DC DC DC DC DC DC D'l' D'2' D'16' D'34359738368' D'12345678912345' D'0.5 ' D'O.l' D'l.l' D'-1.65789516' D'0.000244140625' DC DC DC DC DC DC DC DC DC E'12.78E+8' E'lE+20' E'-22.87035E-12' E'-2.8E+50' D'0.lE+70' D'0.lE-49' D'-9.87654321555E+18' D'-0.00057E-5' D'12345.6789E-5' +41.10000000000000 +41.20000000000000 +42.10000000000000 +49.80000000000000 +4B.B3A73CE5B59000 +40.80000000000000 +40.1999999999999A +41.1199999999999A -41.1A86BD134658D5 +3E.10000000000000 Figure 9-5. Some long floating-point constants 000308 00030C 000310 000314 000318 000320 000328 000330 000338 484C2CBC 5156BC76 B819256E EABF9572 7A25179157C93EC7 173BDCF495A9703E D0891087B9F3A6EC BA187B375E0424FA 401F9ADD3739635F +48.4C2CBC +51.56BC76 -38.19256E -6A.BF9572 +7A.25179157C93EC7 +17.3BDCF495A9703E -50.891087B9F3A6EC -3A.187B375E0424FA +40.1F9ADD3739635F Figure 9-6. Some decimal values with exponents expressed as floating-point constants Floating-Point Arithmetic 129 FLOATING-POINT INSTRUCTIONS F our special registers, used only by the floating-point instructions, are part of the System/360 floating-point feature. They are 64 bits in length and are numbered 0, 2, 4, and 6. All 64 bits are used for long-precision operands and results, and only 32 bits for short-precision (except for the product in Multiply). Use of registers for floating-point arithmetic avoids the many operations that would otherwise be necessary for storing and loading results and operands. All floating-point operations are register-toregister (RR) or storage-to-register (RX), and most of the instructions are available with a choice of either format. All floating-point instructions are also available with a choice between the use of long or short numbers. In addition, the programmer may select an Add or Subtract instruction in the execution of which the intermediate and fmal results are normalized or are not normalized. All these choices mean a long list of instructions in the floating-point instruction set (as we see in Table 9-2, there are eight separate Add instructions), but the basic functions are simply to Add, Subtract, Multiply, Divide, Halve, Compare, Store, and Load. The Load instructions also provide the programmer with the ability to control the signs of operands. Note that the mnemonics of instructions for long precision are distinguished by the letter D, and for short precision by the letter E. In Add Unnormalized and Subtract Unnormalized, these change to Wand U. Perhaps the best way to get an idea of how the instructions actually operate is to study an example. Figure 9-7 is an assembly listing of a program to evaluate the following formula, using short precision throughout. y _ A ( +.!!.::..£ )2 2 3.17-2D The flIst processing instruction is Load Short (LE), which places the value of D in floating-point register 2. The fact that the 2 in this instruction refers to a floating-point register, rather than to a general purpose register, is implied in the operation code; floating-point is understood by the assembler when it encounters the code LE. This short operation will load the left half of the double-length register, leaving the low-order half unchanged. Any previous value in the low-order 32 bits, will ordinarily have no significant effect on later operations. The second instruction multiplies the contents of floating-point register 2, which we just loaded, by the constant 2 in floating-point form. The result is left in the same register, destroying the previous contents. No other register is involved, in contrast to fIXed-point multiplication. The lower half of the floating-point register is involved, however, because the entire register is used for the result of a Multiply operation. In short precision, the fraction of the product has 14 hexadecimal digits, of which at least two are always zero. 130 Table 9-2. Instruction Set for the System/360 Floating-Point Feature Name Mnemonic Format Load (Long) Load (Long) Load (Short) Load (Short) *Load and Test (Long) *Load and Test (Short) *Load Complement (Long) *Load Complement (Short) *Load Positive (Long) *Load Positive (Short) *Load Negative (Long) *Load Negative (Short) LDR LD LER LE LTDR LTER LCDR LCER LPDR LPER LNDR LNER RR RX RR RX RR RR RR RR RR RR RR RR *Add Normalized (Long) *Add Normalized (Long) *Add Normalized (Short) *Add Normalized (Short) *Add Unnormalized (Long) *Add Unnormalized (Long) *Add Unnormalized (Short) *Add Unnormalized (Short) ADR AD AER AE AWR AW AUR AU RR RX RR RX RR RX RR RX *Subtract Normalized (Long) *Subtract Normalized (Long) *Subtract Normalized (Short) *Subtract Normalized (Short) *Subtract Unnormalized (Long) *Subtract Unnormalized (Long) *Subtract Unnormalized (Short) *Subtract Unnormalized (Short) SDR SD SER SE SWR SW SUR SU RR RX RR RX RR RX RR RX *Compare (Long) *Compare (Long) *Compare (Short) *Compare (Short) CDR CD CER CE RR RX RR RX Halve (Long) Halve (Short) HDR HER RR RR Multiply (Long) Multiply (Long) Multiply (Short) Multiply (Short) MDR MD MER ME RR RX RR RX Divide (Long) Divide (Long) Divide (Short) Divide (Short) DDR DD DER DE RR RX RR RX Store (Long) Store (Short) STD STE RX RX *Operation sets condition code. LOC 000000 000000 000002 000002 000006 OOOOOA OOOOOC 000010 000014 000018 00001A 00001E 000020 000022 000028 000028 00002C 000030 000034 000038 00003C 000040 000000 OBJEC T CODE AODRI ADDR2 05BO 7820 7C20 3322 7A20 7840 7B40 3444 7A40 3042 3C44 7040 B032 B036 00034 00038 B03A B02A B02E 0003C 0002C 00030 B026 00028 B03E 00040 41123456 43356800 43252600 3E203EFD 41200000 41326852 STMT 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 19 20 21 22 23 24 25 26 21 SOURCE ST AT EMENT SHORTFP BEGIN A B C 0 FTWO CONI Y PRINT NOGEN START a BALR 11,0 USING *, II 2,D LE LOAD FLOATING POINT REGISTER 2 WITH D 2,FTWO MULTIPLY D IN REGISTER 2 BY 2 ME LCER 2,2 REVERSE SIGN OF PRODUCT AE 2,CONI ADO CONSTANT 3.17 4,B LE LOAD FLOATING POINT REGISTER 4 WI TH B 4,C SE SUBTRACT C 4,4 HER USE HALVE INSTRUCTION TO DIVIDE BY 2 4,A AE ADO A 4,2 DER DIVIDE NUMERATOR BY DENOMINATOR MER 4,4 SQUARE THE QUOTIENT 4,Y STE STORE THE FINAL RESULT EOJ OS OF DC E'l.1377772805' DC E'854.50' DC E"594.3750' DC E'6.904E-4' DC E' 2' DC E' 3.17' F OS END BEGIN Figure 9-7. Assembly listing of a program to perform simple computations in short floating-point arithmetic In the execution of a Multiply instruction, the machine nonnalizes both operands, if the fractions have leading zeros, before any arithmetic is perfonned. This is done by shifting the fraction left until the leftmost hexadecimal digit is a nOllZ('ro digit and reducing the characteristic by the number of shifts required. When this is done before the arithmetic process (as it is in both Multiply and Divide), the action is called prenormalization. With both operands prenonnalized, the product will either be nonnalized already or have at most one leading zero. In the latter case, the product fraction is shifted left one hexadecimal position to postnormalize it, and the product characteristic is reduced by one. In floating-point multiplication, the arithmetic process is very simple and follows the familiar rules for exponents. It consists of adding the characteristics and multiplying the fractions. To illustrate the procedure, let's consider a simple problem in base 10: to multiply 12,300 by 60. Expressed with decimal exponents, this is (.123· lOS) . (.6 .10 2 ). Multiplying the fractions, we get .123 .. 6 = .0738. Adding the exponents, we get 105+2 = 107 . Together, they give .0738 . 107 = 738,000. In System/360, of course, the machine also has to subtract 64 from the characteristic of an intermediate product because, with both operands in the excess-64 notation, adding the characteristics gets the extra 64 into the product twice instead of once. F or those who wish to follow the arithmetic in the program example, the details are given in Figure 9-8. Each line shows the contents of the two registers used for computation after the execution of each of the floating-point instructions. The operation codes are given in the left-hand column. The program used for this output specified the addition of a point in printing the hexadecimal register contents. The decimal equivalents are in the usual form for floating-point numbers and show the true value of the exponents. The decimal numbers are not all exact equivalents, because exact equivalents of fractional quantities often do not exist in base 10 and base 16. Inspection will show that these discrepancies are small for most practical purposes; they can be made much smaller by the use of long precision, as will be seen later. We noted before that the product fraction of a shortprecision Multiply is 14 hexadecimal digits in length, including some trailing zeros. Normally, after the ME operation in Figure 9-8, we would expect to find at least some nonzero d;igits in the low-order half of register 2. In this case, however, the two fractions that are multiplied yield only six significant digits (.2D3EFD x .200000 = .SA 7DF A 000000), so the low-order half of the register contains eight zeros. The more usual situation can be seen in register 4 after execution of the MER instruction. The next instruction in our program, the Load Complement (RR), reverses the sign of the product as written here. (The instruction can also be used with two different register numbers.) It would of course be acceptable programming practice to have stored the constant 2 as a negative number . Now we add the constant' 3.17, using an Add Normalized instruction. Floating-point addition starts with a comparison of the two operand characteristics; if they are the same, addition of the fractions takes place immediately. Otherwise, the fractional part of the number with the smaller characteristic is shifted right, as many places as the difference in characteristics, until they agree. When this is done, the decimal points (hexadecimal points, really) are "lined up", as addition requires. The fractions are then added. The larger of the two characteristics becomes the "provisional" characteristic of the sum; we say provisional because it may have to be adjusted for a possible overflow carry in the fraction or for postnormalization. If the addition caused overflow of the fraction, the Floating-Point Arithmetic 131 result fraction is now shifted right one place and the characteristic accordingly increased by 1. On the other hand, the addition might have resulted in a sum with leading zeros, which would happen if the operands were of about the same size but of opposite sign, and the characteristic would be decreased in the process of normalization. If these actions cause the characteristic to go below or above the range of zero to 127, exponent underflow or overflow is signaled, and normally a program interruption occurs. If addition or subtraction results in an all-zero fraction, the loss of significance is complete, which may in some cases destroy the validity of all results of the computation. If this happens without the problem originator's knowledge, he may place confidence in results that are in fact meaningless. For this reason, System/360 provides a warning in the form of a significance exception, and a program interruption occurs, enabling the programmer to cope with the situation in a subroutine. For certain types of data, the programmer may wish to prevent an interruption, and he can .do so in case of an exponent underflow or a significance exception. In case of exponent overflow, however, the interrupt action cannot be overridden. With the values that have been entered in our sample program, there will be no loss of significance or other exceptions. To review what has been covered in the program so far, we have evaluated the denominator within the parentheses. We leave the result in floating-point register 2 and tum now to an evaluation of the numerator. In loading B and subtracting C, instructions are used that are now familiar. Floating-point subtraction is just like addition, with the sign of the second operand reversed before adding the fractions. Since both addition and subtraction are completely algebraic, and since either one can involve any of the four combinations of signs of the operands, they are truly very similar. The division by 2 is handled in a rather different way from what one might expect and illustrates an interesting member of the floating-point instruction set. The Halve instruction (HER) divides the second operand by 2 and places the result in the first operand; both registers are the OP LE ME LCER AE LE SE HER AE OER MER STE same here, as they so often are in using the RR-format instructions. What actually happens is that the fraction part is shifted right by one binary place, which is equivalent to dividing by 2. If the consequence of this is an intermediate result with all zeros in the first four bits of the fraction, the final result is postnormalized. The next instruction is another Add Normalized, the details of which we discussed before. So far, however, we have not mentioned a feature of System/360 floating-point operations that is designed to increase the significance of final results. It is called the guard digit. As we know, the fractions in final results have six hexadecimal digits in short precision, and 14 in long precision. Intermediate results may have one additional significant low-order digit in the Add, Subtract, Compare, Halve, and Multiply operations, which participates in postnormalization of final results. This extra digit materializes when right-shifting into the guard digit position occurs during the operations named, as in Adding, for example, when the two operands are lined up with each other. When final results are subsequently shifted left in the process of postnormalization, the guard digit is simply included in the move. At this point in our problem, we have the numerator in floating-point register 4 and the denominator in register 2. A Divide (RR, Short) places the quotient in register 4. Floating-point division works as follows. Both operands are prenormalized. Division of the fractions yields the quotient fraction. The characteristic of the denominator ( or divisor) is subtracted from that of the numerator (or dividend), and then 6410 is added to get the characteristic back into excess-64 form. The arithmetic process here is similar, but opposite, to the Multiply instruction. In short-precision Divide, the low-order half of the registers is ignored, and the fraction of the result is six digits in length. Division of two normalized six-digit fractions will always yield either six or seven digits, never more or less. Postnormalization is never necessary, but the quotient fraction may need to be shifted right by one position and the characteristic increased correspondingly by 1. Our program now requires us to square the result of the FLOATING POINT REGISTER 2 CONTENTS IN HEX DEC EQUIVALENT FLOATING POINT REGISTER 4 CONTENT S IN HEX DEC EQUIVALENT 3E.2D3EFD 3E.5A7DFA BE.5A7DFA 41.32B2AA 00000000 00000000 00000000 00000000 +.6903999E-03 +.1380800E-02 -.1380800E-02 +.31b8b19E+01 00.000000 00.000000 00.000000 00.000000 00000000 00000000 00000000 00000000 +.OOOOOOOE+OO +.OOOOOOOE+OO +.OOOOOOOE+OO +.OOOOOOOE+OO 41.32B2AA 41.32B2AA 41.32B2AA 41.32B2AA 00000000 00000000 00000000 00000000 +.3168619E+Ol +.3168619E+Ol +.3168619E+Ol +.3168619E+Ol 43.356800 43.104200 42.821000 42.833345 00000000 00000000 00000000 00000000 +.8545000E+03 +.2601250E+03 +.1300625E+03 +.1312003E+03 41.32B2AA 00000000 41.32B2AA 00000000 41.32B2AA 00000000 ... 3168619E+Ol +.3168619E+Ol +.3168619E+Ol 42.2967F8 00000000 43.6B277A 98040000 43.6B271A 98040000 +.4140613E+02 ".171446143012~64~E+04 +.1714461430129646E+04 Figure 9-8. The contents of floating-point registers 2 and 4 after execution of each of the short-precision instructions in the program in Figure 9-7 132 registers may be seen in Figure 9-10. Here the full capacity of the registers is used in each operation, and the increase in precision of the arithmetic results can readily be seen. Except for the length of operands and results and the fact that in short precision the low-order halves of the registers are generally ignored, there is no difference in the execution of the instructions for long and short precision. division, which is standing in register 4. A Multiply (RR, Short), in which the quantity in register 4 is specified for both operands, does the job. Finally, a Store puts the result in the fullword storage location Y. Figure 9-9 shows a listing of the same program, with identical decimal values, rewritten to do all processing in long precision. Step-by-step changes in the contents of the LOC 000000 000000 000002 000002 000006 OOOOOA OOOOOC 000010 000014 000018 OOOOlA DOODlE 000020 000022 000028 000028 000030 000038 000040 000048 000050 000058 000000 OBJEC T CODE AOoRl ADoR2 05BO 6820 6C20 2322 6A20 6840 6B40 2444 6A40 2042 2C44 6040 B03E B046 00040 00048 B04E B02E B036 00050 00030 00038 B026 00028 B056 00058 STMT SOURCE STATEMENT 1 2 LONGF P 3 BEGIN 4 5 6 7 8 9 10 11 12 13 41123455F3lE 11BO 4335680000000QOO 4325260000000000 3E203EFD6B010972 4120000000000000 4132B 8 51EB 851E B8 14 15 16 19 20 21 22 23 24 25 26 27 A B C D FTWO CONI Y PRINT START BALR USING LD MD LCDR AD LO SO _ HDR AD DDR MDR STO EOJ OS DC DC DC DC DC DC DS END NOGEN 0 11,0 *,11 2,0 2, FTWO 2,2 2,CONl 4,B 4,C 4,4 4,A 4,2 4,4 4,Y LOAD FLOATING POINT REGISTER 2 WI TH D MULTIPLY 0 IN REGISTER 2 BY 2 REVERSE SIGN OF PRODUCT ADD CONSTANT 3.17 LOAD FLOATING POINT REGISTER 4 WI TH B SUBTRACT C USE HALVE INSTRUCTION TO DIVIDE BY 2 ADD A DIVIDE NUMERATOR BY DENOMINATOR SQUARE THE QUOTIENT STORE THE FINAL RESULT 00 D'1_1377772805' 0'854.50' 0'594.3750' D'6.904E-4' 0'2' 0' 3 .17' D BEGIN Figure 9-9. Assembly listing of the same program as in Figure 9-7, modified to perform all computations in long floating-point arithmetic OP FLOATING POINT REGISTER 2 CONTENTS IN HEX DEC EQUIVALENT FLOATING POINT REGISTER 4 CONTENT SIN HEX DEC EQUIVALENT LO MD LCDR AD 3E.2D3EFO 6B010~72 3E.5A7DFA 01A212E4 BE.5A70FA o7A212E4 41.32B2AA OB07A496 +.6903999999999997E-03 +.138019999999999BE-'2 -.1380799999999998E-02 +.3168619199999991E+01 00.000000 00.000000 00.000000 00.000000 00000000 00000000 00000000 00000000 +.OOOOOOOOOOOOOOOOE+OO +.OOOOOOOOOOOOOOOOE+OO +.OOOOOOOOOOOOOOOOE+OO +.OOOOCOOOOOOOOOOOE+OO Lo 41.3282AA 41.32B2AA 41.32B2AA 41.32B2AA OB07A496 OB07A496 0801A496 0807A496 +.3168619199999997E+Ol +.3168619199999991E+01 +.3l68619199999997E+01 +.3168619199999997E+Ol 43.356800 43.104200 42.821000 42.833345 00000000 00000000 00000000 5F31EllB +.854500000COOOOOOE+03 +.2601250000000000E+03 +.1300625000000000E+03 +.l312002772804998E+03 41.32B2AA 0807A496 41.3282AA 0807A496 41. 32B 2AA OB07A496 +.3168619199999997E+Ol +.3168619199999997E+Ol +.3168619199999997E+01 42.2967F8 86170C30 43.6B2170 4E08C861 43.6B2770 4E08C861 +.4140613592207608E+03 +.1714468091997438E+04 +.1714468091997438E+04 50 HDR AD DOR "'DR 5TD Figure 9-10. The contents of floating-point registers 2 and 4 after execution of each of the long-precision instructions in the program in Figure 9-9 Floating-Point Arithmetic 133 QUESTIONS AND EXERCISES 1. Write the DC instructions for the following short floating-point numbers: 3.14159265 -2.78 38754 x 106 .00000278 -.000236 x 10-7 2. Write the DC instructions for the following long floatingpoint numbers: 3.141592653589793 -2.78 -0.003 x 10- 3 3.8 X 1030 0.000000008 3. Show the hexadecimal form that the following DC entries will generate in storage. (Note that 16777216 equals 166 and that .59604644 x 10-7 equals 16-7.) DC E'32' DC fi3i DC E'16777216' DC E '.59604644E-7' DC E' -.59604644E-7' DC E'-16777216' 134 4. After execution of each of the following sets of instructions, what will be in the registers used? a. LE 2,A AE 2,B HER 4,2 given A =41789ABC and B =41876544 in hexadecimal short floating-point. b. What would be the results of the same instructions if A = 41200000 and B = 446oo044? c. LE 6,A SR 6,6 ADC d. L A E'15' 3,A 3,B E/I.O' X' 01000000' BOC 5. Write a program segment to calculate the value of X in short floating-point arithmetic and put it into storage: ADC X = A-(BxC) A + (B x C) Answers to Questions and Exercises EOJ DS F DS F H pS END BEGIN 7. (c) Condition code is 1 or 3 ~ 8. Be 15,NEWONE The extended mnemonic equivalent is B NEWONE. 9. LM 2,5,Xl 10. SR 5,5 11. I t will be the of the contents of register 3 (the base register), register 11 (the index register), and the displacement. 12. BXLE 5,6,NEWONE Chapter 3: Fixed-Point Arithmetic XANDY X Y 1. Fullword 2. Receives 3. Sends Exception 4. No. The first operand must specify an even-numbered register for an even-odd pair. S. An even-numbered register of an even-odd pair that contains the dividend Divisor The quotient is in the odd-numbered register. The remainder is in the even-numbered register. 6. START 256 BEGIN BALR 11,0 USING *,11 L 2,XANDY SRDL 2,12'" SRL 3,20 ST 2,X STH 3,Y sum Chapter 4: Programming with Base Registers and the USING Instruction 1a. USING *,11 b. BALR 11,0 2, 3, and 4. See illustration below. 5. See illustration on next page. (Continued in next column) During execution with program loaded at 320016 During assembly 1\ LOCATION OF STATEMENT PROGG BEGIN START 512 BALR II,£) USING *,11 L ~ 1\ STORAGE OPERAND Base Reg, 000202 LOCATION OF STATEMENT Address lOt. 1:t1- 00030+ 003301- OOOJJ.4- 0033U _------::::::::::""'1 A 2~ 000206 S 2QATA+}) 000234 6 10&, 00030B 0032'4 ST 2QESULj) 000238 6 126 OOOJ:t8 003Z38 L 6<[ffiD 000252 8 141- 0003# OO~J.,2 DC 000304 DC F' 25 ' F'15' TEN DC F'lf1J' 000324 Q03111- RESULT DS F 000328 P033:tB 000344 0033/4 DATA 000308 ADDRESS OF STORAGE OPERAND* Displacement VALUE LOADED IN BASE REGISTER 11 003308 IJOJ'28 -- OOJ3H OOJ104OIJJ30B SYMBOL BINl DC END F'12' BEGIN *Base and displacement remain the same as during assembly, LENGTH VALUE BEGIN 02 000200 BINI 04 000344 DATA 04 000304 RESULT 04 000328 TEN 04 000324 Answers to questions 2, 3, and 4 Answers to Questions and Exercises 135 During execution with program loaded at 100016 During assembly 1\ LOCATION OF STATEMENT 1\ LOCATION OF STATEMENT STORAGE OPERAND Base Reg. Displacement Address ADDRESS OF STORAGE OPERAND'" VALUE LOADED INTO BASE REGISTERS PROGH START f) BEGIN BALR 11,0 USING FIRST,ll FIRST BC 15,SKIP 000002 001001- II DATA DC F'3472' 000008 tJO/OO8 10 BASEl DC A (FIRST+4096) 000024 OO/OJ-/- BASE2 DC A(FIRST+8192) 000028 000104 During assembly (assumed) 001000 000000 9 L 1~ USING FIRST+4096,10 L 9~ USING FIRST+8192,9 BC LOOP LOOPB SKIP CK8 (}(}Ooot (JO/oot OOIOOt Of)1, Oot IJf)1002 00300t OO/O~6 -8 OJ~ 0000]4 00//04- 001011 000108 B OJ6 OO()Ot8 (JOIIOB O()IOt8 1~ 001504 9 9rJ2 (JOJ9tJ4 002104 001904- A 4<§ID 001898 B 006 OO(}OOB tJ02898 s 5,DATA 002204 BC 8@£D 002508 BC 8{kOOPB~ BEGIN 002904 END During execution (actual) ClO/008 O()JJO/ A 896 - 001898 - - - 00J508 9 001898 10t ()ottOI- 003904 ()OJto; SYMBOL VALUE 8ASti BASE! BEGIN CK8 0000;1000028 000000 PArA 000008 FIR5'T (..OOP lOOPB SKIP tJ02904 OOOOO~ 00/898 oot1.04000101- '"Base and displacement remain the same as during assembly. Answers to questions 5 Chapter 5: Decimal Arithmetic la. CON3 DC PL5'3' b. 000OOOO03C 2. Assembler Data defmitions Programmer 3. Equal to 4. One less than Sa. The multiplicand in the low-order positions and zeros in t.he high-order positions b. In the storage area specified by the first operand 02 48 9C 10 3C 6a. 00 158 159 15A 15B 15C 15D b. 15A 7. Storage area containing the dividend Divisor The quotient will be in the left portion of the dividend area, and the remainder in the right portion. 8a. SOURCE 66 55 44 33 22 11 DEST 11 22 66 55 44 6S 136 b. SOURCE 66 55 44 33 22 11 DEST 11 22 33 4S 55 6S c. SOURCE 66 55 44 33 22 11 DEST 00 00 00 04 43 3S 9. No. The ZAP instruction, as all the decimal arithmetic instructions and the decimal compare instructions, requires a legitimate sign in the low-order byte of the "sending" field. RESULT+5(1),FACTOR+4 lOa. MVN MVO RESULT ,F ACTOR(4) FACTOR+3(1),FACTOR+4 b. MVN ZAP RESULT ,F ACTOR(4) lla. SI b. NI HOLD,X'OO' c. NI HOLD+3,X'OF' 12. In both cases, each bit position of the referenced storage operand is analyzed against the corresponding bit position of the immediate portion of the instruction. The storage byte referenced by the first operand, after execution will be: a. For the And Immediate instruction, a 1 in each bit position in which both operands had 1s, and zeros elsewhere. b. For the Or Immediate instruction a 1 in the bit positions in which either or both operands had ai, and a zero where both operands had zeros. 13. Packed decimal 14. PACK 15. UNPK (Unpack) 16a. DC F'578' or DC H'578' b. DC ZL3'578' c. DC PL2'578' 17. There are at least four ways to write the DC statement. Keep in mind that 4B is the hexadecimal equivalent of 7510 • a. DC F'75' would generate the 4-byte constant: 00 00 00 4B. b. DC H'75' would generate the 2-byte constant: 00 4B. c. DC X'4B' would generate the I-byte constant: 4B. The advantage of methods a and b over method c is that the programmer does not have to convert from decimal to hexadecimal. A disadvantage is that more space is used than is perhaps necessary. d. The statement DC FLI '75' would remove this disadvantage since the characters Ll specify that the length (L) of the constant is to be 1 byte. Thus a I-byte field of 4B would be generated. A point to remember is that when a length is stated for an F-type constant no boundary alignment is performed by the assembler. 18. IC 6,OLD 19. STC 6,OLD 20a. No. MASK is not located on a fullword boundary. The N instruction requires the operand in storage to be on a fullword boundary. b. The statement DS OF could be inserted immediately before the DC defining MASK. c. DC F'15' Chapter 6: Logical Operations on Characters and Bits 1. XI KEY,IS (immediate data in decimal) XI KEY,X:OF' (immediate data in hexadecimal) XI KEY,B'OOOOIIII' (immediate data in binary) 2. TM ADDR,X' 30' BC S,ANIMAL 3. TM ADDR,X'06' Be 4,LIST2 There are many acceptable ways of performing tests such as 2 and 3. The TM instruction, where it can be used, has the advantages of leaving storage unchanged and obviating the need for registers or work areas. 4a. 05 b. 2C (the fmal C is the code for a plus sign) c. 43 5. 8 bits, 1 byte 6. 2048 bits, 256 bytes 7. (b) Alphameric characters. Despite their plausibility, a and c are not correct in the general case because of possible difficulty with sign codes. 8. (c) An inequality. All codes are valid. 9. (a) 5, (b) 2, ( c) 3 plus the contents of general register 1, (d) the computed effective address for FIELD, not the word stored at that address 10. Among the many ways to solve this are the following: CLC FIELD(I), FIVE BC 6,NOT5 FIVE DC X'OS' CLI BC FIELD,X'05' 6,NOT5 or: or: TM FIELD,X'05' BC 12,NOT5 TM FIELD,X'F A' BC 5,NOT5 11. The second byte of the BC instruction, containing the mask M1 and index X2 fields. 12. (d) The 01 instruction changes the BC 0 instruction, which never branches, to a BC 15 instruction, which branches unconditionally. Hence, after the first time around, the sequence between the BC and symbolic address ADDR is always skipped. 13. The instruction sequence between the BC instruction and the address ADDR will be alternately executed and skipped. 14. N 5,MASK MASK DC X'FFOOOOOO' Chapter 7: Edit, Translate, and Execute Instructions 1. 2. 3. 4. S. 6. 7a. b. c. 8. 9. 10. 11. BBBB1540 BBBBS721BB BBBBBBB.OIBCR BBBBBBBBB BBO,OOO.10BB BBBBI01.43CRBBBBBB1.07BCR PATRN DC X' 40206B2020206B2020214B20204OC3D9' BBBB92,500.0lBCR BBBB92,SOO.OlBCR (c) PATRN+2 No (e) ACBD (d) Address of A'REA+2 and X'Ol' respectively Answers to Questions and Exercises 137 12. 12345678991000000000 Area is first set to zeros by the MVI and MVC instructions. The EX instruction first causes the low-order 8 bits of register 2 (OA) to be Or'd with the 8-bit length code portion (00) of the Move instruction. The result of the Or'ing is a length code of OA (10 in decimal). Since the object instruction length code is always one less than the number of bytes to be affected, the Move instruction will cause 11 bytes to be moved. 13. Chapter 9: Floating-Point Arithmetic 1. 2. 10000000000000000000 Chapter 8: Subroutine Linkages and Program Relocation la. The return address is entered in Register 14, and an unconditional branch is made to the address in Register 15. b. The return address is entered in Register 14, and an unconditional branch is made to the location designated by SUB. c. BR 14 2. 1 d 13 c 14 a 15 b 3a. Assures alignment of address constants by use of a CNOP. b. Places the address of subroutine in Register 15 c. Places the address of return in Register 14 d. Sets up parameter list address by use of a BALR 1,15 e. Defmes as many address constants as there are in parameter list. 4a. 000 408 b. 000 404 c. 000 404 d. 000 406 e. 000 402 f. 000 402 5a. STM 14,12,12(13) b. LM 14,12,12(13) BR 14 6a. EXTRN assembler instruction b. ENTRY assembler instruction 138 3. 4a. b. c. d. 5. DC DC DC E'3.14159265' E'-2.78' E'38754E+6' (DC E'3.8754E+I0' is another possibility) DC E'0.278E-5' DC E'-2.36E-ll' DC D'3.141592653589793' DC D'-2.78' DC D'-3E-6' DC D'3.8E+30' DC D'8E-9' 42200000 4220000000000000 47100000 3AI00000 BAIOOOOO C7100000 Floating-point register 2: 42100000 XJO(XXJO(X Floating-point register 4: 41800000 ~XJCXXJC( Floating-point register 2: 44600244 XXXXXXXX Floating-point register 4: 44300 122 ~XJCXXJC( Floating-point register 6: 41FOOOOO XXXXXXXX General register 6: 00000000 General register 3: 42100000 A 13 C X LE ME LCER AE AE DER STE 2,B 2,C 4,2 2,A 4,A 4,2 4,x DS DS DS DS F F F F B in Reg. 2 B x C in Reg. 2 -(B x C) in Reg. 4 A+(B x C) in Reg. 2 A-(B x C) in Reg. 4 A-(B x C) -7 A+(B x C) Store final result Appendix SYSTEM/360 MACHINE INSTRUCTIONS STANDARD INSTRUCTION SET NAME * Add , Add , Add Halfword , Add Logical * Add Logical , AND , AND • AND 'AND Branch and Link Branch and Link Branch on Condition Branch on Condition Branch on Count Branch on Count Branch on Index High Branch on Index Low or Equal * Compare , Compare * Compare Halfword * Compare Logical * Compare Logical * Compare Logical * Compare Logical Convert to Binary Convert to Decimal Diagnose Divide Divide * Exclusive OR * Exclusive OR * Exclusive OR , Exclusive OR Execute * Halt I/O Insert Character Load Load Load Address * Load and Test * Load Complement Load Halfword Load Multiple * Load Negative * Load Positive t Load PSW Move Move Move Numerics Move with Offset Move Zones Multiply Multiply Multiply Halfword * OR * OR * OR * OR Pack Set Program Mask Set System Mask * Shift Left Double * Shift Left Single Shift Left Double Logical Shift Left Single Logical * Shift Right Double , Shift Right Single Shift Right Double Logical Shift Right Single Logical , Start I/O Store Store Character Store Halfword Store Multiple * Subtract * Subtract t MNEMONIC TYPE CODE AR A AH ALR AL NR N NI NC BALR BAL BCR BC BCTR BCT BXH RR RX RX RR RX RR RX SI SS RR RX RR RX RR RX RS IA SA 4A IE BXLE CR C CH CLR CL CLC RS RR RX RX RR RX SS SI RX RX SI RR RX RR RX SI SS RX SI RX RR RX RX RR RR RX RS CLI CVB CVD DR D XR X XI XC EX HIO IC LR L LA LTR LCR LH LM LNR LPR LPSW MVI MVC MVN MVO MVZ MR M RR RR SI SI SS SS SS SS RR MH RX RX OR RR o RX SI SS SS 01 OC PACK SPM RR SSM SLDA SLA SI 5E 14 54 94 D4 05 45 07 47 06 46 86 87 19 59 49 IS 55 05 95 4F 4E OPERANDS (Assembler Format) RI, R2 RI, D2 (X2, B2) RI, D2 (X2, B2) RI, R2 RI, D2 (X2, B2) RI,R2 RI, 02 (X2, B2) D1 (BI), I2 01 (L, B1), 02 (B2) RI,R2 RI, 02 (X2, B2) MI,R2 MI, D2 (X2, B2) RI,R2 RI, 02 (X2, B2) RI, R3, 02 (B2) RI, R3, 02 (B2) RI, R2 RI, 02 (X2, B2) RI, 02 (X2, B2) RI,R2 RI, 02 (X2, B2) 01 (L, BI), 02 (B2) D1 (B1), 12 RI, 02 (X2, B2) RI, 02 (X2, B2) NAME 1D 50 17 57 97 07 44 9E 43 18 58 41 12 13 48 98 11 10 82 92 02 01 FI D3 IC 5C 4C 16 56 96 06 F2 RI, R2 RI, 02 (X2, B2) RI,R2 RI, 02 (X2, B2) 01 (BI), I2 01 (L, B1), 02 (B2) RI, 02 (X2, B2) DI (B1) RI, 02 (X2, B2) RI,R2 RI, 02 (X2, B2) RI, 02 (X2, B2) RI,R2 RI,R2 RI, 02 (X2, B2) RI, R3, 02 (B2) RI, R2 RI,R2 D1 (BI) 01 (BI), I2 01 (L, BI). 02 (B2) DI (L, BI). 02 (B2) 01 (Ll, B1), D2 (L2, B2) DI (L, B1). 02 (B2) RI, R2 RI, 02 (X2. B2) RI. 02 (X2, B2) RI, R2 RI, 02 (X2. B2) DI (B1), 12 DI (L, BI). 02 (B2) DI (Ll, B1), D2 (L2, B2) RI RS RS 8B DI (BI) RI, 02 (B2) RI,02 (B2) SLDL RS 80 RI, 02 (B2) SLL SRDA SRA RS RS RS 89 RI, 02 (B2) 8E 8A RI, 02 (B2) RI, 02 (B2) SRDL RS 8C RI, D2 (B2) SRL SIO ST STC STH STM RS SI 88 RX RX RX 50 RI, 02 (B2) 01 (BI) RI, 02 (X2, B2) RI, 02 (X2, B2) RI, D2 (X2, B2) RI, R3, D2 (B2) RI. R2 RI. 02 (X2, B2) RX RX RR RX RR SI SI SI SI SS SS SS • Subtract Logical Supervisor Call * Test and Set * Test Channel * Test I/O * Test Under Mask Translate • Translate and Test Unpack OPERANDS (Assembler Format) CODE 4B IF SF F3 RI, D2 (X2, B2) Rl, R2 RI, D2 (X2, B2) I DI (BI) DI (BI) DI (BI) DI (BI), 12 D1 (L, B1). D2 (B2) DI (L, BI). D2 (B2) DI (LI, BI), D2 (L2, B2) FA F9 FD DE DF FC FB F8 DI DI Dl Dl DI DI DI Dl OA 93 9F 9D 91 OC DD DECIMAL FEATURE INSTRUCTIONS * Add Decimal • Compare Decimal Divide Decimal * Edit • Edit and Mark Multiply Decimal • Subtract Decimal * Zero and Add SS SS SS SS SS SS SS SS AP CP OP EO EOMK MP SP ZAP (Ll, BI), 02 (L2, (Ll, BI),02 (L2, (Ll, B1), 02 (L2. (L, BI). 02 (B2) (L. Bl), D2 (B2) (Ll. B1), D2 (L2, (Ll, B1), 02 (L2, (Ll, B1), 02 (L2, B2) B2) B2) B2) B2) B2) DIRECT CONTROL FEATURE INSTRUCTIONS Read ~irect Write Oirect RDD SI SI WRD 85 84 DI (BI), 12 Dl (BI), 12 09 08 Rl, R2 Rl, R2 PROTECTION FEATURE INSTRUCTIONS Insert Storage Key Set Storage Key 9C 42 40 90 IB 5B ISK SSK RR RR Floating-point feature instructions are listed in Chapter 9. MACHINE FORMAT Second Halfword First Halfword Third Halfword Byte 2 Byte 1 Register Register Operand 1 Operand 2 RR OpCode :0 I~~N 7 8 1112 IS Address of Operand 2 Register Operand 1 I RX Op Code :0 c;J 7 8 X2 11 12 15 16 , 19 20 31 I : Immediate : I OperiW1d Address of Operand 1 I I~~I I SI Op Code I I I 12 B1 °1 I ~:0----7""'8----1-'5'-1-6-1-9....2 - 0 - - - - - 3 - ' 1 ; I Register Register One rand ; Operand 3 I Address of Operand 2 I ~. __- - - - I i Op Code ~o I : S SH SLR SL SVC TS TCH TIO TM TR TRT UNPK * Subtract Halfword * Subtract Logical RS RS RR TYPE 83 04 80 8F SR MNEMONIC [ j o R1 R3 11 12 I B2 I 1516 Length Operand 1 Operand 2 OpCode OpCode D2 I 31 i 1920 Address of Operand 1 Address of Operand 2 I 1~1~~-]~~~2=i 78 10 SSl! I I I 7 8 111215161920 '~ I~ 7 8 1112 15 16 31323536 Address of Operand 1 B1 Address of Operand 2 °1 19 20 31 32 35 36 47 * Condition code is set t New condition code is loaded Appendix 139 CONDITION CODE SETTINGS Code State Mask Bit Position 0 8 4 EXTENDED MNEMONIC CODES FOR THE BRANCH ON CONDITION INSTRUCTION 2 2 Assembler Code Fixed-Point Arithmetic zero zero no carry equal zero zero zero zero zero zero zero zero zero Add H/F Add Logical Compare H/F (A:B) Load and Test Load Complement Load Negative Load Positive Shift Left Double Shift Left Single Shift Right Double· Shift Right Single Subtract HIF Subtract Logical zero zero carry A high > zero > zero zero zero zero zero > zero > zero zero carry > > > > overflow not zero carry B BR D2(X2,B2) R2 NOP NOPR D2(X2,B2) R2 carry overflow overflow overflow overflow overflow not zero carry Machine Instruction Generated Meaning Branch Unconditional Branch Unconditional (RR format) No Operation No Operation (RR format) BC BCR lS,D2(X2,B2) lS,R2 BC O,D2(X2,B2) BCR O,R2 Used after compare instructions (A :B) BH BL BE BNH BNL BNE D2(X2,B2) D2(X2,B2) D2(X2,B2) D2(X2,B2) D2(X2,B2) D2(X2,B2) Branch on High Branch on Low Branch on Equal Branch on Not High Branch on Not Low Branch on Not Equal BC BC BC BC BC BC 2,D2(X2,B2) 4,D2(X2,B2) 8,D2(X2,B2) 13,D2(X2,B2) 11,D2(X2,B2) 7,D2(X2,B2) BC BC BC BC BC BC BC 1,D2(X2,B2) 2,D2(X2,B2) 4,D2(X2,B2) 8,D2(X2,B2) 13,D2(X2,B2) 11,D2(X2,B2) 7.D2(X2.B2) BC BC BC BC 1,D2(X2,B2) 4,D2(X2,B2) 8,D2(X2,B2) 14,D2(X2,B2) Used after arithmetic instructions Decimal Arithmetic Add Decimal Compare Decimal (A:B) Subtract Decimal Zero and Add zero equal zero zero zero > zero > overflow overflow overflow Logical Operations And Compare Logic~ (A:B) Edit Edit and Mark Exclusive Or Or Test Under Mask Translate and Test BO BP BM BZ BNP BNM BNZ A high > zero > zero D2(X2,B2) D2(X2,B2) D2(X2,B2) D2(X2,B2) D2(X2,B2) D2(X2,B2) D2(X2,B2) Branch on Overflow Branch on Plus Branch on Minus Branch on Zero Branch on Not Plus Branch on Not Minus Branch on Not Zero Used after Test under Mask instructions one(s) complete BO BM BZ BNO D2(X2,B2) D2(X2,B2) D2(X2,B2) D2(X2,B2) Branch if Ones Branch if Mixed Branch if Zeros Branch if Not Ones EBCDIC CHART The 256-position chart at the right, outlined by the heavy black lines, shows the graphic characters and control character representations for the Extended Binary-Coded Decimal Interchange Code (EBCDIC). The bit-position numbers, bit patterns, hexadecimal representations and card· hole patterns for these and other possible EBCDIC characters are also shown. To rmd the card hole patterns for most characters, partition the chart into four blocks as follows: Fifteen positions, indicated by circled numbers, are exceptions to the above arrangement. The card hole patterns for these positions are given below the chart. Following are some examples of the use of the EBCDIC chart: Character [Tal ~ 140 Bit Pattern Hex Hale Pattern Zone Punches PF Block 1: Zone punches at top of table; digit punches at left Block 2: Zone punches at bottom of table; digit punches at left Block 3: Zone punches at top of table; digit punches at right Block 4: Zone punches at bottom of table; digit punches at right Type % R a Contral Character Special Graphic Upper Case Lower l:ase Control Character I function not yet assigned 00 00 0100 01 101100 II 01 1001 10000001 00 11 0000 04 6C 09 81 30 I Digit Punches 12 - 9 1- 4 0-8-4 11,- 9 12 - 01- I 12 - 11 - 0 - 91- 8 - 1 I .. Bit Positions 01 234567 1 EBCDIC CHART 01 00 '0, r-... Ci -0' . D '1")' 'u ' c: .,g ~ a; 00 01 10 11 00 01 10 11 00 01 10 11 00 01 10 11 0 1 2 3 4 5 6 7 B 9 A B C D E F 12 12 12 12 E 12 ~ ~ ::r: Ir '> '" " ..t: u c: a.. "U c: 8 ....... .:. .. , ~ I .< '.' 0 . . ..... ".< CD CD Ci "". 0 ,8-1 .. 0 " "',9' SOH DCl SOS FS . 1 0010 2 2 STX DC2 0011 3 ,3 ETX TM 0100 4 :;4 0101 5 , . .••.. , ··,O? .....'. .. .... ..•. .. ," " FF IFS '.: D 18-? CR IGS ENQ SO IRS ACK SI IUS BEL .' ~; 1110 E 1111 i', ::" F 18-7 I.. :~r:, ',> I:,' ,. . " .. ,"',/>. :,::': ": i),( ...... : ;.. ' \ DC4 < * NAK ( ) + ; > --, ? I SUB ,'''.' , "', ::0 .' ' $ CU3 >: .'. ".:":,.11'.';.::. :, .. ,. • '.' ® ! C; CC ..... .' :.' ", .". :", >.' ·,'·t':, [: ': "",.. 12-0-9-8-1 12-11-9-8-1 11-0-9-8-1 12-11-0-9-8-1 CD CD CD ./ 8-4 : 8,.6 i' . . .•.:•...,. •. .' 12', .... I ' , ... [ ' " . ::".' ...... ;.' : ...• ..'... :,,;" : . . , ,'.' ~ .•...... '.. ~... , I:,':"':': I',"~..J ~'.. '.... ":, , ' ; < !. <\;" ;' '.;::, ".' i . :i:,'::: ,.'. ...0 .·. · ·.·. ,0 '/>;":" :< '.". CD Acknowledge Bell Backspace Bypass Cancel Cursor Control Carriage Return Customer Use 1 Customer Use 2 Customer Use 3 Device Control 1 Device Control 2 Device Control 4 Delete Data Link Escape Digit Select End of Medium Enquiry EOT ESC ETB ETX FF FS HT IFS IGS IL IRS IUS LC LF NAK NL NUL 'li .. ' ', .... .: I ','."', . .12 12 ..••.. 0 < @ 12-0 12 @ 11-0 11 ® 0-8-2 12-11-0 @ 11 II 0 0 9 9 ) .' ;":.' 9 Zon e Punches 0-1 ® ® 11-0-9-1 12-11 0 SpeCial Graphic Characters Control Character Representations ACK BEL BS BYP CAN CC CR CUl CU2 CU3 DCl DC2 DC4 DEL DLE DS EM ENQ )a. .JJ > chart~ No Punches ® @ = 0 .... Card Hole Patterns (exceptions to punches shown in CD CD CD CD 8-3 8-7 . S.··>,· I':?' # 8'-5 '. ;"', ,,'." 8-2 - ·····:Jr., . , % : End of Transmission Escape End of Transmission Block End of Text Form Feed Field Separator Horizontal Tab Interchange Fi Ie Separator Interchange Group Separator Idle Interchonge Record Seporator Interchange Unit Separator Lower Case Line Feed Negative Acknowledge New Line Null PF PN R~S RS SI SM SMM SO SOH SOS SP STX SUB SYN TM UC VT Punch Off Punch On Restore Reader Stop Shift In Set Mode Start of Manua I Message Shift Out Start of Heading Start of Significance Space Start of Text Substitute Synchronous Idle Tope Mark Upper Case Vertical Tab < I & Ce'nt Sign Period, Decimal Point Less-th'i'n Sign Left Parenthesis Plus Sign Logical OR Ampersand Exclamation Point Dollar Sign Asterisk Right Parenthesis Semicolon Logical NOT / % > @ Minus Sign, Hyphen Slosh Comma Percent Underscore Greater-than Sign Question Mark Colon Number Sign At Sign Prime, Apostrophe Equal Sign Quotation Mark Appendix 141 SYSTEM/360 ASSEMBLER INSTRUCTIONS Following is a representative list of assembler instructions, grouped according to use. The mnemonics used for conditional assembly and macro defmition are included simply to clarify classification of assembler instructions as a whole. Information on these two subjects is given in the System/ 360 Assembler Language manuals (see Preface). The meaning of the extended mnemonics for the Branch on Condition machine instructions, and the machine code generated by each, appear elsewhere in this Appendix. MNEMONIC MNEMONIC For conditional assembly GBLA GBLB GBLC LCLA LCLB LCLC SETA SETB SETC AIF AGO MEANING For symbol definition EQU Equate Symbol Extended mnemonics for the BC and BCR machine instructions For data definition DC DS CCW B BR NOP NOPR BH BL BE Define Constant Define Storage Define Channel Command Word For program sectioning and linking START CSECT DSECT ENTRY EXTRN COM Start Assembly Identify Control Section Identify Dummy Section Identify Entry-point Symbol Identify External Symbol Identify Blank Common Control Section BNH BNL BNE BO BP BM For base register assignment USING DROP BZ BNP Use Base Address Register Drop Base Address Register BNM BNZ BNO For control of printed listings TITLE EJECT SPACE PRINT Identify Assembly Output Start New Page Space Listing Print Optional Data TYPES OF ASSEMBLER LANGUAGE CONSTANTS Code Type Machine Format C 8-bit code for each character 4-bit code for each hexadecimal digit Binary Signed, flxed-point binary; normally a fullword Signed, flxed-point binary; normally a halfword Short floating-point; normally a fullword Long floating-point; normally a doubleword Packed decimal Zoned decimal Value of address; normally a fullword Value of address; normally a halfword Base register and displacement value; a halfword Space reserved for external symbol addresses; each address normally a fullword For program control ICTL ISEQ ORG LTORG CNOP COPY END PUNCH REPRO For macro definition MACRO MNOTE MEXIT MEND 142 Input Format Control Input Sequence Checking Set Location Counter Begin Literal Pool Conditional No Operation Copy Predefined Source Coding End Assembly Punch a Card Reproduce Following Card B F Character Hexadecimal Binary Fixed-point H Fixed-point E D Floating-point Floating-point P Z A y S Decimal Decimal Address Address Address V Address X Index In this index, assembler and macro instructions are identified as such. Machine instructions are listed by name in capital letters. Adcon (see address constant) ADD (A) 9,11 ,30,42-44,113 Incrementing an indexed address 45,47 ADD DECIMAL (AP) 57,62,71,73,81 ADD HALFWORD (AH) 48 ADD NORMALIZED (AD) 133 ADD NORMALIZED (A E) 131 Address arithmetic Use of binary for 18,20 Address calculations by assembler 6,10,11,55,57 Address changes in program modification 15 Address constant A-type 108,111,113,119 in examples 76,94,101,103,108,112,116 to load base register 54,56 Modification by linkage editor 57,115,119,123 for subroutine linkage 108 to test completion of loop 57 V-type 109,115, 11 7 , 119 Address, effective 30,52 Addressing byte locations 18 Addressing main storage 9,18,19,24,30,51 Addressing registers 19 Address specification Use of zero in 10,11,24,30 Alignment, boundary (see boundary alignment) AND (N) 69,70,79,83 AND (immediate) (NI) 67,68,78,79,83 Argument 92,93,97 Arithmetic and logical unit 19 Arithmetic instructions, effect on condition code 140 Assembler coding form 7 Assembler instruction In assembler language 2,4 List of 142 Assembler instructions mentioned in text CNOP (conditional no operation) 113,114 CSECT (identify control section) 115 DC (define constant) 2,10,11,31,127 DROP (drop base address register) 57 DS (define storage) 2,10,11 ,31 ,56,61,70 ENTRY (identify entry-point symbol) 121 EJECT (start new page) 3 END (end assembly) 10,11,31 EXTRN (identify external symbol) 120 ICTL (input format control) 7 ISEQ (input sequence checking) 7 ORG (set location counter) 54,55 PRINT (print optional data) 29,94 START (start assembly) 9,61,115 TITLE (identify assembly output) 2,9,120 USING (use base address register) 3,9,10,51-57 Assembler language Basic concepts 9,10 Comparison with high-level languages 4 Description 1- 4 Types of instructions 3,4 Assembler program 1,5,6,52,53 "Assembler" program example 100,101 Asterisk in a comment, use of 8 Asterisk in an instruction, use of 11 Averaging problem 48,49,68 Base address 24,51,118,123 Base register 9,11 ,24,51-57,123 Use of zero 30 Base register range restriction 109 Base registers, use of multiple 54-57 Base address chosen by assembler 56 Baudot teletypewriter code 92 Binary arithmetic (see fixed-point operations) Binary-coded decimal (BCD) data format 20 Binary -decimal number conversion 28 Binary fractions 35 Bit 17 Bit manipulation 82,83 Bits, setting on and off 79 Blanking storage area 100,102 Boundaryalignment 10,15,18,102 Of DC's and DS's 10 Of machine instructions 23 Use of CNOP instruction 113,114 Use of DS zero duplication factor 70 Branch if Zeros (BZ) extended mnemonic 99 BRANCH AND LINK (BAL) 109 BRANCH AND LINK (BALR) 9,10,24,51,52,108,109,111 Branching 23,39 BRANCH ON CONDITION (BC) 40,42-44,71,78,79,81 102 After COMPARE 45,76 BRANCH ON CONDITION (BC, BCR) with EDIT 89 Extended mnemonic code 44,78,79,83,140 No-Operation (NOP, NOPR) 39,140 Test of condition code 39 Uses 23,24 Index 143 BRANCH ON COUNT (BCT) 23,73,80,83,99 BRANCH ON COUNT (BCTR) 23,91 Branch on Equal (BE) extended mnemonic 49 BRANCH ON INDEX HIGH (BXH) 23,47 BRANCH ON INDEX LOW OR EQUAL (BXLE) 23, 46-49,113 Branch on Low (BL) extended mnemonic 44 Branch on Minus (BM) extended mnemonic 99,102 Branch on Not Equal (BNE) extended mnemonic 99 Branch on Not High (BNH) extended mnemonic 94 Branch on Overflow (BO) extended mnemonic 110 Branch Unconditional (B) extended mnemonic 98,101 110,113 Branch Unconditional (BR) extended mnemonic 108 Branch, unconditional 39,42 Branching alternate times through loop 81 Branching by use of Execute instruction 99 Byte 17,18 CALL macro instruction 116,117,120,123 Calling program 108 Capacity of main storage 17 Capacity of registers 19 Central processing unit (CPU) 19 Character codes 21 Characteristic in floating-point number 126,127 Character testing and manipulation 80,82,83 Check bit 17 Check digit, use of 80 Clearing storage area to blanks 100,102 CNOP assembler instruction 113,114 Code conversion by use of TR 92,93 Coding form, use of 7 Collating sequence, EBCDIC 21,76 Collating sequence, changing normal 93,94 Comma and decimal point insertion 86-88 Comments, use of 8 COMPARE (C) 42-44,45,83 COMPARE (CR) 40 COMPARE DECIMAL (CP) 71 COMPARE HALFWORD (CH) 49 Compare instructions, logical contrasted to arithmetic 76 COMPARE LOGICAL (character) (CLC) 76,77,81,93,94 COMPARE LOGICAL (immediate) (CLI) 93 Comparison of alphanumeric characters 76,77 Complementing bits 79 Composite number problem 82,83 Condition code 23,39 after COMPARE 40 in EDIT 89 List of instructions that affect 130,139 Masks for testing 39 Summary of result settings 140 after TEST UNDER MASK 78 in TRT 97 144 Constant, address (see address constant) Constants, assembling of Effect of PRINT DATA on 94 Constants, types of 9,127,142 Control characters in EDIT 86-90 Control program 5,30,115,123 Control section 115 Conversion Binary-hexadecimal-decimal numbers 26-28 Character codes 92,93 Zoned-packed formats 69,70,81 CONVERT TO BINARY (CVB) 73,100 CONVERT TO DECIMAL (CVD) 9,70,102 Core storage (see main storage) Counter, location 6,9,10,54,115 CPU (central processing unit) operations 19 Cross-reference table 6,8,52 CSECT assembler instruction 115 Currency symbols, floating 91 Data constants (see DC assembler instruction) Data exception 25 Data format 17,20-22,60,75,126 (See also individual types of data) Data introduced in instructions 75,113 DC assembler instruction 2,10,11,31,127 Types of constants 142 Decimal-binary number conversion 28 Decimal data format 20,21,60 Decimal data manipulation 82,83 Decimal digits, clearing excess 63,71 Decimal feature instruction set 60,139 Decimal-hexadecimal number conversion 27,28 Decimal operations 20,21,60-73 Addition and subtraction 61-62 Comparison 71 ,72 Division 64,68 Effect on condition code 140 Format conversions 69,70 Multiplication 63 Providing storage locations for data 62 Requirement for packed format 21,60 "Shifting" 66-68 Decimal places, rule for number of 64,68 Decimal point and comma insertion 86-88 Decimal point in binary numbers 34,35 Decimal point in floating-point arithmetic 125,127 Decimal signs, clearing excess 67 Decision-making 23,39,42-44 Delimiters, scan of data fields for 97,100 Diagnostic messages 6,12-14 Dictionaries 6,115 (see also ESD and RLD) Digit selector 86-90 Disk Operating System (DOS) macros 111 Displacement 11,24,51 .. 57 Assignment of smallest 56 Restrictions in size and sign 54,55 DIVIDE (D) 34,42-44 DIVIDE (DDR) 133 DIVIDE DECIMAL (DP) 64,68 Before and after results of operation 64 DIVIDE (DER) 132 DIVIDE (DR) 49,113 Dollar amounts, editing of 87-91 Dollar sign, insertion of floating 91 DOS (Disk Operating System) macros 111 Doubleword 10,17,18 Doubling a binary number by shifting 49 DROP assembler instruction 57 DS assembler instruction 2,10,11,31 Length modifier in 61 Zero duplication factor in 56,70 Dumps, "reading" 117-119,121-123 EBCDIC character set 21,11 Collating sequence 21 Complete 256-position chart 140,141 EDIT AND MARK (EDMK) 85,86,91 Use of register 1 91 EDIT (ED) 85-90 Editing Conditions and results, summary of 88 Dollar amounts 87-91 Multiple fields 90 Pattern, rule for length 87 EJECT assembler instruction 3 END assembler instruction 10,11,31 Entries on coding form, rules for 7,8 ENTRY assembler instruction 121 Entry point 121 Entry point register 111 EOJ (End of Job) macro instruction 3,9,30 Error analysis by assembler 12-14 Error code 13 Error detection by CPU 23 Error detection by use of TRT 97 Error messages 12-14 Error return 110 ESD (external symbol dictionary) 6,109,115,119,121 Even, odd registers in BXLE 46,47 in fixed-point multiplication 32 Exception 25,32,114,132 Excess-64 notation 126 EXCLUSIVE OR (immediate) (XI) 79 EXCLUSIVE OR (XR) 78 EXCLUSIVE OR (XR, X, XI, XC) 78 Executable instructions 1,3 EXECUTE (EX) 85,99,100,102 Explicit length specification 60,61,77 Exponent 126,127,131 Exponent overflow and underflow 132 Extended binary coded decimal interchange code (See EBCDIC character set) Extended mnemonic code 44,78,79,83,140,142 External interruption 25 External references 115 External symbol 120,121 External symbol dictionary (ESD) 6,109,115,119,121 EXTRN assembler instruction 120 F (fullword) 31 Field length 17 Characters 21 Fixed-length (fixed-point numbers) 20 Variable length (decimal numbers) 20 Field separator 90 Fill character 86-90 Fixed-length data, alignment of 10,18 Fixed-point operations 19,20,29-50 Addition 30 Decimal point in 34,35 Division 34,35 Effect on condition code 140 Multiplication 32,33,34,35 Number format 20,82 Subtraction 30 Floating currency symbols 91 Floating-point operations 21,125-133 Advantages of using 125 Constants 127-129 Instruction set 130 Number format 126-129 Registers 19,21,130 Flowchart, program 40,42,82 Format Machine instruction 10,11 ,23,24,139 (see also RR, RX, SI, RS, SS) Data 17,20-22,60,75,126 (see also specific data formats) Fraction in floating-point number 126,127,130 Fullword 10,17,18,20,31 Function bytes 93,97 Function table 92-94,97,100 General registers (see registers, general) Graphic characters 21,22,141 Guard digit 132 Halfword 10,17,18,20 Halfword operands, machine instructions using 48 HALVE (HDR) 133 HALVE (HER) 132 Hexadecimal code 10,11,21 For EBCDIC characters 22,86-93,141 Four-bit binary code 26 Interpreting in storage printout 26,117-119,121-123 Uses of 26,27 Index 145 Hexadecimal-decimal number conversion 27,28 Hexadecimal dump of storage area 117 Hexadecimal numbers 26-28,33 ICTL assembler instruction 7 Immediate operand 23,24,75,78,79 Implicit length 60,61,77 Indexed addressing 45,46 Index, limit value of 46,47,49 Index register 24,45,46,49,80,83 Index register zero 11 "Indian" problem 73 Initialization of program 48 INSERT CHARACTER (IC) 69,80,83 Instruction address 23 Next sequential III PSW 25 Length code in PSW 25 Instruction execution out of sequence 99 Instruction format 10,11,23,24,139 (See also RR, RX, SI, RS, and SS) Instruction sets, System/360 Decimal feature 60,139 Floating-point feature 130 Standard 29,139 Integral boundaries ~ 8 Interchanging addresses instead of records 77 Interchanging contents of registers 40,76,94 Interest problem In decimal arithmetic 63 In fixed-point arithmetic 34,35 Using both decimal and binary 73 Interruption, program 25 Interruptions, types of 25 Interruption system 23,25 Invalid delimiter 13 Invalid symbol 13 Inventory problem In decimal arithmetic 61 In fixed-point arithmetic 29 I/O devices, requirement for zoned decimal format 21,60 I/O interruption 25 I/O operations Use of DC's and DS's as substitute 31 ISEQ assembler instruction 7 Keys, sorting on 76,77,93-96 Leading zeros, suppression of 86-88 Left "shifting" in decimal operations 67 Length code in machine instructions 60,61 Length modifier in DC 61 Limit value of index 46,47,49 Linkage editor 6,53,109,115-118,121,123 Linkage registers 111 Linkage subroutine 107-124 146 Listings produced by assembler 6 Literal 113,117 LOAD ADDRESS (LA) 46,47,48,80,91,97,100,111 LOAD AND TEST (LTR) 49 LOAD COMPLEMENT (LCDR) 133 LOAD COMPLEMENT (LCER) 131 LOAD HALFWORD (LH) 48 LOAD (L) 9,11,29,30,42-44,70,113 LOAD (LD) 133 LOAD (LE) 130 Load location, program 53,117 LOAD (LR) 40,42,43,48 LOAD macro instruction 121,123 Load module 115,116 LOAD MULTIPLE (LM) 40,77 LOAD POSITIVE (LPR) 40 Location counter 6,9,10,54,115 Logical instructions, using RX forms of 79 Logical operations 21 Logical operations 75-104 Effect on condition code 140 Summary of 75 Long precision 130,133 Loop control 23 Looping with an index 45-49,80,83,113 Loop, parts of 45 Loop reiteration by loading of base address 57 Loops, counting in 73,80,81 Machine check interruption 25 Machine instruction format 10,11,23,24,139 (See also RR, RX, SI, RS, SS) Machine instructions In assembler language 1,4 (See name of individual instruction) Machine instructions, list of 130,139 Machinelanguage 1 Machine operations, basic concepts of 9,10 Macro instructions 3,4 Effect of PRINT instruction on listing 29,94 (See individual mnemonics) Macros for subroutine linkage, DOS 111 Main storage 17,18 Addressing 24 Use of in decimal arithmetic 20 Main storage area, contents of, printed out 117,118,122 Mask Use of hexadecimal code 26,69 Use in testing 24 Mask field in machine instructions 24 Message characters 87-90 Mnemonics 1,2,3 Machine instructions, list of 139 Assembler instructions, list of 142 Mnemonic code, extended 44,78,79,83,140,142 MOVE (characters) (MVC) 57,61,66,68,73,77,86,90,94 Propagating character through field, use for 89,97,102 MOVE (immediate) (MVI) . 100 Move instructions, use for "shifting" decimal fields 66,67 MOVE NUMERICS (MVN) 63,66,71,73,75 MOVE WITH OFFSET (MVO) 60,66,67,71 MULTIPLY DECIMAL (MP) 63,73 MULTIPLY (M) 32-35,42-44 MULTIPLY (MD) 133 MULTIPLY (MDR) 133 MULTIPLY (ME) 130,131 MULTIPLY (MER) 133 Name of program In CSECT statement 115 In START statement 9,115 Naming fields in a storage area 57 Negative fixed-point numbers 20,31,38 Negative floating-point numbers 128 No-Operation (NOP) extended mnemonic 113,114 No-Operation (NOPR) extended mnemonic 39,99,140 Normalization 131 Normalized number 126,129 Notation, scientific 126 Number base 27,126 Number of places in decimal division 64,68 Numerical place values in base 2, 10, and 16 27 Object code 1,5,10,115 Object deck 120 Object module 115 Object program 5,10 Operand 1,8,20,21,23 Operating system 5 Operation code 1,23,24 ORG assembler instruction 54,55 OR (immediate) (01) 79,83 OR (0) 69,81 Overflow in floating-point fraction 131,13 2 Overlapping fields in decimal operations 66,67,73 Overtime pay problem 71 PACK (PACK) 60,70,73,83,100 Packed decimal numbers 20,60,61,69,70,81 Parameter list III ,117 Parity bit 17 Pattern field in EDIT 86,87 PDUMP macro instruction 117,121 Pool, literal 113 Postnormalization - 131,132 Prenormalization 131,132 PRINT assembler instruction 29,94 Printout of storage area contents 117,118,122 Program checkout, use of dump for 117 Program execution 6,23-25,118-119,121-123 Use of main storage in 17 Program interruption 25 Program listing 6 Programming errors 12-14 Programming support system 5 Program phase 115 Program status word (PSW) 23-25 Propagating character through field 89,97,102 PSW (program status word) 23-25 Punch card patterns for EBCDIC code 22,140,141 Recomplement 33 Records, processing fixed-length 45 Records, processing fixed-length blocked 56,57 Register contents shown in printout 117-119,121-123 Registers, floating-point 19,130 Registers, general 19,20 Available to programmer 5 1 Clearing to zero 42,45 Functions in a program summarized 48 Saving contents of 111,112 Specification 24 Standard assignments III Registers, general, use of with BXLE instruction 46,47 in fixed-point multiplication 46,47 by operating system 51 in shifting operations 37 for subroutine linkages 108,111 Registers 1 and 2, use of, by TRT 97 Register zero 10,11 Register 1, use of, by EDMK 91 Relative addressing 9 Relocatability 115 Relocation dictionary (RLD) 6,115,119 Relocation factor 53,115 Relocation, program 24,53,115-119 RETURN macro instruction 113,123 Return point, subroutine 108,110,111 Return register 111 Right "shifting" in decimal operations 66 RLD (relocation dictionary) 6,115,119 Rounding errors, accumulated 43 Rounding off 34,35,42,43,49 In decimal arithmetic 63,68,71 RR instruction format 11,23,24 RS instruction format 11,23,24 RX instruction format 11,23,24,29 Save area 111,112,123 SAVE macro instruction 112,113 Scale factor For binary constant 3S Scanning data fields for delimiters 97,100 Scientific notation 126 Sequential instruction execution 23 Setting bits on and off 79 Shifting, fixed-point 34,37,38,113 Index 147 Shifting, logical contrasted to algebraic 37,38 "Shifting" of decimal fields 66,67 SHIFT LEFT SINGLE (algebraic) (SLA) 9,11,49,52,83, 110 SHIFT LEFT SINGLE LOGICAL (SLL) 80 SHIFT RIGHT DOUBLE (algebraic) (SRDA) 38 SHIFT RIGHT DOUBLE LOGICAL (SRDL) 37,38 SHIFT RIGHT SINGLE (algebraic) (SRA) 35,38 SHIFT RIGHT SINGLE LOGICAL (SRL) 37,38 Short precision 130 Sign bits In algebraic shifting 35 In fixed-point numbers 20,32 In logical shifting 37 Propagation in halfword instructions 49 Sign control in EDIT 88 Significance exception 132 Significance indicator 87-90 Significance starter 87-91 Sign of numbers EBCDIC 20 Fixed-point 20 Floating-point 126,128 SI instruction format 23,24,139 Social security tax problem In decimal arithmetic 72,73 In fixed-point arithemtic 42-44 Sorting problem 39-41,76,77,93-96 Source deck 5 Source of program interruption, locating 25\ Source program 5 Specification exception 32,114 SS instruction format 23,24,56,60-73,139 Standard instruction set 29,139 Standard linkage registers 111 START assembler instruction 9,52,53,115 Use of zero in 61 Status, program (in PSW) 25 Storage addressing 18,24 Storage, main 17,18,20 STORE CHARACTER (STC) 69,80,83 STORE HALFWORD (STH) 37 STORE MULTIPLE (STM) 40,112 STORE (ST) 9,30,43,44,113 STORE (STD) 133 STORE (STE) 133 Subroutine 107-123 Assembled separately 120-122 Entry point 111 Program design 107 Register availability for 113 Relocatable 115 SUBTRACT DECIMAL (SP) 62 SUBTRACT HALFWORD (SH) 49 SUBTRACT NORMALIZED (SD) 133 148 SUBTRACT NORMALIZED (SE) 132 SUBTRACT (S) 9,30 Decrementing an indexed address 46 SUBTRACT (SR) 42,43,45,48,80,113 Supervisor 5 SUPERVISOR CALL (SVC) 30,123 Interruption 25 Suppression of leading zeros 86-88 Symbol (cross-reference) table 6,8,52 Symbolic address 9,24,30 Syntax error 13 Table, function 92-94,97,100 Table of decimal-hexadecimal values 28 Table requirements for Translate instruction 92,94 Table requirements for TRT instruction 97,100 Table, use ofMVI to insert values in 100,102 Teletypewriter code, Baudot 92 Testing bit combinations 78,79 Testing characters 82,83 Testing, terminating loop by 45 Testing the condition code 23,32 Tests Of address arithmetic 23 Of counting 24 With masks 24 TEST UNDER MASK (TM) 78,79 TITLE assembler instruction 2,9,120 TRANSLATE AND TEST (TR) 85,97-104 Starting address for 100 Use to determine data length 102 Translate table 92-94 TRANSLATE (TR) 85,92-94 TRT table, -97,100 Two's-complement notation 31,33 Type specification of constants 10,86,127,142 Undefined symbol 12,13 Unnormalized number 126 UNPACK (UNPK) 60,70,73,102 USASCII code 20,76 USING assembler instruction 3,9,10,51-57 Valid data, checking for 44,93 Variable-length data, use by SS-format instructions 60 Variable length of decimal data 20· Wallpaper problem 78,79 Work areas in storage -Setting up in decimal operations 63,71 ZERO AND ADD (ZAP) 63,66,73 Zeros, suppression of leading 86-88 Zone and sign tests 82,83 Zoned decimal numbers 21,60 Conversion to packed 69,70,81 READER'S COMMENT FORM C20-1646-5 A Programmer's Introduction to IBM System/360 Assembler Language Please comment on the usefulness and readability of this publication, suggest additions and deletions, and list sp~cific errors and omissions (give page numbers). All comments and suggestions become the property of IBM. If you wish a reply, be sure to include your name and address. COMMENTS fold fold fold fold • Thank you for your cooperation. No postage necessary if mailed in the U.S.A. FOLD ON TWO LINES, STAPLE AND MAIL. YOUR COMMENTS PLEASE ... Your comments on the other side of this form will help us improve future editions of this publication. Each reply will be carefully reviewed by the persons responsible for writing and publishing this material. Please note that requests for copies of publications and for assistance in utilizing your IBM system should be directed to your IBM representative or the IBM branch office serving your locality. fold fold ...................................................................................................................... FIRST CLASS PERMIT NO. 10 ENDICOTT, N. Y. BUSINESS REPLY MAIL NO POSTAGE NECESSARY IF MAILED IN THE UNITED STATES POSTAGE Will BE PAID BY ... IBM Corporation 1701 North Street Endicott, N. Y. 13760 Attention: DP Education Development, Dept. 611 ..................................................................................................................... fold International Business Machines Corporation Data Processing Division 112 East Post Road, White Plains, N.Y. 10601 [USA Only] IBM World Trade CDrporation 821 United NatiDns Plaza, New York, New York 10017 [International] fold International Business Machines Corporation Data Processing Division 112 East Post Road, White Plains, New York 10601 (USA only) IBM World Trade Corporation 821 United Nations Plaza, New York, N. Y. 10017 (International) C20-1646-5
Source Exif Data:
File Type : PDF File Type Extension : pdf MIME Type : application/pdf PDF Version : 1.3 Linearized : No XMP Toolkit : Adobe XMP Core 4.2.1-c043 52.372728, 2009/01/18-15:56:37 Producer : Adobe Acrobat 9.13 Paper Capture Plug-in Modify Date : 2009:09:02 16:28:25-07:00 Create Date : 2009:09:02 16:28:25-07:00 Metadata Date : 2009:09:02 16:28:25-07:00 Format : application/pdf Document ID : uuid:e4cb46fd-e40d-4495-8756-1af47ddc68a0 Instance ID : uuid:4a2697ad-7686-4e25-9c9c-6e7ee75df998 Page Layout : SinglePage Page Mode : UseOutlines Page Count : 159EXIF Metadata provided by EXIF.tools