Presentation is loading. Please wait.

Presentation is loading. Please wait.

David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)

Similar presentations


Presentation on theme: "David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)"— Presentation transcript:

1 David Evans http://www.cs.virginia.edu/evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)

2 4 November 2003CS 201J Fall 20032 Menu Running Programs –Crash Course in Architecture (CS333) –Crash Course in Compilers (CS571) Java Virtual Machine Byte Codes

3 4 November 2003CS 201J Fall 20033 Computer Architecture Processor does computation Memory stores bits Input Devices (mouse, keyboard, accelerometer) get input from user and environment Output Devices (display, speakers) present output to user

4 4 November 2003CS 201J Fall 20034 Central Processing Unit (CPU)

5 4 November 2003CS 201J Fall 20035 Intel 4004 First general purpose microprocessor, 1971 4-bit data 46 instructions –8-bit instructions!

6 4 November 2003CS 201J Fall 20036 PC Motherboard From http://www.cyberiapc.com/hardwarebeg.htm Memory CPU

7 4 November 2003CS 201J Fall 20037 Inside the CPU Registers Loads and decodes instructions from memory ALU: Arithmetic Logic Unit –Does arithmetic –Can only operate on values in registers –Must load values from memory into registers before computing with them

8 4 November 2003CS 201J Fall 20038 Compiler Translates a program in a high-level language into machine instructions Calling convention –How are parameters passed to functions –How is the stack managed to return Register allocation –Figure out how to use registers efficiently

9 4 November 2003CS 201J Fall 20039 6: int max (int a, int b) { 00401010 push ebp 00401011 mov ebp,esp 00401013 sub esp,40h 00401016 push ebx 00401017 push esi 00401018 push edi 00401019 lea edi,[ebp-40h] 0040101C mov ecx,10h 00401021 mov eax,0CCCCCCCCh 00401026 rep stos dword ptr [edi] 7: if (a > b) { 00401028 mov eax,dword ptr [ebp+8] 0040102B cmp eax,dword ptr [ebp+0Ch] 0040102E jle max+25h (00401035) 8: return b; 00401030 mov eax,dword ptr [ebp+0Ch] 00401033 jmp max+28h (00401038) 9: } else { 10: return a; 00401035 mov eax,dword ptr [ebp+8] 00401038 pop edi 00401039 pop esi 0040103A pop ebx 0040103B mov esp,ebp 0040103D pop ebp 0040103E ret int max (int a, int b) { if (a > b) { return b; } else { return a; } push instruction is 1 byte In Visual C++, see assembly code by running Debug, then Window | Disassembly mov instruction is 2 bytes Dealing with function call: updating stack, moving arguments Cleanup and return

10 4 November 2003CS 201J Fall 200310 Java Virtual Machine

11 4 November 2003CS 201J Fall 200311 Java Ring (1998)

12 4 November 2003CS 201J Fall 200312 Java Card

13 4 November 2003CS 201J Fall 200313 Java Virtual Machine Small and simple to implement All VMs will run all programs the same way Secure

14 4 November 2003CS 201J Fall 200314 Implementing the JavaVM load class into memory set the instruction pointer to point to the beginning of main do { fetch the next instruction execute that instruction } while (there is more to do); Some other issues we will talk about Thursday and next week: Verification – need to check byte codes satisfy security policy Garbage collection – need to reclaim unused storage

15 4 November 2003CS 201J Fall 200315 Java Byte Codes Stack-based virtual machine Small instruction set: 202 instructions (all are 1 byte opcode + operands) –Intel x86: ~280 instructions (1 to 17 bytes long!) Memory is typed Every Java class file begins with magic number 3405691582 = 0xCAFEBABE in base 16

16 4 November 2003CS 201J Fall 200316 Stack-Based Computation push – put something on the top of the stack pop – get and remove the top of the stack Stack push 2 2 push 3 3 add Does 2 pops, pushes sum 5

17 4 November 2003CS 201J Fall 200317 Some Java Instructions OpcodeMnemonicDescription 0nopDoes nothing 1aconst_null Push null on the stack 3iconst_0Push int 0 on the stack 4iconst_1Push int 1 on the stack …

18 4 November 2003CS 201J Fall 200318 Some Java Instructions OpcodeMnemonicDescription 18ldc Push a one-word (4 bytes) constant onto the stack ldc “Hello” ldc 201 Constant may be an int, float or String The String is really a reference to an entry in the string constant table!

19 4 November 2003CS 201J Fall 200319 Arithmetic OpcodeMnemonicDescription 96iaddPops two integers from the stack and pushes their sum iconst_2 iconst_3 iadd

20 4 November 2003CS 201J Fall 200320 Arithmetic OpcodeMnemonicDescription 96iadd Pops two integers from the stack and pushes their sum 97ladd Pops two long integers from the stack and pushes their sum … 106fmul Pops two floats from the stack and pushes their product … 119dneg Pops a double from the stack, and pushes its negation

21 4 November 2003CS 201J Fall 200321 Java Byte Code Instructions 0: nop 1-20: putting constants on the stack 96-119: arithmetic on ints, longs, floats, doubles What other kinds of instructions do we need?

22 4 November 2003CS 201J Fall 200322 Other Instruction Classes Control Flow (~20 instructions) –if, goto, return Method Calls (4 instructions) Loading and Storing Variables (65 instructions) Creating objects (1 instruction) Using object fields (4 instructions) Arrays (3 instructions)

23 4 November 2003CS 201J Fall 200323 Control Flow ifeq Pop an int off the stack. If it is zero, jump to the label. Otherwise, continue normally. if_icmple Pop two ints off the stack. If the second one is <= the first one, jump to the label. Otherwise, continue normally.

24 4 November 2003CS 201J Fall 200324 Method Calls invokevirtual –Invokes the method on the parameters and object on the top of the stack. –Finds the appropriate method at run-time based on the actual type of the this object. invokevirtual

25 4 November 2003CS 201J Fall 200325 Method Calls invokestatic –Invokes a static (class) method on the parameters on the top of the stack. –Finds the appropriate method at run-time based on the actual type of the this object.

26 4 November 2003CS 201J Fall 200326 Example public class Sample1 { static public void main (String args[]) { System.err.println ("Hello!"); System.exit (1); }

27 4 November 2003CS 201J Fall 200327 > javap -c Sample1 Compiled from Sample1.java public class Sample1 extends java.lang.Object { public Sample1(); public static void main(java.lang.String[]); } Method Sample1() 0 aload_0 1 invokespecial #1 4 return Method void main(java.lang.String[]) 0 getstatic #2 3 ldc #3 5 invokevirtual #4 8 iconst_1 9 invokestatic #5 12 return public class Sample1 { static public void main (String args[]) { System.err.println ("Hello!"); System.exit (1); } }

28 4 November 2003CS 201J Fall 200328 Referencing Memory iload –Pushes the int in local variable (1 bytes) on the stack istore –Pops the int on the top of the stack and stores it in local variable

29 4 November 2003CS 201J Fall 200329 Referencing Example Method void main(java.lang.String[]) 0 iconst_2 1 istore_1 2 iconst_3 3 istore_2 4 iload_1 5 iload_2 6 iadd 7 istore_3 8 getstatic #2 11 new #3 14 dup 15 invokespecial #4 18 ldc #5 20 invokevirtual #6 23 iload_3 24 invokevirtual #7 27 invokevirtual #8 30 invokevirtual #9 33 return public class Locals1 { static public void main (String args[]) { int a = 2; int b = 3; int c = a + b; System.err.println ("c: " + c); } }

30 4 November 2003CS 201J Fall 200330 Charge PS6 will involve reading and writing Java byte codes Use javap –c to look at what the javac compiler produces for your code Thursday: what would this program do? Method void main(java.lang.String[]) 0 iconst_2 1 iadd 2 return


Download ppt "David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 18: 0xCAFEBABE (Java Byte Codes)"

Similar presentations


Ads by Google