Base Displacement Addressing
A Visible/Z Lesson
Internal memory on an IBM mainframe is organized as a sequential collection of bytes. The bytes are numbered starting with 0 as pictured below,
The IBM System/z architecture supports several addressing modes. The mode we focus on for beginning assembly language allows each address to be expressed as a collection of 31 consecutive bits. The smallest address would be represented by 31 consecutive 0’s which denotes address 0. The largest address would be represented by 31 consecutive 1’s whose value is 231 - 1 = 2,147,483,647. If we work with addresses in this form (let’s call these direct addresses), each address occupies a fullword, or four bytes.
Why do we need an address in the first place? Consider the following instruction,
In order for the machine to move the contents of COSTIN to COSTOUT, it must know the locations of these two fields. The computer identifies a field by the address of the first byte in the field. When an instruction is assembled, the assembler converts any symbols like “COSTOUT” to their corresponding addresses. The assembler does not, however, generate direct addresses, but produces an address in base / displacement format. Addresses in this form consist of 4 hexadecimal digits or two bytes, BDDD, where B represents a base register and DDD represents a displacement. The smallest displacement is x’000’ = 0 and the largest displacement is x’FFF’ = 4095. Ultimately, the base / displacement address must be converted to a direct address. So how does this occur? The diagram below indicates how this process occurs starting with the base/displacement address x’8003’.
Notice that the effective address, which is the direct address equivalent to the base/displacement address, is computed by adding the contents of the base register x’1002’ and the specified displacement of x’003’. This produces an effective address of x’1005’.
Effective address = Contents( Base Register) + Displacement
There are two advantages of using base/displacement addresses instead of direct addresses in the object code that the assembler produces:
1) Every address is shorter. Instead of being 4 bytes long, the addresses are only 2 bytes, so all our programs are shorter.
2) The base/displacement addresses are correct no matter where the program is loaded in memory. Each symbol is represented by a displacement from a fixed point inside the program. If the program is relocated in memory, the displacement to a given variable does not change. The base register remains fixed as well. The main thing that changes when we relocate a program is the contents of the base register. This can be handled when the program runs. As a result, the base/displacement address is correct. On the other hand, if we had used direct addresses, every symbol would have a new address if the program were relocated.
Trying It Out:
1) Load the program basedisplacement.obj from the \Codes directory and single step through each instruction. Here is the original object code program with the object code listed on the left and the equivalent assembler instruction on the right:
0d c0 BASR R12,R0
d2 03 c0 08 c0 0c MVC X,Y
07 f0 BCR 15,R12
00 00 00 00 X DS F
ff ff ff ff Y DS F
2) Modify the program so that it swaps the contents of X and Y. This will require another fullword and some more MVCs. Write the object code that implements the swap. Step through it in VisibleZ. Check each MVC to make sure the target address is red and the source address is green.
3) Load and run basedisplacement1.obj. Notice that the program executes the same MVC instruction ( d2 03 c0 10 c0 14 ) two times. How is it possible that the same instruction moves different fields during these executions?