2011-05-16

Practice Final Problem 4.

Originally Posted By: Jay_Reynolds_Freeman
Problem 4 solution:

The intuition here is that if register 1 contains a zero, then the input number n is zero, and the answer is zero; if not, then we can use register 1 as scratch space, which we will need to do the more general case of the problem. With that in mind, we proceed with either psuedocode or actual RAM instructions on the left and in any case actual RAM instructions on the right. Sometimes it takes several actual RAM instructions to implement one line of psuedocode.

This answer is different from the one presented in class, I think I found an off-by-one error in that one ...

The web site does not like to format things neatly; I have added lots of "."s to line up things in columns, and it probably still won't work ... ...

....................................Statement #
acc Jay_Reynolds_Freeman — Mon May 16, 2011 3:46 pm <hr>
'''Originally Posted By: Jay_Reynolds_Freeman''' Problem 4 solution:<br><br>The intuition here is that if register 1 contains a zero, then the input number n is zero, and the answer is zero; if not, then we can use register 1 as scratch space, which we will need to do the more general case of the problem. With that in mind, we proceed with either psuedocode or actual RAM instructions on the left and in any case actual RAM instructions on the right. Sometimes it takes several actual RAM instructions to implement one line of psuedocode.<br><br>This answer is different from the one presented in class, I think I found an off-by-one error in that one ...<br><br>The web site does not like to format things neatly; I have added lots of &quot;.&quot;s to line up things in columns, and it probably still won't work ... ...<br><br>....................................Statement #<br>acc Jay_Reynolds_Freeman &mdash; Mon May 16, 2011 3:46 pm <hr>
2011-05-18

-- Practice Final Problem 4
Originally Posted By: jnewth
Would a viable solution be:

Load 0 //loads 0 in to accumulator
Add R1 //Adds the value of register R1 to accumulator
Add R1
Add R1
Add R1
Add R1
HALT

This will add the contents of R1 (where the input integer n is initially stored before the program starts) to the accumulator 5 times, producing the result 5n. This will work for all integer values, including 0.
'''Originally Posted By: jnewth''' Would a viable solution be:<br><br>Load 0 //loads 0 in to accumulator<br>Add R1 //Adds the value of register R1 to accumulator<br>Add R1<br>Add R1<br>Add R1<br>Add R1<br>HALT<br><br>This will add the contents of R1 (where the input integer n is initially stored before the program starts) to the accumulator 5 times, producing the result 5n. This will work for all integer values, including 0.

-- Practice Final Problem 4
Originally Posted By: lambert
Yeah that works but why not have a Jzero so we dont waste five steps adding 0's
so according to you machine we can edit it like this

1. Read R1 //read in value of register 1
2. Jzero 7 //jump to halt if register was 0
3. Add R1 //Adds the value of register R1 to accumulator (2n)
4. Add R1 // 3(n)
5. Add R1 // 4(n)
6. Add R1 // 5(n)
7. HALT
'''Originally Posted By: lambert''' Yeah that works but why not have a Jzero so we dont waste five steps adding 0's<br>so according to you machine we can edit it like this<br><br>1. Read R1 //read in value of register 1 <br>2. Jzero 7 //jump to halt if register was 0<br>3. Add R1 //Adds the value of register R1 to accumulator (2n)<br>4. Add R1 // 3(n)<br>5. Add R1 // 4(n)<br>6. Add R1 // 5(n)<br>7. HALT<br>

-- Practice Final Problem 4
Originally Posted By: Jason_Liu
lambert wrote:
Yeah that works but why not have a Jzero so we dont waste five steps adding 0's
so according to you machine we can edit it like this
1. Read R1 //read in value of register 1
2. Jzero 7 //jump to halt if register was 0
3. Add R1 //Adds the value of register R1 to accumulator (2n)
4. Add R1 // 3(n)
5. Add R1 // 4(n)
6. Add R1 // 5(n)
7. HALT


This works under the condition that the number is completely stored in R1.

Our original solution was similar to this; however, the number is actually stored differently.

We were told that the number was stored from Register 1 thru N (N being the integer). Meaning if the number was 20. Registers 1 - 20 would have a "1" in it, and all registers after 20 would have a 0. If it was 45, Registers 1 - 45 would be "1" and all registers after would be a 0. So that particular solution doesn't work when it is formatted this way.
'''Originally Posted By: Jason_Liu''' lambert wrote:<br>Yeah that works but why not have a Jzero so we dont waste five steps adding 0's<br>so according to you machine we can edit it like this<br>1. Read R1 //read in value of register 1 <br>2. Jzero 7 //jump to halt if register was 0<br>3. Add R1 //Adds the value of register R1 to accumulator (2n)<br>4. Add R1 // 3(n)<br>5. Add R1 // 4(n)<br>6. Add R1 // 5(n)<br>7. HALT<br><br><br>This works under the condition that the number is completely stored in R1.<br><br>Our original solution was similar to this; however, the number is actually stored differently. <br><br>We were told that the number was stored from Register 1 thru N (N being the integer). Meaning if the number was 20. Registers 1 - 20 would have a &quot;1&quot; in it, and all registers after 20 would have a 0. If it was 45, Registers 1 - 45 would be &quot;1&quot; and all registers after would be a 0. So that particular solution doesn't work when it is formatted this way.
X