flat assembler
Message board for the users of flat assembler.
Index
> Main > Comparing Strings Confused Goto page 1, 2 Next |
Author |
|
revolution 25 Jun 2010, 01:20
You never restored that value of SI. It INCrements and then permanently points to the trailing zero byte,
|
|||
25 Jun 2010, 01:20 |
|
nathanpc 25 Jun 2010, 01:23
You're saying that I should clean the SI register after the jmp .loop?
|
|||
25 Jun 2010, 01:23 |
|
revolution 25 Jun 2010, 01:26
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. |
|||
25 Jun 2010, 01:26 |
|
nathanpc 25 Jun 2010, 01:29
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.
|
|||
25 Jun 2010, 01:29 |
|
revolution 25 Jun 2010, 01:45
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 |
|||
25 Jun 2010, 01:45 |
|
nathanpc 25 Jun 2010, 01:47
I've done as you said, but it still prints me the same(the help command output).
|
|||
25 Jun 2010, 01:47 |
|
bitshifter 25 Jun 2010, 01:48
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 |
|||
25 Jun 2010, 01:48 |
|
revolution 25 Jun 2010, 01:58
nathanpc wrote: I've done as you said, but it still prints me the same(the help command output). |
|||
25 Jun 2010, 01:58 |
|
nathanpc 25 Jun 2010, 02:01
Nothing changes, I'm still getting the wrong output.
|
|||
25 Jun 2010, 02:01 |
|
bitshifter 25 Jun 2010, 02:01
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. |
|||
25 Jun 2010, 02:01 |
|
nathanpc 25 Jun 2010, 02:04
revolution wrote:
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. _________________ Developer: C++, Java, C/C++, Java ME, Java EE, Pascal, Assembly(8086, 6502, z80, ARM and MIPS), Delphi, Visual Basic, Pascal, Ruby and Perl |
|||
25 Jun 2010, 02:04 |
|
bitshifter 25 Jun 2010, 02:05
Look closer at my last post...
|
|||
25 Jun 2010, 02:05 |
|
nathanpc 25 Jun 2010, 02:07
bitshifter wrote: I see you getinput into DI but never is pointed at any buffer... 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 |
|||
25 Jun 2010, 02:07 |
|
bitshifter 25 Jun 2010, 02:09
It was meant to work my previous post...
http://board.flatassembler.net/topic.php?p=116049#116049 |
|||
25 Jun 2010, 02:09 |
|
nathanpc 25 Jun 2010, 02:15
bitshifter wrote: It was meant to work my previous post... Sadly not... _________________ Developer: C++, Java, C/C++, Java ME, Java EE, Pascal, Assembly(8086, 6502, z80, ARM and MIPS), Delphi, Visual Basic, Pascal, Ruby and Perl |
|||
25 Jun 2010, 02:15 |
|
revolution 25 Jun 2010, 02:17
nathanpc: Please show your latest code so we can all know what you are working with now.
|
|||
25 Jun 2010, 02:17 |
|
bitshifter 25 Jun 2010, 02:23
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. |
|||
25 Jun 2010, 02:23 |
|
bitshifter 25 Jun 2010, 03:36
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 |
|||
25 Jun 2010, 03:36 |
|
nathanpc 25 Jun 2010, 09:57
The first code didn't work, but I'm trying the second one right now.
|
|||
25 Jun 2010, 09:57 |
|
Goto page 1, 2 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.