How Local Variables in Forth Work --- Using Apple’s Open Firmware Implementation: Difference between revisions

From OpenBIOS
Jump to navigation Jump to search
(Created page with "<P><FONT SIZE=5><B>How Local Variables in Forth Work --- Using Apple�s Open Firmware Implementation<BR> </B></FONT><BR> <FONT SIZE=3><FONT COLOR="#AA0000"><B><U>Contents:</...")
 
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
<P><FONT SIZE=5><B>How Local Variables in Forth Work  --- Using Apple&#146;s Open Firmware Implementation<BR>
<FONT SIZE=4><FONT COLOR="#AA0000"><B><U>Contents:</U></B></FONT><BR>
</B></FONT><BR>
<FONT SIZE=3><FONT COLOR="#AA0000"><B><U>Contents:</U></B></FONT><BR>
How to try out these code examples<BR>
How to try out these code examples<BR>
Declaring local variables<BR>
Declaring local variables<BR>
Setting local variables<BR>
Setting local variables<BR>
Setting a local variables&#146; value automatically<BR>
Obtaining local variables&#146; values<BR>
Obtaining local variables&#146; values<BR>
Booting Mac OS X in OpenBIOS<BR>
<BR>
<BR>
<BR>
<BR>
Line 20: Line 18:
<FONT COLOR="#003399"><B>Example 1</B></FONT><BR>
<FONT COLOR="#003399"><B>Example 1</B></FONT><BR>
<BR>
<BR>
: myword { ; cat dog }<BR>
<pre>
;<BR>
: myword { ; cat dog }
;
</pre>
<BR>
<BR>
Omitting the semicolon will initialize the local variable&#146;s values to values popped from the stack. Make sure the stack has enough values in it before proceeding. <BR>
Omitting the semicolon will initialize the local variable&#146;s values to values popped from the stack. Make sure the stack has enough values in it before proceeding. <BR>
Line 32: Line 32:
<FONT COLOR="#003399"><B>Example 2</B></FONT><BR>
<FONT COLOR="#003399"><B>Example 2</B></FONT><BR>
<BR>
<BR>
: myword { ; cat dog }<BR>
<pre>
4 -&gt; cat<BR>
: myword { ; cat dog }
5 -&gt; dog<BR>
4 -&gt; cat
;<BR>
5 -&gt; dog
;
</pre>
<BR>
<BR>
In the above example, the numbers are each pushed into the stack first. Then the -&gt; word pops a value out of the stack and sets each variable&#146;s value to a popped value. If a value is already in the stack, the following code would work as well: -&gt; cat (The 4 has been omitted).<BR>
In the above example, the numbers are each pushed into the stack first. Then the -&gt; word pops a value out of the stack and sets each variable&#146;s value to a popped value. If a value is already in the stack, the following code would work as well: -&gt; cat (The 4 has been omitted).<BR>
<BR>
<BR>
<FONT COLOR="#AA0000"><B><U>Obtaining a local variable&#146;s value</U></B></FONT><BR>
<BR>
<BR>
To push a local variable&#146;s value onto the stack, just enter a local variable&#146;s name in a word. <BR>
<FONT COLOR="#AA0000"><B><U>Setting a local variable&#146;s value automatically</U></B></FONT><BR>
<BR>
When you don&#146;t use the semicolon in the local variable declaration, the variables get their value from the stack. The order is the last variable declared is set to the last value pushed on the stack. <BR>
<BR>
<BR>
<FONT COLOR="#003399"><B>Example 3</B></FONT><BR>
<FONT COLOR="#003399"><B>Example 3</B></FONT><BR>
<BR>
<BR>
: myword { ; cat dog }<BR>
stack before myword is called ...<BR>
<BR>
<BR>
4 -&gt; cat<BR>
3 &lt;---- top of stack<BR>
5 -&gt; dog<BR>
2<BR>
1<BR>
<BR>
<BR>
cat  \ cat&#146;s value pushed onto stack  ( - cat)<BR>
<pre>
dog  \ dog&#146;s value pushed onto stack  (cat - cat dog )<BR>
: myword { one two three }
+<BR>
cr ." one = " one .
cr ." two = " two .
cr ." three = " three .
;
</pre>
<BR>
<BR>
cr<BR>
after myword is called ...<BR>
.&#148; Total animals = &#147; . <BR>
<BR>
cr<BR>
one = 1<BR>
;<BR>
two = 2<BR>
three = 3<BR>
<BR>
<BR>
<FONT COLOR="#AA0000"><B><U>Obtaining a local variable&#146;s value</U></B></FONT><BR>
<BR>
To push a local variable&#146;s value onto the stack, just enter a local variable&#146;s name in a word. <BR>
<BR>
<BR>
<FONT COLOR="#003399"><B>Example 4</B></FONT><BR>
<BR>
<BR>
<FONT COLOR="#AA0000"><B><U>Booting Mac OS X using OpenBIOS. </U></B></FONT><BR>
<pre>
: myword { ; cat dog }
4 -> cat
5 -> dog
 
cat  \ cat's value pushed onto stack  ( - cat)
dog  \ dog's value pushed onto stack  (cat - cat dog )
+
 
cr
." Total animals = " .
cr
;
</pre>
<BR>
<BR>
The reason why OpenBIOS can&#146;t boot Mac OS X is because of missing local variable support. Maybe you can help fix this problem. <BR>
<BR>
<BR>
</FONT></P>
</FONT>

Latest revision as of 19:18, 8 September 2012

Contents:
How to try out these code examples
Declaring local variables
Setting local variables
Setting a local variables&#146; value automatically
Obtaining local variables&#146; values


How to try out these code examples

The following example code may be entered and ran in Apple&#146;s Open Firmware. In order to access Open Firmware, just hold down Command-Option-O-F while you boot your pre-Intel Mac. PowerPC iMacs and PowerBooks, iBooks, PowerMac G3&#146;s, G4&#146;s, and G5&#146;s all have Open Firmware built-in.


Declaring local variables:

Place curly braces next to the word name. The curly braces must be on the same line as the word&#146;s name. The semicolon is optional. The semicolon lets the system know that the variables following it will not be initializing their values to the values that would have been popped from the stack.

Example 1

: myword { ; cat dog }
;


Omitting the semicolon will initialize the local variable&#146;s values to values popped from the stack. Make sure the stack has enough values in it before proceeding.


Setting local variables:

The word &#147;->&#148; (hyphen greater-than) is used to set the values of local variables.

Example 2

: myword { ; cat dog }
4 -> cat
5 -> dog
;


In the above example, the numbers are each pushed into the stack first. Then the -> word pops a value out of the stack and sets each variable&#146;s value to a popped value. If a value is already in the stack, the following code would work as well: -> cat (The 4 has been omitted).


Setting a local variable&#146;s value automatically

When you don&#146;t use the semicolon in the local variable declaration, the variables get their value from the stack. The order is the last variable declared is set to the last value pushed on the stack.

Example 3

stack before myword is called ...

3 <---- top of stack
2
1

: myword { one two three }
cr ." one = " one . 
cr ." two = " two . 
cr ." three = " three . 
;


after myword is called ...

one = 1
two = 2
three = 3


Obtaining a local variable&#146;s value

To push a local variable&#146;s value onto the stack, just enter a local variable&#146;s name in a word.

Example 4

: myword { ; cat dog }
4 -> cat
5 -> dog

cat  \ cat's value pushed onto stack   ( - cat)
dog  \ dog's value pushed onto stack   (cat - cat dog )
+

cr
." Total animals = " . 
cr
;