Answered step by step
Verified Expert Solution
Question
1 Approved Answer
dwarf csv. file: JMP , * , 2 , # , 0 DAT , # , 0 , # , 5 ADD , # ,
dwarf csv. file:
JMP , * , 2 , # , 0 DAT , # , 0 , # , 5 ADD , # , 5 , * , 19 MOV , # , 0 , @ , 18 JMP , * , 18 , # , 0
imp csv file:
MOV , * , 0 , * , 1
4 Part C For this part of this assignment, you will write utility functions that convert between tuples representing fairly readable assembly language instructions and their machine code counterparts. You can implement these functions in either order. I will describe the process of finding the machine code integer from the assembly instruction. For this part of the assignment you need write a function that follows this description and then you need to figure out how to go the other way and write a function for that. 6 Name the function to convert an assembly language instruction into an in- teger value: build_memory_value_from_readable_tuple This function will take in a 5-tuple. The parts of the tuple are described in section 2.4. To see how to use this to describe a non-negative integer, think of how one writes numbers using a place value system. There are two differences between this situation and the usual way that we work with place value systems. The first is that we will always have exactly 5 places to put symbols and the second is that there is a special type of symbol in each place. For the restriction that we have only 5 places for symbols, think of having a form where one has to specify a number and there are five boxes for digits. In such a form, on can specify numbers from 00000 up to 99999. The restriction of having just five digits gives us an upper bound on the numbers that we can express. For the generalization of using different sorts of symbols for the diffent places, we can translate those symbols into numbers. For example, for the four op-codes (DAT, MOV, ADD and JMP), we can use the digits 0,1,2,3 and thus for that particular space in the form we can imagine that we are working with a number expressed in base 4. The same works for the addressing modes #, * and @) but since there are three of them we imagine we are working in base 3. The other two fields could be nearly any fixed size. Since we will be using them to specify memory locations, it makes some sense to say that the largest they can be is the largest address in our memory space. So, they can take on integer values in the range from 0 to 19 which means that we can think of them as symbols in base 20. Using one single base to express a number lets us use powers of the base to figure out the value of a place in a place value system. Without a single base, things are a bit more complicated. However, since we need to tell the computer to build up the value step by step, it ends up not making things much more complicated." That is, to build up a value we can add in the value of each symbols reading from left to right where we multiply by the appropriate) base before adding in the next value. I will illustrate this using a normal number and using an assembly instruc- tion. For the number, I will choose the number 12345. I've written the number using a typewriter font so that you can think of it as a sequence of symbols (which all happen to be base 10 digits). Our goal is to get 12345 (written in a different font so that you think of it as a number rather than a sequence of symbols). We start out with a value of 0 and then look at the first symbol. It is 1. We interpret that as 1 and we add that into our value so far giving 0+1=1. We now want to move on to the next symbol. To move on, we multiply our value so far by our base (which is 10) and then add in the number associated with the next symbol. Multiplying our value so far by 10 gives 1 x 10 = 10 5I'm going to describe a trick based on Horner's rule. Horner's rule is a clever and effiecient way of evaluating polynomials. You can get a complicated description of it by looking on the Wikipedia page. We won't need all of that but I think it is worth getting a glimpse of how computing techniques often come from other disciplines. 7 and the next symbol is 2 which has a value of 2 so our value so far becomes 10+2 = 12. The process is repeated: multiply 12 by 10 to get 12 x 10 = 120 and then add the value corresponding to the symbol 3 to get 120 + 3 = 123. And again: multiply 123 by 10 to get 123 x 10 = 1230 and add in the value associated with the symbol 4 to get 1230+4 = 1234. Once more: multiply 1234 by 10 to get 1234 x 10 = 12340 and add they value 5 (because we are now on the symbol 5) to get 12340 +5 = 12345. There are no more symbols so we are done and we have the right answer). For the assembly instruction, I'll pick: MOV * 1 # O because it is the instruction in the one instruction program named imp that will be the first program that you simulate (in part B). Just as with a number, we read from left to right. Again, we start with a value of 0. When we examine the first symbol, we see the MOV. That symbol is interpreted as 1 (DAT is 0, MOV is 1, ADD is 2, JMP is 3). So, we add 1 into our starting 0 to get 0+1 = 1. The next symbol is an addressing mode. So the effective base of the next symbol is 3. Thus we multiply by 3 (instead of 10) to get 1 x 3 = 3 and the symbol is * which we interpret as 1 (# is 0, * is 1, @ is 2). So we add in 1 to get 3+1 = 4. The next symbol is an argument and can have a value up to but not including 20 so the effective base is 20. So, we multiply by 20 to get 4 x 20 = 80 and then we add in the value of 1 (since the symbol there is 1 to get 80+1 = 81. The next symbol is an addressing mode so we multiply by 3 to get 81 x 3 = 243 and then add 0 because the symbol # corresponds to 0. The last symbol is an argument so we multiply by 20 to get 243 x 20 = 4860 and then add the value of the symbol o to get 4860+0 = 4860. Going the other way requires the use of the integer division and remainders. Fortunately those operations are built in to Python. Integer division is done with the // operator and one finds remainders with the % operator. Here is an example that contains a hint. To extract the last digit from a number use the % operator with the base: last_digit = the number % 10 and to get the rest of the number do integer division with the base rest_of_the_number = the number // 10 Name your function to convert back from an assembly tuple to an integer code readable_tuple_from_value 4 Part C For this part of this assignment, you will write utility functions that convert between tuples representing fairly readable assembly language instructions and their machine code counterparts. You can implement these functions in either order. I will describe the process of finding the machine code integer from the assembly instruction. For this part of the assignment you need write a function that follows this description and then you need to figure out how to go the other way and write a function for that. 6 Name the function to convert an assembly language instruction into an in- teger value: build_memory_value_from_readable_tuple This function will take in a 5-tuple. The parts of the tuple are described in section 2.4. To see how to use this to describe a non-negative integer, think of how one writes numbers using a place value system. There are two differences between this situation and the usual way that we work with place value systems. The first is that we will always have exactly 5 places to put symbols and the second is that there is a special type of symbol in each place. For the restriction that we have only 5 places for symbols, think of having a form where one has to specify a number and there are five boxes for digits. In such a form, on can specify numbers from 00000 up to 99999. The restriction of having just five digits gives us an upper bound on the numbers that we can express. For the generalization of using different sorts of symbols for the diffent places, we can translate those symbols into numbers. For example, for the four op-codes (DAT, MOV, ADD and JMP), we can use the digits 0,1,2,3 and thus for that particular space in the form we can imagine that we are working with a number expressed in base 4. The same works for the addressing modes #, * and @) but since there are three of them we imagine we are working in base 3. The other two fields could be nearly any fixed size. Since we will be using them to specify memory locations, it makes some sense to say that the largest they can be is the largest address in our memory space. So, they can take on integer values in the range from 0 to 19 which means that we can think of them as symbols in base 20. Using one single base to express a number lets us use powers of the base to figure out the value of a place in a place value system. Without a single base, things are a bit more complicated. However, since we need to tell the computer to build up the value step by step, it ends up not making things much more complicated." That is, to build up a value we can add in the value of each symbols reading from left to right where we multiply by the appropriate) base before adding in the next value. I will illustrate this using a normal number and using an assembly instruc- tion. For the number, I will choose the number 12345. I've written the number using a typewriter font so that you can think of it as a sequence of symbols (which all happen to be base 10 digits). Our goal is to get 12345 (written in a different font so that you think of it as a number rather than a sequence of symbols). We start out with a value of 0 and then look at the first symbol. It is 1. We interpret that as 1 and we add that into our value so far giving 0+1=1. We now want to move on to the next symbol. To move on, we multiply our value so far by our base (which is 10) and then add in the number associated with the next symbol. Multiplying our value so far by 10 gives 1 x 10 = 10 5I'm going to describe a trick based on Horner's rule. Horner's rule is a clever and effiecient way of evaluating polynomials. You can get a complicated description of it by looking on the Wikipedia page. We won't need all of that but I think it is worth getting a glimpse of how computing techniques often come from other disciplines. 7 and the next symbol is 2 which has a value of 2 so our value so far becomes 10+2 = 12. The process is repeated: multiply 12 by 10 to get 12 x 10 = 120 and then add the value corresponding to the symbol 3 to get 120 + 3 = 123. And again: multiply 123 by 10 to get 123 x 10 = 1230 and add in the value associated with the symbol 4 to get 1230+4 = 1234. Once more: multiply 1234 by 10 to get 1234 x 10 = 12340 and add they value 5 (because we are now on the symbol 5) to get 12340 +5 = 12345. There are no more symbols so we are done and we have the right answer). For the assembly instruction, I'll pick: MOV * 1 # O because it is the instruction in the one instruction program named imp that will be the first program that you simulate (in part B). Just as with a number, we read from left to right. Again, we start with a value of 0. When we examine the first symbol, we see the MOV. That symbol is interpreted as 1 (DAT is 0, MOV is 1, ADD is 2, JMP is 3). So, we add 1 into our starting 0 to get 0+1 = 1. The next symbol is an addressing mode. So the effective base of the next symbol is 3. Thus we multiply by 3 (instead of 10) to get 1 x 3 = 3 and the symbol is * which we interpret as 1 (# is 0, * is 1, @ is 2). So we add in 1 to get 3+1 = 4. The next symbol is an argument and can have a value up to but not including 20 so the effective base is 20. So, we multiply by 20 to get 4 x 20 = 80 and then we add in the value of 1 (since the symbol there is 1 to get 80+1 = 81. The next symbol is an addressing mode so we multiply by 3 to get 81 x 3 = 243 and then add 0 because the symbol # corresponds to 0. The last symbol is an argument so we multiply by 20 to get 243 x 20 = 4860 and then add the value of the symbol o to get 4860+0 = 4860. Going the other way requires the use of the integer division and remainders. Fortunately those operations are built in to Python. Integer division is done with the // operator and one finds remainders with the % operator. Here is an example that contains a hint. To extract the last digit from a number use the % operator with the base: last_digit = the number % 10 and to get the rest of the number do integer division with the base rest_of_the_number = the number // 10 Name your function to convert back from an assembly tuple to an integer code readable_tuple_from_valueStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started