1

Well, say I have a program that only accepts letters, regardless of its case (uppercase or lowercase), converts and displays its opposite case (eg. A to a, a to A). The program then displays the next letters through ascending and descending order BUT ONLY WITHIN THE ALPHABET.

For example, if i input 'l' and choose ascending order, it will output its corresponding uppercase equivalent as well as the next letters after it only until Z.

Input: l
Display
[a]ascending
[d]descending
Choice:a
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Z

likewise if I input E and choose descending order, it will output its corresponding lowercase equivalent as well as the next letters before it only until a.

Input:E
Display
[a]ascending
[d]descending
Choice:d
e
d
c
b
a

Here is a snippet of code I have accomplished so far, I have managed to convert the input letters to its uppercase and lowercase equivalents and was also able to display them vertically. My only problem is how to limit the output only to the alphabet.

    startOver:
    call cls
    mov ah,9
    lea dx,string8      ;Display the string "Input:"
    int 21h

    mov ah,1        ;input starting letter
    int 21h
    mov bl,al

    call nlcr               ;nlcr is a procedure for new line and carriage return

    mov ah,9
    lea dx,string9      ;Display the string "Display"
    int 21h

    call nlcr

    mov ah,9
    lea dx,string10     ;Display the string "[a]ascending"
    int 21h

    call nlcr

    mov ah,9
    lea dx,string11     ;Display the string "[d]descending"
    int 21h

    call nlcr       ;nlcr is a procedure for newline and carriage return

    mov ah,9        ;Display the string "Choice:"
    lea dx,string12     
    int 21h

    mov ah,1        ;input display choice if it's ascending or descending
    int 21h         
    mov cl,al
    call nlcr

    cmp cl,'a'      ;validate the display choice
    je ascending
    cmp cl,'d'
    je descending

    checkd:
    cmp cl,'d'
    je descending
    cmp cl,'d'

    mov ah,9
    lea dx,string14
    int 21h
    jne startOver

    ascending:      ;display in ascending order
    xor bl,20h      ;converts letter to uppercase or lowercase
    mov ah,2
    mov dl,bl
    int 21h

    mov cx,15          ;display the letters vertically, I put a default of 15 for I am not sure how to limit it to the alphabet only          
    asc:
    mov bl,dl   
    call nlcr
    mov dl,bl
    inc dl
    int 21h
    loop asc
    jmp exit

    descending:     ;display in descending order
    xor bl,20h      ;converts letter to uppercase or lowercase
    mov ah,2
    mov dl,bl
    int 21h

    mov cx,15   
    desc:
    mov bl,dl   
    call nlcr
    mov dl,bl
    dec dl
    int 21h
    loop desc
    jmp exit

    exit:
    ;call cls
    mov ah,4ch
    int 21h
    main endp 

Thanks for the replies!

2 Answers 2

1

My only problem is how to limit the output only to the alphabet.

Just like any other language: per character you either do a range check (one or more) or a look up table (probably built using a range check or few). ASCII you only have to deal with one byte so 256 entries in a table, 256 bytes if you do it a byte per entry so that is not a large table at all to deal with, easy to generate and easy to use.

If using ascii (knowing that upper and lower case are separate), you loop until it fails the alphabet test, then back it up 26 or 27, then continue until you hit the start character.

You can play some bit games as well, I believe upper and lower differ by one bit, if the msbit is 0 then force that bit and do a single range check to determine if it is within the alphabet or not. Not as fast as a look up table but faster than two or more range checks.

0

I'd change it to something like this:

  asc:
  mov bl,dl   
  call nlcr
  mov dl,bl
  inc dl
  int 21h
  and bl,0DFh  ; make sure bl is upper case 
  cmp bl,'Z'
  jb asc       ; loop until the next character would be 'z'+1 or 'Z'+1
  jmp exit

And similarily for descending order; execpt then you'd compare against 'A' and use ja instead of jb.

1
  • wow! those couple of lines made it work like a charm! Thanks!
    – AJ Lupac
    Aug 28, 2013 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.