Presentation is loading. Please wait.

Presentation is loading. Please wait.

MEMORY in 80X86 Design. Outline oAddressing memory oData types oMOV instruction oAddressing modes oInstruction format.

Similar presentations


Presentation on theme: "MEMORY in 80X86 Design. Outline oAddressing memory oData types oMOV instruction oAddressing modes oInstruction format."— Presentation transcript:

1 MEMORY in 80X86 Design

2 Outline oAddressing memory oData types oMOV instruction oAddressing modes oInstruction format

3 Basics oMemory in the x86 processors is byte- addressable oWhenever we present an address to the address bus, we specify the location of a byte in memory oByte is the basic memory unit oIt is possible to retrieve/store more than one bytes with a single memory access o16-bit words, consecutive bytes o32-bit doublewords, consecutive bytes

4 Logical vs. physical memory oLogical memory is the view of memory seen by the programmer oA large byte-addressable array of bytes oWe can read/write bytes, words or doublewords oWe do not worry about how data is fetched from memory, we only see the result of the memory access as 1,2, or 4 bytes oPhysical memory oThe physical organization of memory cells, which is not visible to the programmer oThe unit of access to physical memory is equal to the width of the data bus oE.g. 16 bits in 8086, 32 bits in and later

5 8086 physical memory oRead a byte of address 0: result=FF oRead a word from address 0: result=ABFF oRead a doubleword from address 0: result=5512ABFF AB 1 FF DE 0146 F124 E B D F A C E Odd BankEven Bank Data Bus (15:8)Data Bus (7:0)

6 x86 byte ordering oMemory locations 0 and 1 contain FF and AB… oBut a word access from address 0 returns ABFF ox86 uses little endian byte order oThe requested address (0) points to the lower order byte of the result oThe higher order byte of the result is taken from the next higher sequential address (1)

7 Byte ordering oLittle endian vs. big endian oIn big endian ordering the higher order byte of the result is retrieved from the requested address oUsed in many UNIX servers oByte ordering is a property of the architecture

8 Byte alignment When we read from word 0 data(15:8)=AB,data(7:0) =FF When we read from word 0 data(15:8)=AB,data(7:0) =FF Bytes as presented in the data bus are in the right order! Bytes as presented in the data bus are in the right order! This is an aligned memory access This is an aligned memory access AB 1 FF DE 0146 F124 E B D F A C E Odd BankEven Bank Data Bus (15:8)Data Bus (7:0)

9 Byte alignment What if read a word from address 1 ?What if read a word from address 1 ? It is a valid memory access, you can always read from odd addressesIt is a valid memory access, you can always read from odd addresses Result should be 12ABResult should be 12AB But the bytes in the data bus are not aligned data(15:8)=AB,data(7:0)=12But the bytes in the data bus are not aligned data(15:8)=AB,data(7:0)=12 AB 1 FF DE 0146 F124 E B D F A C E Odd BankEven Bank Data Bus (15:8)Data Bus (7:0)

10 Byte alignment oWhenever we read more than one bytes from memory the bytes presented in the address bus must be aligned according to the architecture byte ordering oIf we read a word from address 1, the byte in address 2 must be stored in the higher order byte of the data bus and the byte in address 1 in the lower order byte of the data bus o10 years ago I had to do that by hand… oBut you dont have to do it anymore, the processor takes care of it

11 Byte alignment oDo we have to worry about unaligned memory accesses ? oYes, if you want your program to run fast! oIn an unaligned memory access the processor has to oRead odd byte (one memory access) oRead even byte (second memory access) oAlign the two bytes (some overhead in the hardware) oIn an aligned memory access the processor just oReads even byte (one memory access) oAligned access is least twice as fast

12 Data Types oInteger numbers oBits, Nibbles, Bytes, Words, Doublewords oSigned/unsigned oFloating point numbers oDifferent format than integers oText o7-bit ASCII characters (letters, characters) o8-bit ASCII encoding allows for 128 more graphical symbols oStrings: sequences of characters oDocuments: collection of strings

13 Data Types oArrays oSequences of numbers, characters oFiles oImages (.jpg,.gif,.tiff,…) oVideo (MPEG,.avi, Quicktime,…) oAudio (.wav,.mp3,…) oEverything is managed as a sequence of bytes stored in the memory of your computer! oThe data type actually depends on the way you access and use these bytes!

14 Data Types o16 signed/unsigned 8- bit integers o8 signed/unsigned 16- bit words o4 signed/unsigned 32- bit doublewords oAn incomprehensible string… oInstructions and operands… AB 1 FF DE 0146 F124 E B D F A C E Odd BankEven Bank Data Bus (15:8)Data Bus (7:0)

15 Real vs. protected mode oIn real mode we address the first megabyte of memory (00000h- FFFFFh), we are limited to a 20-bit address bus and 16-bit registers oIn protected mode we can address 4 Gigabytes of memory using 32-bit address bus and 32-bit registers

16 Protected mode at a glance oUsed in and higher oMemory accessing based on segmentation oSegments can be considered as protected regions of memory oSomehow more more complex address translation mechanism Contd…

17 Protected mode at a glance oStill segment:offset, segment is 16-bit, offset can be 16-bit or 32-bit oSegment field used as pointer to a segment table that contains the starting address of the segment in physical memory oYou are not allowed to modify the segment table in user mode but you can in privileged mode, e.g. if writing an operating system

18 Using segments oLogical partitioning of your program Data (variables) CS Code (your program) Unused stack Used stack DS SS DS:DI DS:SI CS:IP SS:SP SS:BP Original SP Memory

19 The MOV instruction oMove data oFrom memory to a register oFrom a register to memory oFrom a register to another register oNever from memory to memory! oSyntax oMOV destination, source oDestination and source can be registers or memory addresses defined in different ways which we call addressing modes

20 Addressing modes oRegister, the fastest! oMOV AX, BX oMOV AL, BL oMOV DX, SI oMOV DS, BX oRemember, source/destination must be of equal size oImmediate oLoad a value to a register oMOV AX, 1234h

21 Addressing modes oDirect oMOV AX, [1234h] oMove data from a memory location to a register o1234h is a displacement within the data segment DS oRegister Indirect (base relative, or indexed) oMOV AX,[BX] oMOV AX,[BP] Contd…

22 Addressing modes oMOV AX,[SI] oMOV AX,[DI] oDisplacement is put in a register oBX, SI, DI define displacements in the data segment oBP defines a displacement in the stack segment

23 Addressing modes oBase plus index (base relative indexed) oMOV AX, [BX+DI] oMOV AX, [BX+SI] oBase can be BX or BP, index SI or DI oRegister relative oMOV AX, [BX+1234h] oRegister on the left can be BX, BP, SI, or DI oBase relative plus index oMOV AX, [BX+DI+1234h]

24 How do I learn all this ? oAll you have to remember is this table oPick 0 or 1 item from each of the columns, just make sure you end up with at least one item oNever pick two items from the same column o17 valid addressing modes BX BP SI DI DISP

25 Addressing mode examples 89 D8 MODE OP Move to AX the 16-bit value in BXMOV AX, BXRegister Memory ContentsCommentInstruction Addressing Mode 89 F8 MODE OP Move to AX the 16-bit value in DIMOV AX, DIRegister 88 C4 MODE OP Move to AL the 8-bit value in AXMOV AH, ALRegister B4 12 DATA8 OP Move to AH the 8-bit value 12HMOV AH, 12hImmediate B8 34 DATA16 OP Move to AX the value 1234hMOV AX, 1234hImmediate B8 lsb msb DATA16 OP Move to AX the constant defined as CONST MOV AX, CONSTImmediate B8 lsb msb DATA16 OP Move to AX the address or offset of the variable X MOV AX, XImmediate A DISP16OP Move to AX the value at memory location 1234h MOV AX, [1234h]Direct A1 lsb msb DISP16 OP Move to AX the value in memory location DS:X MOV AX, [X]Direct

26 Addressing mode examples A3 lsb msb DATA16OP Move to the memory location pointed to by DS:X the value in AX MOV [X], AXDirect Memory ContentsCommentInstruction Addressing Mode 8B 05 MODE OP Move to AX the 16-bit value pointed to by DS:DI MOV AX, [DI]Indexed MODE OP Move to address DS:DI the 16-bit value in AX MOV [DI], AXIndexed 8B 07 MODE OP Move to AX the 16-bit value pointed to by DS:BX MOV AX, [BX] Register Indirect MODE OP Move to the memory address DS:BX the 16-bit value stored in AX MOV [BX], AX Register Indirect MODE OP Move to memory address SS:BP the 16-bit value in AX MOV [BP], AX Register Indirect 8B 87 lsb msb MODE OP Move to AX the value in memory at DS:BX + TAB MOV AX, TAB[BX] Register Relative lsb msb DISP16OP Move value in AX to memory address DS:BX + TAB MOV TAB[BX], AX Register Relative 8B 01 MODE OP Move to AX the value in memory at DS:BX + DI MOV AX, [BX + DI] Base Plus Index DISP16 MODE

27 Addressing mode examples MODEOP Move to the memory location pointed to by DS:X the value in AX MOV [BX + DI], AX Base Plus Index Memory ContentsComment Instruction Addressing Mode 8B MODE OP Move word in memory location DS:BX + DI h to AX register MOV AX, [BX + DI h] Base Rel Plus Index DISP16 C Move immediate value 5678h to memory location BX + DI h MOV word [BX + DI h], 5678h Base Rel Plus Index

28 Machine language oEverything (instructions, operands, data) is translated to bytes stored in memory oYou need to be able to interpret the meaning of these bytes to debug your programs oIn particular, we need to learn the format of instructions so that we can interpret the bytes that correspond to instructions in memory ox86 instructions are complex, they vary in size from 1 byte to 13 bytes

29 Generic instruction format OpcodeModeDisplacementData/Immediate No operands Example: NOP OP DATA8 OP DATA16 OP DISP8 DISP16 MODE OP DISP8 MODEOPDISP16 w/8-bit data Example: MOV AL, 15 w/16-bit data Example: MOV AX, 1234h w/8-bit displacement Example: JE +45 w/16-bit displacement Example: MOV AL, [1234h] w/mode – register to register Example: MOV AL, AH w/mode & 8-bit displacement Example: MOV [BX + 12], AX w/mode & 16-bit displacement Example: MOV [BX+1234], AX

30 Instruction Basics oEach instruction can have only one operand that points to a memory location oThe sizes of the operands of an instruction must match oThe mode byte encodes which registers are used by the instruction oIf the data size used is ambiguous you have to specify it! oMOV BYTE [BX], 12h oMOV [BX], WORD 12h

31 Anatomy of an instruction oOpcode contains the type of instruction we execute plus two special bits, D and W oThe mode byte is used only in instructions that use register addressing modes and encodes the source and destination for instructions with two operands D W OPCODE MODR/MREG OpcodeModeDisplacementData/Immediate Contd…

32 Anatomy of an instruction oD stands for direction and defines the data flow of the instruction oD=0, data flows from REG to R/M oD=1, data flows from R/M to REG oW stands for the size of data oW=0, byte-sized data oW=1, word (in real mode) or double-word sized (in protected mode)

33 Anatomy of an instruction MOD field specifies the addressing mode MOD field specifies the addressing mode 00 – no displacement 00 – no displacement 01 – 8-bit displacement, sign extended 01 – 8-bit displacement, sign extended 10 – 16-bit displacement 10 – 16-bit displacement 11 – R/M is a register, register addressing mode 11 – R/M is a register, register addressing mode If MOD is 00,01, or 10, the R/M field selects one of the memory addressing modes If MOD is 00,01, or 10, the R/M field selects one of the memory addressing modes D W OPCODE MODR/MREG OpcodeModeDisplacementData/Immediate

34 Registers in the REG and R/M fields Registers in the REG and R/M fields CodeW=0 (Byte)W=1 (Word)W=1 (DWord) 000ALAXEAX 001CLCXECX 010DLDXEDX 011BLBXEBX 100AHSPESP 101CHBPEBP 110DHSIESI 111BHDIEDI

35 Example oConsider the instruction 8BECh o binary oOpcode > MOV oD=1 data goes from R/M to REG oW=1 data is word-sized oMOD=11, register addressing oREG=101 destination, R/M=100 source oMOV BP, SP CodeW=0W=1 000ALAXEAX 001CLCXECX 010DLDXEDX 011BLBXEBX 100AHSPESP 101CHBPEBP 110DHSIESI 111BHDIEDI

36 Displacement addressing oIf MOD is 00, 01, or 10 R/M has an entirely different meaning R/M CodeFunction 000DS:BX+SI 001DS:BX+DI 010SS:BP+SI 011SS:BP+DI 100DS:SI 101DS:DI 110SS:BP 111DS:BX 00 MOD 01 FUNCTION No displacement 8-bit sign-extended displacement 16-bit displacement R/M is a register (register addressing mode) Examples: If MOD=00 and R/M=101 mode is [DI] If MOD=01 and R/M=101 mode is [DI+33h] If MODE=10 and R/M=101 modes is [DI+2233h]

37 Example oInstruction 8A15h o oOpcode > MOV oD=1, data flows from R/M to REG oW=0, 8-bit argument oMOD=00 (no displacement) oREG=010 (DL) oREG=101 ([DI] addressing mode) oMOV DL, [DI] CodeW=0W=1 000ALAXEAX 001CLCXECX 010DLDXEDX 011BLBXEBX 100AHSPESP 101CHBPEBP 110DHSIESI 111BHDIEDI R/M CodeFunction 000DS:BX+SI 001DS:BX+DI 010SS:BP+SI 011SS:BP+DI 100DS:SI 101DS:DI 110SS:BP 111DS:BX

38 Direct Addressing Mode oMOD is always 00 oR/M is always 110 oREG encodes the register to/from we take data as usual oThird byte contains the lower-order bytes of the displacement, fourth byte contains the high order byte of the displacement

39 Direct Addressing oExample: o oOpcode > MOV oW=0 (byte-sized data) oD=0 data flows from REG oMOD 00, REG=010 (DL), R/M=110 oLow-order byte of displacement 00 oHigh-order byte of displacement 10 oMOV [1000h], DL CodeW=0W=1 000ALAXEAX 001CLCXECX 010DLDXEDX 011BLBXEBX 100AHSPESP 101CHBPEBP 110DHSIESI 111BHDIEDI

40 Oops oR/M=110 points to BP when MOD=00! oWhat happens with MOV DL, [BP] oNo displacement, MOD=00 o[BP] addressing mode, R/M=110 oHack… oMOV DL,[BP+0] oMOD=01 (8-bit displacement) oR/M=110 oThis also means that MOV [BP] instructions are at least three bytes long (there is always a displacement even if it is 00)

41 Immediate addressing oMOV WORD [BX+1000h], 1234h OPCODE W MOD R/M Displacement-low Displacement-high Byte 1Byte Data-low Data-high Byte 3Byte 4 Byte 5Byte 6 R/M CodeFunction 000DS:BX+SI 001DS:BX+DI 010SS:BP+SI 011SS:BP+DI 100DS:SI 101DS:DI 110SS:BP 111DS:BX

42 Segment MOV instructions oDifferent opcode oSegments are selected by setting the REG field REG CodeSegment reg. 000ES 001CS 010SS 011DS 100FS 101GS Example MOV BX, CS Opcode MOD=11 (register addressing) REG=001 (CS) R/M=011 (BX) 8CCB


Download ppt "MEMORY in 80X86 Design. Outline oAddressing memory oData types oMOV instruction oAddressing modes oInstruction format."

Similar presentations


Ads by Google