Addressing Modes and Machine Code

Addressing Mode:

  • Data which instructions operate on is stored in

Memory

Registers

In the instruction itself (memory)

  • Addressing modes are for machine instructions to allow the CPU to get the needed data.
  • Assembler manages where in memory each instruction and data item goes.
  • The assembler maintains a counter called LOCATION COUNTER “LC” that points to the next available byte of memory where the assembler could store machine code of data items. The book uses the “.” to represent LC. LC starts from 0 for each module.
  • LC is used only during the assembly time.

Machine Code

Each instruction will look like the following:

opcode

1 or 2

bytes

first operand

specifier

one or more

bytes

.

.

last operand

specifier

Each Operand Specifier

Mode

Byte

additional

information

zero or more

bytes

Mode Byte

mode number Reg. Number

Instruction Execution

PC points to the instruction opcode.

1- Get the opcode and increment the PC by one.

2- Decode the opcode, and determine how many operands

follow.

3- For each operand

a- get the mode byte and increment the PC by 1

b- from the mode byte determine the number of

additional bytes, fetch them and increment the PC by that number.

4- Repeat ...PC points to the next instruction.

Get Opcode andGet the Mode byte

decode itdetermine # bytes

PC =PC+1remaining

Get the rest of

operand and

increment.the PC

Addressing Modes:

  • There are 20 addressing modes that the VAX architecture supports.
  • There is a different machine representation for each mode.
  • For each mode we will answer the following two questions:

- Where is the operand (the data to be operated on)

- How to get this operand

  • All addressing modes use a mode byte except for literals

1- Register Direct

Syntax Rn

ex. ADDL2 R3, R4

  • Operand is in registers R0 - R14
  • n is the number of the register.
  • If data is less than 32 bits, then only the rightmost byte or word is used.
  • For data greater than 32 bits, the operand will be in Rn and Rn+1,....

Machine representation

- Takes only one byte (mode byte)

5 n

ex. MOVBR6, R4

2- Register Indirect (Deferred)

Syntax

(Rn)

  • Operand is in memory
  • Address of the memory location is in Rn
  • Use the register to get the address of operand, then put the operand in the scratch area in the CPU

ex.

ADDL2 R3, (R2)

Machine Representation

- One byte (mode byte)

6n

3- Register Deferred Autoincrement (Postincrement)

Syntax

(Rn)+

  • Operand is in memory
  • Address of memory location is in Rn
  • Use the register to get the address and save it in the scratch area in the CPU
  • Increment the Rn by 1, 2, or 4 depending on the data type being used B, W, or L respectively
  • Use the saved address to get the operand.

ex.

ADDL2 R4, (R5)+

Machine Representation

- One byte (mode byte)

8n
4- Register Deferred Autodecrement(Predecrement)

Syntax

-(Rn)

  • Same as autoincrement, except before getting the address from Rn decrement Rn by 1, 2, or 4 depending on B, W, or L
  • Use the new content of Rn as the address of the operand

ex.

ADDL2 R3, -(R6)

Machine Representation

- one byte (mode byte)

7n

ex. 0000 08D4R5B50000 08D2

95 0000 08D3

51 0000 08D4

700000 08D5

CLRW (R5)+

CLRW -(R5)

5- Literal and Immediate Modes

Syntax

#expression

  • Operand is specified in the instruction
  • Literal and immediate look the same as far as the assembly programmer is concerned.

ex.

ADDL2 #3, R4

here the first operand is given directly to the instruction.

  • This mode can not be used as a destination location.
  • In machine language the literal is represented as a six bit positive data item (0 - 63) where immediate may be longer.

Machine Representation

Literal....

- one byte (used to store the value of the operand)

- Operand must be a value in the range 0 - 63, otherwise it

will be treated as immediate value

00literal

Ex.

MOVW #25, R11

ADDL3 #5,R11,(R8)+

Immediate Mode:

  • Assembler uses it if the operand is larger than 6 bits (outside 0 -63)
  • In machine language, immediate is represented as an autoincrement mode with Rn being the PC (R15).
  • The operand is expressed in 1, 2, or 4 bytes depending on the data type B, W, or L respectively.

operand stored in the bytes following the mode byte8F 1, 2, or 4 bytes

ex.

ADDB2 #65, R1

ADDW2 #-3, R4

6- Displacement Mode

Syntax

disp(Rn)

  • Rn is one of R0 - R14
  • Operand is in memory
  • Rn has an address
  • To get to operand do the following

-Add the displacement that is stored with

the instruction to the content of Rn

-Use the result as an address of the

operand

- Rn value remains the same.

ex. A, C

ADDL2 23(R4), R5 or E n

Machine RepresentationDisplacement

1, 2, or 4 bytes

- Mode byte with mode number

A if the displacement fits in one byte

C if the displacement fits in one word

E otherwise

-The mode byte is followed by 1 ,2, or 4 bytes

depending on the displacement size.

- The assembler tries to fit the displacement in the smallest

possible size(memory)

- Displacement is a 2’s complement singed number

- it can be 1A

2C

4E

ex. MOVL29(R9), (R7)+

DIVW3 R5, R11, R5

CMPC3 #6, (R7), (R8)

7- PC-Relative

Syntax

Address_Expression

  • Operand is in memory
  • instruction has the address of the operand
  • The address of the operand is represented as a displacement from the PC. (therefore the name, relative to PC)
  • To get the operand

- get the displacement (distance from the PC)

- add it to PC

- use the result as an address to the operand

ex.

ADDL2 Alpha, Beta

CLRW Gamma

  • The address of the operand =

PC + displacement

Machine Representation

displacement 1, 2, or 4 bytesA, C, or E F

  • Assembler uses the LC value to calculate the displacement as follows:

disp = Symbol_value - LC

  • LC value must be the address of the next byte following the operand specifier.

ex.

Suppose that Alpha has an address value 0000 1264

Then

MULL3#3, Alpha, (R6)

LC C5

03

CF

B9

FE

LC=0000 13AB66

given

disp = 0000 1264 - 0000 13AB

= FFFF FEB9 (fits in one word, use C)

  • PC is always pointing at the next operand specifier, or the next instruction (while processing the last operand specifier)
  • When calculating the displacement, the assembler must take into account that PC will be updated while processing the operand.

Disp. = Symbol value - Location of the following byte after the operand specifier

SO,

We have Two unknowns in this calculation:

1 - LC is not known since the size of the displacement is not known

2 - Displacement is not known.
Assembler Solution

Use a trial and error technique.

1- try to fit the displacement in 1 byte, increment the LC by

2 (1 for the mode byte, and one for the displacement)

soLC = LC + 2

Calculate the displacement using the formula.

if the displacement is in the range -128 to 127 then

Done. store the displacement and use mode A

else Get the old value of LC, and go to step 2

2- try to fit the displacement in two bytes, increment the LC by 3 (2 for disp. and 1 for mode )

soLC = LC + 3

Calculate the displacement using the formula.

if the displacement is in the range -215 to 215 - 1 then

Done. store the displacement and use mode C

else Get the old value of LC, and go to step 3

3- Fit the displacement in 4 bytes (longword), increment the LC by 5 (4 for disp., and 1 for mode).

calculate the displacement, and use mode E.

ex.

Suppose that the LC = 0000 014D when the assembler reaches the instruction

SUBL3Alpha, R9, R10

and Alpha = 0000 00B8

LC = 0000 014D

* remember that LC points to the

next available byte.

ex.

BLBS(R6), ODD where ODD = 021C and this

instruction starts at 0242

ex.

SUBL2#8, Placewhere Place = 1A74 and this instruction starts at 0382

Why PC-Relative is used?

Addresses are encoded as a displacement from the PC rather than storing the absolute addresses why?

1- Save space (memory space)

Since the PC contains the right address always. (next instruction or next operand specifier). Instead of storing a 32 bit address we can use the PC as a reference and therefore, save on the number of bytes.

2- Addresses are maintained relative to the beginning of the module or program section. The program module will be linked with other programs and loaded into memory at different location each time it is used. True addresses are not known by the assembler at assembly time. CPU must compute the true addresses at run-time. The distance between the address (operand) and PC is always constant. Doing so assures that the program will run correctly any where in memory without adjustments.

Operand Expressions

In MACRO assembly, operands can be specified using an address expression.

ex.

ALPHA + 2

  • These expressions are for the assembler only (they are not executable expressions)

Therefore R1 + R2 is not valid.

  • Operators +, -, /, *
  • These operators do not have precedence, and are evaluated left to right.
  • The expression must be made up of symbols already defined.
  • All expressions always get evaluated to 32 bits.

ex.

ALPHA: .LONG 3

.LONG 5

BETA: .BLKW 2

Suppose we want to add 3 + 5

ADDL3 ALPHA, ALPHA + 4, BETA

  • After the assembler translates the instruction containing an expression, the expression will look like a symbol (address) for some memory location expressed in PC-relative mode.

8- Deferred Addressing Modes

Syntax

@operand_specifier

  • Operand is in memory
  • The operand specifier specifies the address of the address of the operand.
  • The operand specifier could be displacement, relative, or autoincrement.
  • The operand specifier is computed the usual way, and gives the address for the address of the operand of interest.

ex.

ADDL2 @ALPHA, R4

-here the first operand is in memory and its address is

stored at the memory location ALPHA.

ADDW2 @(R8)+, R10

-here the first operand is in memory, its address is stored in

the memory location whose address is in R8. R8 is

incremented by 4

** note in deferred autoincrement, the register is always incremented by 4 regardless of the data type why?

ADDB2 @34(R4), R7

Machine Representation

Deferred Displacement

  • Same as before except we use B (for byte disp.), D (for word disp.), or F (for longword disp.)

Displacement... B, D n

or F

Deferred Relative

  • Same as above except we use F for the register number n.

Deferred Autoincrement

  • Same as before except we use 9 for mode number instead of 8.

9 n

Assembler performs the following functions

1- Maintains symbol table containing the values of all user-defined labels and symbols

2- Location counter tells where the next instruction or data items will be placed in memory.

3- Translates symbolic instructions to binary object program

4- Produces a listing.

Assembler Task

The assembly process of a program is done in two passes. (Two-Pass Assemblers)

Pass One

  • During this pass the assembler constructs a symbol table. This table has every symbol and its value. Also the assembler expands macros definitions, and processes some of the directives in the source code.
  • To compute the symbol values correctly, it is critical that the assembler be able to determine during the first pass how many bytes the encoding of each instruction will require.
  • When the assembler encounter a source instruction with relative mode (PC-relative) operand during the first pass, it can evaluate the address expression if the symbols in the expression are already in the symbol table. It can compute the appropriate displacement using the LC, and chooses the smallest mode (A,C, or E) needed to represent the displacement in 2's complement.
  • If the symbol is not in the symbol table yet, the assembler will allow for the max possible displacement (longword).

Remember that the LC starts at 0 for each program section.

How are symbols assigned their values?

  • Symbols are either labels (an address) or user defined symbols (ex. x = 20)
  • User defined symbols are given the values associated with them.

ex.

X = 20

the assembler stores the symbol X in the table with the value 20 associated with it.

  • For labels, the assembler gives the value of LC at the time they are encountered. LC starts at zero for each program section.

ex.

Suppose that the LC = 0000 4405 at the time the

following statement is encountered by the assembler

ALPHA: .BLKB 3

the assembler will store the symbol ALPHA in the table with the value 0000 4405 associated with it.

Pass Two

In this pass the assembler translates the program into machine code. LC starts again from zero.

During first pass the assembler must compute the size of each instruction so it could give the right values to each symbol.