OSDev.org https://forum.osdev.org/ |
|
How do I compare a string? https://forum.osdev.org/viewtopic.php?f=13&t=31205 |
Page 1 of 1 |
Author: | TheDev100 [ Wed Jan 11, 2017 3:32 pm ] |
Post subject: | How do I compare a string? |
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 |
Author: | DixiumOS [ Wed Jan 11, 2017 3:41 pm ] |
Post subject: | Re: How do I compare a string? |
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). Code: cmp eax, 'clear' je clearscreen 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 |
Author: | TheDev100 [ Wed Jan 11, 2017 3:45 pm ] |
Post subject: | Re: How do I compare a string? |
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. |
Author: | DixiumOS [ Wed Jan 11, 2017 3:50 pm ] |
Post subject: | Re: How do I compare a string? |
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. |
Author: | glauxosdever [ Wed Jan 11, 2017 3:50 pm ] |
Post subject: | Re: How do I compare a string? |
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 |
Author: | matt11235 [ Wed Jan 11, 2017 3:53 pm ] |
Post subject: | Re: How do I compare a string? |
I think that there's an example on the wiki - Real mode assembly I |
Author: | Brendan [ Wed Jan 11, 2017 8:10 pm ] |
Post subject: | Re: How do I compare a string? |
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 |
Author: | Gigasoft [ Wed Jan 18, 2017 12:05 pm ] |
Post subject: | Re: How do I compare a string? |
DixiumOS wrote: Let's assume what the user typed is in EAX, and we're using intel syntax (destination, source). Code: cmp eax, 'clear' je clearscreen 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 |
Author: | mikegonta [ Wed Jan 18, 2017 1:05 pm ] |
Post subject: | Re: How do I compare a string? |
DixiumOS wrote: TheDev100 wrote: Hey guys. I am trying to compare a string. Code: cmp eax, 'clear' Hope this worksje clearscreen 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: 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] |
Author: | onlyonemac [ Thu Jan 19, 2017 10:11 am ] |
Post subject: | Re: How do I compare a string? |
DixiumOS wrote: Let's assume what the user typed is in EAX, and we're using intel syntax (destination, source). 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.
Code: cmp eax, 'clear' je clearscreen 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. |
Author: | MichaelFarthing [ Thu Jan 19, 2017 10:23 am ] |
Post subject: | Re: How do I compare a string? |
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. |
Author: | mikegonta [ Thu Jan 19, 2017 1:52 pm ] |
Post subject: | Re: How do I compare a string? |
onlyonemac wrote: DixiumOS wrote: Let's assume what the user typed is in EAX, and we're using intel syntax (destination, source). That is literally THE worst string comparison function that I have ever seen. Note that character literals are not the same as stringCode: cmp eax, 'clear' This code compares eax (assuming it contains something like 'clear') and if equal/succesful, it jumps to the label clearscreen whereje clearscreen you can later clear the screen and reinitialize the shell. 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. |
Page 1 of 1 | All times are UTC - 6 hours |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |