torsdag den 22. oktober 2015

Base register in assembler (1)

Introduction to Base Register

The base register for instructions and data areas seems to be one of the more difficult topics when programming assembler.
First of all, I will explain what the base register does for the program and the operating system. *1) The reason for using base register is that you (or the operating system) can load the program into whatever place in the storage (memory) where there is space. So the base register is usually the start of the program and every instruction can be located by adding a displacement (or offset) to the base register. Any of the 16 registers is eligible as baseregister except zero. An example:
Loc    Object Code    Addr1 Addr2  Stmt   Source Statement
00001E 0DB0                          46+         BASR  BASEREG,0
                R:B  00020          47+         USING *,BASEREG
-------Several instructions and data
000044 47F0 B030            00050    61+RSA      B     IHB0009C
-------Several instructions and data
000050                               67+IHB0009C DS     0H
000050 5800 B028            00048    68+         L     0,IHB0009L
From a assembler list
“Loc” is the location (or offset) of the instruction relative to zero. It is a counter for the assembler so it can keep track of where to place the instructions in the output object module.
Take a look at location 000044. That is a branch-instruction (a Go To) to label IHB0009C. Addr2 tells you the location of where to go to. However, the machine instruction (X’47F0’) says go to X’B030’. I means that the CPU is supposed to branch to what the base register (X’B’) points at in storage plus X’030’. How does the assembler know that? Well, take a look at the instructions above the branch instruction. These two instructions tells the assembler where the base register will point when the program is executed. It is loaded at the first instruction at location X’1E’. The BASR-instruction loads the actual location of the next instruction into BASEREG (X’B’). The next (assembler)instruction tells the assembler that the displacement is relative to here (Addr1 = X’00020’).
That explains why the branch has a displacement of X’030’ because it is relative to location X’00020’. The ‘R:B’ tells you that it will use register X’B’ as base for the displacement, hence the name Base Register.

Homework:

Tell me the Location of the label IHB0009L? The last instruction loads the value of the four bytes of that location.
Answer:
000048 00000048                      62+IHB0009L DC    A(SA_LEN)
Yes, it loads the full word from location X’48’ which is base register 11 (X’B’) plus X’28’. The base register points at location X’20’ in the program. 
BTW. It is a coincidence that the value and location is the same. But that did not confuse you. Am I right?
There is a minor problem with the displacement though. It can be maximum X’FFF’ which is 4095, due to the two bytes in the instruction and one half byte is occupied by the base register. Does that mean that a program can only be 4095 big? Cliffhanger!

I will come to that next time.
*1) I am not sure whether base register is used in other instruction sets.)

Ingen kommentarer:

Send en kommentar