Encoding and Decoding

One of the skills we need to master in this course is the encoding and decoding of instructions.

Encoding refers to translating an assembly-language instruction into machine code.

Decoding refers to translating machine code into an assembly-language instruction.


Any given instruction is encoded in some format. Formats are listed in the yellow card. The first two formats we see in the course are RR and RX. More formats will be seen later.

Each instruction has an op code (1 byte long) identifying it. This is always the first byte in the encoded instruction.

Notice that when we write assembly-language instructions, we normally write numbers (such as displacements) in base 10, but when these are encoded, we end up with everything in base 16.

RR format: (instructions involving 2 registers)

8 bits of op code
4 bits to identify the first register
4 bits to identify the second register

RX format: (instructions involving a register R and a D(X,B) address)

 8 bits of op code
 4 bits to identify the register R
 4 bits to identify the index register X
 4 bits to identify the base register B
12 bits giving the value of the displacement D


Examples

         L     3,4(5,6)

Here we have R = 3, D = 4, X = 5 and B = 6. The op code for L is 58.

The encoded form is: 5835 6004

         AR    7,8

Here we have registers 7 and 8. The op code for AR is 1A.

The encoded form is: 1A78


Exercises

For each of these, determine the encoded form of the instruction.

  1. ST 10,178(3,12)

  2. CR 13,2

For each of these, determine the decoded form of the instruction.

  1. 19 B3

  2. 5B80 F114


Implicit Addresses

If we have an implicit address, as in

         ST    7,NUM1

or

         L     3,TAB(4)     Here 4 is an index register.

we will have:

B = whatever base register is specified on the USING statement (typically 15 at this point in the course)

X = 0 unless an index register was specified (such as 4 in the above example)

D = location counter value of the label - value of the base register; the latter is typically 0 at this point in the course