assembly - Loop structure skips some letters? -
i have here code supposed to:
- take input
- compare each lower-case letter of alphabet
"a"-"z"
/97d-122d
- compare if current character equal input: if true, proceed conversion , echo
- increment current character
- compare if counter equal limit, while not continue testing characters
- if not found
a
-z
, display invalid input message , go input prompt
here's code:
.model small .stack 100h .data prompt db 10, 13, 10, 13, "enter character: $" upper db 10, 13, "upper case: $" invalid db 10, 13, "invalid character!$" .code mov ax, @data mov ds, ax mov cx, 26 input: mov ah, 09h lea dx, prompt int 21h mov ah, 01h int 21h cmp al, ' ' je exit mov bl, 0 mov bh, 97d ; bh == 'a' tst: add bh, bl cmp al, bh ; cmp (input,bl) je continue ; if equal go continue add bl, 1 cmp bl, 122 ; cmp (bh,'z') jne tst ; if not equal loop ; invalid if not found 'a' - 'z' mov ah, 09h lea dx, invalid int 21h jmp input continue: sub al, 32 ; subtract 32 make upper case mov ah, 09h lea dx, upper int 21h mov ah, 02h mov dl, al int 21h jmp input exit: mov ah, 4ch int 21h end
but there letters considers invalid: c
, f
, h
, i
, n
, r
, s
, w
, x
, y
, z
what causes these behaviours?
info: use tasm.exe assembler, tlink.exe linker , dos-box emulator
to fix problem should change this:
mov bl, 97 tst: cmp al, bl ; cmp (input,bl) je continue ; if equal go continue add bl, 1 cmp bl, 123 ; cmp (bh,'z'+1 include z well) jne tst ; if not equal loop
however, code rather ineffecient. should take @ instructions jg
, jl
. loop not needed @ because can cmparison.
pseudocode:
if(ah < 'a') jmp invalid if(ah > 'z') jmp invalid printupper jmp continue
also, if value intended character makes code more readbable acutally use character, instead of it's ascii value. i.e. move bl, 'a'
.
btw: exit on space not according spec. :)
Comments
Post a Comment