Skip to content
Joachim Stolberg edited this page Jan 4, 2023 · 23 revisions

Strings are a sequence of characters. This is a string: "Hello world". Strings start and end with a double quote and are automatically zero terminated. (Strings have to be zero terminated. The zero character ('\0') marks the end of the string)

Please note: The CPU does not support 8-bit characters. Therefore, up to 2 characters are always stored in a word. The length of the string "test" is two words, the length of the string "hello" is 3 words.

A character is an 8 bit value. Character are limited to the ASCII character set. By means of octal escape sequences you can store any binary data into a string. This is typically used for command/communication purposes. Escape sequences start with a backslash ('\') followed by three digits:

var s1 = "\001\002\003";   // corresponds to: arr[3] = {1, 2, 3};

strcpy

Copies the string src into the array dest, including the terminating null word.

strcpy(dst, src)
  • dst Pointer to the destination array where the content is to be copied
  • src String to be copied

The function returns dst.

Example:

import "sys/stdio.asm"
import "sys/string.asm"

var s = "Hello world!";
var arr[7];

func foo() {
  strcpy(arr, s);
  putstr(arr);
  return;
}

strlen

Calculates and returns the length of the string str in words (without including the terminating null word itself).

strlen(str)

Example:

import "sys/stdio.asm"
import "sys/string.asm"

var s = "Hello world!";

func foo() {
  putstr("Length of s = ");
  putnum(strlen(s));
  return;
}

strcmp

strcmp(str1, str2)

Compare characters of two strings.

This function starts comparing the first words of each string. If they are equal to each other, it continues with the following pairs until the words differ, or until a terminating null-word is reached.

  • str1 String to be compared.
  • str2 String to be compared.

The function returns 0, if the contents of both strings are equal.

Example:

func init() {
  if(strcmp(s, "123") == 0) {
    putstr("strings are equal");
  }
}

strcat

strcat (dst, src)

Concatenate two strings. Appends a copy of the source string to the destination string. The terminating null-word in destination is overwritten by the first word of source, and a null-word is included at the end of the new string.

  • dst Pointer to the destination array, which should be large enough to contain the concatenated resulting string.
  • src String to be appended. This should not overlap destination.

Example:

import "sys/string.asm"

var s[32] = {0};

func init() {
  strcpy(s, "Hello");
  strcat(s, "");
  strcat(s, "world");
  putstr(s);
}

func loop() {
  halt();
}

strpack

strpack(s)

Compresses the specified string into a normalized string. Adding to strings with strcat() will give you a valid sting, but presumably with an unnormalized memory layout. Example:

  strcpy(s, "123");   ; => {0x3132, 0x0033, 0x0000}
  strcat(s, "45");    ; => {0x3132, 0x0033, 0x3435, 0x0000} 
  strpack(s);         ; => {0x3132, 0x3334, 0x0035, 0x0000}

E. g. strcmp(s, "12345") would return an incorrect result, if you do not normalize the string.