Lab for Automated Reasoning and Analysis LARA

Exercises 08

Exercise 1

Translate the following function to JVM instructions.

def middle(small: Int, big: Int): Int = {
  val mid = small + (big - small) / 2
  return mid	


Exercise 2

In addition to the instructions introduced in class, consider the following instructions.

  • Conditional Branch : if_icmp<comp> <label>

<comp> can be any of the following operators.

<comp> op
eq ==
ne !=
lt <
le <=
gt >
ge >=

Two integers from the stack are compared using the indicated operator and if the condition is true then the instruction at the <label> is executed.

  • Load reference : aload <i>

Push the reference value stored in the local variable <i> onto the stack

  • Array Dereference : iaload

An index (i) and an array (a) are popped from the stack and an integer from the array ( a(i)) is pushed back onto it.

Using these instructions and the ones you already know, convert the following piece of code to JVM instructions.
It is assumed that the parameters are pushed onto the stack prior to a function call, and the return value has already been pushed prior to returning.

def binarySearch(array: Array[Int], value: Int, left: Int, right: Int): Int = {
  if (left > right) return -1
  val middle = (left + right) / 2
  if (array(middle) == value) return middle
    else if (array(middle) > value)
      return binarySearch(array, value, left, middle - 1)
      return binarySearch(array, value, middle + 1, right)           
cc10/exercises_08.txt · Last modified: 2010/11/17 15:57 by hossein