Page 1 of 1
How do I compare a string?
Posted: Wed Jan 11, 2017 3:32 pm
by TheDev100
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.
Thanks
John
Re: How do I compare a string?
Posted: Wed Jan 11, 2017 3:41 pm
by DixiumOS
TheDev100 wrote: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.
Thanks
John
About the question itself:
Let's assume what the user typed is in EAX, and we're using intel syntax (destination, source).
This code compares eax (assuming it contains something like 'clear') and if equal/succesful, it jumps to the label clearscreen where you can later clear the screen and reinitialize the shell.
Hope this works,
Dixium
Re: How do I compare a string?
Posted: Wed Jan 11, 2017 3:45 pm
by TheDev100
Thanks man
but is it possible for this to be 16-bit and also give me an example of how to do the other part?
I mean the part when you type in clear and press enter then it clears the screen.
Re: How do I compare a string?
Posted: Wed Jan 11, 2017 3:50 pm
by DixiumOS
TheDev100 wrote:Thanks man
but is it possible for this to be 16-bit and also give me an example of how to do the other part?
I mean the part when you type in clear and press enter then it clears the screen.
It is 16bit-compatible so real-mode operating systems can do this. However, change eax to ax (eax is only for 32bit and 64bit and later)
I don't know about the typing part (get user's character, append it to (e)ax and when enter is the last character, don't append the enter and see if the command is available. otherwise, print a error)
About the clear screen part, you can switch graphics mode to your current graphics mode (in 80x25 text operating systems, 3) to do that.
Re: How do I compare a string?
Posted: Wed Jan 11, 2017 3:50 pm
by glauxosdever
Hi,
The string "clear" takes 5 bytes (or 6, if NUL terminator is accounted for), while eax can hold only 4 bytes. You need a string comparison function that takes two pointers as input and traverses the strings pointed to.
I suggest you first learn before giving advice.
Regards,
glauxosdever
Re: How do I compare a string?
Posted: Wed Jan 11, 2017 3:53 pm
by matt11235
I think that there's an example on the wiki -
Real mode assembly I
Re: How do I compare a string?
Posted: Wed Jan 11, 2017 8:10 pm
by Brendan
Hi,
TheDev100 wrote: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".
Cheers,
Brendan
Re: How do I compare a string?
Posted: Wed Jan 18, 2017 12:05 pm
by Gigasoft
DixiumOS wrote:Let's assume what the user typed is in EAX, and we're using intel syntax (destination, source).
This code compares eax (assuming it contains something like 'clear') and if equal/succesful, it jumps to the label clearscreen where you can later clear the screen and reinitialize the shell.
Hope this works,
Dixium
What is this I don't even
Re: How do I compare a string?
Posted: Wed Jan 18, 2017 1:05 pm
by mikegonta
DixiumOS wrote:TheDev100 wrote:Hey guys. I am trying to compare a string.
Hope this works
glauxosdever wrote:The string "clear" takes 5 bytes (or 6, if NUL terminator is accounted for), while eax can hold only 4 bytes.
You need a string comparison function that takes two pointers as input and traverses the strings pointed to.
I suggest you first learn before giving advice.
Gigasoft wrote:What is this I don't even
While his
advice is not technically correct, it is none the less an
old school technique.
Code: Select all
mov esi, buffer
xor eax, eax
mov esi, buffer
mov edi, esi
stosd ; clear buffer to all zeros
; get user input and limit to 4 ascii characters
lodsd ; esi points to user input string
or eax, 0x20202020 ; convert to lower case
; and pad less than 4 character input with spaces for comparison
cmp eax, 'clea' ; r screen
je clear_screen
cmp eax, 'time'
je get_time
cmp eax, 'xxx '
je show_pomography
; ... and so on
Beware of the
homoglyph in
show_pomography.
[edit - corrected code example]
Re: How do I compare a string?
Posted: Thu Jan 19, 2017 10:11 am
by onlyonemac
DixiumOS wrote:Let's assume what the user typed is in EAX, and we're using intel syntax (destination, source).
This code compares eax (assuming it contains something like 'clear') and if equal/succesful, it jumps to the label clearscreen where you can later clear the screen and reinitialize the shell.
That is literally THE worst string comparison function that I have ever seen. Note that character literals are not the same as string literals and that strings are arrays of integer values with each element containing the ASCII code of the respective character in the string. It is standard practice to pass a pointer to (i.e. the memory address of) these arrays around in registers. This is not the same as storing the string itself in a register. Once you understand that registers are fixed sizes and that strings can have any number of characters in them, it should be fairly to understand why we don't store strings themselves in registers.
Re: How do I compare a string?
Posted: Thu Jan 19, 2017 10:23 am
by MichaelFarthing
onlyonemac wrote:
That is literally THE worst string comparison function that I have ever seen. Note that character literals are not the same as string literals and that strings are arrays of integer values with each element containing the ASCII code of the respective character in the string. It is standard practice to pass a pointer to (i.e. the memory address of) these arrays around in registers. This is not the same as storing the string itself in a register. Once you understand that registers are fixed sizes and that strings can have any number of characters in them, it should be fairly to understand why we don't store strings themselves in registers.
Slightly unfair (though the code being discussed has one or two little difficulties like trying to use stos with esi instead of edi).
There are situations where holding a complete string in eax might make sense, if for example you were searching a list of small, fixed length codes such as the 3 letter ISO country codes or the table of elements.
Re: How do I compare a string?
Posted: Thu Jan 19, 2017 1:52 pm
by mikegonta
onlyonemac wrote:DixiumOS wrote:Let's assume what the user typed is in EAX, and we're using intel syntax (destination, source).
This code compares eax (assuming it contains something like 'clear') and if equal/succesful, it jumps to the label clearscreen where
you can later clear the screen and reinitialize the shell.
That is literally THE worst string comparison function that I have ever seen. Note that character literals are not the same as string
literals and that strings are arrays of integer values with each element containing the ASCII code of the respective character in the
string. It is standard practice to pass a pointer to (i.e. the memory address of) these arrays around in registers. This is not the same
as storing the string itself in a register. Once you understand that registers are fixed sizes and that strings can have any number of
characters in them, it should be fairly to understand why we don't store strings themselves in registers.
MichaelFarthing wrote:Slightly unfair (though the code being discussed has one or two little difficulties like trying to use stos with esi instead of edi).
The code that you are referring to is not the same code as referred to by
onlyonemac, however his critique applies equally well to both.
The
stos business is in my code (which I threw in at the last moment) in my example. I'm not a big fan of such instructions (although I was
recently doing some 8086 boot sector programming (where every byte counts). I've corrected the example and thank you for pointing it out.
What's the "other little difficulty"?
Getting back to
onlyonemac's critique - I fully agree, however in the problem solving realm, it's best to approach such thing without
preconceived notions however correct those notions may be.