Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in

Lecture notes · chapter 3

Addressing modes

The different ways to designate an operand, in a register, in the instruction or in memory, and how they are used to walk through an array or read a field of a structure.

Designating an operand

An instruction acts on operands. The addressing mode says where to find them: in a register, in the instruction itself, or in memory at an address that must be computed. This computed address is called the effective address.

All the examples in this chapter start from the same situation: A0 is $2000, D1 is 4, and memory holds four words from $2000 onwards.

Address$2000$2002$2004$2006
Word$0011$0022$0033$0044

The modes, one by one

Register and immediate

ModeSyntaxThe operand is
data registerD0the contents of D0
address registerA0the contents of A0
immediate#12, #$FFthe value written in the instruction

No memory access. An immediate can only be a source: you cannot store anything into a constant.

Absolute

ModeSyntaxThe operand is
absolute$2004, or a label SOMMEthe contents of memory at that address

MOVE.W $2004,D0 stores $0033 into D0. It is the mode of the variables of chapter 2: MOVE.W VAL1,D0 reads the word at the address named VAL1.

Register indirect

ModeSyntaxEffective address
indirect(A0)the contents of A0

MOVE.W (A0),D0 reads the word at the address held in A0, that is $2000: D0.W is $0011. The parentheses read as “the contents of memory at the address…”. A0 acts as a pointer.

Post-increment and pre-decrement

ModeSyntaxEffect
post-increment(A0)+use the address A0, then advance A0 by the size of the operand
pre-decrement-(A0)move A0 back by the size of the operand, then use the address

The size matters: A0 advances by 1 for a byte, by 2 for a word, by 4 for a long word.

These two modes are used to walk through an array, element by element, and to manage the stack (chapter 5).

Displacement and indexed

ModeSyntaxEffective address
with displacement4(A0)A0 + 4
indexed2(A0,D1.W)A0 + D1.W + 2

Neither A0 nor D1 is modified.

  • MOVE.W 4(A0),D0 reads at $2004: $0033.
  • MOVE.W 2(A0,D1.W),D0 reads at $2000 + 4 + 2 = $2006: $0044.

The displacement is used to read a field of a structure whose start is given by A0; the indexed mode, to read element $i$ of an array.

Loading an address: LEA

LEA src,An (load effective address) computes the effective address of src and stores it in An, without reading memory.

  • LEA TAB,A0: A0 receives the address of TAB. That is how you make a register point to an array.
  • LEA 6(A0),A1: A1 receives A0 + 6, that is $2006.

Three uses

Walking through an array

We add up the first three words of the array TAB:

        LEA     TAB,A0      ; A0 points to the first word
        CLR.W   D0          ; sum <- 0
        ADD.W   (A0)+,D0    ; + TAB[0], A0 advances by 2
        ADD.W   (A0)+,D0    ; + TAB[1]
        ADD.W   (A0)+,D0    ; + TAB[2]

Writing one line per element will not get you far: chapter 4 will turn it into a loop. But the traversal is already there, in (A0)+.

Reading element i

The words of an array are 2 bytes apart: element $i$ is at address TAB + 2$i$. With $i$ in D1:

        LEA     TAB,A0
        ADD.W   D1,D1           ; D1 <- 2i
        MOVE.W  0(A0,D1.W),D0   ; D0 <- TAB[i]

Reading a field

A point of the plane is stored as two consecutive words, x then y. If A0 points to the point, x is at 0(A0) and y at 2(A0):

        MOVE.W  (A0),D0     ; x
        ADD.W   2(A0),D0    ; x + y

Try it

Run this program step by step, and follow A0 and A1 at each line: the post-increment and pre-decrement modes move them, the others do not. At the end, D0 to D5 hold what each mode read. Change D1, or the values of TAB, and start again.

        ORG     $1000
START   LEA     TAB,A0          ; A0 points to TAB
        MOVEQ   #2,D1           ; a byte index, for the indexed mode
        MOVE.W  (A0),D0         ; indirect: TAB[0]
        MOVE.W  (A0)+,D2        ; post-increment: TAB[0], then A0 advances by 2
        MOVE.W  4(A0),D3        ; displacement: A0 + 4
        MOVE.W  0(A0,D1.W),D4   ; indexed: A0 + D1
        LEA     TAB+8,A1        ; A1 just after the array
        MOVE.W  -(A1),D5        ; pre-decrement: A1 moves back by 2, then read
        SIMHALT

TAB     DC.W    $0011,$0022,$0033,$0044
        END     START