Figure 4.7 Array processing example, using pointer-based access to array elements.

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Figure 4.7 Array processing example, using pointer-based access to array elements.

ouverson
4.3 Hack Programming >
Example 3 >
Figure 4.7 Array processing example, using pointer-based access to array elements.

Pseudocode:
-----------
// Program: PointerDemo
// Starting at base adress R0,
// sets the first R1 words to -1
    n = 0
LOOP:
    if (n == R1) goto END
    *(R0 + n) = -1
    n = n + 1
    goto LOOP
END:
-----------

The issue I had with this pseudocode:

If I place value 10 in R1 and then run the program:

Iteration #1 sets R0 to -1
Iteration #2 sets R1 to -1
R1 is now -1 and not 10; the program runs until memory is gone (I assume.)
Reply | Threaded
Open this post in threaded view
|

Re: Figure 4.7 Array processing example, using pointer-based access to array elements.

WBahn
Administrator
The issue appears to be understanding pointer notation.

R0 = 42

Means set the value of R0 to 42.

Read this as "Change the value of R0 to 42".

*R0 = 42

Means to set the value of the register whose address is currently in R0 to 42.

Read this as "Change the value pointed to by R0 to 42".

So say

R0 = 100

*R0 = -1

Doesn't change the value of RAM[0], it changes the value of RAM[100].
Reply | Threaded
Open this post in threaded view
|

Re: Figure 4.7 Array processing example, using pointer-based access to array elements.

ouverson
Correct. I'm still thinking there's an issue with this code/pseudocode.

Pseudocode:
-----------
// Program: PointerDemo
// Starting at base address R0,
// sets the first R1 words to -1
    n = 0
LOOP:
    if (n == R1) goto END
    *(R0 + n) = -1
    n = n + 1
    goto LOOP
END:
-----------

"Starting at base address R0, sets the first R1 words to -1."

I place the value 10 in R1 and then run the program.

The second time through the loop, R1's value is now -1.

There is now no end to the loop as R1 will never be 10.

My corrected version:

Pseudocode:
-----------
// Program: PointerDemo
// Starting at base address R1,
// sets the first R0 words to -1
    n = 0
LOOP:
    if (n == R0) goto END
    *(R1 + n) = -1
    n = n + 1
    goto LOOP
END:
-----------

I could place the value 10 in R0 and things would work fine.

Am I correct? If not, what am I missing?

Reply | Threaded
Open this post in threaded view
|

Re: Figure 4.7 Array processing example, using pointer-based access to array elements.

WBahn
Administrator
You aren't reading the description.

"Starting at base address R0, sets the first R1 words to -1."

You are assuming that R0 has the value 0 stored in it. Well, if that's the case, then of course things aren't going to work properly.

But R0 is supposed to have a base address stored in it, which is the starting address of the block of RAM that is supposed to get set to -1. The size of that block is in R1.

So if R0 has 1000 stored in it and R1 has 20 stored in it, then after the loop finished RAM[1000] through RAM[1019] will be set to -1. RAM[0] will still have 1000 in it and RAM[1] will still have 20 stored in it. What ever register is being used to store n (let's assume it's RAM[16]) will have 20 stored in it.

Reply | Threaded
Open this post in threaded view
|

Re: Figure 4.7 Array processing example, using pointer-based access to array elements.

ouverson
Got it.

I read it as the base address was R0; after your explanation, I now see that RO holds the base address.

Given the context -- "Array processing example, using pointer-based access to array elements." -- I should have sleuthed it.

Thank you.
Reply | Threaded
Open this post in threaded view
|

Re: Figure 4.7 Array processing example, using pointer-based access to array elements.

sam11
In reply to this post by WBahn
in this case my understanding is that *@n is some arbitrary memory address used to store the counter of loop iterations. what happens if math is such that this *@n memory location getting overwritten due to it being in within @R1 address range from @R0 base address.

eg. R0 is 200 and R1 is 10. and n is picked to be 205, so RAM[205] will be overwritten at some point. what ensures n is a safe place to store counter variable on which the assembly program depends.
Reply | Threaded
Open this post in threaded view
|

Re: Figure 4.7 Array processing example, using pointer-based access to array elements.

WBahn
Administrator
sam11 wrote
in this case my understanding is that *@n is some arbitrary memory address used to store the counter of loop iterations. what happens if math is such that this *@n memory location getting overwritten due to it being in within @R1 address range from @R0 base address.

eg. R0 is 200 and R1 is 10. and n is picked to be 205, so RAM[205] will be overwritten at some point. what ensures n is a safe place to store counter variable on which the assembly program depends.
The only thing that ensures that your code behaves correctly is you writing code that behaves correctly.

At this level of programming, you have complete control over what happens -- which means you also have complete responsibility for doing it right. There's no guard rails or safety nets.