Question

Swapping the values of two variables involves the exchange of their values. For example, assume that...

Swapping the values of two variables involves the exchange of their values. For example, assume that a == 5; and b == 10; If the values of these variables were swapped, the resultant values would be: a == 10; b == 5.

Write a program to initialise two memory locations with values 10 and 5 before then swapping them without the use of a third memory location using:

  1. The IJVM instruction set.
  2. Create a table with the values of all relevant memory locations and registers at each step

Homework Answers

Answer #1

// Java program to swap two numbers without using third memory location

import java.*;

class Swap {   

    public static void main(String a[])

    {

        int a = 10;

        int b = 5;

a = a + b;

b = a - b;

a = a - b;

    }

}

Local Variable indices: a = 10, b = 5.

Java IJVM Assembly IJVM ISA
ILOAD a 0X15 0X02
{ ILOAD b 0X15 0X03
a = a + b; IADD 0X60
ISTORE a 0X36 0X02
ILOAD a 0X15 0X02
ILOAD b 0X15 0X03
b = a - b; ISUB 0X64
ISTORE b 0X36 0X03
ILOAD a 0X15 0X02
ILOAD b 0X15 0X03
a = a - b; ISUB 0X64
} ISTORE a 0X36 0X02

Output : a = 5, b = 10.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Example the Pep/9 system contains the following four hexadecimal values register A: 25B6 register X: AE20...
Example the Pep/9 system contains the following four hexadecimal values register A: 25B6 register X: AE20 Mem[091D]: 0FF0 Mem[0A71]: 301D If these are the values before each of the following instructions executes, indicate which of these values is changed when each statement is executed, and show the new value. If a register or a memory location shown above is unchanged by the instruction, don't show its contents; only show the item that changed and what the new 16-bit value of...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their...
Leave comments on code describing what does what Objectives: 1. To introduce pointer variables and their relationship with arrays 2. To introduce the dereferencing operator 3. To introduce the concept of dynamic memory allocation A distinction must always be made between a memory location’s address and the data stored at that location. In this lab, we will look at addresses of variables and at special variables, called pointers, which hold these addresses. The address of a variable is given by...
Write a Python program to create a dictionary from two lists without losing duplicate values. If...
Write a Python program to create a dictionary from two lists without losing duplicate values. If there is more values in the key list, then provided key should return an empty set if there is no match. (Hint: use defaultdict) Example: class_list = ['Class-V', 'Class-VI', 'Class-VII', 'Class-VIII','Class-IX'] id_list = [1, 2, 2, 3] Output: assuming you store the values in a data structure named temp print(temp["Class-V"]) # {1} print(temp["Class-IX"]) # set() can you please try and do on google colab
consider two sets of integer values stored as arrays x and y of size m and...
consider two sets of integer values stored as arrays x and y of size m and n respectively. For example, x[5]={2,68,10,90,4} and y[4]={68,3,10,22}. Write a program to print the intersection (intersection returns the elements that are common in both sets) of these two sets, i.e., the set {68, 10}. In JAVA Programming language
1.Write a function which takes a string that contains two words separated by any amount of...
1.Write a function which takes a string that contains two words separated by any amount of whitespace, and returns a string in which the words are swapped around and the whitespace is preserved. Hint: use regular expressions where \s detects the whitespaces in a string. Example: given "Hello     World", return "World         Hello" 2.Pandas exercises: Write a python program using Pandas to create and display a one-dimensional array-like object containing an array of data. Write a python program using Pandas to...
(Write in C++) Write a program that reads in two numbers and, if the input is...
(Write in C++) Write a program that reads in two numbers and, if the input is valid, outputs 2 times the product of the integers that lie between the two values (including the values themselves). If either number is not an integer, or if the first number is not less than the second number, just output an error message. The sample runs below should give the idea. User inputs are in bold. Important Notes: Your program should use a loop...
You are working for a company that is responsible for determining the winner of a prestigious...
You are working for a company that is responsible for determining the winner of a prestigious international event, Men’s Synchronized Swimming. Scoring is done by eleven (11) international judges. They each submit a score in the range from 0 to 100. The highest and lowest scores are not counted. The remaining nine (9) scores are averaged and the median value is also determined. The participating team with the highest average score wins. In case of a tie, the highest median...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
Dice Rolling) Write an application to simulate the rolling of two dice. The application should use...
Dice Rolling) Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first die and again to roll the second die. The sum of the two values should then be calculated. Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the...
1.   There are two major websites that use mathematical models to predict the price of houses...
1.   There are two major websites that use mathematical models to predict the price of houses based on things like # of bedrooms, square feet, location, what similar houses sell for, all kinds of variables. You’re going to select a sample of houses and see which website predicts better. a.   Decide on a location for your sample. Anywhere in the US works. Write the location here. Fresno, CA b.   Go to www.zillow.com and check “Recently Sold.” Randomly select at least...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT