From 9edefd887713e0b8634de85214ae0091c25a3a99 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 10 Dec 2023 16:05:22 +0100 Subject: [PATCH] better build --- build.sh | 2 +- src/Factorial.jas | 31 +++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/build.sh b/build.sh index 29b1e4e..42c8807 100755 --- a/build.sh +++ b/build.sh @@ -8,5 +8,5 @@ else ls -lah bin/$1.class echo -e "executing ================================================\n" cd bin - java $1 + java $1 "${@:2}" # the first arg is what java should do, the other are args fi diff --git a/src/Factorial.jas b/src/Factorial.jas index 5ad0ca2..3a9b9f5 100644 --- a/src/Factorial.jas +++ b/src/Factorial.jas @@ -9,9 +9,10 @@ .method public static factorial(I)I ; calc `given`! .limit locals 3 - .limit stack 5 - bipush 1 ; - istore 0 ; store the parameter `given` + .limit stack 2 + ; note that parameters are automatically stored as + ; local variables, we dont need to do `istore 0` to + ; store the given integer in a local variable. bipush 1 ; we start calculating with `p`=1 istore 1 ; store `p` bipush 2 ; we start calculating with `j`=1 @@ -43,11 +44,25 @@ .end method .method public static main([Ljava/lang/String;)V - .limit locals 1 - .limit stack 2 - getstatic java/lang/System/out Ljava/io/PrintStream; - bipush 5 ; set parameter 1 + .limit locals 3 + .limit stack 4 + ; parse cli args to int + aload 0 + aaload 0 + invokestatic java/lang/Integer/parseInt(Ljava/lang/String;)I + istore 1 + + ; format + iload 1 + ldc "! = " + iload 1 invokestatic Factorial/factorial(I)I ; call factorial - invokevirtual java/io/PrintStream/println(I)V + invokevirtual java/lang/String/format(ILjava/lang/String;I)I + astore 2 + + ; output + getstatic java/lang/System/out Ljava/io/PrintStream; + aload 2 ; load formatted str + invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V return .end method