ASM USING Tutorial | Assembly Language | Instruction Set

Please download to get full document.

View again

of 9
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Information Report



Views: 9 | Pages: 9

Extension: PDF | Download: 0

Related documents
Chapter 15 - A USING Instruction Tutorial Introduction One of the most challenging concepts encountered by students of IBM Assembler Language is that of the USING assembler instruction, or directive. It doesn' t generate any object code itself, but it directly influences how machine instructions are assembled. It is a promise by the programmer that certain register(s) will be available at execution time, and so determines which base registers and displacements are chosen by the assembler when co
  90 Chapter 15 - A USING Instruction Tutorial Introduction One of the most challenging concepts encountered by students of IBM Assembler Language isthat of the USING assembler instruction, or directive. It doesn't generate any object codeitself, but it directly influences how machine instructions are assembled. It is a promise by the programmer that certain register(s) will be available at execution time, and so determineswhich base registers and displacements are chosen by the assembler when converting implicitaddresses.The most common application of USING is in program addressability. The following bit of code is fairly typical (although not necessarily recommended): 1 MAIN CSECT2 STMINST STM 14,12,12(13) Save regs; note explicit base (13)3 USING MAIN,15 R15 will have A(MAIN) at execution4 LAINST LA 14,SAVEAREA Get address of new save area5 ST 13,4(,14) and save back pointer in new area6 ST 14,8(,13) and forward pointer in previous7 LR 13,14 Set new save area pointer8 BALR 12,0 Get address of next inst into R12,9 DROP 15 say we no longer have need of R15,10 USING PART1,12 and tell assembler our new promise11 PART1 B SAVEAREA+18*4 Remainder of program follows12 SAVEAREA DC 18F'-1'... This bit of code has two USING instructions, one for location MAIN and one for locationPART1. The first USING, at statement 3, is a promise that, no matter where the program isloaded at execution time, register 15 can be expected to contain the address of the firstinstruction of the program. This information is used only during the assembly of the implicitaddress (of SAVEAREA) in the LA instruction at statement 4, in order to construct the objectcode of statement 4 with a valid base (R15) and displacement.The second USING, at statement 10, is a promise that R12 will have the address of PART1 atexecution time. Note that this promise will be fulfilled when, at execution time, the BALR instruction at statement 8 is executed.Two definitions which will be very helpful in describing what follows are the domain and the range of a USING instruction. ! The domain of a USING instruction begins where the USING instruction appears in a program, and it continues until the end of the program, except when: A subsequent DROP instruction specifies the same base register  A subsequent USING instruction specifies the same register   91 ! The range of a USING instruction is the 4096 bytes within a Control Section (CSECT) beginning at the base address specified in the USINGIn order for the assembler to convert implicit addresses to base/displacement, any instructionwhich refers to an implicit address must be situated within the domain of a USING whose range includes the referenced location.Here is the same bit of code with domain s and range s indicated: 1 MAIN CSECT <-|2 STMINST STM 14,12,12(13) |Domain of |-> 3 USING MAIN,15 |USING at | 4 LAINST LA 14,SAVEAREA | Range of USING atStmt 3 | 5 ST 13,4(,14) | Statement 3 (up| 6 ST 14,8(,13) | to 4096 bytes)| 7 LR 13,14 || 8 BALR 12,0 ||-> 9 DROP 15 |Domain of|-> 10 USING PART1,12 |USING at | 11 PART1 B SAVEAREA+18*4 | <-| Range of USINGStmt 10 | 12 SAVEAREA DC 18F'-1' | | at Stmt 10 (upV ... V V to 4096 bytes) In the example above, the range of the USING at statement 3 begins at location MAIN andends at location MAIN+4095 (or earlier if the program is shorter than 4096 bytes). Thismeans that location SAVEAREA referenced in statement 4 must be located between MAINand MAIN+4095. The domain of that USING begins at statement 3 and ends at statement 9(the DROP instruction). This means that no instructions following statement 9 can beassembled with R15 as a base register.Similarly, the range of the USING at statement 10 is PART1 through PART1+4095. Its domain begins at statement 10, so no instruction which preceeds statement 10 can beassembled with R12 as a base register. Note that the STM instruction at statement 2 is not within the domain of any USING, so itcannot have an implicit address for its second operand (which must be given explicitly, as it isin the example). Also, since statement 2 is not within the range of the second USING, labelSTMINST cannot be specified as an implicit address on any instruction following the DROP atstatement 9 (assuming only these two USINGs, of course).It is clearly possible for USINGs to have overlapping range s, as is the situation in the exampleafter Statement 10. This is unimportant, though, because the USINGs do not have overlapping domain s, and so the assembler has only one USING available for each instruction from whichto select a base register. When it happens that there are USINGs with both overlapping domain s and overlapping range s, the assembler chooses for each implicit operand the baseregister which will result in the smallest displacement. This fact is important in what follows.  92 As an aside, it is probably useful to notice that, in general, the instruction which will actuallyset the promised contents of a base register need not be proximate to the correspondingUSING. In our example, the second USING almost directly follows the instruction which willset the register, while the instruction setting the register contents promised by the first USINGis nowhere to be seen. A Closer Look   Now, let's examine what the assembler does when its processes this program, particularly howit handles: 4 LAINST LA 14,SAVEAREA Get address of new save area and 11 PART1 B SAVEAREA+18*4 Remainder of program follows Since Statement 4 lies within the domain of the USING at Statement 3, and sinceSAVEAREA is within the range of that USING, R15 is an available base register. Since it isthe only register available, the assembler chooses it. Since label SAVEAREA is4+4+4+4+2+2+4=24=X'18' bytes into the USING range , the instruction is assembledas X'41E0F018' [  student: verify this ].Similarly, since Statement 11 lies within the domain of the USING at Statement 10, and sinceSAVEAREA+18*4 is within the range of that USING, R12 is an available base register.Since it is the only base register available (R15 was DROPped), the assembler chooses it.Since SAVEAREA+18*4 is 4+18*4=76=X'4C' bytes into the USING range , theinstruction is assembled as X'47F0C04C' .All of this should be verified by the student. Nothing unusual has been done, and verificationshould be straightforward. It should be clear that the reason Statement 3 is coded just so is theexpectation that R15 really will  have the memory address of the first byte of program code (theSTM instruction) at the time of execution. In fact, let's see what would happen if we lie to theassembler and tell it something which is incorrect! Let's change Statement 3 to pretend thatR15 will have the address of LAINST (instead of MAIN) at execution time: 3 USING LAINST,15 R15 will have A(LAINST) at execution Since label SAVEAREA is now 4+4+4+2+2+4=20=X'14' bytes into the USING range,the instruction at Statement 3 is assembled as X'41E0F014' . Of course, we lied when wewrote the new Statement 3, but the assembler didn't know that and went ahead as though wewere telling the truth. Now the object code for statement 4 actually loads the address of four  bytes before SAVEAREA rather than SAVEAREA.  93 Our conclusion is that the assembler is very gullible! It believes whatever we tell it and then behaves accordingly. It doesn't know what we intend, only what we write. On the one hand,this means we must be careful in writing our USINGs so that the assembler generates thedesired object code. On the other hand, this also means that we can trick the assembler intogenerating the object code we want, in circumstances where telling the truth simply isn't possible. The remainder of this note describes how to fool the assembler! (Well, not really,of course, but it helps to think of it that way.) How Can We Use Implicit Addresses Everywhere? Let's consider that program MAIN above begins a typical first semester exercise for thestudent of assembler. In this exercise, the student is to process two tables. Each table willhave, say, up to 25 entries, and each entry will have, say, two fields: name (22 characters) andage (fullword binary), in that order. Before beginning the process (whatever that might be),each table entry must be initialized so that the name is blank and the age is zero. Thus, we willhave a routine called INIT to which is passed each of the tables, in turn, for initialization: [tosimplify the code, the address of the table, rather than a parameter list, is passed] ...LA 1,TABLE1 Get address of first tableBAL 14,INIT and pass to INITLA 1,TABLE2 Do the same for the second tableBAL 14,INIT... When INIT receives control, it does the following: ...SR 0,0 Initial age value=0LA 10,25 Get counter for loop (size of table)LR 11,1 Copy passed address of table* LOOP MVC 0(22,11),BLANKS *1* Set name to blanksST 0,24(,11) *2* and age to zeroLA 11,28(,11) *3* Get to next entry *BCT 10,LOOP Continue until done...BLANKS DC CL22' ' Initial value for name... For the purpose of this discussion, let's assume that the data BLANKS is located at X'63A' bytes into the range of the USING at statement 10 and that the LOOP code lies within the domain of that USING. This means that the object code generated by the three statementsmarked *1*, *2*, and *3* is [  student to verify this ]: D215B000C63A5000B01841B0B01C
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks