addressing modes on the x86

28
Addressing Modes on the x86 register addressing mode mov ax, ax, mov ax, bx mov ax, cx mov ax, dx constant addressing mode mov ax, 25 mov bx, 195 mov cx, 2056 mov dx, 1000 accessing data in memory There are three addressing modes which deal with accessing data in memory mov ax, [1000] //direct addressing mov ax, [bx] // indirect addressing mode mov ax, [1000+bx] // indexed addressing

Upload: others

Post on 23-Feb-2022

5 views

Category:

Documents


0 download

TRANSCRIPT

Addressing Modes on the x86• register addressing mode

– mov ax, ax,

– mov ax, bx

– mov ax, cx

– mov ax, dx

• constant addressing mode– mov ax, 25

– mov bx, 195

– mov cx, 2056

– mov dx, 1000

• accessing data in memory– There are three addressing modes which deal with accessing data in memory

• mov ax, [1000] //direct addressing

• mov ax, [bx] // indirect addressing mode

• mov ax, [1000+bx] // indexed addressing

Encoding x86 Instructions

To determine a particular instruction’s opcode, you need only select the appropriate bits for the iii, rr, and mmm fields. For example, to encode the

mov ax, bxiii=110 (mov reg, reg), rr=00 (ax), and mmm=001 (bx).

This produces the one-byte instruction 11000001 or 0C1h.Some x86 instructions require more than one byte

Examplemov ax, [1000] //encoding 11000110 or 0C6h mov=110, ax=00, [1000] 110

//loads Ax from memory location 1000mov ax, [2000] //encoding 11000110 or 0C6h mov=110, ax=00, [1000] 110

//loads Ax from memory location 1000The above instructions do different things,

• To encode an address for the [xxxx] or [xxxx+bx] addressing modes, or to encode the constant for the immediate addressing mode.

• Follow the opcode with the 16-bit address or constant, with the L.O. byte immediately following the opcode in memory and the H.O. byte after that. – The three byte encoding for mov ax, [1000] would be 0C6h, 00h,10h and

– The three byte encoding for mov ax, [2000] would be 0C6h, 00h, 20h.

opcode r r m m m H.O L.O

1 1 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0

opcode r r m m m H.O L.O

1 1 0 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

1. expands the instruction set with a set of zero-operand instructions 2. provides all the x86 jump instructions3. bitwise logical not operation that inverts all the bits in the destination register or

memory operand4. Not assigned

The special opcode allows the x86 CPU to expand the set of available instructions.

This opcode handles several zero and one-operand instructions as shown in Figure 3.20 and Figure 3.21.

Memory Layout and Access8086 Purpose Registers

• There are eight 16 bit general purpose registers on the 8086: ax, bx, cx, dx, si, di, bp, andsp. – can use many of these registers interchangeably in a computation

• The ax register (Accumulator) I– most arithmetic and logical computations take place. – most arithmetic and logical operations can be done in other registers– More efficient to use the ax register

• The bx register (Base) – has some special purposes– It is commonly used to hold indirect addresses

• The cx register (Count)– Counts things. – use it to count off the number of iterations in a loop or specify the number of

characters in a string.

• The dx register (Data) has two special purposes: – holds the overflow from certain arithmetic operations, – holds I/O addresses when accessing data on the 80x86 I/O bus.

8086 Purpose Registers (cont)• The si and di registers (Source Index and

Destination Index )

– have some special purposes

– You may use these registers as pointers (much like the bx register) to indirectly access memory.

– these registers used with the 8086 string instructions when processing character strings.

• The bp register (Base Pointer) is similar to the bx register.

– generally used to access parameters and local variables in a procedure.

• The sp register (Stack Pointer) has a very special purpose

– Maintains the program stack.

– Usually not used arithmetic computations.

– Carefully used

Besides the eight 16 bit registers, the 8086 CPUs also have eight 8 bit registers.

Called al, ah, bl, bh, cl, ch, dl, and dh.

Changing of al does not effect ah while it affect AX.

The si, di, bp, and sp registers are only 16 bits.

8086 Segment Registers• Segment: special areas defined to contain CODE,

DATA and STACK• The 8086 has four special segment registers (16

bits):– Cs Code Segment– Ds Data Segment– Es Extra Segment– Ss Stack Segment

• They deal with selecting blocks (segments) of main memory. – e.g., cs points at the beginning of a segment in

memory.

Stack Segment

Data SegmentSS

DS

CS Code Segment

• Code segment cs register points at the segment containing the currently executing machine instructions. – Limitation to 64k

• Data segment register, ds, generally points at global variables for the program. – limited to 65,536 bytes of data in the data segment; possible to

change the value of the ds register to access additional data in other segments.

• Extra segment register, es, – Used to gain access to segments when it is difficult or

impossible to modify the other segment registers.

• Stack Segment ss– points at the segment containing the 8086 stack. – The stack is where the 8086 stores important machine state

information, subroutine return addresses, procedure parameters, and local variables.

– In general, you do not modify the stack segment register because too many things in the system depend upon it.

8086 Special Purpose Registers

• accessing these registers is different than the other registers

• CPU generally manipulates these registers directly.

• instruction pointer (ip)– Contains the address of the currently executing instruction

– provides a pointer into the current code segment

– 16 bits allow selection any one of 65,536 216 different memory locations.

• flags register.

– unlike the other registers on the 8086.

– The flags register is simply an eclectic collection of one bit values which help determine the current state of the processor.

8086 uses only nine of those bits. 1. Zero, 2. carry, 3. sign, and 4. overflow. These flags are the 8086 condition codes.

80286 Registers

• The 80286 microprocessor adds one major programmer-visible feature to the 8086 –

• protected mode operation.

• We shall not cover the 80286 protected mode of operation because the protected mode of the 80286 was poorly designed.

80386/80486 Registers

• The 80386 processor extended the 8086 register set• The 80486 did not add any new registers to the 80386’s

basic register set, but it defines a few bits in some registers left undefined by the 80386.

• The most important change– the introduction of a 32 bit register set. The ax, bx, cx, dx, si, di,

bp, sp, flags, and ip registers were all extended to 32 bits. – The 80386 calls these new 32 bit versions eax, ebx, ecx, edx,

esi, edi, ebp, esp, eflags, and eip to differentiate them from their 16 bit versions

– 80386 also provides two new 16 bit segment registers, fs and gs, • Allow concurrently access six different segments in memory without

reloading a segment register.

• SS: contains the address of the beginning of the stack

• CS: contains the address of the beginning of the code segment

• DS: contains the address of the beginning of the data segment

• SP: contains the size of stack

80x86 Physical Memory Organization

• We discussed the basic organization of a Von Neumann Architecture VNA computer system

• In VNA machine, – the CPU connects to memory via the bus. – The 80x86 selects some particular memory element using a binary

number on the address bus.

• View memory as an array of bytes. – Memory : array [0..MaxRAM] of byte;– The value on the address bus corresponds to the index supplied to this

array. E.g., – Writing data to memory is equivalent to

• Memory [address] := Value_to_Write;

– Reading data from memory is equivalent to• Value_Read := Memory [address];

Addressing data in Memory

• Difference between address of a memory

Location and its content

• Processor stores the data in memory in

reverse by sequence

• Two types of addressing schemes

absolute address

segment-offset address

• Segmented address contains a segment component and an offset component.

• Segmented addresses as segment:offset.

• On the 8086 through the 80286, these two values are 16 bit constants.

• On the 80386 and later, the offset can be a 16 bit constant or a 32 bit constant.

• The actual (physical) memory connected to the CPU is linear array of bytes.

converts the segment value to a physical memory address

• 8086, 8088, 80186, and 80188 (and other processors operating in real mode)

• The CPU multiplies the segment value by sixteen (10h=16) and adds the offset portion.– example, the segmented address 1000:1F00. – To convert this to a physical address

• multiply the segment value (1000h) by sixteen (add 0 to the right)• Add 1F00h to (1000h), the obtained physical address is 11F00h.

8086 Register Addressing Modes

• mov destination, source

• This instruction copies the data from the source operand to the destination operand.

• The only restriction is that both operands must be the same size– mov ax, bx ;Copies the value from BX into AX

– mov dl, al ;Copies the value from AL into DL

– mov si, dx ;Copies the value from DX into SI

– mov sp, bp ;Copies the value from BP into SP

– mov dh, cl ;Copies the value from CL into DH

– mov ax, ax ;Yes, this is legal!

• There aretwo restrictions on the use of the segment registers with the mov instruction.

1. may not specify cs as the destination operand,

2. only one of the operands can be a segment register.

You cannot move data from one segment register to another with a single mov instruction.

1. mov ax, cs

2. mov ds, ax

Displacement Only Addressing Mode• The easiest addressing mode to understand, is the

displacement-only (or direct) addressing mode.

• consists of a 16 bit constant that specifies the address of the target location.

• The instruction mov al,ds:[8088h]– loads the al register with a copy of the byte at memory location 8088h

• The instruction mov ds:[1234h],dl • stores the value in the dl register to memory location 1234h

Register Indirect Addressing Modes

• As with the x86 [bx] addressing mode, these four addressing modes reference the byte at the offset found in the bx, bp, si, or di register, respectively. The [bx], [si], and [di] modes use the ds segment by default. The [bp] addressing mode uses the stack segment (ss) by default.

Indexed Addressing Modes

• If bx contains 1000h, then the instruction – mov cl,20h[bx] will load cl from memory location ds:1020h.

• if bp contains 2020h, – mov dh,1000h[bp] will load dh from location ss:3020.

• The offsets generated by these addressing modes are the sum of the constant and the

• specified register.