Assembler Programming Lecture 2
Lecture 2 8086 family architecture. From 8086 to Pentium4. Registers, flags, memory organization. Logical, physical, effective address. Addressing modes.
Processor Processor is the device that manages all actions and services in the system. Processor handles synchronous and asynchronous events. All those actions processor handles executing the programs or the procedures.
8086 family processors evolution 8086 real mode only, 16-bit, 1MB addressing. 80186 real mode only, 16-bit, 1MB. 80286 real and protected mode, 16-bit, 16MB. 80386 real and protected mode, 32-bit, 4GB. i486 real and protected mode, 32-bit, 4GB. Pentium real and protected, 32-bit, 4GB. Pentium Pro real and protected, 32-bit, 64GB. Pentium II real and protected, 32-bit, 64GB. Pentium III real and protected, 32-bit, 64GB. Pentium 4 real and protected, 32-bit, 64GB.
8086 architecture general purpose registers AH AX AL SI BH BX BL DI CH CX CL BP DX DH DL
8086 architecture special purpose registers SP IP Carry Parity Auxiliary Carry Zero Sign Trace Interrupt Direction Overflow Flags OF DF IF TF SF ZF AF PF CF
8086 architecture segment registers CS DS ES SS
Real mode 80386 registers EAX AH AX AL ESI SI EBX BH BX BL EDI DI ECX CH CX CL EBP BP EDX DH DX DL ESP SP
EIP and extended flag register EIP IP I/O Privilege Level Nested Task Resume Flag Virtual Mode Alignment Check Virt. Interrupt Flag Virt. Interrupt Pending Identification EFlags ID VIP VIF AC VM RF NT PL PL OFDF IF TF SF ZF AF PF CF
80386 segment registers CS DS ES FS GS SS
Other registers 80386 has registers for protected mode operation. i486 and above has math coprocessor s registers. Pentium and above has multimedia extension registers for MMX. Pentium4 has another multimedia registers for SSE.
Memory addressing in real mode Memory is organized in segments for 16-bit real mode segments size is 64kB, for 32-bit mode segments size is 4GB. Logical address consists of two values segment address, offset within segment. Logical address is recalculated into linear address which in real mode corresponds to the physical address of the memory.
Real address calculating Segment (16 bit) Shift left 4 bits Segment 0 0 0 0 Fill with four zeros + Offset (16 bit) Add Offset value Linear address (20 bit)
Segmented addressing CS DS SS Code Segment Data Segment Stack Segment ES FS GS Data Segment Data Segment Data Segment
Addressing modes Instructions usually have the operands. the right operand is the source, the left operand is the destination. Operands can be of one of the types: register, immediate, direct memory, indirect memory. In the examples we will use mov instruction.
8086 register addressing mov ax, bx mov dl, al mov si, dx mov sp, bp mov dh, cl mov ax, ax mov ax, cs mov ds, ax
8086 immediate values Immediate value is a constant or result of constant expression. It is calculated during assembling. It is placed in the code as part of the instruction. mov ax, A mov ax, 0 mov bx, 12*7
8086 memory addressing Direct memory addressing (displacement only). Indirect memory addressing. Base addressing. Index addressing. Base addressing with indexing. Base addressing with indexing and displacement.
8086 direct memory addressing Specifies the data at given address mov ax, variable mov ax, [variable] mov si, ES:[variable] mov di, ES:[100]
8086 direct memory addressing BL mov ds:[0800h], bl one byte 0800h mov ax, ds:[0600h] high byte 0601h low byte 0600h AX
Indirect memory addressing Address of the data is held in the base or index register. While BX, SI or DI is used the processor as a default takes DS segment register. While BP is used the processor takes SS segment register. mov ax, [bx] mov ax, [bp] mov al, [di] mov ah, [si]
Indirect memory addressing mov al, [bx] data AL BX + DS
Base memory addressing Address of the beggining of the data table is held in the base register. Displacement inside the table is a constant. All of the following variations are legal: mov ax, element[bx] mov ax, [bx+element] mov ax, [bx]+element
Base memory addressing mov al, [bx]+element data AL element + BX + table beginning DS
Index memory addressing Address of the data table is a constant. Number of the element (byte) is held in the index register. All of the following variations are legal: mov ax, table[si] mov ax, [si+table] mov ax, [si]+table
Index memory addressing mov al, table[di] data AL DI + table + table beginning DS
Base memory addressing with indexing Address of the data table is held in the base register. Number of the element (byte) is held in the index register. All of the following variations are legal: mov ax, [bx][si] mov ax, [bx+si] mov ax, [si][bx]
Base memory addressing with indexing mov al, [bp][di] data AL DI BP + + base address SS
Base memory addressing with indexing It s illegal to use two registers of the same type. Only the following register variations are possible: mov ax, [bx][si] mov ax, [bx][di] mov ax, [bp][si] mov ax, [bp][di]
Base memory addressing with indexing and displacement Address of the data table is an constant. Displacment of the structure is held in the base register. Number of element in the structure (byte) is held in the index register. All of the following variations are legal: mov ax, table[bx][di] mov ax, table[di][bx] mov ax, table[bx+di] mov ax, [table+bx+di] mov ax, [bx][di]+table
Base memory addressing with indexing and displacement mov al, table[bx][si] SI BX + + data AL structure address table + table address DS
Additional notes Calculated offset is called effective address. Different addressing modes have different timings. More complicated modes take much time. If effective address is greater than 0FFFFh the carry bit is ignored. Constant displacement is a 8-bit or 16-bit signed value. Using 8-bit value is faster.
80386 register addressing The same as in 8086 with additional 32-bit registers. Only lower 16-bit half of 32-bit registers is accessible directly. mov eax, ebx mov esi, edx
Indirect memory addressing Address of the data is held in the base and/or index register. It is possible to add the constant displacement too. Almost any of the 32-bit registers can be base or index register. ESP can be the base register only. In 16-bit addressing mode the displacement must not exceed 0FFFFh. In this mode maximum segment size is 64kB.
Indirect memory addressing with scaling It is very useful mode for tables containing values greater than 1 byte. Index registers can be scaled by a factor of 1, 2, 4 or 8. If the scaling factor is 1 the base register is the first used in the instruction. If EBP is scaled it s treated as index register and DS segment is used instead of SS.
Indirect memory addressing with scaling mov ax, [ebx][ebp] ;DS bx-base mov ax, [ebp][ebx] ;SS bp-base mov ax, [ebp][ebx*2] ;SS bp-base mov ax, [ebp*2][ebx] ;DS bx-base mov ax, [ebp*2] ;DS no base mov ax, [ebp] ;SS no index mov ax, es:[ebp][ebx*2] ;ES