How do I compare a string?

Programming, for all ages and all languages.
Locked
TheDev100
Member
Member
Posts: 27
Joined: Wed Jan 11, 2017 3:29 pm

How do I compare a string?

Post 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
User avatar
DixiumOS
Member
Member
Posts: 84
Joined: Tue Jan 10, 2017 3:19 pm
Libera.chat IRC: NunoLava1998

Re: How do I compare a string?

Post 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).

Code: Select all

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
(not so frequently updated) Code is at:

https://github.com/NunoLava1998/DixiumOS-1
TheDev100
Member
Member
Posts: 27
Joined: Wed Jan 11, 2017 3:29 pm

Re: How do I compare a string?

Post by TheDev100 »

Thanks man :D 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.
User avatar
DixiumOS
Member
Member
Posts: 84
Joined: Tue Jan 10, 2017 3:19 pm
Libera.chat IRC: NunoLava1998

Re: How do I compare a string?

Post by DixiumOS »

TheDev100 wrote:Thanks man :D 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.
(not so frequently updated) Code is at:

https://github.com/NunoLava1998/DixiumOS-1
glauxosdever
Member
Member
Posts: 501
Joined: Wed Jun 17, 2015 9:40 am
Libera.chat IRC: glauxosdever
Location: Athens, Greece

Re: How do I compare a string?

Post 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
User avatar
matt11235
Member
Member
Posts: 286
Joined: Tue Aug 02, 2016 1:52 pm
Location: East Riding of Yorkshire, UK

Re: How do I compare a string?

Post by matt11235 »

I think that there's an example on the wiki - Real mode assembly I
com.sun.java.swing.plaf.nimbus.InternalFrameInternalFrameTitlePaneInternalFrameTitlePaneMaximizeButtonWindowNotFocusedState
Compiler Development Forum
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: How do I compare a string?

Post 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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Gigasoft
Member
Member
Posts: 855
Joined: Sat Nov 21, 2009 5:11 pm

Re: How do I compare a string?

Post by Gigasoft »

DixiumOS wrote:Let's assume what the user typed is in EAX, and we're using intel syntax (destination, source).

Code: Select all

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
mikegonta
Member
Member
Posts: 229
Joined: Thu May 19, 2011 5:13 am
Contact:

Re: How do I compare a string?

Post by mikegonta »

DixiumOS wrote:
TheDev100 wrote:Hey guys. I am trying to compare a string.

Code: Select all

cmp eax, 'clear'
je clearscreen
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]
Last edited by mikegonta on Thu Jan 19, 2017 1:54 pm, edited 1 time in total.
Mike Gonta
look and see - many look but few see

https://mikegonta.com
onlyonemac
Member
Member
Posts: 1146
Joined: Sat Mar 01, 2014 2:59 pm

Re: How do I compare a string?

Post by onlyonemac »

DixiumOS wrote:Let's assume what the user typed is in EAX, and we're using intel syntax (destination, source).

Code: Select all

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.
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.
When you start writing an OS you do the minimum possible to get the x86 processor in a usable state, then you try to get as far away from it as possible.

Syntax checkup:
Wrong: OS's, IRQ's, zero'ing
Right: OSes, IRQs, zeroing
User avatar
MichaelFarthing
Member
Member
Posts: 167
Joined: Thu Mar 10, 2016 7:35 am
Location: Lancaster, England, Disunited Kingdom

Re: How do I compare a string?

Post 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.
mikegonta
Member
Member
Posts: 229
Joined: Thu May 19, 2011 5:13 am
Contact:

Re: How do I compare a string?

Post by mikegonta »

onlyonemac wrote:
DixiumOS wrote:Let's assume what the user typed is in EAX, and we're using intel syntax (destination, source).

Code: Select all

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.
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.
Mike Gonta
look and see - many look but few see

https://mikegonta.com
Locked