it works a bit
This commit is contained in:
parent
84812d7664
commit
73e402bcb7
Binary file not shown.
|
@ -0,0 +1,51 @@
|
||||||
|
.class public Factorial
|
||||||
|
.super java/lang/Object
|
||||||
|
|
||||||
|
.method public <init>()V
|
||||||
|
aload 0
|
||||||
|
invokenonvirtual java/lang/Object/<init>()V
|
||||||
|
return
|
||||||
|
.end method
|
||||||
|
|
||||||
|
.method public static factorial(I)I ; calc `given`!
|
||||||
|
.limit locals 3
|
||||||
|
.limit stack 5
|
||||||
|
bipush 1 ;
|
||||||
|
istore 0 ; store the parameter `given`
|
||||||
|
bipush 1 ; we start calculating with `p`=1
|
||||||
|
istore 1 ; store `p`
|
||||||
|
bipush 2 ; we start calculating with `j`=1
|
||||||
|
istore 2 ; store `j`
|
||||||
|
loop:
|
||||||
|
; p *= j
|
||||||
|
iload 1 ; load p
|
||||||
|
iload 2 ; load j
|
||||||
|
imul ; multiply p and j
|
||||||
|
istore 1 ; store p*j in p
|
||||||
|
|
||||||
|
; j++
|
||||||
|
iload 2 ; load j
|
||||||
|
bipush 1 ; put a 1 on the stack
|
||||||
|
iadd ; add j and 1
|
||||||
|
istore 2 ; store j+1 in j
|
||||||
|
|
||||||
|
; if j<given
|
||||||
|
iload 2 ; load j
|
||||||
|
iload 0 ; load given
|
||||||
|
if_icmplt loop ; if j < given goto loop
|
||||||
|
|
||||||
|
; else return given * p
|
||||||
|
iload 0 ; load given
|
||||||
|
iload 1 ; load p
|
||||||
|
imul ; multiply given and p
|
||||||
|
|
||||||
|
ireturn ; return given*p
|
||||||
|
.end method
|
||||||
|
|
||||||
|
.method public static main([Ljava/lang/String;)V
|
||||||
|
.limit locals 1
|
||||||
|
.limit stack 3
|
||||||
|
getstatic java/lang/System/out Ljava/io/PrintStream;
|
||||||
|
invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V
|
||||||
|
return
|
||||||
|
.end method
|
Loading…
Reference in New Issue