Saturday, November 22, 2014

Assembly Tutorial 5: Strings

Accepting input from the user is basically pointless if you don't have the ability to do anything with it.  That is where string functions come in handy.

It is always handy to be able to change the case of a string.  This is handy for formatting the output or for string comparison.

To Upper
To make a character uppercase we will first check to make sure that it is a lowercase character by comparing it to 'a' and 'z' to make sure that it is between them.  After that we will simply subtract the difference between 'a' and 'A' from the character.

To Lower
Making a character lowercase is almost exactly the same as making it upper case.  We simply make sure it is between 'A' and 'Z' and then add the difference between 'a' and 'A' to the character.

For an entire string we simply loop through the whole string and do this to each character.

Substring
Taking a substring is a little more involved than changing the case, though not too complicated.  Using a length parameter in register CX and the address of the string in BX.

We need to copy the character at BX into a buffer and then decrement CX and increment BX.  When CX or BX are zero we null terminate the string in the buffer and return the buffer address in BX.

String Compare
Comparing two strings is very similar to taking a substring, except without the count or copy.

We simply take the addresses of 2 strings and then compare the characters at their addresses.  If they are equal and not 0 we increment the addresses and repeat.  If they are both zero we return that they are equal, if the characters are not equal we return which one was greater in value as a 1 or -1.

Like usual examples of this are in the repository for this tutorial.
https://github.com/musicman89/Assembly-Tutorial

No comments:

Post a Comment