The Hermes has a byte based memory. But it contains no memory to memory instructions to manipulate characters. This appendix will rectify that and provide an example of the way the Hermes machine code can be written. Let's build a copy string procedure first.
What is a text string? There are at least two common definitions. It can be considered as a list of characters characterized by some length. These can basically be stored in two ways as a list with a special character that marks the end of the list or as a length followed by that many characters.
Alternatively a string can be considered a block of storage which currently contains of a series of characters. In this case two sizes are needed, the size of the block, and the number of characters currently occupying the block. In this case at least one length needs to be stored somewhere.
For our implementation let's choose the first definition: a list of
characters characterized by some length.
Let's also choose a method for specifying the length, that is
a special character marks the end of the text.
We will use the special character with the value 0x00, to mark
the end of the string.
So here is the procedure linkage parts of the STRCPY routine:
This shows how the procedure linkage to STRCPY will work. The JMP to the procedure stores the address of the argument list in ir[15] (the Jr). This will be used in the procedure code to get the arguments. Just before we return we increment the register to point to the return address and then JMP back.
First in the copy strings routine we need to set up the copy from the argument list in the caller.
This routine is a bit slow. It only moves one character each time round the loop. But after all, 4 characters can be loaded into a register at once. By moving 4 at a time the procedure would be about 4 times faster. Can you rewrite the code to improve it in this way? What happens when the number of characters to move is not evenly divisible by 4?
As an exercise try turning the pseudo-English code into hexadecimal Hermes machine code. To do that you need to pick a place in memory to put the STRCPY procedure. Let me suggest 0x02f00 as a nice place. You can put the call in 0x003000. Location 0x002e00 is a good place for the text string, and move it to location 0x002c00.
With this procedure Hermes has gained a STRCPY instruction. There are several other character handling instructions that might be nice to have. How about a STRCMP which takes two strings just like STRCPY but it sets the CCr to represent the relationship between the two strings. You might think about writing a pseudo-English version of this procedure.
One could also write a STRCHR procedure which would take two strings and return in ir[1] the address of the first string which contains any character from the second string. If none are found then it should return a 0. This would be a good one to practice on as well.
Maybe you can think of other string handling functions that would also
be make nice procedures.