Addressing and registers


Registers are essentially extremely high speed local memory that is resident in the CPU. They are used for many operations. Results are placed here, tests are performed on data here, and arithmetic is done on values in these registers. This is normally done for speed, inasmuch as register access does not involve going outside the CPU to make a bus access or a memory reference.

Registers can be used to hold addresses as well as data. One form of addressing, known as register-indirect, uses the value in a register as if it were a pointer: the data is fetched from the specified memory location. A modification of this, auto-increment mode, will not only use the register contents as an address but will automatically update the register so that it points to the next location. This can lead to some very efficient code. On the DEC PDP-11 architecture, the C statement,

 while (*d++ = *s++) ; 

which performs the equivalent of a string copy, could be done in a two-instruction loop:

 x:  movb  (r0)+,(r1)+   ; move a byte, increment both addresses      bnz    x            ; branch back if the byte moved was not zero 

In the above example, the CPU would take a source address from a register ( r0 ), a destination address from another register ( r1 ), and do a memory-to-memory move. The same instruction would update both the source and destination addresses appropriately and would set a flag based on the value of the byte it moved so that the following conditional branch would detect the case of a zero-byte and stop the sequence.

There is normally a corresponding auto-decrement mode as well. Unfortunately, SPARC does not use either of these modes. If you want to add a one to a register, a separate instruction is needed.

Another mode of addressing is known as PC-relative. This mode uses a value in an instruction or in a register as an offset from the location of the current instruction, adds the two, and finds the data address that way. This is very useful for PIC, or position-independent code.

Consider the requirements for writing a shared library, like libc . The library can be mapped into any arbitrary location in memory, depending on the program that uses it. This means that the code cannot ever use an absolute address of a variable, because it can't tell where that variable will be. Everything must be relative to the place at which the library was mapped.

With PC-relative addressing, the actual data address is irrelevant, as long as it doesn't change with respect to the code. SPARC does not have any PC-relative data addressing capabilities, which makes designing and writing position-independent code rather interesting for the programmer, to say the least.



PANIC. UNIX System Crash Dump Analysis Handbook
PANIC! UNIX System Crash Dump Analysis Handbook (Bk/CD-ROM)
ISBN: 0131493868
EAN: 2147483647
Year: 1994
Pages: 289
Authors: Chris Drake

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net