Search This Blog

Tuesday, 5 February 2013

Use of Addressing Modes in Assembly Language

1 comments

Addressing Modes
            A program is a set of instructions that operate on data that reside in the computer's memory.  Programs are normally written in a high-level language, which enables the programmer to use constants, local and global variables, pointers, and arrays.  When translating a high-level language program into assembly language, the compiler must be able to implement these constructs using the facilities provided in the instruction set of the computer in which the program will be run.  The different ways in which the location of an operand is specified in an instruction are referred to as addressing modes.

Implementation of Variables and Constants:
            Variables and constants are the simplest data types and are found in almost every computer program.  In assembly language, a variable is represented by allocating a register or a memory location to hold its value.  The  addressing modes that are used for specifying the constants and variables in an assembly language are: Immediate mode, Register mode and Absolute mode.
            Constant values are used frequently in high-level language programs.  For example, the statement
                                    A = B + 6
contains the constant 6.  Assuming that A and B have been declared earlier as variables and may be accessed using the Absolute mode, this statement may be compiled as follows:
                                    Move B, R1
                                    Add #6, R1
                                    Move R1, A
            In the above example, the first instruction Move B, R1 uses two addressing modes to access the variable B.  Normally, operands available in a variable are accessed by specifying the name of the register or the address of the memory location where the operand is located.  By moving the content of the variable B into the register R1, the first operand is accessed in the first instruction.  Now R1 contains the first operand.
The two addressing modes used for this purposes are explained here:
Register mode:  The operand is the contents of a processor register; the name (address) of the register is given in the instruction.
Absolute mode: The operand is in a memory location; the address of this location is given explicitly in the instruction as in the instruction given below:
                                    Move LOC, R2
            In Immediate mode, the operand is given explicitly in the instruction.  For example, the instruction:
                                    Add 6immediate, R1
adds the value 6 with the content of R1.  Clearly, the Immediate mode is only used to specify the value of a source operand.  Using a subscript to denote the Immediate mode is not appropriate in assembly languages.  A common convention is to use the sharp sign (#) in front of the value to indicate that this value is to be used as an immediate operand.  Hence we write the instruction above in the form
                                    Add #6, R1

Indirection and Pointers:
            In indirect mode of addressing, the instruction does not give the operand or its address explicitly.  Instead, it provides information from which the memory address of the operand can be determined.  We refer to this address as the effective address (EA) of the operand.  We denote indirection in an instruction by placing the name of the register or the memory address in parentheses as illustrated below:
                                    Move (A0), DO
            In this instruction, the address of the first operand is specified in the register A0, which is used for fetching it and then moving it into the register DO for further processing.
           

             Thus in the  indirect mode, the effective address of the operand is the contents of a register or memory location whose address address appears in the instruction.  The register or memory location that contains the address of an operand is called a pointer.  Indirection and the use of pointers are important and powerful concepts in programming.  The following program illustrates the use of indirect addressing in programming:
                                    Move N, R1
                                    Move #2000, R2
                                    Clear R0
                                    Add (R2), R0
                                    Add #4, R2
                                    Decrement R1
                                    Branch>0 LOOP
                                    Move R0, SUM
            In this program, Indirect addressing is used to access successive numbers in the list.  Register R2 is used as a pointer to the numbers in the list, and the operands are accessed indirectly through R2.  The initialization section of the program loads the counter value n from memory location N into R1 and uses the Immediate addressing mode to place the address value 2000, which is the address of the first number in the list, into R2.  Then it clears R0 to 0.
            The first two instruction in the loop:
                        Add (R2), R0
                        Add #4, R2
implement the unspecified instruction block starting at LOOP.  The first time through the loop, the instruction
                        Add (R2), R0
fetches the operand at location 2000 and adds it to R0.  The second Add instruction adds 4 to the contents of the pointer R2, so that it will contain the address value of the next number in the list.  After each iteration is over, the value of R1, which acts as a counter is decremented and checked if it reaches the value 0.  Till it reaches the value zero, the process is repeated to find the sum of all the items in the list.

Indexing and Arrays:
            The next addressing mode is Index mode, which is useful in dealing with lists and arrays.  In this addressing mode, the effective address of the operand is generated by adding a constant value to the contents of a register.  The register used may be a special register for this purpose, or more commonly, it may be any one of a set of general-purpose registers in the processor.  In either case, it is referred to as an index register.
            We indicate the Index mode symbolically as:
                        X(Ri)
where X denotes the constant value contained in the instruction and Ri is the name of the register involved.  The effective address of the operand is given by:
                        EA = X + [Ri]
            The contents of the index register are not changed in the process of generating the effective address.  In an assembly language program, the constant X may be given either as an explicit number or as a symbolic name representing a numerical value.
            There are two ways of using the Index mode:
1.      Offset is given as a constant
2.      Offset is in the index register
            Consider the example:
                        Add 20(R1), R2
            In this example, offset is given as a constant (20).  The effective address is calculated by adding the constant 20 with the content of the register R1 (1000), which is equivalent to 1020 (20 + 1000).  The following example illustrates the use of second method:
                        Add 1000(R1), R2
            In this instruction, the offset value is given in the register R1, which may contain the value 20.  Now, the effective address is calculated by adding 1000 with 20, which results in 1020.  The following sample assembly program illustrates the use of Index addressing in accessing a list of test scores:
                                    Move #LIST, R0
                                    Clear R1
                                    Clear R2
                                    Clear R3
                                    Move N, R4
            LOOP             Add 4(R0), R1
                                    Add 8(R0), R2
                                    Add 12(R0), R3
                                    Add #16, R0
                                    Decrement R4
                                    Branch>0 LOOP
                                    Move R1, SUM1
                                    Move R2, SUM2
                                    Move R3, SUM3

One Response so far

  1. Anonymous says:

    No matter if some one searches for his necessary thing, therefore
    he/she desires to be available that in detail, thus that thing is
    maintained over here.

    Look into my webpage; minecraft games free

Leave a Reply