public
Created

Getting And Comparing Input

  • Download Gist
get_comp.asm
Assembly
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
; How to call it from the program
 
main:
call getinput
mov si, buffer
cmp byte[si], 0 ; Blank line
je main ; Ignore it
mov si, buffer
mov di, cmd_help ; Input compare: help
call strcmp
jc .help
mov si, buffer
mov di, cmd_hello ; Input compare: hello
call strcmp
jc .hello
mov si, err_cmd ; input entered is invalid
call printf
jmp main ; Infinity loop
.hello:
mov si, msg_hello
call printf
jmp main
.help:
mov si, msg_help
call printf
jmp main
 
 
; The function
 
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
 
printf:
lodsb
mov ah, 0eh
mov bh, 99h
 
.nextchar:
lodsb
or al, al
jz .return
int 10h
jmp .nextchar
 
.return:
ret
cmd_help db "help", 0
msg_help db " Get help on our forums! :D", 0dh, 0ah, 0
cmd_hello db "hello", 0
msg_hello db " Hi all from Dream.In.Code!", 0dh, 0ah, 0
err_msg db "Input don't have any to compare.", 0dh, 0ah, 0

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.