go to the table of contents or go back home

notes on forth programming in collapse os

part 03: memory

addresses

collapse os doesn't have a CELLS word, so you just have to remember that the size of a cell is 2 bytes and plan accordingly. or just define : CELLS 2 * ;. collapse os also doesn't have a ? [question-mark] word for viewing the number stored at an address, but it's basically equivalent to @ . [at, and then dot].

there also appear to be no built-in words for working with arrays, but you can approximate them pretty well by creating an address with the appropriate number of bytes allocated. if you're making a tic-tac-toe game (as is one of the problems in starting forth), CREATE BOARD 18 ALLOT0 gives you 18 bytes (9 cells) to work in. -1 BOARD ! stores -1 in the first cell, -1 BOARD 2 + ! stores it in the second cell, -1 BOARD 4 + ! stores it in the third cell, etc.

some things we aren't yet sure of the utility of, but since they're mentioned in starting forth: DUMP ( n a -- ) prints a hexdump of n bytes starting at address a. many other forths take the address before the number of bytes. the start address of the terminal input buffer is given by IN( [in left-paren], and there's no built-in word to give the length, but it can be found with IN) IN( -.

values (basically, variables)

VALUE x ( n -- ) is much like CREATE, but always allots two bytes, and when you call the name of the address, it will directly give you the number stored inside (instead of giving you the address, from which you can then fetch the value).

basically, if you want a value called MONTH, and you want to store 11 in it, you'd type 11 VALUE MONTH and now whenever you type MONTH, you'll get that 11 back. at least, until you store a different number in it. if it's december now, you can type 12 TO MONTH from the command line, or 12 [TO] MONTH within a definition. [TO] with brackets around it is the immediate version of TO without brackets. beyond that, we don't know why they aren't interchangeable.

if you want to create several values at once, you can use VALUES x ( n -- ). for example, 3 VALUES FROGS LIZARDS TURTLES will create the three given values, all with a "0" stored in them to start.

strings

if you define a word containing only a string—like : TEST ." sample" ;—the beginning of the string can be found 6 bytes after the start address of the word. so, ' TEST 6 + 7 STYPE gets the address of TEST and adds 6 to it, then types the first 7 characters found at TEST-plus-6.

to load an entire block worth of text, you can use BLK@ [b l k at], which stores the given block's contents in a buffer beginning at BLK( [b l k left-paren] and ending at BLK) [b l k right-paren].

go to the table of contents