Prior Page     This Chapter    Prior Chapter    Next Chapter







"Appendix

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.


The things in angle brackets must be replaced by an effective address expression that will yield the proper value for the argument. Constant arguments can be hard coded into the parameter slots after the call; then the corresponding initialization code can be deleted. The label CALLSUB is the byte address of the beginning of the JMP instruction. The pseudo-operation CON like the BYTES operation we met before just puts constants into memory, CON stores full words, BYTES only stores single bytes. The symbol *-* used in the operand portion of the CON pseudo-operation is to tell you that value will be initialized by the code.

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.








Prior Page     This Chapter    Prior Chapter    Next Chapter


Copyright © 1995 R. Uzgalis. All rights reserved.
Contact: buz@cs.aukuni.ac.nz