Question about example on pg 65

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Question about example on pg 65

textbook91
On page 65 of the text, there's a sample Hack program that adds 1 continuously to a memory location 'i', until its value reaches 100. I looked through it and I think I understood it, but I don't yet get the use of the second mem. location 'sum'. It doesn't get checked for any conditional jumps, or increment 'i' in  anyway. Am I overlooking something?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question about example on pg 65

cadet1620
Administrator
Look at the "C" code in the figure. sum is initialized to 0 before the while loop, and i is added to sum in the body of the while loop.

At the end on the program, sum = 1 + 2 + ... + 100.

If you don't know that syntax, "sum += i" means "sum = sum + i".

--Mark
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Question about example on pg 65

textbook91
Thanks for the speedy reply! I guess I misunderstood the purpose of the program. Upon looking at it again I see what you're saying (sum=1+2+3...+100). I thought the sole purpose was just to add 1 to 'i' until 'i' reached 100, but 'i' just acts as a counter, and increments 'sum' while 'sum' adds to itself (which is what the program is for, and what is meant by 1+...+100).  
Loading...