progs/fib.j
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Fri, 02 Oct 2015 07:48:56 +0100
changeset 341 ac1187b2e5c9
parent 323 4ce07c4abdb4
child 373 b018234c9126
permissions -rw-r--r--
updated program


.class public fib.fib
.super java/lang/Object

.method public <init>()V
   aload_0
   invokenonvirtual java/lang/Object/<init>()V
   return
.end method

.method public static write(I)V 
    .limit locals 5 
    .limit stack 5 
    iload 0 
    getstatic java/lang/System/out Ljava/io/PrintStream; 
    swap 
    invokevirtual java/io/PrintStream/println(I)V 
    return 
.end method

.method public static read()I 
      .limit locals 10 
      .limit stack 10

      ldc 0 
      istore 1  ; this will hold our final integer 
Label1: 
      getstatic java/lang/System/in Ljava/io/InputStream; 
      invokevirtual java/io/InputStream/read()I 
      istore 2 
      iload 2 
      ldc 10   ; the newline delimiter 
      isub 
      ifeq Label2 
      iload 2 
      ldc 32   ; the space delimiter 
      isub 
      ifeq Label2

      iload 2 
      ldc 48   ; we have our digit in ASCII, have to subtract it from 48 
      isub 
      ldc 10 
      iload 1 
      imul 
      iadd 
      istore 1 
      goto Label1 
Label2: 
      ;when we come here we have our integer computed in Local Variable 1 
      iload 1 
      ireturn 
.end method


.method public static main([Ljava/lang/String;)V
   .limit locals 200
   .limit stack 200

invokestatic fib/fib/read()I
istore 0
ldc 0
istore 1
ldc 1
istore 2
ldc 0
istore 3

Loop_begin_0:

ldc 0
iload 0
if_icmpge Loop_end_1
iload 2
istore 3
iload 1
iload 2
iadd
istore 2
iload 3
istore 1
iload 0
ldc 1
isub
istore 0
goto Loop_begin_0

Loop_end_1:

iload 2
invokestatic fib/fib/write(I)V


   return

.end method