1. [Understanding Machine Language] What is in Register R1 when the following machine language program halts? ------------------------------------- 10: Load R0 <- 0001 11: Load R1 <- 0001 12: Load R2 <- Memory[20] 13: Add R1 <- R1 + R1 14: Sub R2 <- R2 - R0 15: Jump to 13 if (R2>0) 16: halt ... 20: (A number x is stored here.) ------------------------------------- You may assume x is positive. Describe what is in R1 at the end of the program as it relates to the number x. 2. [Writing Machine Language Programs] Write a TOY machine language program (like the one above) that calculates 2 + 4 + 6 + 8 + ... + N, where: * Assume the number N is already stored in Register R1 when the program starts. Assume N is always even. * The final output should be stored in Register R2. 3. In class, we defined a machine language and showed its instruction set. We were then able to write programs for a few Simple tasks. In this problem, you are asked to make some modifications to those programs. a) Create a machine language program that will add the N numbers starting at the number K. These would be the numbers K, K+1, K+2, ... K+N-1. b) Create a machine language program that will compute 6! (that is, 6 factorial which is defined as 1x2x3x4x5x6). c) Create a machine language program that will let you put a value N in a register and then compute N! (N factorial which is the product of the first N integers). (Hint: this will be a modification of one of the programs for adding the first N integers. d) The quantity N! grows very quickly. On our machine where memory stores values that are no more than 16 bits long, we can not store N! for values of N that are greater than 8. If your program from part c) were to try and compute 9!, it could cause the toy machine to crash or would cause an error to occur. To fix this, modify your program from part c) so that it checks whether the value of N is more than 8. If it is, it should reset the value to 8 and compute 8!. 4. You must have noted that you need rather similar code in Q2 and Q3, subpart a. Lets rewrite this part in high level langauge. a) Write a procedure which takes 4 parameters, a starting value K, an offset A to add, the total count N of numbers to add and a Sum S where the result will be stored. ie, given K, A, N and S, the procedure adds the N numbers K + (K+A) + (K + 2A) .... + (K + (N-1)A) and sets S to the resulting value. b) using the above procedure, write the program (again in high level langauge) for Q2 and Q3, subpart a. c) As you know from the lectures, programs written in high level language ultimately get translated into machine level code when the program is run. To properly convert procedures into machine laguage, you need some scheme to pass parameters that a procedure needs to work. Put yourself in the role of the person who has to design this scheme. Tell us your scheme. Note: There is no single correct answer to this question. The high-level langauge abstraction lets a person describe what they want to do in terms closer to how they think instead of getting bogged down in the details of how the specific machine understands and expects things. Somebody (ie, the compiler writers) does the dirty work of creating the scheme which converts high level language into machine level langauge. Here you are being asked to play the part of the compiler writer (ie, do the dirty work). But only a very small part of it. There are many possible ways of doing this -- some which will keep working as you try to handle more and more complicated conversions from high level langauge to machine langauge, some will work for the specific part (handling parameter passing) but will need to be changed to handle more complicated things. You dont have to worry about all of that. Just think of one correct way of doing the translation (for parameter passing to procedures) and tell us that.