Electronic Design

  
Reprints     Printer-Friendly    Email this Article    RSS        Font Size     What's This?


[Design Application]
Forth Still Suits Embedded Applications
Although Sometimes Viewed Merely As An Interesting Relic, Forth Still Holds A Role In Today's Embedded Systems.

Tom Napier  |   ED Online ID #7489  |   November 22, 1999


Of course, a Forth word needs to know the location of those flags and other parameters. Like most compiled languages, then, Forth passes them on a stack. Each successive word processes the parameters left on the stack by the previous one. A parameter can be anything you need it to be: a number, a Boolean value, a variable address, or a pointer to a data structure. The next word in the code knows how to interpret it correctly.

All arithmetical and logical operations are done between such numbers on the parameter stack. Since Forth works by calling successive words, it needs a return stack to keep track of where it came from. The return stack also stores loop limits and indices.

Unlike other languages, Forth doesn't hide the stack from you. The programmer has complete access to the parameter stack and should be aware of what's on it. Since Forth keeps intermediate results on the stack, it uses fewer named variables than most languages. When programming, keep track of the stack rather than worry about whether a named variable has been overwritten. Luckily, one rarely needs to think about more than three or four stack items at any one time.

This stack access is one of the language's more powerful (and dangerous) features. Because you can manipulate the return stack, for example, you could drop a return address to abort execution of the current word. You also can push a temporarily inconvenient parameter stack value onto the return stack for deferred handling. (Forgetting to pop it again, however, can lead to spectacular crashes.)

Along with providing the programming power of stack manipulation, Forth harnesses the computational power of today's 32-bit processors. Early Forths standardized on 16-bit width for both the parameter and return stacks. This suited the addressing range of the first microprocessors, and allowed both integers and addresses to be processed by the same routines. (Forth allows arithmetical operations on addresses. Rather than defining an "array" data type, which might only be used once, it's common practice to add an offset to a variable address to fetch an array member or a string character.)

The earliest Forths even had some double precision (32-bit) operations. One number occupied two stack levels. Now, except on the smallest microprocessors, Forth uses 32-bit single-precision numbers. Double precision is available.

Numbers can be defined to be either integer or floating-point values. But beyond that, Forth has little or no data typing. It assumes that programmers don't need babysitting and would rather have flexibility than over-fussy error messages. If you have a good reason to add a Boolean value to a number, the compiler won't try to outguess you. Thus, you can mix and match values as you wish, so long as you're careful about it. Of course, you must comment on your code thoroughly or you will exasperate future maintenance programmers.

This flexibility makes it easy to define new data types. I regularly use a Forth that offers 32-bit floating-point numbers. When writing a filter-analysis program, I defined pairs of them to be complex numbers. I wrote my own library of complex arithmetic routines. It took about 20 minutes—less time than it would have taken to find and understand a precoded library.

It can take a little effort to become accustomed to Forth because it's conceptually different from other languages. For example, a variable name in Forth is a word that puts an address on the parameter stack. If you want to fetch the value of the variable or store the number on the stack as the variable's value, you follow the variable name with "fetch" or "store." These are abbreviated to @ and ! respectively, which looks a little cryptic at first sight. If you leave out the @, you end up processing the variable's address rather than its contents. Since this too is a legitimate operation in Forth, no error message is generated.

Constants are another data type that Forth handles a bit differently. A constant is a word that puts a predetermined number on the stack. But if you don't want to define them as constants, Forth will happily compile numbers as literals.

While producing code, a Forth compiler acts much like an assembler (see the figure). All it does is look up each word of the source in a dictionary. (Many Forths use hashing to speed the search.) If the compiler doesn't find the word, it tries to interpret it as a number. If that fails, it prints an error message. If it finds it, it does one of two things, depending on whether it's currently in the middle of compiling a definition or has finished one definition and is waiting for the start of the next.

When in the middle of compiling a definition, the compiler takes the address (or token) of the word from the dictionary and adds it to the object code. If the compiler has finished a definition, however, it executes the code for the word. This is what happens when you type a word at the keyboard. To execute the code for a word, many versions of Forth use a short machine-code routine to read each address in the word code and jump to it.


<-- prev. page     1 [2] 3 4     next page -->

Reprints   Printer-Friendly  Email this Article  RSS    Font Size   What's This?


  • Network-On-Chip Tools Arrive for The Masses
  • Tackling System Design Challenges Through Early Verification
  • ESL Tools Take Center Stage As Designers Move Up
  • Parasitic Extraction Tool Targets Next-Generation Custom ICs
  • Synopsys Jumps Into ESL-Synthesis Pool
  • Verify Control Systems Before Committing To Hardware
  • You're Using How Many FPGAs?
  • Tool Up For The FPGA Blitz
    1) Build A Smart Battery Charger Using A Single-Transistor Circuit
    (187 views today)
    2) Hot Hands For Some Cool Rock: Motion Sensing Meets Audio Engineering
    (166 views today)
    3) Science Fiction Meets Science Fact In Today's Robot Research
    (96 views today)
    4) GPS-Derived Grandmaster Clock Delivers Ultra-Precise Time And Frequency Sync
    (89 views today)
    5) What's All This Transimpedance Amplifier Stuff, Anyhow? (Part 1)
    (86 views today)
    ALL TOP 20



    POST YOUR COMMENTS HERE
    Name:

    Email:
    Your Comments:

    Enter the text from the image below


    Please refresh the page if you have trouble reading this text.

    Search Electronic Design
         
      
     
    Web Seminar
    Sponsored By:
    Title: Read Pacing: A Performance Enhancing Feature of PCI Express Gen 2 Switch Devices
    Speakers: 
    Date: 07/01/08
    Register: 

    Electronic Design Europe Electronic Design China EEPN Power Electronics Auto Electronics Microwaves & RF
    Mobile Dev & Design Schematics Find Power Products Military Electronics EE Events Related Resources