Appendix B: Solutions to the Exercises


Chapter 1

Exercise 1 A cluster of eight bytes can take on approximately 20 quintillion different values. (One quintillion is a 1 followed by 18 zeroes, or 10 to the 18th power.) Estimate the number of different values that a cluster of 16 bytes can have. Just estimate, do not count. Can you think of anything that comes in such quantities?

Solution 1 The exact number of values is 2 to the power of the number of bits. This is 2128, or about 3.4 x 1038. We can make a good estimate by just squaring the number of possibilities for eight bytes, which is given as approximately 20 x 1018. The square of that is approximately 400 x 1036, or approximately 4 x 1038.

To put this in perspective, there are about 2 x 1011 stars in a typical galaxy, and there are about 1010 galaxies in the universe. So 16 bytes can easily store the number of stars in the universe (2 x 1021).

Exercise 2 The SimCom animated illustration is written in Java. When you run the program, how many virtual machines are at work?

Solution 2 SimCom is a virtual machine that runs on the Java Virtual Machine that runs on your physical computer. So there are two virtual machines.

Exercise 3 Write a SimCom program that adds 255 to the value in byte 31 and stores the result in byte 30. Observe the program's behavior. What do you notice?

Solution 3 The following program adds 255 to the contents of byte 31, and stores the result in byte 30. The program appears in the solutions on the CD-ROM, in answers/ Ch1/Add255.simcom. In addition to the following code, the program also stores the number 1 in byte 29:

LOAD  31 ADD   29 STORE 30 HALT

SimCom acts as if adding 255 were the same as subtracting 1. We will look at this in more detail in the next chapter.

Exercise 4 Write a SimCom program that computes the square of the value in byte 31 and stores the result in byte 30. What happens when you try to compute the square of 254?

Solution 4 The following program squares the contents of byte 31, and stores the result in byte 30. The program appears in the solutions on the CD-ROM, in answers/Ch1/ Square.simcom:

LOAD  31 STORE 29 LOAD  31 ADD   30 STORE 30 LOAD  29 SUB   28 STORE 29 JUMPZ 10 JUMP   2 HALT

This program is almost the same as the Times5 program that you saw earlier in Chapter 1. The difference is that instead of using a hard-coded value as the loop counter, the first two lines of this program store the value to be squared in the loop counter.

This program produces 4 as the square of 254.

Exercise 5 What features could be added to SimCom to make it more useful?

Solution 5 This is a subjective issue. It would be reasonable to want more opcodes, especially for multiplying and dividing. More memory would also be good. But bear in mind that, since SimCom forces you to be aware of all features of the architecture, adding more features would just give you more to juggle. The benefit of a high-level programming language such as Java is that you can take advantage of a computer's features without having to think on too low a level.




Ground-Up Java
Ground-Up Java
ISBN: 0782141900
EAN: 2147483647
Year: 2005
Pages: 157
Authors: Philip Heller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net