NCL 211: Working with Text

We've learned how to store strings, examine their contents, change their appearance, and combine them with other values.

We've also seen how strings can contain Unicode characters, control characters, and Display attributes.

Now let's put those techniques together.

In this lesson, we'll build a formatted report from several pieces of information. Rather than sending each piece to the Display separately, we'll construct the entire report in memory and display it when it's ready.

The Report

Imagine a program that displays information about a file.

We want it to produce something like this:

FILE INFORMATION
notes.txt

------------------------
SIZE                2048
PERMISSIONS          644
OWNER              ALICE
STATUS             SAVED
------------------------
FILE READY

The report has a heading, a filename, four information fields, and a closing message.

The labels and values are aligned into columns, the separators have a consistent width, and some text will use different colours.

We'll build all of that from individual values.

Starting with Data

First, let's define the information our report will contain.

#filename "notes.txt"
#size 2048
#permissions 0b110100100 -- rw-r--r--
#owner "alice"
#status "saved"

Most of these values are straightforward.

The filename, owner, and status are strings. The file size is an integer.

The permissions are also an integer, but we've written them in binary. Each group of three bits describes the read, write, and execute permissions for a particular category of user.

Understanding File Permissions

Unix-style file permissions use three groups of three bits: read (r), write (w), and execute (x), assigned to the file's owner, group, and everyone else.

Each group corresponds to one octal digit:

| Binary | Octal | Permissions | Description |
| ------ | ----- | ----------- | ----------- |
| `000` | `0` | `---` | No permissions |
| `001` | `1` | `--x` | Execute only |
| `010` | `2` | `-w-` | Write only |
| `011` | `3` | `-wx` | Write and execute |
| `100` | `4` | `r--` | Read only |
| `101` | `5` | `r-x` | Read and execute |
| `110` | `6` | `rw-` | Read and write |
| `111` | `7` | `rwx` | Read, write, and execute |

Our value, `0b110100100`, can be divided into `110 100 100`, giving us octal `644`, or `rw-r--r--`.

Binary makes the individual permissions easy to see. Octal represents the same nine bits using just three digits.

NCS/e does not inherently enforce Unix-style file permissions. A program could store permission information in file metadata, but enforcing those permissions would be the responsibility of software implementing such a system.

Our report will display the permissions in octal, as is customary for Unix-style file information.

Preparing the Values

Before assembling the report, we'll prepare the values we want to display.

#name $000
#mode $001
#state $002
#output $003
#separator $004
#field $005
#label $006

We'll use #name, #mode, and #state for formatted values.

#output will hold the complete report, while the remaining locations will help us construct it.

Changing Case

We want the owner and status to appear in uppercase.

S.UPR #name #owner
S.UPR #state #status

The results are:

#name  = "ALICE"
#state = "SAVED"

The original constants remain unchanged.

Converting to Octal

Our permissions are stored as an integer, but we want to display them as octal digits.

The String Coprocessor provides S.OCT for this:

S.OCT #mode #permissions

The result is the string "644".

Notice that we didn't need to convert the binary value into another integer first. Binary and octal are simply different representations of the same number.

S.OCT gives us a string suitable for displaying that number.

Aligning the Fields

Consider these two lines:

SIZE                2048
PERMISSIONS          644

The labels begin at the same position, and the values end at the same position.

We can accomplish this with S.PADR and S.PADL.

First, pad a label to 12 characters:

S.PADR #label "SIZE" " " 12

This produces "SIZE ".

Then pad the value to 10 characters:

S.PADL #field #size " " 10

This produces " 2048".

Joining them gives us:

S.JOIN #output #label #field

And the result is:

SIZE                2048

The label occupies 12 characters, and the value occupies 10, giving the complete row a width of 22 characters.

We'll use the same widths for every information row.

Padding Does Not Truncate

Padding makes a string at least as long as the requested width. It does not shorten strings that are already longer.

If a label or value exceeds its allotted width, the resulting row will be wider than expected.

Building the Header

Let's start constructing our report.

We'll begin with the heading and filename:

S.MOVE #output "FILE INFORMATION\r\n"
S.JOIN #output #output #filename
S.JOIN #output #output "\r\n\r\n"

The first instruction stores the heading in #output.

The second appends the filename.

The third adds two line breaks, leaving an empty line before the information rows.

At this point, #output contains:

FILE INFORMATION
notes.txt

Each S.JOIN uses the existing value of #output as its left-hand operand, appending more text to the report.

We can keep doing this until the report is complete.

Making a Separator

We also want a horizontal separator made of 24 dashes.

We could write all 24 characters manually, but there's a more useful approach:

S.PADR #separator "" "-" 24

We're padding an empty string with "-" until it reaches 24 characters.

The result is:

------------------------

This is a handy consequence of padding: padding an empty string can also be used to repeat a character.

Now append the separator and a line break:

S.JOIN #output #output #separator
S.JOIN #output #output "\r\n"

Our report is beginning to take shape.

Adding the Information Rows

We'll construct each row using the same pattern:

  1. Pad the label on the right.
  2. Pad the value on the left.
  3. Append both to the report.
  4. Append a line break.

For the file size:

S.PADR #label "SIZE" " " 12
S.PADL #field #size " " 10

S.JOIN #output #output #label
S.JOIN #output #output #field
S.JOIN #output #output "\r\n"

Next, the permissions:

S.PADR #label "PERMISSIONS" " " 12
S.PADL #field #mode " " 10

S.JOIN #output #output #label
S.JOIN #output #output #field
S.JOIN #output #output "\r\n"

Notice that we're using #mode, which already contains the octal representation of our permissions.

For the owner:

S.PADR #label "OWNER" " " 12
S.PADL #field #name " " 10

S.JOIN #output #output #label
S.JOIN #output #output #field
S.JOIN #output #output "\r\n"

And finally, the status:

S.PADR #label "STATUS" " " 12
S.PADL #field #state " " 10

S.JOIN #output #output #label
S.JOIN #output #output #field
S.JOIN #output #output "\r\n"

All four rows use the same widths, so their columns line up.

We can finish the report by adding another separator and a closing message:

S.JOIN #output #output #separator
S.JOIN #output #output "\r\n"
S.JOIN #output #output "FILE READY"

The report's text is now complete.

Adding Colour

Our report is readable, but we can make its important parts stand out.

The Display supports text attributes embedded directly inside strings.

For example:

  • \s06 selects blue text.
  • \s0C selects light green text.
  • \s0F selects white text.

We can use blue for the heading and green for the closing message.

Instead of starting the report with:

S.MOVE #output "FILE INFORMATION\r\n"

We can write:

S.MOVE #output "\f\s06FILE INFORMATION\s0F\r\n"

The \f moves the Display cursor to the top-left corner.

\s06 changes the text colour to blue, and \s0F restores white after the heading.

Similarly, we can change the final append to:

S.JOIN #output #output "\s0CFILE READY\s0F"

The closing message will appear in light green.

These attributes are part of the string we're constructing. The String Coprocessor doesn't render them; it simply stores and joins the text.

The Display interprets them when we eventually send the string to D.TXT.

Clearing the Display

Before displaying our report, we should make sure the screen doesn't contain leftover text from an earlier program.

We already know that \f moves the cursor to the top-left corner, but it doesn't erase anything.

For now, we can clear the visible area by taking advantage of the Display's scrolling behaviour.

The standard Display is 12 rows tall.

If we move the cursor down 24 rows and then print a space, the Display will scroll until the cursor is visible. By then, all the old rows will have been pushed off the screen.

We can store this sequence in a constant:

#clear "\s0F\f\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \f"

The sequence does four things:

  1. Selects normal white text.
  2. Moves the cursor home and then down 24 rows.
  3. Prints a space, triggering the scrolling.
  4. Returns the cursor to the top-left corner.

It's an unusual way to clear a screen, but it works with the Display operations we've already encountered.

Clearing by Scrolling

Clearing a screen by scrolling isn't as unusual as it might sound.

The original IBM PC's BIOS provided a video service, `INT 10h`, function `06h`, for scrolling a rectangular text window upward. Newly exposed rows were filled with blank characters using a specified text attribute.

Interestingly, requesting **zero rows** cleared the entire window instead.

NCS/e uses a related mechanism. When scrolling, it discards the top row and creates a fresh bottom row using the current style. Our clearing trick repeats this process until the old contents have disappeared.

Unlike the IBM PC's BIOS service, however, our trick triggers scrolling indirectly by moving the cursor beyond the visible Display and printing a character.

Sending the Report

With our report assembled in memory, displaying it is simple:

D.TXT #clear
D.TXT #output
D.BLT

The first instruction clears the visible area.

The second sends our completed report to the Display.

The third updates the visible screen.

We didn't need to position the cursor for every field or send each line separately. All the formatting was already included in #output.

That's the advantage of constructing text before displaying it: the program can prepare a complete result and then hand it to the output device in one operation.

Putting It All Together

Here's the complete program.

-- File information
#filename "notes.txt"
#size 2048
#permissions 0b110100100 -- rw-r--r--
#owner "alice"
#status "saved"

-- Working memory
#name $000
#mode $001
#state $002
#output $003
#separator $004
#field $005
#label $006

-- Clear the Display by scrolling
#clear "\s0F\f\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n \f"

-- Prepare values
S.UPR #name #owner
S.UPR #state #status
S.OCT #mode #permissions

-- Create the separator
S.PADR #separator "" "-" 24

-- Header
S.MOVE #output "\f\s06FILE INFORMATION\s0F\r\n"
S.JOIN #output #output #filename
S.JOIN #output #output "\r\n\r\n"
S.JOIN #output #output #separator
S.JOIN #output #output "\r\n"

-- File size
S.PADR #label "SIZE" " " 12
S.PADL #field #size " " 10
S.JOIN #output #output #label
S.JOIN #output #output #field
S.JOIN #output #output "\r\n"

-- Permissions
S.PADR #label "PERMISSIONS" " " 12
S.PADL #field #mode " " 10
S.JOIN #output #output #label
S.JOIN #output #output #field
S.JOIN #output #output "\r\n"

-- Owner
S.PADR #label "OWNER" " " 12
S.PADL #field #name " " 10
S.JOIN #output #output #label
S.JOIN #output #output #field
S.JOIN #output #output "\r\n"

-- Status
S.PADR #label "STATUS" " " 12
S.PADL #field #state " " 10
S.JOIN #output #output #label
S.JOIN #output #output #field
S.JOIN #output #output "\r\n"

-- Footer
S.JOIN #output #output #separator
S.JOIN #output #output "\r\n"
S.JOIN #output #output "\s0CFILE READY\s0F"

-- Display the completed report
D.TXT #clear
D.TXT #output
D.BLT

Run the program and you should see the file information report, with a blue heading, aligned information rows, and a green closing message.

The report is built entirely from the constants at the top of the program.

Inspecting the Result

The complete report lives in #output before it's sent to the Display.

We can inspect its length with S.LEN:

S.LEN #field #output

This stores the length of the report string in #field.

Remember that NCL strings have a maximum length of 511 Unicode characters. A report like ours fits comfortably within that limit, but larger reports may need to be constructed and displayed in sections.

Also remember that the Display is 32 characters wide. Our information rows are 22 characters wide, and our separators are 24 characters wide, so neither reaches the right edge.

Try It Yourself

Try modifying the report without changing its overall structure.

  1. Change the file information. Give the file a different name, size, owner, and status. Try different permission bits and see how the octal result changes.
  2. Adjust the formatting. Change the widths used by S.PADL and S.PADR. What happens if you make the value column wider? What happens when a value is longer than its allotted width?
  3. Change the separator. Try a different character or width. You only need to change the S.PADR instruction that creates it.
  4. Add another field. Introduce a file type or creation date and give it its own formatted row.
  5. Make the footer dynamic. Our report always ends with FILE READY, even if #status contains something else. Can you construct a closing message using the actual status instead?

The last exercise is particularly useful. Rather than writing the closing message directly, you'll need to build it from multiple strings, just as we did with the information rows.


In the 200-level, we've learned how to store, inspect, manipulate, and construct text. We've also learned how numbers, Unicode characters, control characters, and Display attributes can be represented inside strings.

In this lesson, we used those pieces together to build a formatted report from several values.

Ready to continue? In the next lesson, NCL 301: Reading the Keyboard, we'll start getting information from the person using the computer.

Want to try one yourself first? The 200-Level Challenge asks you to parse a structured conversation and turn it into a formatted chat log.