Pages

Friday, 25 January 2013

8086 microprocessor Programming (Part 2)


THE 8086 ADDRESSING MODES


Aims of the Experiment: 


  • Give an introduction to the different addressing modes available in the 8086 microprocessor.
  • Enable the students to grasp further the concept of segment offset addressing introduced in the last experiment. 

Introduction:

When the 8086 microprocessor executes an instruction, it performs the specified function on data. These data are called its operands and these may be part of the instruction, reside in one of the internal registers of the 8086, stored at an address in memory, or held at an I/O port. To access these different types of operands, the 8086 is provided with various addressing modes. Here are the modes available on the 8086. 


  • Register addressing
  • Immediate addressing
  • Direct addressing 
  • Register indirect addressing 
  • Based addressing 
  • Indexed addressing
  • Based indexed addressing
  • Starting addressing
  • Port addressing 


We’ll not cover the last two modes in this experiment. Note that if an instruction has two operands, each one of them may have different addressing modes. Of these nine modes, all but register addressing and immediate addressing make reference to an operand stored in memory. Therefore, they require the microprocessor to initiate memory read or write operation. Thus the addressing modes provide different ways of computing the 20-bit address of the operand that is output on the address bus. We’ll explore these addressing modes in this experiment. 
During the course of this experiment, we’ll be referring time and again to the segment: offset concept introduced in the last experiment. It is, therefore appropriate to describe now the most generally used segment registers and corresponding offset registers. We have already figured out in the last experiment that CS:IP (Code Segment: Instruction Pointer) register pair determines the real address of the next instruction to be executed. The DS (Data Segment) register has its offset in BX (Base) Register, BP (Base Pointer) Register, SI (Source Index) Register, DI (Destination Index) Register, or in an Effective Address word (explained later). The SS (Stack Segment) Register has its offset in SP (SP (Stack Pointer) Register or in the BP (Base Pointer) Register. The ES (Extra Segment) Register is generally used with DI (Destination Index) Register or with an Effective Address word. 


Important Notes:

We’ll be using the ‘mov’ command most of the time to explain different addressing modes. This command has the following general format:   
mov destination, source 
The destination and the source are the two operands, which depend on the type of addressing mode. 

  • Register Addressing Mode 


In this mode, the operand to be accessed is specified as residing in an internal register of the 8086. Some examples are: 
mov ax, bx 
mov ah, dh
mov bx, dx
Figure out the destination and source in the above examples.

  • Immediate Addressing Mode 

In this mode, the source is a constant (byte or word). Some examples are: 
mov ax, 12AFh 
mov ch, F0h
mov ds, 2270h
   Explain what will these instructions do. 

  • Direct Addressing Mode 


This mode is different from the immediate addressing mode in that the constant following the instruction op code represents an effective memory address (EA) instead of data. This effective address is a 16-bit offset of the storage location of the operand from the current value in the DS (Data Segment) Register. EA is combined with the contents of DS to produce the real address. Some examples are:
mov cx, [1F34h]
mov [23IDH], ah 

Load DEBUG program. Fill the memory within the range 3340:1000 with the letters ‘ABCDEFGH’ Write down the command you’ll use. 
Assemble and trace following program and explain the purpose of each one of the following instructions: 
mov ax,3340 
mov ds, ax 
mov ax, [200] 
mov ch, [1F0] 
int 20
Also figure out the type of operands (both destination as well as source) in each one of the above instructions. We have already pointed out that in case an instruction has two operands, e.g. ‘mov’ the two operands may correspond to different addressing modes. Note that the third instruction loads two bytes or a 16-bit word into AX. The Ah will come from EA of 201 and AL from EA of 200. 
Calculate the real addresses of the memory registers in the third and fourth instructions.

  • Register Indirect Addressing Mode 

This mode is similar to direct addressing in that an effective address (EA) is combined with the contents of DS to obtain a real/physical address. However , it differs in the way the offset is specified. This time EA resides in either a pointer register or an index register within the 8086. The pointer register can be BX or BP, and the index register can be SI or DI. Some examples are:
mov ax, [si] 
mov [bp] , cx
Check the current settings of registers and calculate the real/physical addresses corresponding to the above two instructions. 
Note that in direct addressing method, EA is a constant, whereas in the register indirect method EA is a variable. 

  • Based Addressing Mode

In this mode, the physical address of the operand is obtained by adding a displacement to the contents of either Base Register (BX) or Base Pointer Register (BP) and the current value in DS and SS, respectively. Some examples are:  
mov [bx] 34F6h, al ; move contents of AL to DS:BX+34F6
mov ch, [bx] 334Fh ; move to CH contents of SS:BP + 334F 

The above instructions can also be written in the following simpler from:
mov [bx+34F6h] , al
mov ch, [bp+334F] 
If DS has 200h and BX has 3EE0h, what will be the address of the memory location in the first instruction given above? 

  • Indexed Addressing Mode 

This mode works identically to based addressing mode described above, however, it uses the contents of one of the index registers (SI or DI) , instead of BP or BX. Some examples are: 

mov ah, [si] 2323h ; move to AH contents of DS :SI +2323
mov [si]3F6F, d1 ; move to DS :SI+3F6F contents of DL 

Again these instructions can be written in the following from:
mov ah, [si+2323h]
mov si+3F6F}, d1

  • Based Indexed Addressing Mode 

This is a combination of the based addressing and indexed addressing modes, and therefore, it is the most powerful mode. You can have a constant displacement as well. The only restriction is that in the same operand both index registers or both base registers should not be used. Some examples are:
mov al, [bx] [si]   ;move to AL contents of DS:BX+SI 
mov dx, {bx} 1233.[si] ; move to DX contents of DS:BX+SI+1233
mov bx+di+[EED0h], cx ; move contents of DS:BX+DI+EED0 to CX 


ASSIGNMENT

Fill the memory in the range 3300:100 to 3300:17f with the following string: ‘abcdefghijklmnop’.  Check the memory contents in the range 3300:200 to 33000:27f. They should not contain the above string. Now assemble the following program at offset 100, run it, and indicate the types of operands in all ‘mov’ instructions. 
mov ax3300
mov ds,ax
mov si, 0000
mov bx,0200
mov cx, 80
mov ah, [si+100] 
mov [si+bx], ah 
inc si 
dec cx 
jnz 010E
int 20 

Now display the contents of the memory in the range 3300:200 to 3300:27f. Explain the purpose of the above program. 
Calculate the real address range where the data was moved in the above program.
Put the following data bytes in the real address range 270F0 to 270F7.
12,0a,13,14,28,15,1a,1c.
Now write down a program that brings these numbers in the AH register one by one, adds them together using the indexed mode of addressing, and finally saves the result at real address 28011h using direct addressing moved. 

In this problem, you have to add the following numbers:
0228h, -225Ah, 0524h, 25Afh, -023Bh, -1133h, 2FABh, 1230h
This problem is different form the previous one in that now you’ll be working with data words (16-bit numbers) rather than data bytes. It also contains some negative numbers. To solve this problem proceed as follows:
a. Convert the negative numbers to 16-bit2’s complement form. 
b. Store the data words in the memory block starting at CS:200 using following command:
i) Enter ‘a 200’ at DEBUG prompt. 
ii) Enter DW (for data word), followed by the above list of numbers separated by commas. 
c) Now write a program using ‘a100’ command that gets these words from memory and then adds them together. Store the sum at real address 2910F. Good Luck!

APPENDIX NOTES:

Save and Load in DEBUG: 
In order to save your work to disk, first give a name to your file (if it already doesn’t) have one) using the ‘n’ command as under:
N filename 
Now find out the number of bytes your program has, or the number of bytes of the memory you want to store. Put this number in CX register, and put zero in the BX register (if your program occupies less than 64K of memory!). Now enter ‘w’ (for write) at the DEBUG prompt. 
In order to load a file, first initialize its name with the ‘n’ command. Use ‘l’ command to load the file. If no address is specified with ‘l’  the file will be loaded at CS:100. BX and CX will be set to the number of bytes read. 

REFERENCES:


  1. Kip R. Irvine, Assembly Language for the IBM-PC Macmillan Publishing Company, 1990. 
  2. Avtrar Singh and Walter A Triebel, The 8086 and 80286 Microprocessors, Hardware, Software, and Interfacing. Prentice- Hall Inc. 1990.


No comments:

Post a Comment