techElder
Well-known member
Indirection
From Wikipedia, the free encyclopedia
In computer programming, indirection (also called "dereferencing") is the ability to reference something using a name, reference, or container instead of the value itself. The most common form of indirection is the act of manipulating a value through its memory address. For example, accessing a variable through the use of a pointer.
For me, indirection is the holy grail of programming. Indirection allows me to make my code more generalized in such a way to make the changes (that always become necessary) to the program without having to go through and edit parts of the program line-by-line.
Here are a few ways that I provide indirection in my programming. Changes are made in one place. These references in my code never change.
ADDRESSES: Address references can change for all sorts of reasons. One obvious reason is that you might have to change processors. However, if in your definitions of constants, you had referenced all of your addresses by indirection, you would have saved so much work.
Code:
symbol LED_1 = 0x01 + 0xC0
symbol LED_2 = 0x03 + 0xC0
symbol LED_3 = 0x05 + 0xC0
symbol LED_4 = 0x07 + 0xC0
symbol LED_5 = 0x09 + 0xC0
symbol LED_6 = 0x0B + 0xC0
symbol LED_7 = 0x0D + 0xC0
symbol LED_8 = 0x0F + 0xC0
RAM BUFFER ASSIGNMENT: If I have a buffer in RAM/MEMORY where I want to save data, I always define it with indirection and only reference the starting address. Usually my buffer is not just one buffer, but could be many, and they are usually strung out all in one block. I can move the whole block around by just changing the address of the beginning of the block. The remaining segments of the buffer are all referenced back to the beginning with lengths of blocks defined. Like this.
Code:
symbol PACKETRAM_BEG = 56 ; packet
symbol PACKETRAM_END = PACKETRAM_BEG + 7 ; 8 bytes inclusive
symbol SANDBOX_BEG = PACKETRAM_END + 1 ; 10 bytes for temporary storage
symbol SANDBOX_END = SANDBOX_BEG + 9 ; 10 bytes for temporary storage
PIN ASSIGNMENTS: Pins / legs change a lot in my designs. If it weren't for indirection, I would be going through a lot of code (and mistakes) adjusting statements.
Code:
symbol d_strobe = B.2 ; strobe
symbol d_clk = B.3 ; clock (output pin)
symbol d_data = B.4 ; data (output pin for shiftout)