Hey guys. I am trying to compare a string. I am wanting to make a shell with commands.
For example, if I type clear, it will clear the screen.
I've tried INT 16 and cmp but it hasn't worked.
How would I make it so if I type a command and press enter, it would do something.
The simplest approach is to store it in memory somewhere, then have a loop that compares each character in one string with the character in the same position in the other string.
The first improvement would be to use "rep cmpsb" to compare characters (and there are ways to speed it up a lot more with SSE/AVX if you're into that).
This is possibly fine if you only need to do one string comparison; however you're going to have more than one command so you're typically going to need to do more than one string comparison.
For your case (multiple commands/strings that are static/known in advance) the next improvement would be to use the length of the input string to find a list of command strings that have the same length. For example, if the user types "hello" then you might use "length = 5" to find a list that contains "clear" and any other 5 letter commands and avoid comparing against any command strings that have different lengths ("dir" and "exit" and "echo" and...) and make it a lot faster. Of course that's still not fast.
For your case, the fastest/best way would be to implement a finite state machine
using a tree
. The basic idea here is that you have a root table that has an addresses for each letter, where that address determines where to find a child table. Then if the user types "c" you use the entry corresponding to "c" to find the child table for all commands that begin with "c". Then if the user types "l" you use the entry (in the child table) to find the next child (the root table's grandchild) which would be used for all commands that begin with "cl". When the user presses enter, you already know which command it is so you can start doing that command immediately without doing any string comparisons.
Not only is this so fast that it's virtually instantaneous, you can avoid the need for storing the string in memory. It also means that you can implement "auto-complete" efficiently - e.g. if the user types "cl" and there's only one command that begins with those letters ("clear") then it'd be trivial to guess that the command will be "clear" and let the user press (e.g.) the tab key to auto-complete it (and let the user avoid typing the remaining 3 characters themselves).
Note that as performance increases, both complexity and code size tend to increase too. This is fairly normal - there's always a compromise between "slower" and "more complex and larger".