flat assembler
Message board for the users of flat assembler.

Index > Main > Comparing Strings Confused

Goto page 1, 2  Next
Author
Thread Post new topic Reply to topic
nathanpc



Joined: 23 Aug 2009
Posts: 40
Location: Brazil
nathanpc
Hello,
I'm building a program that have a primitive command-line to execute some simple tasks(at the time I'm just using HelloWorld things on it to test), but I don't know why, it isn't comparing ok. Here is my code:
Code:
main:
    mov si, welcome
    call printf
    
    mloop:
          mov si, prompt          ; The prompt is beginning
           call printf
         call getinput
       
    mov di, cmd_hello       ; Command: hello
        call strcmp
         jc .hello
           
            mov di, cmd_help        ; Command: help
 call strcmp
         jc .help
            
            mov di, cmd_stats       ; Command: stats
        call strcmp
         jc .stats
           
            mov si, err_cmd         ; Command entered is invalid
        call printf
         jmp mloop               ; Infinity loop
     
            .hello:
             mov si, msg_hello
           call printf
         jmp mloop
       
            .help:
              mov si, msg_help
            call printf
         jmp mloop
           
        .stats:
             mov si, ax
          call printf
         jmp mloop

; === Functions ===
printf:
    lodsb
    mov ah, 0eh
    mov bh, 99h

    .nextchar:
        lodsb
       or al, al
        jz .return
        int 10h
        jmp .nextchar

    .return:
        ret

getinput:
    xor cl, cl
    
    .loop:
         mov ah, 00h
         int 16h         ; Key press wait
            
            cmp al, 08h     ; Backspace key
     je .backspace   ; Handle
            
            cmp al, 0dh     ; Enter key
         je .enter       ; Handle
            
            cmp cl, 3fh     ; Inputted 63 characters
            je .loop        ; Only backspace and enter are accepted

         mov ah, 0eh   
      int 10h
     
            stosb
       inc cl
      jmp .loop
           
    .backspace:
     cmp cl, 0       ; Begin of the input
        je .loop        ; Ignore
            
            dec di
      mov byte[di], 0 ; Delete character
          dec cl          ; Decrementing the string counter
           
            mov ah, 0eh
         mov al, 08h
         int 10h         ; Print the backspace
       
            mov al, ' '
       int 10h         ; Blank character
           
            mov al, 08h
         int 10h         ; Backspace one more time
           
            jmp .loop       ; Goes back to the loop
     
    .enter:
         mov al, 0       ; Null terminator
           stosb
       
            mov ah, 0eh
         
            mov al, 0dh     ; Enter character
           int 10h
     
            mov al, 0ah     ; Newline character
         int 10h
     
            ret
         
strcmp:
    .loop:
           mov al, [si]    ; Grab byte from SI
         mov bl, [di]    ; Grab byte from DI
         cmp al, bl      ; Compare if they are equal
         jne .notequal   ; They aren't equal
        
            cmp al, 0       ; Both bytes are null
       je .done
            
            inc di          ; Increment DI
      inc si          ; Increment SI
      jmp .loop       ; Start looping
     
    .notequal:
      clc             ; Clear the carry flag
      ret
    
    .done:
       stc             ; Set the carry flag
        ret

welcome db "Welcome To The Second DOS Shell For Your DOS Razz", 0dh, 0ah, 0ah, 0
prompt db " > ", 0

; ==== Commands ==========================
cmd_help db "help", 0
msg_help db " Valid Commands: help - hello", 0dh, 0ah, 0

cmd_hello db "hello", 0
msg_hello db " Hi all!", 0dh, 0ah, 0

cmd_stats db "stats", 0

err_cmd db " The command you entered doesn't exist!", 0dh, 0ah, 0    

But when I run that, doesn't matter what command I type(help, stats, hello, test, fasm_rulez... - Even those that should return the error message of invalid command), I always got the help command message:
Code:
> test
Valid Commands: help - hello
> hello
Valid Commands: help - hello
> fasm rules
Valid Commands: help - hello
> wtf!
Valid Commands: help - hello    

What I need to do?

Best Regards,
Nathan Paulino Campos

_________________
Developer: C++, Java, C/C++, Java ME, Java EE, Pascal, Assembly(8086, 6502, z80, ARM and MIPS), Delphi, Visual Basic, Pascal, Ruby and Perl
Post 25 Jun 2010, 01:16
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
You never restored that value of SI. It INCrements and then permanently points to the trailing zero byte,
Post 25 Jun 2010, 01:20
View user's profile Send private message Visit poster's website Reply with quote
nathanpc



Joined: 23 Aug 2009
Posts: 40
Location: Brazil
nathanpc
You're saying that I should clean the SI register after the jmp .loop?
Post 25 Jun 2010, 01:23
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
There are two ways to approach a solution.

1) push push/pop pairs in strcmp to save register values that you alter. Or,
2) use mov si,prompt before every call to strcmp.
Post 25 Jun 2010, 01:26
View user's profile Send private message Visit poster's website Reply with quote
nathanpc



Joined: 23 Aug 2009
Posts: 40
Location: Brazil
nathanpc
I've opted for the first method, but I'm not so good with stacks, then I want to know if you could help me to know how to do this. Rolling Eyes
Post 25 Jun 2010, 01:29
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
Code:
strcmp:
        push si
    .loop:
        mov al, [si]    ; Grab byte from SI
        mov bl, [di]    ; Grab byte from DI
        cmp al, bl      ; Compare if they are equal
        jne .notequal   ; They aren't equal

        cmp al, 0       ; Both bytes are null
        je .done

        inc di          ; Increment DI
        inc si          ; Increment SI
        jmp .loop       ; Start looping

    .notequal:
        clc             ; Clear the carry flag to indicate failure
        pop si
        ret

    .done:
        stc             ; Set the carry flag to indicate success
        pop si
        ret    
Post 25 Jun 2010, 01:45
View user's profile Send private message Visit poster's website Reply with quote
nathanpc



Joined: 23 Aug 2009
Posts: 40
Location: Brazil
nathanpc
I've done as you said, but it still prints me the same(the help command output). Sad
Post 25 Jun 2010, 01:47
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
Code:
strcmp:
        pusha; ---[NEW]---
    .loop: 
        mov al, [si]    ; Grab byte from SI 
        mov bl, [di]    ; Grab byte from DI 
        cmp al, bl      ; Compare if they are equal 
        jne .notequal   ; They aren't equal 
         
        cmp al, 0       ; Both bytes are null 
        je .done 
         
        inc di          ; Increment DI 
        inc si          ; Increment SI 
        jmp .loop       ; Start looping 
         
    .notequal: 
        popa; ---[NEW]---
        clc             ; Clear the carry flag
        ret 
     
    .done: 
        popa ; ---[NEW]---
        stc             ; Set the carry flag
        ret
    
Post 25 Jun 2010, 01:48
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
nathanpc wrote:
I've done as you said, but it still prints me the same(the help command output). Sad
Where does getinput store its data? You have no buffer space assigned for it. You need to initialise the value of DI before calling getinput. And also set the value of SI so that strcmp can see what was entered.
Post 25 Jun 2010, 01:58
View user's profile Send private message Visit poster's website Reply with quote
nathanpc



Joined: 23 Aug 2009
Posts: 40
Location: Brazil
nathanpc
Nothing changes, I'm still getting the wrong output. Crying or Very sad
Post 25 Jun 2010, 02:01
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
I see you getinput into DI but never is pointed at any buffer...
Code:
        mov si, prompt
        call printf 

        mov di, buffer
        call getinput

        mov si, buffer
         
        mov di, cmd_hello
        call strcmp 
        jc .hello
         
        mov di, cmd_help
        call strcmp 
        jc .help 
         
        mov di, cmd_stats
        call strcmp 
        jc .stats   

;data
buffer db 64 dup(0)
    

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 25 Jun 2010, 02:01
View user's profile Send private message Reply with quote
nathanpc



Joined: 23 Aug 2009
Posts: 40
Location: Brazil
nathanpc
revolution wrote:
nathanpc wrote:
I've done as you said, but it still prints me the same(the help command output). Sad
Where does getinput store its data? You have no buffer space assigned for it. You need to initialise the value of DI before calling getinput. And also set the value of SI so that strcmp can see what was entered.

I've done this for the buffer:
Code:
    mloop:
       mov si, prompt          ; The prompt is beginning
           call printf
         
            mov di, buffer
      call getinput

; .... Lots of code ....

buffer: times 64 db 0    

But Nothing changed. Crying or Very sad

_________________
Developer: C++, Java, C/C++, Java ME, Java EE, Pascal, Assembly(8086, 6502, z80, ARM and MIPS), Delphi, Visual Basic, Pascal, Ruby and Perl
Post 25 Jun 2010, 02:04
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
Look closer at my last post...
Post 25 Jun 2010, 02:05
View user's profile Send private message Reply with quote
nathanpc



Joined: 23 Aug 2009
Posts: 40
Location: Brazil
nathanpc
bitshifter wrote:
I see you getinput into DI but never is pointed at any buffer...
Code:
        mov si, prompt
        call printf 

        mov di, buffer
        call getinput

        mov si, buffer
         
        mov di, cmd_hello
        call strcmp 
        jc .hello
         
        mov di, cmd_help
        call strcmp 
        jc .help 
         
        mov di, cmd_stats
        call strcmp 
        jc .stats   

;data
buffer db 64 dup(0)
    

I've done it, but now, every time I enter the hello command, I start to don't have outputs:
Code:
> test
Valid Commands: help - hello
> hello
Valid Commands: help - hello
> help
> test
> something
>    

_________________
Developer: C++, Java, C/C++, Java ME, Java EE, Pascal, Assembly(8086, 6502, z80, ARM and MIPS), Delphi, Visual Basic, Pascal, Ruby and Perl
Post 25 Jun 2010, 02:07
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
It was meant to work my previous post...
http://board.flatassembler.net/topic.php?p=116049#116049
Post 25 Jun 2010, 02:09
View user's profile Send private message Reply with quote
nathanpc



Joined: 23 Aug 2009
Posts: 40
Location: Brazil
nathanpc
bitshifter wrote:
It was meant to work my previous post...
http://board.flatassembler.net/topic.php?p=116049#116049

Sadly not... Crying or Very sad

_________________
Developer: C++, Java, C/C++, Java ME, Java EE, Pascal, Assembly(8086, 6502, z80, ARM and MIPS), Delphi, Visual Basic, Pascal, Ruby and Perl
Post 25 Jun 2010, 02:15
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
nathanpc: Please show your latest code so we can all know what you are working with now.
Post 25 Jun 2010, 02:17
View user's profile Send private message Visit poster's website Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
Code:
printf: 
    ;lodsb <---------remove this
    mov ah, 0eh 
    mov bh, 99h
    


Edit: Working code is here...
Code:
org 0100h

main:
    mov si, welcome 
    call printf 
     
    mloop: 
        mov si, prompt          ; The prompt is beginning 
        call printf 

        mov di, buffer
        call getinput
        mov si, buffer
         
        mov di, cmd_hello       ; Command: hello
        call strcmp 
        jc .hello
         
        mov di, cmd_help        ; Command: help
        call strcmp 
        jc .help 
         
        mov di, cmd_stats       ; Command: stats
        call strcmp 
        jc .stats 
         
        mov si, err_cmd         ; Command entered is invalid
        call printf 
        jmp mloop               ; Infinity loop 
         
        .hello: 
            mov si, msg_hello 
            call printf 
            jmp mloop 
         
        .help: 
            mov si, msg_help 
            call printf 
            jmp mloop 
             
        .stats: 
            mov si, ax 
            call printf 
            jmp mloop 

; === Functions === 
printf:
    mov ah, 0eh 
    mov bh, 99h 

    .nextchar: 
        lodsb 
        or al, al 
        jz .return 
        int 10h 
        jmp .nextchar 

    .return: 
        ret 

getinput: 
    xor cl, cl 
     
    .loop: 
        mov ah, 00h 
        int 16h         ; Key press wait 
         
        cmp al, 08h     ; Backspace key 
        je .backspace   ; Handle 
         
        cmp al, 0dh     ; Enter key 
        je .enter       ; Handle 
         
        cmp cl, 3fh     ; Inputted 63 characters 
        je .loop        ; Only backspace and enter are accepted 

        mov ah, 0eh    
        int 10h 
         
        stosb 
        inc cl 
        jmp .loop 
         
    .backspace: 
        cmp cl, 0       ; Begin of the input 
        je .loop        ; Ignore 
         
        dec di 
        mov byte[di], 0 ; Delete character 
        dec cl          ; Decrementing the string counter 
         
        mov ah, 0eh 
        mov al, 08h 
        int 10h         ; Print the backspace 
         
        mov al, ' ' 
        int 10h         ; Blank character 
         
        mov al, 08h 
        int 10h         ; Backspace one more time 
         
        jmp .loop       ; Goes back to the loop 
         
    .enter: 
        mov al, 0       ; Null terminator 
        stosb 
         
        mov ah, 0eh 
         
        mov al, 0dh     ; Enter character 
        int 10h 
         
        mov al, 0ah     ; Newline character 
        int 10h 
         
        ret 
         
strcmp:
        pusha
    .loop: 
        mov al, [si]    ; Grab byte from SI 
        mov bl, [di]    ; Grab byte from DI 
        cmp al, bl      ; Compare if they are equal 
        jne .notequal   ; They aren't equal 
         
        cmp al, 0       ; Both bytes are null 
        je .done 
         
        inc di          ; Increment DI 
        inc si          ; Increment SI 
        jmp .loop       ; Start looping 
         
    .notequal: 
        popa
        clc             ; Clear the carry flag
        ret 
     
    .done: 
        popa
        stc             ; Set the carry flag
        ret 

welcome db "Welcome To The Second DOS Shell For Your DOS ", 0dh, 0ah, 0ah, 0 
prompt db " > ", 0 

; ==== Commands ========================== 
cmd_help db "help", 0 
msg_help db " Valid Commands: help - hello", 0dh, 0ah, 0 

cmd_hello db "hello", 0 
msg_hello db " Hi all!", 0dh, 0ah, 0 

cmd_stats db "stats", 0 

err_cmd db " The command you entered doesn't exist!", 0dh, 0ah, 0

buffer db 64 dup(0)
    

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
Post 25 Jun 2010, 02:23
View user's profile Send private message Reply with quote
bitshifter



Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
Also, you may want to consider using a lookup table of handlers.
This makes the code smaller and easier to add/remove functionality.
Code:
org 0100h

macro MESSAGE_MAP _command*,_handler* {
  local _marker
  db _marker - $ - 1     ; prefixed length
  db _command            ; command string
_marker:
  dw _handler            ; command handler
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ENTRY POINT
;;;;;;;;;;;;;;;;;;;;;;;;;;;

        mov     si,welcome
        call    print
mloop:
        mov     si,prompt
        call    print
        mov     di,buffer
  .getinput:
        mov     ah,00h
        int     16h
        cmp     al,08h          ; Backspace key
        je      .backspace
        cmp     al,0dh          ; Enter key
        je      .enter
        cmp     di,buffer.max   ; Is buffer full?
        je      .getinput       ; Only backspace and enter are accepted
        stosb
        mov     ah,0eh
        xor     bh,bh
        int     10h
        jmp     .getinput
  .backspace:
        cmp     di,buffer       ; Is buffer empty?
        je      .getinput
        dec     di
        mov     si,backspace
        call    print
        jmp     .getinput
  .enter:
        cmp     di,buffer       ; Is buffer empty?
        je      .getinput
        mov     byte[di],0      ; Null terminator
        mov     si,newline
        call    print
  .uppercase:
        dec     di
        cmp     di,buffer
        jb      .process
        cmp     byte[di],'a'
        jb      .uppercase
        cmp     byte[di],'z'
        ja      .uppercase
        sub     byte[di],32
        jmp     .uppercase
  .process:
        mov     si,commands-2
        xor     cx,cx
  .compare:
        add     si,cx
        add     si,2
        lodsb
        test    al,al
        jz      .unknown
        mov     di,buffer
        mov     cl,al
        repe    cmpsb
        jnz     .compare
        cmp     byte[di],0
        jne     .compare
        call    word[si]
        jmp     mloop
  .unknown:
        mov     si,err_cmd
        call    print
        jmp     mloop

;;;;;;;;;;;;;;;;;;;;;;;;;;;
; MESSAGE HANDLERS
;;;;;;;;;;;;;;;;;;;;;;;;

on_help:
        mov     si,msg_help
        call    print
        ret
             
on_hello:
        mov     si,msg_hello
        call    print
        ret

on_exit:
        int     20h
        jmp     $

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; PRINT ASCIIZ STRING FROM DS:SI
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

print:
        mov     ah,0eh
        mov     bh,0
        jmp     .load
  .putc:
        int     10h
  .load:
        lodsb 
        test    al,al
        jnz     .putc
        ret

;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; DATA AREA
;;;;;;;;;;;;;;;;;;;;;;;;;;;

welcome   db "Welcome To The Second DOS Shell For Your DOS ", 0dh, 0ah, 0ah, 0
prompt    db " > ", 0
msg_help  db " Valid Commands: help - hello - exit", 0dh, 0ah, 0
msg_hello db " Hi all!", 0dh, 0ah, 0
err_cmd   db " The command you entered doesn't exist!", 0dh, 0ah, 0
backspace db 8,32,8,0
newline   db 13,10,0
buffer    db 64 dup(0)
          .max = $-2

commands:
  MESSAGE_MAP 'HELP',  on_help
  MESSAGE_MAP 'HELLO', on_hello
  MESSAGE_MAP 'EXIT',  on_exit
  db 0 ; SENTINEL
    

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.


Last edited by bitshifter on 27 Jun 2010, 14:17; edited 1 time in total
Post 25 Jun 2010, 03:36
View user's profile Send private message Reply with quote
nathanpc



Joined: 23 Aug 2009
Posts: 40
Location: Brazil
nathanpc
The first code didn't work, but I'm trying the second one right now.
Post 25 Jun 2010, 09:57
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.