Addressing Modes

ALL ABOUT ADDRESSING MODES:

Moving into the depths of low-level programming, we land upon assembly language. Assembly language is the closest language to us programmers and hardware. The assembly language is hardware dependent. You can’t write the same code for all the devices that you are about to use. Before you write any assembly code, you need to look into the data sheet of the processor/controller you are using and write the code accordingly.

When we talk about coding in assembly, we definitely need to take care about the way your code accesses the memory. Apart from the main memory, the CPU’s have a small number of memory locations known as registers. There are only a limited number of registers on the CPU. When you write a code (in C) to add a value such as:

Int A=10;
Int B=20;
Int C =A+B;

The values of A and B are stored in the main memory of the system. When the operation is executed, the CPU instruction fetches the value of A and B stored in the main memory, keep it in registers, execute it and then send it back to the main memory where the data would be stored in C.

The memory location present in the main memory have addresses while the registers on the CPU have names.

Now, as I said, the data needs to be sent all over the place in the system for calculations and storage, we have different addressing modes where we can tell how the data can be accessed/addressed.

  • Register Direct Addressing
  • Memory Direct Addressing
  • Immediate Addressing
  • Indirect Addressing
    • Indirect Addressing with auto post increment
    • Indirect Addressing with auto pre decrement
  • Indexed Addressing

Register Direct Addressing:

As the name suggests, when this mode of addressing is used, the operand data is stored in the register itself i.e the instruction would contain the address of the register.

(Considering the intel x86)

Ex: MOV AX,DX

In the above operation, the contents of the register DX is moved into AX.

The contents of DX are destroyed and moved into AX.

Ex 2:
MOV A,B

With the MOV instruction is a byte-move instruction i.e only the least significant bytes of A and B registers are used. The higher bytes remain the same. To move even the upper bytes, use MOV.W.
            MOV.W A,B

The C language equivalent to the above statement would be:
            register int a,b;
            a=10;b=20;
            a=b;

Memory Direct Addressing:

Memory direct addressing also commonly known as direct addressing is the scheme/method where the value given is stored at a given location in the memory.

Example:

MOV.W 95 200

Here, the location 95 consists of the value 200. If I do some other operation such as MOV.W 95 99, then the location 95 consists of the variable 99. It’s similar to initializing variables in C.

int var =20; // the value 20 is initialized to the variable var

var =30; // the variable var is overwritten with 30. The new value of var is 30.

Immediate Addressing:

Immediate addressing mode is a way of specifying a value directly within an instruction. A ‘#’ is added before the number that is to be operated upon the location. Consider the following from the above example:

MOV #93, 233

The value 93 is directly kept into 233 memory location. It simply loads the value into the specified location. Another example would be

ADD #5, R1

The value 5 is directly added with the contents of the register R1 without being stored. It can be useful where we know we don’t want the variable to be stored and be operated upon. The C equivalent of the above is of immediate addressing mode is

register int loc1;
loc1= ‘&’; // Store the ASCII value of & (38) in the loc1 variable.

Indirect Addressing:

The first thought you get when you hear about indirect addressing is the pointers. We have already seen about that in one of the posts. Let’s direct to assembly indirection now.

The contents of memory cell whose address is in the indicated register or memory location is accessed. Simply said, the information held in the memory location referred to by the register or memory location provided is obtained. It uses the notation (register).

Example:

MOV (300),(199) ;This instruction moves the contents of the memory location 300 into 199.

Indirect addressing is an important concept as it allows manipulation of data. I.e, the register contains the memory location whose value is manipulated but not the register itself. Just as a pointer.

register int *ptr;
int val1,val2;
ptr=&val1;
*ptr=1000;
val2=*ptr; // Self Exp..

Indirect Addressing with post increment:

Indirect Addressing with post increment and post and pre decrement addressing modes are not the most commonly heard modes. What are they? The notation used for this mode is (register)+. The register holds the address of the cell that is to be accessed. Once this operation is performed, the register itself is changed after its contents are addressed. If the operation is performed on a byte sized object, then 1 is added to the register else, if the operation is performed on a word sized object, then 2 is added to the register. It is similar to the increment operator in C.

MOV A,10
MOV (A)+,B

A hold the address10, the next instruction transfers the content 10 to the register B, then A is automatically incremented to 11. The A and B registers are modified, but the content remains the same.

Indirect Addressing with pre decrement:

Similar to the above, this uses the notation –(register). Instead of incrementing after the operation, the register itself is decremented.

MOV A,10
MOV –(A),B

The register a holds 10 in the first instruction. Once the second instruction starts to execute, it first decrements the register location A, and then transfers the contents at the decremented location to register B. It is similar to the decrement operation (–) in C.

Indexed Addressing Mode:

To represent this addressing mode, we use, x(register) where x is added to the contents of register and then the contents of the cell whose address is the result of addition is accessed. Here x is simply an offset, it may either be positive or negative.

MOV A,150
MOV 20(A),B ; moves memory location (150+20) to B.

The A register isint modified in this case. This mode is useful while accessing single element in an array.

int array[5]={1,2,3,4,5};
int i;
array[2]=7; // modified to {1,2,7,4,5}

Similarly, it can be used in conditional loops.

Apart from the addressing modes, there are plethora of concepts that come into light in the assembly. The instruction sets, labels, comments, compilation and more. In the next article, I would be describing about the general process of subroutine processing.

Stay tuned, See you soon.

Follow to be updated with content on Embedded Systems.

Written By: Yashwanth Naidu Tikkisetty

Leave a Reply