Page 1 of 1

Cutting off the first 6 characters in a string

Posted: Fri Sep 26, 2008 6:32 pm
by Troy Martin
I'm working on a BASIC interpreter for an OS (not mine) and I need to know how to chomp the first six (or any other number or) characters off of a string.

Example:

User types print "Hello, world!"
program chops off print (space removed as well)
program removes all "'s (have a syscall for that in MikeOS already)
program prints out the final string, Hello, world!

Re: Cutting off the first 6 characters in a string

Posted: Fri Sep 26, 2008 9:04 pm
by cr2
:? :?:

What the **** are you doing on the forums?

Man, this should be easy... #-o

Re: Cutting off the first 6 characters in a string

Posted: Fri Sep 26, 2008 10:49 pm
by Troy Martin
Sorry, was half asleep at the time.

I'm doing this in 32-bit assembly, so it makes it a little harder.

That's what the **** I'm doing on the forums.

Re: Cutting off the first 6 characters in a string

Posted: Sat Sep 27, 2008 1:45 am
by Brendan
Hi,
Troy Martin wrote:I'm working on a BASIC interpreter for an OS (not mine) and I need to know how to chomp the first six (or any other number or) characters off of a string.
No, you need to know how to parse a string and extract a command name and a list of parameters, so that for a string like:

Code: Select all

Foo 1, 0x1234567, "Hello" + ASC(13) + "there", bar
You end up with:
  • Command: Foo
  • Parameters: 4
  • Parameter 1: Type = integer constant, value = 0x00000001
  • Parameter 2: Type = integer constant, value = 0x01234567
  • Parameter 3: Type = string, value = "Hello\nthere"
  • Parameter 4: Type = variable name, value = "bar".
You also need to handle syntax errors while you're doing this.

After you've done this you'd try to find the command name in a list of commands, and see if the number of parameters and the type of each parameter is acceptable.

Lastly, I'd recommend inventing some way to store the results from parsing so that if the same BASIC command is executed a second time you can skip the parsing and just use the previous results. That way you'll end up doing a lot less parsing in the middle of loops.


Cheers,

Brendan

Re: Cutting off the first 6 characters in a string

Posted: Sat Sep 27, 2008 8:59 am
by kubeos

Code: Select all

string db "print something",0
cmd1 db "print"
cmd2 db "goto"

parser:
mov si,string
mov di,cmd1
mov cx,5
rep cmpsb
je printcalled
.... (more checks)

printcalled:
mov si,string
add si,6
;now si is pointing to the 's' in 'something'
This isn't perfect but it should give you an idea of one way.

Re: Cutting off the first 6 characters in a string

Posted: Mon Nov 10, 2008 6:27 am
by DeletedAccount
Source Code of a small basic intrerpreter : http://forum.osdev.org/viewtopic.php?f= ... +construct .


Regards
Sandeep