www.delorie.com/gnu/docs/gforth/gforth_86.html   search  
 
Buy GNU books!


Gforth Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.7.5 Address arithmetic

Address arithmetic is the foundation on which you can build data structures like arrays, records (see section 5.20 Structures) and objects (see section 5.21 Object-oriented Forth).

ANS Forth does not specify the sizes of the data types. Instead, it offers a number of words for computing sizes and doing address arithmetic. Address arithmetic is performed in terms of address units (aus); on most systems the address unit is one byte. Note that a character may have more than one au, so chars is no noop (on platforms where it is a noop, it compiles to nothing).

The basic address arithmetic words are + and -. E.g., if you have the address of a cell, perform 1 cells +, and you will have the address of the next cell.

In ANS Forth you can perform address arithmetic only within a contiguous region, i.e., if you have an address into one region, you can only add and subtract such that the result is still within the region; you can only subtract or compare addresses from within the same contiguous region. Reasons: several contiguous regions can be arranged in memory in any way; on segmented systems addresses may have unusual representations, such that address arithmetic only works within a region. Gforth provides a few more guarantees (linear address space, dictionary grows upwards), but in general I have found it easy to stay within contiguous regions (exception: computing and comparing to the address just beyond the end of an array).

ANS Forth also defines words for aligning addresses for specific types. Many computers require that accesses to specific data types must only occur at specific addresses; e.g., that cells may only be accessed at addresses divisible by 4. Even if a machine allows unaligned accesses, it can usually perform aligned accesses faster.

For the performance-conscious: alignment operations are usually only necessary during the definition of a data structure, not during the (more frequent) accesses to it.

ANS Forth defines no words for character-aligning addresses. This is not an oversight, but reflects the fact that addresses that are not char-aligned have no use in the standard and therefore will not be created.

ANS Forth guarantees that addresses returned by CREATEd words are cell-aligned; in addition, Gforth guarantees that these addresses are aligned for all purposes.

Note that the ANS Forth word char has nothing to do with address arithmetic.

doc-chars doc-char+ doc-cells doc-cell+ doc-cell doc-aligned doc-floats doc-float+ doc-float doc-faligned doc-sfloats doc-sfloat+ doc-sfaligned doc-dfloats doc-dfloat+ doc-dfaligned doc-maxaligned doc-cfaligned doc-address-unit-bits


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003