Professional Documents
Culture Documents
Lecture 3
Lecture 3
Outline
Addressing memory Data types MOV instruction Addressing modes Instruction format
Basics
Memory in the 8086 processors is byte-addressable
Whenever we present an address to the address bus, we specify the location of a byte in memory
Byte is the basic unit of information that can be stored in the memory unit It is possible to retrieve/store more than one bytes with a single memory access
16-bit words, consecutive bytes 32-bit doublewords, consecutive bytes
Physical memory
The physical organization of memory cells, which is not visible to the programmer The unit of access to physical memory is equal to the width of the data bus E.g. 16 bits in 8086, 32 bits in 80386 and later
E C A 8 6 4 2 0
90 E9 F1 01 76 14 55 AB
87 11 24 46 DE 33 12 FF
Read a byte of address 0: result=FF Read a word from address 0: result=ABFF Read a doubleword from address 0: result=5512ABFF
Byte ordering
Little endian vs. big endian
In big endian ordering the higher order byte of the result is retrieved from the requested address Used in many UNIX servers
Byte ordering is a property of the architecture The right-most bit is assigned the index 0 and the bit s are labeled in increasing order from right to lef .
Byte alignment
Odd Bank F D B 9 7 5 3 1 Even Bank
E C A 8 6 4 2 0
90 E9 F1 01 76 14 55 AB
87 11 24 46 DE 33 12 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! This is an aligned memory access
Byte alignment
Odd Bank F D B 9 7 5 3 1 Even Bank
E C A 8 6 4 2 0
90 E9 F1 01 76 14 55 AB
87 11 24 46 DE 33 12 FF
What if read a word from address 1 ? It is a valid memory access, you can always read from odd addresses Result should be 12AB But the bytes in the data bus are not aligned data(15:8)=AB,data(7:0)=12
Byte alignment
Whenever we read more than one bytes from memory the bytes presented in the address bus must be aligned according to the architecture byte ordering If 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 10 years ago I had to do that by hand But you dont have to do it anymore, the processor takes care of it
Byte alignment
Do we have to worry about unaligned memory accesses ? Yes, if you want your program to run fast! In an unaligned memory access the processor has to
Read odd byte (one memory access) Read even byte (second memory access) Align the two bytes (some overhead in the hardware)
Data Types
Integer numbers
Bits, Nibbles, Bytes, Words, Doublewords Signed/unsigned
Text
7-bit ASCII characters (letters, characters) 8-bit ASCII encoding allows for 128 more graphical symbols Strings: sequences of characters Documents: collection of strings
Data Types
Arrays
Sequences of numbers, characters
Files
Images (.jpg, .gif, .tiff,) Video (MPEG, .avi, Quicktime,) Audio (.wav, .mp3,)
Everything is managed as a sequence of bytes stored in the memory of your computer! The data type actually depends on the way you access and use these bytes!
Data Types
Odd Bank F D B 9 7 5 3 1 Even Bank
E C A 8 6 4 2 0
90 E9 F1 01 76 14 55 AB
87 11 24 46 DE 33 12 FF
16 signed/unsigned 8-bit integers 8 signed/unsigned 16-bit words 4 signed/unsigned 32-bit doublewords An incomprehensible string Instructions and operands
Using segments
Logical partitioning of your program
Memory
Original SP SS:BP SS:SP
CS:IP
DS:DI DS:SI
DS
Syntax
MOV destination, source Destination and source can be registers or memory addresses defined in different ways which we call addressing modes
Addressing modes
Register, the fastest!
MOV AX, BX MOV AL, BL MOV DX, SI MOV DS, BX Remember, source/destination must be of equal size
Immediate
Load a value to a register MOV AX, 1234h
Addressing modes
Direct
MOV AX, [1234h] Move data from a memory location to a register 1234h is a displacement within the data segment DS
Addressing modes
Base plus index (base relative indexed)
MOV AX, [BX+DI] MOV AX, [BX+SI] Base can be BX or BP, index SI or DI
Register relative
MOV AX, [BX+1234h] Register on the left can be BX, BP, SI, or DI
BX SI BP DI
Pick 0 or 1 item from each of the columns, just make sure you end up with at least one item Never pick two items from the same column 17 valid addressing modes
DISP
OP
MODE
OP
MODE
OP
DATA8
OP
DATA16
OP
DATA16
OP
DATA16
OP
DISP16
OP
DISP16
OP
MODE
OP
MODE
OP
MODE
OP
MODE
OP
MODE
OP
MODE
DISP16
OP
MODE
DISP16
OP
MODE
OP
MODE
DISP16
C7 81 34 12 78 56
Machine language
Everything (instructions, operands, data) is translated to bytes stored in memory You need to be able to interpret the meaning of these bytes to debug your programs In particular, we need to learn the format of instructions so that we can interpret the bytes that correspond to instructions in memory 8086 instructions are complex, they vary in size from 1 byte to 13 bytes
Mode
Displacement
No operands Example: NOP w/8-bit data Example: MOV AL, 15 w/16-bit data Example: MOV AX, 1234h w/8-bit displacement Example: JE +45
Data/Immediate
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
Instruction Basics
Each instruction can have only one operand that points to a memory location The sizes of the operands of an instruction must match The mode byte encodes which registers are used by the instruction If the data size used is ambiguous you have to specify it!
MOV BYTE [BX], 12h MOV [BX], WORD 12h
Anatomy of an instruction
Opcode Mode Displacement Data/Immediate
D W
OPCODE
Opcode contains the type of instruction we execute plus two special bits, D and W The mode byte is used only in instructions that use register addressing modes and encodes the source and destination for instructions with two operands D stands for direction and defines the data flow of the instruction
D=0, data flows from REG to R/M D=1, data flows from R/M to REG
MOD
REG
R/M
Anatomy of an instruction
Opcode Mode Displacement Data/Immediate
D W
OPCODE
MOD field specifies the addressing mode 00 no displacement 01 8-bit displacement, sign extended 10 16-bit displacement 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
MOD
REG
R/M
Example
Consider the instruction 8BECh 1000 1011 1110 1100 binary Opcode 100010 -> MOV D=1 data goes from R/M to REG W=1 data is word-sized MOD=11, register addressing REG=101 destination, R/M=100 source MOV BP, SP
W=0 AL CL DL BL AH CH DH BH
W=1 AX CX DX BX SP BP SI DI
Displacement addressing
If MOD is 00, 01, or 10 R/M has an entirely different meaning
MOD 00 01 10 11 No displacement 8-bit sign-extended displacement 16-bit displacement R/M is a register (register addressing mode) FUNCTION
R/M Code 000 001 010 011 100 101 110 111
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]
Example
Instruction 8A15h 1000 1010 0001 0101 Opcode 100010 -> MOV D=1, data flows from R/M to REG W=0, 8-bit argument MOD=00 (no displacement) REG=010 (DL) REG=101 ([DI] addressing mode) MOV DL, [DI]
W=0 AL CL DL BL AH CH DH BH
W=1 AX CX DX BX SP BP SI DI
R/M Code 000 001 010 011 100 101 110 111
Direct Addressing
Example: 8816 00 10 1000 1000 0001 0110 0000 0000 0001 0000 Opcode 100010 -> MOV Code W=0 000 AL W=0 (byte-sized data) 001 CL 010 DL D=0 data flows from REG 011 BL 100 AH 101 CH MOD 00, REG=010 (DL), R/M=110 110 DH 111 BH Low-order byte of displacement 00 High-order byte of displacement 10 MOV [1000h], DL
W=1 AX CX DX BX SP BP SI DI
Immediate addressing
MOV WORD [BX+1000h], 1234h
OPCODE W MOD R/M
R/M Code 000 001 010 011 100 101 110 111
Example MOV BX, CS Opcode 10001100 MOD=11 (register addressing) REG=001 (CS) R/M=011 (BX) 8CCB