Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Principles of Computers 12 th Lecture Pavel Ježek, Ph.D.

Similar presentations


Presentation on theme: "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Principles of Computers 12 th Lecture Pavel Ježek, Ph.D."— Presentation transcript:

1 CHARLES UNIVERSITY IN PRAGUE http://d3s.mff.cuni.cz/~jezek faculty of mathematics and physics Principles of Computers 12 th Lecture Pavel Ježek, Ph.D. pavel.jezek@d3s.mff.cuni.cz

2 Examples of CPU Architectures CPU arch. CPU nameData widthLogical address width Current instruction register(s) Physical address width Special mode 8-bit 6502 MOS 65028-bit data16-bit PC 16-bit (64 kB) 16-bit x86-16 x86 Intel 80888-bit data16 + 16 bit CS:IP 20-bit (1 MB) Intel 808616-bit data Intel 8028616-bit data16 + 16 bit CS:IP 24-bit (16 MB) protected 16 (+ real) mode 32-bit x86 IA-32 INTEL32 Intel 8038632-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

3 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 65028-bit data16-bit PC 16-bit (64 kB) 01 S 16-bit x86-16 x86 Intel 80888-bit data16 + 16 bit CS:IP 20-bit (1 MB) SS:SP Intel 808616-bit data Intel 8028616-bit data16 + 16 bit CS:IP 24-bit (16 MB) protected 16 (+ real) mode SS:SP 32-bit x86 IA-32 INTEL32 Intel 8038632-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

4 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) 66 68 xx xxPUSH xxxxh (or PUSH WORD PTR xxxxh ) push 16-bits of x

5 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) 66 68 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

6 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) 66 68 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

7 ... 00 $00007A08 00 (00) 00 (20) 00 (00)$00007A04 00 $00007A02 00 $00007A00 B... $00002100 A... $00002000 C2 00 0D F5 JMPE9$00001306 00 7A 04 JMP indir 25 FF$00001300 C1... $00001000... 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 := @P1; ptr; P2; γ 2 end. A B C1 C2 $00002100 ← $00002100 – ($001306 + 5) = $00002100 – $0000130B = $00000DF5 E9 = relative jump $00007A04

8 ... 00 $00007A08 00 (00) 00 (20) 00 (00)$00007A04 00 $00007A02 00 $00007A00 B C3... $00002100 A C3... $00002000 C2 00 0D F5 CALLE8$00001306 00 7A 04 CALL indir 15 FF$00001300 C1... $00001000... 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 := @P1; ptr; P2; γ 2 end. A B C1 C2 $00002100 ← $00002100 – ($001306 + 5) = $00002100 – $0000130B = $00000DF5 E8 = relative call ( E9 = relative jump) $00007A04 FF 15 = indirect call ( FF 25 = indirect jump)

9 procedure P1(a : word; b : longword); begin... P1($AABB, $11223344);... end; begin... P1(5, 7);... end.... ?? SP →??$0000101A ??...

10 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? SP →??$0000101A ??... ← IP P1($AABB, $11223344); P1(5, 7);

11 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 SP →07$00001016 ??... ← IP procedure argument b

12 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 SP →07$00001016 ??... ← IP procedure argument b

13 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 SP →05$00001014 ??... ← IP procedure argument b procedure argument a

14 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 SP →05$00001014 ??... ← IP procedure argument b procedure argument a

15 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 SP →05$00001010 ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program

16 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 SP →05$00001010 ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program

17 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 SP →05$00001010 ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program

18 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 22 33 SP →44$0000100C ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b

19 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 22 33 SP →44$0000100C ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b

20 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 22 33 44$0000100C AA SP →BB$0000100A ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a

21 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 22 33 44$0000100C AA SP →BB$0000100A ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a

22 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 22 33 44$0000100C AA BB$0000100A 00 0A SP →70$00001006 ??... ← 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

23 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 22 33 44$0000100C AA BB$0000100A 00 0A SP →70$00001006 ??... ← 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

24 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 22 33 44$0000100C AA BB$0000100A 00 0A SP →70$00001006 ??... ← 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

25 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 22 33 44$0000100C AA SP →BB$0000100A 00 0A 70$00001006 ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a

26 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 22 33 44$0000100C AA SP →BB$0000100A 00 0A 70$00001006 ??... ← 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

27 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 SP →22 33 44$0000100C AA BB$0000100A 00 0A 70$00001006 ??... ← 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

28 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h SP := SP + (4+2) $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 22 33 44$0000100C AA SP →BB$0000100A 00 0A 70$00001006 ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program procedure argument b procedure argument a

29 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h SP := SP + (4+2) $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 SP →05$00001010 11 22 33 44$0000100C AA BB$0000100A 00 0A 70$00001006 ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program

30 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h SP := SP + (4+2) $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 SP →05$00001010 11 22 33 44$0000100C AA BB$0000100A 00 0A 70$00001006 ??... ← IP will be used as return address ( ← IP) procedure argument b procedure argument a return address from procedure P1 to main program

31 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h SP := SP + (4+2) $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $0000101A 00 07$00001016 00 SP →05$00001014 00 09 05$00001010 11 22 33 44$0000100C AA BB$0000100A 00 0A 70$00001006 ??... ← IP procedure argument b procedure argument a

32 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h SP := SP + (4+2) $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? SP →??$0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 22 33 44$0000100C AA BB$0000100A 00 0A 70$00001006 ??... ← IP

33 procedure P1(a : word; b : longword); $0A50:... push 11223344h push AABBh call 00000A50h SP := SP + (4+2) $0A70:... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? SP →??$0000101A 00 07$00001016 00 05$00001014 00 09 05$00001010 11 22 33 44$0000100C AA BB$0000100A 00 0A 70$00001006 ??... ← IP

34 ... ?? $0000101A 00 07$00001016 00 05$00001014 00 09 SP →05$00001010 ??... procedure P1(a : word; b : longword); $0A50:...... ret begin... push 00000007h 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

35 procedure P1(a : word; b : longword); $0A50:...... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $0000101A 00 SP + 607$00001016 00 SP + 405$00001014 00 09 SP →05$00001010 ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program

36 procedure P1(a : word; b : longword); var loc1, loc2 : word; $0A50:...... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $0000101A 00 SP + 607$00001016 00 SP + 405$00001014 00 09 SP →05$00001010 ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program

37 procedure P1(a : word; b : longword); var loc1, loc2 : word; $0A50: SP := SP – (2+2)... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $0000101A 00 SP + 1007$00001016 00 SP + 805$00001014 00 09 05$00001010 ?? SP + 2??$0000100E ?? SP →??$0000100C ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program local variable loc1 local variable loc2

38 procedure P1(a : word; b : longword); var loc1, loc2 : word; $0A50: SP := SP – (2+2)... ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $0000101A 00 SP + 1007$00001016 00 SP + 805$00001014 00 09 05$00001010 xx SP + 2xx$0000100E xx SP →Xx$0000100C ??... ← 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

39 procedure P1(a : word; b : longword); var loc1, loc2 : word; $0A50: SP := SP – (2+2)... SP := SP + (2+2) ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $0000101A 00 SP + 1007$00001016 00 SP + 805$00001014 00 09 05$00001010 xx SP + 2xx$0000100E xx SP →xx$0000100C ??... ← IP procedure argument b procedure argument a return address from procedure P1 to main program local variable loc1 local variable loc2

40 procedure P1(a : word; b : longword); var loc1, loc2 : word; $0A50: SP := SP – (2+2)... SP := SP + (2+2) ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $0000101A 00 SP + 607$00001016 00 SP + 405$00001014 00 09 SP →05$00001010 xx $0000100E xx $0000100C ??... ← IP will be used as return address ( ← IP) procedure argument b procedure argument a return address from procedure P1 to main program

41 procedure P1(a : word; b : longword); var loc1, loc2 : word; $0A50: SP := SP – (2+2)... SP := SP + (2+2) ret begin... push 00000007h push 0005h $0900: call 00000A50h $0905: SP := SP + (4+2) nop... end.... ?? $0000101A 00 SP + 607$00001016 00 SP + 405$00001014 00 09 05$00001010 xx SP + 2xx$0000100E xx SP →xx$0000100C ??... 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

42 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 00000007h push 0005h $0900: call 00000A50h $0905: ($00000B00)^ := ? SP := SP + (4+2) nop... end.... ?? $0000101A 00 SP + 1007$00001016 00 SP + 805$00001014 00 09 05$00001010 ?? SP + 2??$0000100E ?? SP →??$0000100C ??... ?? $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;

43 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 00000007h push 0005h SP := SP - 2 $0900: call 00000A50h $0905: ($00000B00)^ := ? SP := SP + (4+2+2) nop... end.... ?? $0000101A 00 SP + 1207$00001016 00 SP + 1005$00001014 ?? SP + 8??$00001012 00 09 05$0000100E ?? SP + 2??$0000100C ?? SP →??$0000100A ??... ?? $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;

44 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 00000007h push 0005h SP := SP - 2 $0900: call 00000A50h $0905: ($00000B00)^ := ? SP := SP + (4+2+2) nop... end.... ?? $0000101A 00 SP + 1207$00001016 00 SP + 1005$00001014 ?? SP + 8??$00001012 00 09 05$0000100E ?? SP + 2??$0000100C ?? SP →??$0000100A ??... ?? $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);

45 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 00000007h push 0005h SP := SP - 2 $0900: call 00000A50h $0905: (^word($00000B00))^ := SP^ SP := SP + (4+2+2) nop... end.... ?? $0000101A 00 SP + 407$00001016 00 SP + 205$00001014 ?? SP →??$00001012 00 09 05$0000100E ?? $0000100C ?? $0000100A ??... ?? $00000B00... procedure argument b procedure argument a reserved space for return value global variable x x := F1(5, 7); ← IP


Download ppt "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Principles of Computers 12 th Lecture Pavel Ježek, Ph.D."

Similar presentations


Ads by Google