Microcomputer systems: assembly programming techniques

Giuliano Donzellini, Domenico Ponta

String handling in assembly




A string is a sequence of coded characters, terminated with a final NULL (00h) code. We store a string as an array of ASCII codes in contiguous memory locations (each one contains the code of one character). By convention the address of the string is the address of its first character.

a) String Copy

Write a subprogram that will copy in RAM a string of your choice. The subprogram will copy the string at the address that the caller specifies in the HL register (we suggest to define a very short string, i.e. composed of a dozen of chars max).

To test the subprogram, you should write a main program linked to the hardware RESET. This main program, after the initialization of the Stack Pointer, will call the sub-program a few times, to copy the same string in different RAM locations of your choice (this program will be modified and reused as 'base code' in the following). Click here to load in the D-McE a template of the program. When finished, save the program with a name of your choice and test it in the debugger.

Note: repeat the save and test operations for each of the following assigments.

b) String length calculation

Write and test another program that calls a subprogram that calculates the length of the string (the number of chars of the string, excluding the termination NULL char).

The new subprogram will receive the string address in the HL register, and will return to the caller the length of the string in the BC register (so it will able to count very long strings). In the template code you will find some suggestion.

c) Character search

Write and execute a new program that calls a subprogram that searches the string for a specified ASCII character.

The caller program will ask the subprogram for the character to find, passing it in the A register. The string address will be specified in the HL register, as in the previous assignment. On exit, the subprogram will return the character position in the BC register (the position is coded from ‘1’ up; if the character is not present, the subprogram will return zero). In the template code you will find a trace of the subprogram code.