IJVM Simulator

Loyola College > Department of Computer Science > Dr. James Glenn > IJVM Simulator > Assembly Language Source Sample
; A sample input file for the IJVM assembler

 BIPUSH 0
 INVOKEVIRTUAL main
 HALT

; int main() {
;   int x, y;
;   cout << "Enter two integers:" << endl;
;   cin >> x >> y;
;   cout << foo(x - y) << endl;
; }

main 1 2
; display prompt
 SPRINT "Enter two integers on separate lines:\n"

; read x and y
 IREAD
 ISTORE 1
 IREAD
 ISTORE 2

; push OBJREF and x-y as arguments, then call foo
 BIPUSH 0
 ILOAD 1
 ILOAD 2
 ISUB
 INVOKEVIRTUAL foo

; display result
 IPRINT
 SPRINT "\n"

; return from main
 IRETURN

; int foo(int z) {
;  if (z < 0)
;    return 0;
;  else
;    return z*z;
; }

foo 2 0
; check if z < 0
  ILOAD 1
  IFLT zero

; calculate and return z*z
  ILOAD 1
  DUP
  IMUL
  IRETURN

:zero
  BIPUSH 0
  IRETURN

This code can be downloaded as the file sample.src.