Page 1 of 2

Code check required

PostPosted: 20 Mar 2012, 19:21
by justine
Hi i am attempting to write my own OS using my Evo and TS-LABS config.
I have written a command parser for which i would ask if someone would check my code to see if i have it correct please ?

Thanx in advance.

Code: Select all
; Input DE points to command buffer start
; HL points to command table

parse
      ld hl, table


eat_space
      ld a, (de)
      cp h'20         ; check for space
      jp nz, parse_start
      inc de
      jp eat_space

parse_start   
      cp h'20         ; space means end of command
      jp z, end_of_command
      cp (hl)         ; Compare a with first character in command table
      jp nz, next_command   ; Move HL to point to the first character of the next command in the table
      inc de
      inc hl
      jp parse_start

next_command
      cp '*'         ; Is it the end off command * ?
      jp z, forward         ; Yes inc HL 3 times to set to begining of next command in the table
      inc hl
      jp next_command

forward
      inc hl
      inc hl
      inc hl
      jp parse_start

end_of_command
      inc hl         ; increase to *
      inc hl         ; Increase to point to jump vector for command
      jp (hl)

; Command table below with all jump vectors.

table
      dw "cls"      ; Command
      db '*'         ; 1 byte
      dw jumpvector1      ; 2 bytes
         
                dw "videomode"      ; Command
      db '*'         ; 1 byte
      dw jumpvector2      ; 2 bytes




Re: Code check required

PostPosted: 20 Mar 2012, 19:24
by lvd
I think that unless you are Linus Torvalds, your OS should start from anything but not code writing :-)

Re: Code check required

PostPosted: 20 Mar 2012, 19:29
by justine
I was only asking if some kind hearted person could check my parse routine and show me mistakes.

Re: Code check required

PostPosted: 20 Mar 2012, 19:40
by breeze
Seems all right at first look.

justine wrote:dw "cls"      ; Command

justine wrote:dw "videomode"


But, maybe you mean DB ?

ps. I need a parser for my script engine . Can I use your code?

Re: Code check required

PostPosted: 20 Mar 2012, 19:44
by justine
Thanx Breeze,
Isn't dw for data word and db for data byte ?
Feel free to use my code, its good to know i am of use to someone ;-)

Re: Code check required

PostPosted: 20 Mar 2012, 19:49
by breeze
justine wrote:Isn't dw for data word and db for data byte ?


"db" is single char 1 byte, "dw" is world - 2 bytes.
If you try to compile like "dw" you will receive error "Bytes lost":

justine.asm(46): error: Bytes lost

justine wrote:Feel free to use my code, its good to know i am of use to someone ;-)


Thanks ;)

Re: Code check required

PostPosted: 20 Mar 2012, 19:56
by justine
Ah yes your right :agree: Was just a silly mistake would have found out when i got home and tried to compile it.

Thanx

Re: Code check required

PostPosted: 20 Mar 2012, 20:08
by justine
Revised code compiled using IAR EWZ80

Code: Select all
; Input DE points to command buffer start
; HL points to command table
; Output DE points to space after last character of command
; HL points to command jump vector

parse
      ld hl, table


eat_space
      ld a, (de)
      cp h'20         ; check for space
      jp nz, parse_start
      inc de
      jp eat_space

parse_start   
      cp h'20         ; space means end of command
      jp z, end_of_command
      cp (hl)         ; Compare a with first character in command table
      jp nz, next_command   ; Move HL to point to the first character of the next command in the table
      inc de
      inc hl
      jp parse_start

next_command
      cp '*'         ; Is it the end off command * ?
      jp z, forward         ; Yes inc HL 3 times to set to begining of next command in the table
      inc hl
      jp next_command

forward
      inc hl
      inc hl
      inc hl
      jp parse_start

end_of_command
      inc hl         ; increase to *
      inc hl         ; Increase to point to jump vector for command
      jp (hl)

; Command table below with all jump vectors.

table
      defb 'cls'                   ; Command
      defb '*'                     ; 1 byte
      defw jumpvector1      ; 2 bytes
         
      defb 'videomode'      ; Command
      defb '*'                    ; 1 byte
      defw jumpvector2     ; 2 bytes



Re: Code check required

PostPosted: 20 Mar 2012, 20:26
by justine
Added function to check for end of command by looking for a space of enter in buffer.

Code: Select all
; Input DE points to command buffer start
; HL points to command table
; Output DE points to space after last character of command
; HL points to command jump vector

parse
      ld hl, table


eat_space
      ld a, (de)
      cp h'20         ; check for space
      jp nz, parse_start
      inc de
      jp eat_space

parse_start   
      cp h'20         ; space means end of command
      jp z, end_of_command
      cp h'0d         ; Enter means end of command
      jp z, end_of_command
      cp (hl)         ; Compare a with first character in command table
      jp nz, next_command   ; Move HL to point to the first character of the next command in the table
      inc de
      inc hl
      jp parse_start

next_command
      cp '*'         ; Is it the end off command * ?
      jp z, forward         ; Yes inc HL 3 times to set to begining of next command in the table
      inc hl
      jp next_command

forward
      inc hl
      inc hl
      inc hl
      jp parse_start

end_of_command
      inc hl         ; increase to *
      inc hl         ; Increase to point to jump vector for command
      jp (hl)

; Command table below with all jump vectors.

table
      defb 'cls'      ; Command
      defb '*'         ; 1 byte
      defw jumpvector1      ; 2 bytes
         
      defb 'videomode'      ; Command
      defb '*'         ; 1 byte
      defw jumpvector2      ; 2 bytes


Re: Code check required

PostPosted: 20 Mar 2012, 20:28
by justine
checking for a space at the end of the command is handy as you can then have additional parameters to supply to the called routine. The called routine just checks for other characters after this space.
If it was an enter then there are no parameters to be passed.