Reading Objet Code A Visible/Z Lesson The Idea: When programming in a high-level language, we rarely have to think about the speifi ode that is generated for eah instrution by a ompiler. But as an assembly programmer, it is ritial that we be able to read the mahine ode that is produed for eah instrution. Otherwise, there will be programs that are extremely diffiult to understand and debug. VisibleZ was built to help you visualize eah instrution as it appears in objet ode and wath it exeute. When the assembler proesses an instrution, it onverts the instrution from its mnemoni form to a standard mahine-language (binary) format alled an instrution format. In the proess of onversion, the assembler must determine the type of instrution, onvert symboli labels and expliit notation to a base/displaement format, determine lengths of ertain operands, and parse any literals and onstants. Consider the following Move Charater instrution, MVC COSTOUT,COSTIN The assembler must determine the operation ode (x D2 ) for MVC, determine the length of COSTOUT, and ompute base/displaement addresses for both operands. After assembly, the result whih is alled objet ode, might look something like the following in hexadeimal, D207C008C020 The assembler generated 6 bytes (12 hex digits) of objet ode in a Storage to Storage (SS) type one format. In order to understand the objet ode whih an assembler will
generate, we need some familiarity with 5 basi instrution formats (there are many other instrution types overing privileged, non-privileged, and semi-privileged instrutions whih are beyond the sope of this disussion). First we onsider the Storage to Storage type one (SS1) format listed below. This is the instrution format for the MVC instrution above. Byte 1 - mahine operation ode Byte 2 - length -1 in bytes assoiated with operand 1 Byte 3 and 4 - the base/displaement address assoiated with operand 1 Byte 5 and 6 - the base/displaement address assoiated with operand 2 Eah box represents one byte or 8 bits and eah letter represents a single hexadeimal digit or 4 bits. The subsripts indiate the number of the operand used in determining the ontents of the byte. For example, the instrution format indiates that operand 1 is used to ompute L1L1, the length assoiated with the instrution. If we reonsider the assembled form of the MVC instrution above we see that the op-ode is x D2, and the length, derived from COSTOUT, is listed as x 07. Sine the assembler always derements the length by 1 when onverting to mahine ode, we determine that COSTOUT is 8 bytes long - 8 bytes will be moved by this instrution. Additionally we see that the base register for COSTOUT is x C (register 12) and the displaement is x 008. The base/displaement address for COSTIN is x C020. Why was register 12 hosen as the base register? How were the displaements omputed? These parts of the objet ode ould not be determined by the information given in the example above. In order to determine base/displaement addresses we must examine the USING and DROP diretives that are oded in the program. These diretives are disussed in the topi alled BASE DISPLACEMENT ADDRESSING on the website.
Being able to read objet ode is a neessary skill for an assembler programmer as knowledge of an instrution s format gives several important lues about semantis of the instrution. For example, knowing that MVC is a storage to storage type one instrution, informs us that both operands are fields in memory and that the first operand will determine the number of bytes that will be moved. Sine the length (L1L1) oupies one byte or 8 bits, the maximum length we an reate is 2 8-1 = 255. Reall that the assembler derements the length when assembling, so the instrution is apable of moving a maximum of 256 bytes. The 256 byte limitation is shared by all storage to storage type one instrutions. Storage to Storage type two (SS2) is a variation on SS1. Byte 1 - mahine operation ode Byte 2 - L1 - the length assoiated with operand 1 (4 bits) L2 - the length assoiated with operand 2 (4 bits) Byte 3 and 4 - the base/displaement address assoiated with operand 1 Byte 5 and 6 - the base/displaement address assoiated with operand 2 The only differene between SS1 and SS2 is the length byte. Notie that both operands ontribute a length in the seond byte. Sine eah length is 4 bits, the maximum value that ould be represented is 2 4-1 = 15. Again, sine the assembler derements the length by 1, the instrution an proess operands that are large as 16 bytes. There are many arithmeti instrutions that require the mahine to use the length of both operands. Consider the example below,
Objet Code Soure Code AFIELD DS PL4 BFIELD DS PL2... FA31C300C304 AP AFIELD,BFIELD AP (Add Paked) is an instrution whose format is SS2. Looking at the objet ode that was generated, we see that x FA is the op-ode and that the length of the first operand is x 3 whih was omputed by subtrating 1 from the length of AFIELD. Similarly, the length of BFIELD was used to generate the seond length of x 1. In exeuting this instrution, the mahine makes use of the size of both fields. In this ase, a 2-byte field is added to a 4-byte field. A third type of instrution format is Register to Register (RR). Byte 1 - mahine operation ode Byte 2 - R1 - the register whih is operand 1 R2 - the register whih is operand 2 Instrutions of this type have two operands, both of whih are registers. An example of an instrution of this type is LR (Load Register). The effet of the instrution is to opy the ontents of the register speified by operand 2 into the register speified by operand 1. The following LR (Load Register) instrution,
LR R3,R12 would ause register 12 to be opied into register 3. The assembler would produe the objet ode listed below as a result of the LR instrution. 183C Examining the objet ode we see that the op-ode is x 18, operand 1 is register 3, and operand 2 is register 12. You should note that 4 bits are enough to represent any of the registers whih are numbered 0 through 15. A fourth type of instrution format is Register to Indexed Storage (RX). Byte 1 - mahine operation ode Byte 2 - R1 - the register whih is operand 1 X2 - the index register assoiated with operand 2 Byte 3 and 4 - the base/displaement address assoiated with operand 2 For instrutions of this type, the first operand is a register and the seond operand is a storage loation. The storage loation is designated by a base/displaement address as well as an index register. The subjet of index registers is disussed in the topi BASE DISPLACEMENT ADDRESSING. L (Load) is an example of an instrution of type RX. Consider the example below.
L R5,TABLE(R7) The Load instrution opies a fullword from memory into a register. The above instrution might assemble as follows, 5857C008 The op-ode is x 58, operand 1 is speified as x 5, the index register is denoted x 7 and Operand 2 generates the base/displaement address x C008. Again, from the information given in the example above, there is no way to determine how the base/displaement address was omputed without looking at the USING statements in the program. Related to the RX type is a similar instrution format alled Register to Storage (RS). In this type the index register is replaed by a register referene or a 4-bit mask (pattern) R3 below. One instrution whih has a Register to Storage format is STM (Store Multiple). An example of how STM an be oded is as follows, STM R14,R12,12(R13) The previous instrution would generate the following objet ode, 90ECD00C
where x 90 is the op-ode, x E = 14 is operand 1, x C = 12, is treated as R3, and x D00C is generated from an expliit base/displaement address (12(R13)). The fifth and final instrution format that we will onsider is alled Storage Immediate (SI). In this format, the seond operand, alled the immediate onstant, resides in the seond byte of the instrution. This onstant is usually speified as a self-defining term. The format for SI instrutions is listed below. Byte 1 - mahine operation ode Byte 2 - I2I2 - the immediate onstant denoted in operand 2 Byte 3 and 4 - the base/displaement address assoiated with operand 1 An example of a storage immediate instrution is Compare Logial Immediate (CLI). This instrution will ompare one byte in storage to the immediate byte whih resides in the instrution itself. We see from the instrution format that operand 2 is the immediate onstant. For example, onsider the instrution below. CLI CUSTTYPE,C A When assembled, the objet ode might look like the following,
95C1C100 The op-ode is x 95, the self-defining term C A is onverted to the EBCDIC representation x C1, and the variable CUSTTYPE would generate the base/displaement address x C100. Again, there is not enough information provided to determine the exat base/displaement address for CUSTTYPE. The x C100 address is merely an example of what might be generated. Trying It Out in VisibleZ: 1) Load the program readingobjetcode.obj from the \Codes diretory and single step through eah instrution. Identify the type and parts of eah instrution below. The ode doesn t do anything but is representative of the different instrution formats you will enounter. Answers are provided at the end. 90 e d0 0 Instrution Type 90 e d 00 0d 0 Instrution Type 0d 0
41 20 0 0e Instrution Type 41 2 0 00e d2 03 0 16 0 1a Instrution Type d2 03 016 01a 92 1 0 26 Instrution Type 92 1 026
fa 42 0 1e 0 23 Instrution Type fa 4 2 01e 023 07 f Instrution Type 07 f Answers: 90 e d0 0 Instrution Type = RS 90 = op ode (STM) e d = operand 1 register = operand 3 register = operand 2 base register 00 = operand 2 displaement
0d 0 Instrution Type = RR 0d = op ode (BASR) = operand 1 register 0 = operand 2 register 41 20 0 0e Instrution Type = RX 41 = op ode (LA) 2 = operand 1 register 0 = operand 2 index register (none) = operand 2 base register 00e = operand 2 displaement d2 03 0 16 0 1a Instrution Type = SS1 d2 = op ode (MVC) 03 = length of operand 1 (minus 1) = operand 1 base register 016 = operand 1 displaement = operand 2 base register 01a = operand 2 displaement
92 1 0 26 Instrution Type = SI 92 = op ode (MVI) 1 = immediate onstant (operand 2) = operand 1 base register 026 = operand 1 displaement fa 42 0 1e 0 23 Instrution Type = SS2 fa = op ode (AP) 4 = length of op 1 (minus 1) 2 = length of op 2 (minus 1) = operand 1 base register 01e = operand 1 displaement = operand 2 base register 023 = operand 2 displaement 07 f Instrution Type = RR 07 = op ode (BCR) f = operand 1 register = operand 2 register