Skip to content

Instantly share code, notes, and snippets.

@mwcampbell
Created June 11, 2012 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwcampbell/2909897 to your computer and use it in GitHub Desktop.
Save mwcampbell/2909897 to your computer and use it in GitHub Desktop.
Sample Java program with both JVM and x86 disassembly
public class PrintLoop {
public static void main(String[] args) {
for (int i = 0; i < 10; i++)
System.out.println("iteration " + i);
}
}
Compiled from "PrintLoop.java"
public class PrintLoop extends java.lang.Object{
public PrintLoop();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: iconst_0
1: istore_1
2: iload_1
3: bipush 10
5: if_icmpge 39
8: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
11: new #3; //class java/lang/StringBuilder
14: dup
15: invokespecial #4; //Method java/lang/StringBuilder."<init>":()V
18: ldc #5; //String iteration
20: invokevirtual #6; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
23: iload_1
24: invokevirtual #7; //Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
27: invokevirtual #8; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
30: invokevirtual #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
33: iinc 1, 1
36: goto 2
39: return
}
.file "cc2uJYfQ.jar"
.section .rodata.jutf8.12,"aM",@progbits,12
.section .rodata.jutf8.8,"aM",@progbits,8
.section .rodata.jutf8.10,"aM",@progbits,10
.section .rodata.jutf8.28,"aM",@progbits,28
.section .rodata.jutf8.16,"aM",@progbits,16
.section .rodata.jutf8.14,"aM",@progbits,14
.section .jcr,"aw",@progbits
.align 8
.quad _ZN9PrintLoop6class$E
.text
.p2align 4,,15
.type _Jv_global_static_constructor, @function
_Jv_global_static_constructor:
.LFB2:
.cfi_startproc
.cfi_personality 0x3,__gcj_personality_v0
subq $8, %rsp
.cfi_def_cfa_offset 16
movl $_ZGr8_$_dummy, %edi
call _Jv_RegisterResource
addq $8, %rsp
ret
.cfi_endproc
.LFE2:
.size _Jv_global_static_constructor, .-_Jv_global_static_constructor
.section .ctors,"aw",@progbits
.align 8
.quad _Jv_global_static_constructor
.text
.p2align 4,,15
.globl _ZN9PrintLoop4mainEJvP6JArrayIPN4java4lang6StringEE
.type _ZN9PrintLoop4mainEJvP6JArrayIPN4java4lang6StringEE, @function
_ZN9PrintLoop4mainEJvP6JArrayIPN4java4lang6StringEE:
.LFB1:
.cfi_startproc
.cfi_personality 0x3,__gcj_personality_v0
pushq %r12
.cfi_def_cfa_offset 16
movl $_ZN9PrintLoop6class$E, %edi
pushq %rbp
.cfi_def_cfa_offset 24
pushq %rbx
.cfi_def_cfa_offset 32
xorl %ebx, %ebx
.cfi_offset 3, -32
.cfi_offset 6, -24
.cfi_offset 12, -16
call _Jv_InitClass
movl $_ZN4java4lang6System6class$E, %edi
call _Jv_InitClass
.p2align 4,,10
.p2align 3
.L7:
movl $_ZN4java4lang13StringBuilder6class$E, %edi
movq _ZN4java4lang6System3outE(%rip), %rbp
call _Jv_AllocObjectNoFinalizer
movq _CD_PrintLoop+8(%rip), %rsi
movq %rax, %r12
movq %rax, %rdi
call _ZN4java4lang13StringBuilderC1EPNS0_6StringE
testq %r12, %r12
je .L10
movl %ebx, %esi
movq %r12, %rdi
call _ZN4java4lang13StringBuilder6appendEJPS1_i
testq %rax, %rax
je .L10
.L6:
movq %rax, %rdi
addl $1, %ebx
call _ZN4java4lang13StringBuilder8toStringEJPNS0_6StringEv
movq (%rbp), %rdx
movq %rax, %rsi
movq %rbp, %rdi
call *248(%rdx)
cmpl $10, %ebx
jne .L7
popq %rbx
popq %rbp
popq %r12
ret
.L10:
.p2align 4,,5
call _Jv_ThrowNullPointerException
.cfi_endproc
.LFE1:
.size _ZN9PrintLoop4mainEJvP6JArrayIPN4java4lang6StringEE, .-_ZN9PrintLoop4mainEJvP6JArrayIPN4java4lang6StringEE
.align 2
.p2align 4,,15
.globl _ZN9PrintLoopC1Ev
.type _ZN9PrintLoopC1Ev, @function
_ZN9PrintLoopC1Ev:
.LFB0:
.cfi_startproc
.cfi_personality 0x3,__gcj_personality_v0
subq $8, %rsp
.cfi_def_cfa_offset 16
call _ZN4java4lang6ObjectC1Ev
addq $8, %rsp
ret
.cfi_endproc
.LFE0:
.size _ZN9PrintLoopC1Ev, .-_ZN9PrintLoopC1Ev
.hidden _ZGr8_$_dummy
.globl _ZGr8_$_dummy
.section .rodata
.align 16
.type _ZGr8_$_dummy, @object
.size _ZGr8_$_dummy, 16
_ZGr8_$_dummy:
.long 6
.long 0
.ascii ".dummy"
.zero 2
.hidden _ZTVN9PrintLoopE
.globl _ZTVN9PrintLoopE
.data
.align 32
.type _ZTVN9PrintLoopE, @object
.size _ZTVN9PrintLoopE, 80
_ZTVN9PrintLoopE:
.quad 0
.quad 0
.quad _ZN9PrintLoop6class$E
.quad 8
.quad _ZN4java4lang6Object8finalizeEJvv
.quad _ZN4java4lang6Object8hashCodeEJiv
.quad _ZN4java4lang6Object6equalsEJbPS1_
.quad _ZN4java4lang6Object8toStringEJPNS0_6StringEv
.quad _ZN4java4lang6Object5cloneEJPS1_v
.quad _ZN4java4lang6Object22throwNoSuchMethodErrorEJvv
.globl _ZN9PrintLoop6class$E
.align 32
.type _ZN9PrintLoop6class$E, @object
.size _ZN9PrintLoop6class$E, 280
_ZN9PrintLoop6class$E:
.quad _ZTVN4java4lang5ClassE+16
.quad 404000
.quad _Utf6
.value 33
.zero 6
.quad _ZN4java4lang6Object6class$E
.long 2
.zero 4
.quad _CT_PrintLoop
.quad _CD_PrintLoop
.quad _MT_PrintLoop
.value 2
.value 6
.zero 4
.quad 0
.long 8
.value 0
.value 0
.quad _ZTVN9PrintLoopE+16
.quad 0
.quad 0
.quad 0
.quad 0
.quad 0
.quad 0
.quad _catch_classes_PrintLoop
.quad 0
.quad 0
.value 0
.byte 1
.zero 5
.quad 0
.value 0
.zero 6
.quad 0
.quad 0
.quad 0
.quad 0
.quad 0
.quad 0
.quad 0
.quad 0
.quad 0
.quad 0
.hidden _ZN9PrintLoop7class$$E
.globl _ZN9PrintLoop7class$$E
.section .rodata
.align 8
.type _ZN9PrintLoop7class$$E, @object
.size _ZN9PrintLoop7class$$E, 8
_ZN9PrintLoop7class$$E:
.quad _ZN9PrintLoop6class$E
.data
.align 16
.type _CD_PrintLoop, @object
.size _CD_PrintLoop, 16
_CD_PrintLoop:
.quad 0
.quad _Utf5
.section .rodata.jutf8.14
.align 2
.type _Utf6, @object
.size _Utf6, 4
_Utf6:
.value 61137
.value 9
.ascii "PrintLoop"
.zero 1
.data
.type _CT_PrintLoop, @object
.size _CT_PrintLoop, 2
_CT_PrintLoop:
.byte 0
.byte 8
.align 32
.type _MT_PrintLoop, @object
.size _MT_PrintLoop, 80
_MT_PrintLoop:
.quad _Utf1
.quad _Utf2
.value 16385
.value -1
.zero 4
.quad .L_ZN9PrintLoopC1Ev0
.quad 0
.quad _Utf3
.quad _Utf4
.value 16393
.value -1
.zero 4
.quad .L_ZN9PrintLoop4mainEJvP6JArrayIPN4java4lang6StringEE1
.quad 0
.align 32
.type _catch_classes_PrintLoop, @object
.size _catch_classes_PrintLoop, 48
_catch_classes_PrintLoop:
.zero 48
.section .rodata.jutf8.16
.align 2
.type _Utf5, @object
.size _Utf5, 4
_Utf5:
.value 60643
.value 10
.ascii "iteration "
.zero 2
.section .rodata.jutf8.12
.align 2
.type _Utf1, @object
.size _Utf1, 4
_Utf1:
.value 626
.value 6
.ascii "<init>"
.zero 2
.section .rodata.jutf8.8
.align 2
.type _Utf2, @object
.size _Utf2, 4
_Utf2:
.value 39797
.value 3
.ascii "()V"
.zero 1
.section .rodata.jutf8.10
.align 2
.type _Utf3, @object
.size _Utf3, 4
_Utf3:
.value 1465
.value 4
.ascii "main"
.zero 2
.section .rodata.jutf8.28
.align 2
.type _Utf4, @object
.size _Utf4, 4
_Utf4:
.value 59434
.value 22
.ascii "([Ljava.lang.String;)V"
.zero 2
.set .L_ZN9PrintLoopC1Ev0,_ZN9PrintLoopC1Ev
.set .L_ZN9PrintLoop4mainEJvP6JArrayIPN4java4lang6StringEE1,_ZN9PrintLoop4mainEJvP6JArrayIPN4java4lang6StringEE
.ident "GCC: (Ubuntu 4.4.3-1ubuntu4.1) 4.4.3"
.section .note.GNU-stack,"",@progbits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment