Thursday, July 11, 2013

Real Mode Segmentation

Real Mode environment is based on 8086/88 processors.  There are 6 segment registers, 4 general purpose registers, 3 pointer registers, 2 index registers and a flag register.  All registers are 16-bit

The first 4 segment registers (CS, DS, SS and ES) store segment selectors which is the first half of a logical address. FS and GS came after 8086/88.

CS stores the base address of the current executing code segment
DS stores the base address of segment storing global data
SS stores the base address of the stack segment
ES stores the base address of segment for string data
FS and GS stores the base address of 2 more segment for global data

The 3 pointer registers are IP (for instruction), SP (stack pointer) and BP used to build stack frames for function calles

The 4 GPR are

AX = accumulator used for arithmetic functions
BX = base register used as index to address memory indirectly
CX = counter often used in loop
DX = data register used with AX

The 3 index registers are

SI = points to address of source in string operation
DI points to address of destination in string operation

Real mode use segmentation to manage memory. Jump operation needs to differentiate if the jump is within segment (NEAR) or across segments (FAR).  There are several instruction resulted in jump.  NEAR and FAR jump are relocation which means that they do not depend on specific address in the binary encoding

INT and IRET are intrinsically far jump as both of them involve the segment selectors.

JMP and CALL can be near or far depends on how they are invoked.

JMP SHORT label
JMP FAR PTR label
JMP DX is a NEAR indirect jump
JMP DS:[label] is a FAR direct jump
JMP DWPRD PTR [BX] is a FAR indirect jump

CALL label is a NEAR jump
CALL BX is a NEAR indirect jump
CALL DS:[label] is a FAR direct jump
CALL DWORD PTR [BX] is a FAR indirect jump
RET is a NEAR return
RETF is a FAR return

No comments: