Addressing modes - Notes PDF

Title Addressing modes - Notes
Author Anonymous User
Course Computer architectue
Institution Indian Institute of Technology Indore
Pages 15
File Size 815 KB
File Type PDF
Total Downloads 4
Total Views 148

Summary

Notes...


Description

K. Gee Geeth th thaaa-C COC OCAAA-U2 U2 U2-A -A -Add dd ddre re resssi sing ng Mod odes es

An assembly language program instruction consists of two parts

The memory address of an operand consists of two components: IMPORTANT TERMS • Starting address of memory segment. • Effective address or Offset: An offset is determined by adding any combination of three address elements: displacement, base and index. ○ Displacement: It is an 8 bit or 16 bit immediate value given in the instruction. ○ Base: Contents of base register, BX or BP. ○ Index: Content of index register SI or DI. Addressing Mode. The way any operand is selected during the program execution is dependent on the addressing mode of the instruction.

The purpose of using addressing modes is as follows: 1. To give the programming versatility to the user. 2. To reduce the number of bits in addressing field of instruction.

1. Implied Addressing ModeIn this addressing mode, • The definition of the instruction itself specify the operands implicitly. • It is also called as implicit addressing mode.

Examples• The instruction “Complement Accumulator” is an implied mode instruction. • In a stack organized computer, Zero Address Instructions are implied mode instructions. (since operands are always implied to be present on the top of the stack)

2. Stack Addressing Mode-

In this addressing mode, • The operand is contained at the top of the stack. Example-

ADD • This instruction simply pops out two symbols contained at the top of the stack. • The addition of those two operands is performed. • The result so obtained after addition is pushed again at the top of the stack. 3. Immediate Addressing Mode-

In this addressing mode, • The operand is specified in the instruction explicitly. • Instead of address field, an operand field is present that contains the operand.

Examples• ADD 10 will increment the value stored in the accumulator by 10. • MOV R #20 initializes register R to a constant value 20. 4. Direct Addressing ModeIn this addressing mode, • The address field of the instruction contains the effective address of the operand. • Only one reference to memory is required to fetch the operand. • It is also called as absolute addressing mode.

Example-

• ADD X will increment the value stored in the accumulator by the value stored at memory location X. AC ← AC + [X]

• Advantages • Shorter instructions and faster instruction fetch. • Faster memory access to the operand(s) • Disadvantages • Very limited address space • Using multiple registers helps performance but it complicates the instructions. • 5. Indirect Addressing ModeIn this addressing mode, • The address field of the instruction specifies the address of memory location that contains the effective address of the operand. • Two references to memory are required to fetch the operand.

Example-

• ADD X will increment the value stored in the accumulator by the value stored at memory location specified by X. AC ← AC + [[X]]

6. Register Direct Addressing ModeIn this addressing mode, • The operand is contained in a register set. • The address field of the instruction refers to a CPU register that contains the operand.

• No reference to memory is required to fetch the operand.

Example-

• ADD R will increment the value stored in the accumulator by the content of register R. AC ← AC + [R]

NOTEIt is interesting to note• This addressing mode is similar to direct addressing mode. • The only difference is address field of the instruction refers to a CPU register instead of main memory.

7. Register Indirect Addressing ModeIn this addressing mode, • The address field of the instruction refers to a CPU register that contains the effective address of the operand. • Only one reference to memory is required to fetch the operand.

Example-

• ADD R will increment the value stored in the accumulator by the content of memory location specified in register R. AC ← AC + [[R]]

NOTEIt is interesting to note• This addressing mode is similar to indirect addressing mode. • The only difference is address field of the instruction refers to a CPU register. 8. Relative Addressing ModeIn this addressing mode, • Effective address of the operand is obtained by adding the content of program counter with the address part of the instruction. • It is a version of Displacement addressing mode. • In this the contents of PC(Program Counter) is added to address part of instruction to obtain the effective address. • EA = A + (PC), where EA is effective address and PC is program counter. • The operand is A cells away from the current cell(the one pointed to by PC)

Effective Address = Content of Program Counter + Address part of the instruction

NOTE-

• Program counter (PC) always contains the address of the next instruction to be executed. • After fetching the address of the instruction, the value of program counter immediately increases. • The value increases irrespective of whether the fetched instruction has completely executed or not. 9. Indexed Addressing ModeIn this addressing mode, • Effective address of the operand is obtained by adding the content of index register with the address part of the instruction.

Effective Address = Content of Index Register + Address part of the instruction

10. Base Register Addressing ModeIn this addressing mode, • Effective address of the operand is obtained by adding the content of base register with the address part of the instruction.

Effective Address = Content of Base Register + Address part of the instruction

11. Auto-Increment Addressing Mode-

• This addressing mode is a special case of Register Indirect Addressing Mode where -

Effective Address of the Operand = Content of Register

In this addressing mode, • After accessing the operand, the content of the register is automatically incremented by step size ‘d’. • Step size ‘d’ depends on the size of operand accessed. • Only one reference to memory is required to fetch the operand. Example-

Assume operand size = 2 bytes. Here, • After fetching the operand 6B, the instruction register R AUTO will be automatically incremented by 2. • Then, updated value of RAUTO will be 3300 + 2 = 3302. • At memory address 3302, the next operand will be found. NOTE-

In auto-increment addressing mode, • First, the operand value is fetched. • Then, the instruction register RAUTO value is incremented by step size ‘d’. 12. Auto-Decrement Addressing Mode-

• This addressing mode is again a special case of Register Indirect Addressing Mode where -

Effective Address of the Operand = Content of Register – Step Size

In this addressing mode, • • • •

First, the content of the register is decremented by step size ‘d’. Step size ‘d’ depends on the size of operand accessed. After decrementing, the operand is read. Only one reference to memory is required to fetch the operand. Example-

Assume operand size = 2 bytes. Here, • First, the instruction register RAUTO will be decremented by 2. • Then, updated value of RAUTO will be 3302 – 2 = 3300. • At memory address 3300, the operand will be found. NOTE-

In auto-decrement addressing mode, • First, the instruction register RAUTO value is decremented by step size ‘d’. • Then, the operand value is fetched.

Applications of Addressing Modes-

Addressing Modes Immediate Addressing Mode Direct Addressing Mode and

Applications •To initialize registers to a constant value •To access static data •To implement variables

Register Direct Addressing Mode Indirect Addressing Mode and Register Indirect Addressing Mode Relative Addressing Mode

Index Addressing Mode Base Register Addressing Mode

Auto-increment Addressing Mode and Auto-decrement Addressing Mode

Assembly Language

•To implement pointers because pointers are memory locations that store the address of another variable •To pass array as a parameter because array name is the base address and pointer is needed to point the address •For program relocation at run time i.e. for position independent code •To change the normal sequence of execution of instructions •For branch type instructions since it directly updates the program counter •For array implementation or array addressing •For records implementation •For writing relocatable code i.e. for relocation of program in memory even at run time •For handling recursive procedures

•For implementing loops •For stepping through arrays in a loop •For implementing a stack as push and pop

When writing programs for a specific computer, such words are normally replaced by acronyms called mnemonics, such as LD, ST, ADD, and BR. A shorthand notation is also useful when identifying registers, such as R3 for register 3. Finally, symbols such as LOC may be defined as needed to represent particular memory locations. A complete set of such symbolic names and rules for their use constitutes a programming language, generally referred to as an assembly language. The set of rules for using the mnemonics and for specification of complete instructions and programs is called the syntax of the language. Programs written in an assembly language can be automatically translated into a sequence of machine instructions by a program called an assembler

The user program in its original alphanumeric text format is called a source program, and the assembled machine-language program is called an object program.

we write a Store instruction as ST R2, SUM The mnemonic ST represents the binary pattern, or operation (OP) code, for the operation performed by the instruction. The assembler translates this mnemonic into the binary OP code that the computer recognizes.

The number sign usually denotes an immediate operand. Thus, the instruction ADD R2, R3, #5 adds the number 5 to the contents of register R3 and puts the result into register R2. ADDI R2, R3, 5

The suffix I in the mnemonic ADDI states that the second source operand is given in the Immediate addressing mode. Indirect addressing is usually specified by putting parentheses around the name or symbol denoting the pointer to the operand. For example, if register R2 contains the address of a number in the memory, then this number can be loaded into register R3 using the instruction LD R3, (R2)

ASSEMBLER DIRECTIVES In addition to providing a mechanism for representing instructions in a program, assembly language allows the programmer to specify other information needed to translate the source program into the object program. Assembly - Variables NASM provides various define directives for reserving storage space for variables. The define assembler directive is used for allocation of storage space. It can be used to reserve as well as initialize one or more bytes. Allocating Storage Space for Initialized Data

The syntax for storage allocation statement for initialized data is − [variable-name] define-directive initial-value [,initial-value]... Where, variable-name is the identifier for each storage space. There are five basic forms of the define directive − Directive Purpose Storage Space

DB DW DD DQ

Define Byte

allocates 1 byte

Define Word allocates 2 bytes Define Doubleword allocates 4 bytes Define Quadword

allocates 8 bytes

DT Define Ten Bytes allocates 10 bytes Following are some examples of using define directives − choice DB 'y' number DW 12345 neg_number DW -12345 big_number DQ 123456789 real_number1 DD 1.234 real_number2 DQ 123.456 Please note that − • Each byte of character is stored as its ASCII value in hexadecimal. • Each decimal value is automatically converted to its 16-bit binary equivalent and stored as a hexadecimal number. • Processor uses the little-endian byte ordering. • Negative numbers are converted to its 2's complement representation. • Short and long floating-point numbers are represented using 32 or 64 bits, respectively.

The reserve directives are used for reserving space for uninitialized data. The reserve directives take a single operand that specifies the number of units of space to be reserved. Each define directive has a related reserve directive. There are five basic forms of the reserve directive − Directive Purpose RESB RESW

Reserve a Byte Reserve a Word

RESD

Reserve a Doubleword

RESQ

Reserve a Quadword

REST Reserve a Ten Bytes Multiple Definitions You can have multiple data definition statements in a program. For example − choice DB 'Y' ;ASCII of y = 79H number1 DW 12345 ;12345D = 3039H number2 DD 12345679 ;123456789D = 75BCD15H The assembler allocates contiguous memory for multiple variable definitions. Multiple Initializations The TIMES directive allows multiple initializations to the same value. For example, an array named marks of size 9 can be defined and initialized to zero using the following statement − marks TIMES 9 DW 0 The TIMES directive is useful in defining arrays and tables. The following program displays 9 asterisks on the screen



Live Dem Assembly - Constants There are several directives provided by NASM that define constants. • EQU • %assign • %define The EQU Directive The EQU directive is used for defining constants. The syntax of the EQU directive is as follows − CONSTANT_NAME EQU expression For example, TOTAL_STUDENTS equ 50 You can then use this constant value in your code, like − mov ecx, TOTAL_STUDENTS cmp eax, TOTAL_STUDENTS The operand of an EQU statement can be an expression − LENGTH equ 20 WIDTH equ 10 AREA equ length * width The %assign Directive The %assign directive can be used to define numeric constants like the EQU directive. This directive allows redefinition. For example, you may define the constant TOTAL as − %assign TOTAL 10 Later in the code, you can redefine it as − %assign TOTAL 20 This directive is case-sensitive. The %define Directive The %define directive allows defining both numeric and string constants. This directive is similar to the #define in C. For example, you may define the constant PTR as − %define PTR [EBP+4] The above code replaces PTR by [EBP+4]. This directive also allows redefinition and it is case-sensitive. Stacks

A stack is a list of data elements, usually words, with the accessing restriction that elements can be added or removed at one end of the list only. This end is called the top of the stack, and the other end is called the bottom. The structure is sometimes referred to as a pushdown stack. last-in–first-out (LIFO) stack, is also used to describe this type of storage mechanism; the last data item placed on the stack is the first one removed when retrieval begins. The terms push and pop are used to describe placing a new item on the stack and removing the top item from the stack, respectively

Basic Operations Stack operations may involve initializing the stack, using it and then de -initializing it. Apart from these basic stuffs, a stack is used for the following two primary operations − • push() − Pushing (storing) an element on the stack. • pop() − Removing (accessing) an element from the stack. When data is PUSHed onto stack. To use a stack efficiently, we need to check the status of stack as well. For the same purpose, the following functionality is added to stacks − • peek() − get the top data element of the stack, without removing it. • isFull() − check if stack is full. • isEmpty() − check if stack is empty.

One processor register, called the stack pointer (SP), is used to point to a particular stack structure called the processor stack The stack pointer, SP, is used to keep track of the address of the element of the stack that is at the top at any given time. If we assume a byte-addressable memory with a 32-bit word length, the push operation can be implemented as Subtract SP, SP, #4 Store Rj, (SP) where the Subtract instruction subtracts 4 from the contents of SP and places the result in SP. Assuming that the new item to be pushed on the stack is in processor register Rj, the Store instruction will place this value on the stack. These two instructions copy the word from Rj onto the top of the stack, decrementing the stack pointer by 4 before the store (push) operation. The pop operation can be implemented as Load Rj, (SP) Add SP, SP, #4...


Similar Free PDFs