Search code examples
assemblyx86tasm

assembly language is there a way to shorten my code


my program is to count all vowels in user input characters my codes is long because of part when comparing the user input if vowel or not. is there a way to shorten my codes? i am new to assembly language so i use basic mnemonic

title sample.prog
cstack segment para stack 'stack'
dw 200h
cstack ends

cdata segment para 'data'
msg1 db 'ENTER 9 CHARACTER: $',10,13
msg2 db 10,13,'NUMBER OF VOWELS: $'
cdata ends

ccode segment para 'code'
assume cs:ccode,ds:cdata,ss:cstack
main:
 mov ax,cdata
 mov ds,ax

 mov ah,09h
 lea dx,msg1
 int 21h

 mov cl,0
 mov bl,30h

input:
 mov ah,01
 int 21h
 inc cl

 cmp al,61h
 je incre
 cmp al,65h
 je incre
 cmp al,69h
 je incre
 cmp al,6fh
 je incre
 cmp al,75h
 je incre
 cmp al,'A'
 je incre
 cmp al,'E'
 je incre
 cmp al,'I'
 je incre
 cmp al,'O'
 je incre
 cmp al,'U'
 je incre

 cmp cl,9
 je ed
 jmp input

incre:
 inc bl

 cmp cl,9
 je ed
 jmp input 

ed:
  mov ah, 09h
  lea dx,msg2
  int 21h


  mov ah,02h
  mov dl,bl
  int 21h

  mov ah, 4ch
  int 21h

ccode ends
end main

Solution

  • Shave off some code by first capitalizing

    and al,1101_1111b
    cmp al,'A'
    jb skip
    cmp al,'Z'
    ja skip
    cmp al,'A'
    je incre
    cmp al,'E'
    je incre
    cmp al,'I'
    je incre
    cmp al,'O'
    je incre
    cmp al,'U'
    je incre
    skip:
    

    Or use a list (in de data section)

    vowels db 'AEIOUaeiou'
    
    ...
    
    mov cx,10
    mov si,vowels
    again:
    cmp al,[si]
    je incre
    inc si
    loop again