CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Principles of Computers 12 th Lecture Pavel Ježek, Ph.D.
Examples of CPU Architectures CPU arch. CPU nameData widthLogical address width Current instruction register(s) Physical address width Special mode 8-bit 6502 MOS bit data16-bit PC 16-bit (64 kB) 16-bit x86-16 x86 Intel bit data bit CS:IP 20-bit (1 MB) Intel bit data Intel bit data bit CS:IP 24-bit (16 MB) protected 16 (+ real) mode 32-bit x86 IA-32 INTEL32 Intel bit data32-bit EIP 32-bit (4 GB) protected 32 mode Intel Pentium Pro64-bit data32-bit EIP 36-bit (64 GB) PAE 64-bit x64 x86-64 AMD64 INTEL64 EM64T AMD Opteron (Intel Pentium 4) 64-bit data64-bit RIP 40-bit (1 TB) long mode 2015 current (e.g. Core i7) 64-bit data64-bit RIP AMD: 48b → 256 TB Intel: 46b → 64 TB
Examples of CPU Architectures CPU arch. CPU nameData widthLogical address width Current instruction register(s) Physical address width Special modeStack top 8-bit 6502 MOS bit data16-bit PC 16-bit (64 kB) 01 S 16-bit x86-16 x86 Intel bit data bit CS:IP 20-bit (1 MB) SS:SP Intel bit data Intel bit data bit CS:IP 24-bit (16 MB) protected 16 (+ real) mode SS:SP 32-bit x86 IA-32 INTEL32 Intel bit data32-bit EIP 32-bit (4 GB) protected 32 mode ESP Intel Pentium Pro64-bit data32-bit EIP 36-bit (64 GB) PAE ESP 64-bit x64 x86-64 AMD64 INTEL64 EM64T AMD Opteron (Intel Pentium 4) 64-bit data64-bit RIP 40-bit (1 TB) long mode RSP 2015 current (e.g. Core i7) 64-bit data64-bit RIP AMD: 48b → 256 TB Intel: 46b → 64 TB RSP
Push Variants on x86 (IA-32) Machine codeIntel assemblerComment 68 xx xx xx xxPUSH xxxxxxxxh (or PUSH DWORD PTR xxxxxxxxh ) push 32-bits of x ( x = immediate) xx xxPUSH xxxxh (or PUSH WORD PTR xxxxh ) push 16-bits of x
Push Variants on x86 (IA-32) Machine codeIntel assemblerComment 68 xx xx xx xxPUSH xxxxxxxxh (or PUSH DWORD PTR xxxxxxxxh ) push 32-bits of x ( x = immediate) xx xxPUSH xxxxh (or PUSH WORD PTR xxxxh ) push 16-bits of x FF 35 xx xx xx xxPUSH [xxxxxxxxh] (or PUSH DWORD PTR [xxxxxxxxh] ) push 32-bits from address x ( x = absolute address) 66 FF 35 xx xx xx xxPUSH WORD PTR xxxxxxxxh push 16-bits from address x
Push and Pop Variants on x86 (IA-32) Machine codeIntel assemblerComment 68 xx xx xx xxPUSH xxxxxxxxh (or PUSH DWORD PTR xxxxxxxxh ) push 32-bits of x ( x = immediate) xx xxPUSH xxxxh (or PUSH WORD PTR xxxxh ) push 16-bits of x FF 35 xx xx xx xxPUSH [xxxxxxxxh] (or PUSH DWORD PTR [xxxxxxxxh] ) push 32-bits from address x ( x = absolute address) 66 FF 35 xx xx xx xxPUSH WORD PTR xxxxxxxxh push 16-bits from address x 8F 05 xx xx xx xxPOP [xxxxxxxxh] (or POP DWORD PTR [xxxxxxxxh] ) pop 32-bits and save them to address x ( x = absolute address) 66 8F 05 xx xx xx xxPOP WORD PTR [xxxxxxxxh] pop 16-bits and save them to address x
$00007A08 00 (00) 00 (20) 00 (00)$00007A04 00 $00007A02 00 $00007A00 B... $ A... $ C2 00 0D F5 JMPE9$ A 04 JMP indir 25 FF$ C1... $ variable j variable ptr padding variable i procedure P2 procedure P1 main program program PascalProgram; type PProc = procedure; procedure P1; begin α end; jmp back procedure P2; begin β end; jmp back var i : word; ptr : PProc; j : word; begin γ 1 ptr ptr; P2; γ 2 end. A B C1 C2 $ ← $ – ($ ) = $ – $ B = $00000DF5 E9 = relative jump $00007A04
$00007A08 00 (00) 00 (20) 00 (00)$00007A04 00 $00007A02 00 $00007A00 B C3... $ A C3... $ C2 00 0D F5 CALLE8$ A 04 CALL indir 15 FF$ C1... $ variable j variable ptr padding variable i procedure P2 procedure P1 main program program PascalProgram; type PProc = procedure; procedure P1; begin α end; jmp back = ret procedure P2; begin β end; jmp back = ret var i : word; ptr : PProc; j : word; begin γ 1 ptr ptr; P2; γ 2 end. A B C1 C2 $ ← $ – ($ ) = $ – $ B = $00000DF5 E8 = relative call ( E9 = relative jump) $00007A04 FF 15 = indirect call ( FF 25 = indirect jump)
procedure P1(a : word; b : longword); begin... P1($AABB, $ );... end; begin... P1(5, 7);... end.... ?? SP →??$ A ??...
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? SP →??$ A ??... ← IP P1($AABB, $ ); P1(5, 7);
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 SP →07$ ??... ← IP procedure argument b
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 SP →07$ ??... ← IP procedure argument b
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ SP →05$ ??... ← IP procedure argument b procedure argument a
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ SP →05$ ??... ← IP procedure argument b procedure argument a
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ SP →05$ ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ SP →05$ ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ SP →05$ ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ $ SP →44$ C ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ $ SP →44$ C ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ $ $ C AA SP →BB$ A ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ $ $ C AA SP →BB$ A ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ $ $ C AA BB$ A 00 0A SP →70$ ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a return address from procedure P1 to procedure P1
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ $ $ C AA BB$ A 00 0A SP →70$ ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a return address from procedure P1 to procedure P1
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ $ $ C AA BB$ A 00 0A SP →70$ ??... ← IP will be used as return address ( ← IP) procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a return address from procedure P1 to procedure P1
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ $ $ C AA SP →BB$ A 00 0A 70$ ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ $ $ C AA SP →BB$ A 00 0A 70$ ??... ← IP will be used as return address ( ← IP) procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $ A 00 07$ $ $ SP → $ C AA BB$ A 00 0A 70$ ??... ← IP used as return address ( ← IP) correct return address procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h SP := SP + (4+2) $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $ A 00 07$ $ $ $ C AA SP →BB$ A 00 0A 70$ ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h SP := SP + (4+2) $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $ A 00 07$ $ SP →05$ $ C AA BB$ A 00 0A 70$ ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h SP := SP + (4+2) $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $ A 00 07$ $ SP →05$ $ C AA BB$ A 00 0A 70$ ??... ← IP will be used as return address ( ← IP) procedure argument b procedure argument a return address from procedure P1 to main program
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h SP := SP + (4+2) $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $ A 00 07$ SP →05$ $ $ C AA BB$ A 00 0A 70$ ??... ← IP procedure argument b procedure argument a
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h SP := SP + (4+2) $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? SP →??$ A 00 07$ $ $ $ C AA BB$ A 00 0A 70$ ??... ← IP
procedure P1(a : word; b : longword); $0A50:... push h push AABBh call 00000A50h SP := SP + (4+2) $0A70:... ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? SP →??$ A 00 07$ $ $ $ C AA BB$ A 00 0A 70$ ??... ← IP
... ?? $ A 00 07$ $ SP →05$ ??... procedure P1(a : word; b : longword); $0A50: ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end. ← IP procedure argument b procedure argument a return address from procedure P1 to main program
procedure P1(a : word; b : longword); $0A50: ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $ A 00 SP + 607$ SP + 405$ SP →05$ ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program
procedure P1(a : word; b : longword); var loc1, loc2 : word; $0A50: ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $ A 00 SP + 607$ SP + 405$ SP →05$ ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program
procedure P1(a : word; b : longword); var loc1, loc2 : word; $0A50: SP := SP – (2+2)... ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $ A 00 SP $ SP + 805$ $ ?? SP + 2??$ E ?? SP →??$ C ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program local variable loc1 local variable loc2
procedure P1(a : word; b : longword); var loc1, loc2 : word; $0A50: SP := SP – (2+2)... ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $ A 00 SP $ SP + 805$ $ xx SP + 2xx$ E xx SP →Xx$ C ??... ← IP will be used as return address ( ← IP) procedure argument b procedure argument a return address from procedure P1 to main program local variable loc1 local variable loc2
procedure P1(a : word; b : longword); var loc1, loc2 : word; $0A50: SP := SP – (2+2)... SP := SP + (2+2) ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $ A 00 SP $ SP + 805$ $ xx SP + 2xx$ E xx SP →xx$ C ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program local variable loc1 local variable loc2
procedure P1(a : word; b : longword); var loc1, loc2 : word; $0A50: SP := SP – (2+2)... SP := SP + (2+2) ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $ A 00 SP + 607$ SP + 405$ SP →05$ xx $ E xx $ C ??... ← IP will be used as return address ( ← IP) procedure argument b procedure argument a return address from procedure P1 to main program
procedure P1(a : word; b : longword); var loc1, loc2 : word; $0A50: SP := SP – (2+2)... SP := SP + (2+2) ret begin... push h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $ A 00 SP + 607$ SP + 405$ $ xx SP + 2xx$ E xx SP →xx$ C ??... procedure argument b procedure argument a return address from procedure P1 to main program procedure prolog procedure body procedure epilog local variable loc1 local variable loc2
function F1(a : word; b : longword ) : word; var loc1, loc2 : word; $0A50: SP := SP – (2+2)... SP := SP + (2+2) ret var x : word; begin... push h push 0005h $0900: call 00000A50h $0905: ($00000B00)^ := ? SP := SP + (4+2) nop... end.... ?? $ A 00 SP $ SP + 805$ $ ?? SP + 2??$ E ?? SP →??$ C ??... ?? $00000B00... ← IP procedure argument b procedure argument a return address from procedure P1 to main program local variable loc1 local variable loc2 x := F1(5, 7); global variable x begin... end;
function F1(a : word; b : longword ) : word; var loc1, loc2 : word; $0A50: SP := SP – (2+2)... SP := SP + (2+2) ret var x : word; begin... push h push 0005h SP := SP - 2 $0900: call 00000A50h $0905: ($00000B00)^ := ? SP := SP + (4+2+2) nop... end.... ?? $ A 00 SP $ SP $ ?? SP + 8??$ $ E ?? SP + 2??$ C ?? SP →??$ A ??... ?? $00000B00... procedure argument b procedure argument a return address from procedure P1 to main program local variable loc1 local variable loc2 reserved space for return value global variable x x := F1(5, 7); ← IP begin... end;
function F1(a : word; b : longword ) : word; var loc1, loc2 : word; $0A50: SP := SP – (2+2)... (SP + 8)^ := retval SP := SP + (2+2) ret var x : word; begin... push h push 0005h SP := SP - 2 $0900: call 00000A50h $0905: ($00000B00)^ := ? SP := SP + (4+2+2) nop... end.... ?? $ A 00 SP $ SP $ ?? SP + 8??$ $ E ?? SP + 2??$ C ?? SP →??$ A ??... ?? $00000B00... procedure argument b procedure argument a return address from procedure P1 to main program local variable loc1 local variable loc2 reserved space for return value global variable x begin... F1 := retval; end; x := F1(5, 7);
function F1(a : word; b : longword ) : word; var loc1, loc2 : word; $0A50: SP := SP – (2+2)... (SP + 8)^ := retval SP := SP + (2+2) ret var x : word; begin... push h push 0005h SP := SP - 2 $0900: call 00000A50h $0905: (^word($00000B00))^ := SP^ SP := SP + (4+2+2) nop... end.... ?? $ A 00 SP + 407$ SP + 205$ ?? SP →??$ $ E ?? $ C ?? $ A ??... ?? $00000B00... procedure argument b procedure argument a reserved space for return value global variable x x := F1(5, 7); ← IP