1 CPE 413 Overview of x86 Architecture-1
1 CPE 413 Overview of x86 Architecture-1
opcode operands
0 1 0 0 0 1 1 0 1 0 1 1 0 1
Prof. Christopher U. Ngene email: [email protected] 4
Assembly Language
• It’s really difficult for humans to read/remember binary
instruction encodings
• We will see that typically one would use hexadecimal encoding, but
still difficult.
• Therefore it is typical to use a set of mnemonics, which form
the assembly language
• It is often said that the CPU understands assembly language
• This is not technically true, as the CPU understand machine code,
which we, as humans, choose the represent using assembly language
• An assembler transforms assembly code into machine code
• Pros
• Assembly is fast (Time efficiency). A LOT faster than any compiler of any
language could ever produce.
• Assembly is a lot closer to machine level than any language because the
commands of assembly language are mapped 1-1 to machine instructions.
• Assembly code is a lot smaller (Space efficiency) than any compiler of any
language could ever produce.
• In Assembly, we can do a lot of things that we can't do in any higher level
language, such as playing with processor flags, etc.
Prof. Christopher U. Ngene email: [email protected] 12
Typical Applications
• Application that need one of the three advantages
of the assembly language
• Time-efficiency
• Time-convenience
• » Good to have but not required for functional correctness
• Graphics
• Time-critical
• Necessary to satisfy functionality
• Real-time applications
• Aircraft navigational systems
• Process control systems
• Robot control software
• Missile control software
Prof. Christopher U. Ngene email: [email protected] 13
Typical Applications…
• Accessibility to system hardware
• System software typically requires direct control of
the system hardware devices
• Assemblers, linkers, compilers
• Network interfaces, device drivers
• Video games
• Space-efficiency
• Not a big plus point for most applications
• Code compactness is important in some cases
• Portable and hand-held device software
• Spacecraft control software
Machine code
010000101010110110
High-level code
101010101111010101
101001010101010001
101010101010100101
char *tmpfilename;
int num_schedulers=0; 111100001010101001
int num_request_submitters=0; 000101010111101011
ASSEMBLER
int i,j;
010000000010000100
if (!(f = fopen(filename,"r"))) { 000010001000100011
xbt_assert1(0,"Cannot open file %s",filename); 101001010010101011
}
while(fgets(buffer,256,f)) { 000101010010010101
if (!strncmp(buffer,"SCHEDULER",9)) 010101010101010101
num_schedulers++;
if (!strncmp(buffer,"REQUESTSUBMITTER",16))
101010101111010101
num_request_submitters++; 101010101010100101
} 111100001010101001
fclose(f);
tmpfilename = strdup("/tmp/jobsimulator_
Assembly code
sll $t3, $t1, 2
add $t3, $s0, $t3
sll $t4, $t0, 2
add $t4, $s0, $t4 Program counter register
lw $t5, 0($t3) register
CPU
lw $t6, 0($t4) register
slt $t2, $t5, $t6
COMPILER
beq $t2, $zero, endif
add $t0, $t1, $zero
Control
sll
add
$t4,
$t4,
$t0, 2
$s0, $t4
ALU Unit
lw $t5, 0($t3)
lw $t6, 0($t4)
slt $t2, $t5, $t6
beq $t2, $zero, endif
ASSEMBLER
sll $t4, $t0, 2
int i,j; 010000000010000100
add $t4, $s0, $t4
if (!(f = fopen(filename,"r"))) { lw $t5, 0($t3) 000010001000100011
xbt_assert1(0,"Cannot open file %s",filename); lw $t6, 0($t4) 101001010010101011
}
while(fgets(buffer,256,f)) {
slt $t2, $t5, $t6 000101010010010101
if (!strncmp(buffer,"SCHEDULER",9)) beq $t2, $zero, endif 010101010101010101
num_schedulers++; 101010101111010101
if (!strncmp(buffer,"REQUESTSUBMITTER",16))
num_request_submitters++; 101010101010100101
} 111100001010101001
fclose(f);
tmpfilename = strdup("/tmp/jobsimulator_
Assembly code
sll $t3, $t1, 2
add $t3, $s0, $t3
sll $t4, $t0, 2
add $t4, $s0, $t4 Program counter register
lw $t5, 0($t3) register
CPU
lw $t6, 0($t4) register
slt $t2, $t5, $t6
COMPILER
beq $t2, $zero, endif
add $t0, $t1, $zero
Control
sll
add
$t4,
$t4,
$t0, 2
$s0, $t4 ALU Unit
lw $t5, 0($t3)
lw $t6, 0($t4)
slt $t2, $t5, $t6
beq $t2, $zero, endif
• We will pick the Intel 80x86 ISA (x86 for short) – CISC
machines
• The most common today in existing computers
• For instance in my laptop
AH AL BH BL CH CL DH DL
AH AL BH BL CH CL DH DL
SI
DI
BP
SP
IP
= FLAGS
CS
DS
SS
ES
16 bits
Control
ALU Unit
Prof. Christopher U. Ngene email: [email protected] 32
The 8386 Registers
The 80386 was the first 32-bit processor of the x86 family.
AX
AH AL = EAX
BX
BH BL = EBX
CS
CX DS
ES
CH CL = ECX FS
DX GS
SS
DH DL = EDX
= ESI 16 bits
SI
DI = EDI
BP = EBP
SP = ESP
FLAGS = EFLAGS
IP = EIP
32 bits
Prof. Christopher U. Ngene email: [email protected] 33
The 8386 Registers…
• EAX, EBX, ECX and EDX are called data or general purpose
registers.
• (E is for extended as they are 32-bit extensions of their 16-bit
counter parts AX, BX, CX and DX in 16-bit ISA).
• The register EAX is also known as accumulator because it
is used as destination in many arithmetic operations.
• Some instructions generate more efficient code if they
reference the EAX register rather than other registers.
• Bits in a register are conventionally numbered from right
to left, beginning with 0 as shown below.
Note!! You don't have segments in Win32 (80386 processors and above)
Prof. Christopher U. Ngene email: [email protected] 35
80386 Registers…
• There are 6 16-bit segment registers. They define segments in
memory:
• Lastly, there are 2 32-bit registers that don't fit into any category:
• Protected mode
• Native mode of x386 and higher
• Uses 32-bit addresses
• Supports segmentation and paging
• Paging is useful to implement virtual memory
Prof. Christopher U. Ngene email: [email protected] 42
Real Mode Memory Architecture
• Real mode memory architecture
• Pentium operates like a faster 8086 processor
• The 8086 has
• 20 address lines (can address 1 MB)
• All registers are 16-bit wide
• Memory is organized as segments of (up to) 64 KB
• Due to 16-bit registers (216 = 64 K)
• Two components are required to specify a location
• Segment base address (segment start address)
• Offset within a segment
• Both are 16-bit numbers
0000
0001
selector offset
0010
0011
address 0 1 x x 0100
16 bytes
0101 of
0110 memory
0111
For a fixed value of the 1000
selector there are 22=4 1001
addressable bytes of 1010
memory. The set of these 1011
bytes is called a memory 1100
segment. 1101
1110
1111
Prof. Christopher U. Ngene email: [email protected] 50
Selector and Offset Example
0000
0001
selector offset
0010
0011
address x x x x 0100
0101
0110
16 bytes
0111 of
We have 16 bytes of memory
1000 memory
We have 4-byte segments
1001
We have 4 segments
1010
1011
1100
1101
1110
1111
Prof. Christopher U. Ngene email: [email protected] 51
Selector and Offset
• The way in which one addresses the memory content is then
pretty straightforward
• First, set the bits of the selector to “pick” a segment
• Second, set the bits of the offset to address a byte within the
segment
• This all makes sense because a program typically addresses
bytes that are next to each other, that is within the same
segment
• So, the selector bits stay the same for a long time, while the
offset bits change often
• Of course, this isn’t true for tiny 4-byte segments as in our example…
selector offset
4 bits 16 bits
0001…
selector offset 0010…
0011…
0101…
0110…
0111…
1MB
We have 1MB of memory of
1000…
We have 64K segments memory
1001…
We have 16 segments 1010…
1011…
1100…
1101…
1110…
1111…
address space
code
• A program constantly references all three regions
• Therefore, the program constantly references
bytes in three different segments
• For now let’s assume that each region is fully data
contained in a single segment, which is in fact not
always the case
• CS: points to the beginning of the code segment
• DS: points to the beginning of the data segment stack
• SS: points to the beginning of the stack segment