Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Swap using a subroutine. a. Write a subroutine that swaps the contents of two by

ID: 3860029 • Letter: S

Question

Swap using a subroutine. a. Write a subroutine that swaps the contents of two bytes in memory. When subroutine is called the index register X contains the memory address of the first byte and the register Y contains the address of the second byte. At the completion of the subroutine the two bytes in memory pointed by registers X and Y should have their values swapped, while the contents of any data registers (i.e. A, B, D, X, Y) should remain unchanged. Describe what steps were required to preserve the original contents of the registers. b. Test the subroutine using the following driver program: Capture multiple simulation screens to show the execution of your program. At least one screen capture should show the state of the CPU while executing instruction inside the subroutine SUB. While executing the subroutine inspect the contents of the stack pointer SP and explain the value found in this register at this time.

Explanation / Answer

/*

Calculate Circle Area using Java Example

This Calculate Circle Area using Java Example shows how to calculate

area of circle using it's radius.

*/

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class CalculateCircleAreaExample {

  public static void main(String[] args) {

  

  int radius = 0;

  System.out.println("Please enter radius of a circle");

  

  try

  {

  //get the radius from console

  BufferedReader br = newBufferedReader(new InputStreamReader(System.in));

radius =Integer.parseInt(br.readLine());

  }

  //if invalid value was entered

  catch(NumberFormatException ne)

  {

  System.out.println("Invalid radius value" + ne);

  System.exit(0);

  }

  catch(IOException ioe)

  {

  System.out.println("IO Error :" + ioe);

  System.exit(0);

  }

  

  /*

* Area of a circle is

* pi * r * r

* where r is a radius of a circle.

*/

  

  //NOTE : use Math.PI constant to get value of pi

  double area = Math.PI * radius * radius;

  

  System.out.println("Area of a circle is " +area);

  }

}

/*

Output of Calculate Circle Area using Java Example would be

Please enter radius of a circle

19

Area of a circle is 1134.1149479459152

*/