File I ∕ O - absoluteAquarian/CSASM GitHub Wiki

CSASM has eight channels through which file I/O operations can take place.

In the following sections, # refers to the number used for the I/O handle.
The number ranges from 0 to 7, inclusive.

$io.f#

The format for the I/O handle.
Expected values are 0 (Read-Only) and 1 (Write-Only).

$io.m#

The mode for the I/O handle.
Expected values are:

  • 1 - Create New
    • Creates a new file if it doesn't exist. If the file already exists, an exception is thrown
    • This mode is only expected to be used for I/O handles in the writing format
  • 2 - Create
    • Creates the file if it doesn't exist. If the file does exist, its length is truncated to zero
    • This mode is only expected to be used for I/O handles in the writing format
  • 3 - Open
    • Attempts to open the file for reading or writing. If the file doesn't exist, an exception is thrown
  • 4 - Open or Create
    • Attempts to open the file for reading or writing. If the file doesn't exist, a new one is created
  • 5 - Truncate
    • Attempts to open an existing file for writing. If the file exists, its length is truncated to zero. Otherwise, an exception is thrown
    • This mode is only expected to be used for I/O handles in the writing format
  • 6 - Append
    • Opens a new file and seeks to the end if it exists or creates a new file if it doesn't exist
    • This mode is only expected to be used for I/O handles in the writing format

$io.n#

If the I/O handle is using the Stream syntax, this register is for whether the handle writes with a newline appended.
If the I/O handle is using the Binary syntax, this register does nothing.
Expected values are 0 (doesn't write newlines) and 1 (writes newlines).

$io.p#

The path to the file the I/O handle will use.
Writing null to this instruction will make the I/O handle close.

If this register is non-null, the handle is currently active.
If a handle is active, only the $io.n# register can be modified.

$io.s#

The syntax for the I/O handle.
Expected values are 0 (Binary) or 1 (Stream).

If an I/O handle is using the Stream syntax, only <char> and <str> values can be read.

Example

.include <stdio>
func main:
    ; Set up one of the I/O registers
    ;   Format:   Write-only
    ;   Mode:     Create New or Truncate
    ;   Syntax:   Stream
    ;   Newlines: Yes
    push FILE_WRITE
    pop $io.f0
    push FILE_CREATE
    pop $io.m0
    push FILE_STREAM
    pop $io.s0
    push STREAM_WRITELINE
    pop $io.n0

    ; Initialize it by setting its file path to a non-null value
    push ".\\iotest - file.txt"
    pop $io.p0

    ; Write stuff to the file
    io.w0 "Hello, World!"
    io.w0 "This is a line."

    ; Set it to No Newlines midway
    push STREAM_WRITE
    pop $io.n0
    io.w0 "Multiple "
    io.w0 "writes "
    io.w0 "in one line! "
    push 33
    pop $a
    io.w0 $a

    ; Close the handle
    push FILE_CLOSE
    pop $io.p0
    ret
end
⚠️ **GitHub.com Fallback** ⚠️