Search code examples
assemblyuser-inputnasmx86-16uppercase

how to take user input and convert to uppercase in assembly


im trying to take a user input, lowercase and convert into UPPERCASE in assembly. I compiled this in NASM but it gives an error.

IDEAL

MODEL small;
STACK 256 

DATASEG
prompt db 13,10,"PLEASE ENTER A CHARACTER IN THE ALPHABET.$"

CODESEG

Start:
  mov ax,@DATA
  mov dx,ax

Mainloop:
  mov ah,9
  mov dx, offset prompt
  int 21h

  mov ah,0
  int 16h

  mov ah,02h
  mov dl,"ah+32"
  int 21h
  jmp Mainloop
  
 END Start

Solution

  • Start:
     mov ax,@DATA
     mov dx,ax
    

    The third line has an error. You want to initialize the DS segment register, not the general purpose register DX.

    im trying to take a user input, lowercase and convert into UPPERCASE

    Your program does not make sure that the input is indeed a lowercase [a-z] but I think that's fine for now.

    mov dl,"ah+32"

    As Peter commented, this instruction will not add 32 to the AH register!
    And why would you want to?

    • The (lowercase) character is in the AL register
    • The conversion to uppercase would need you to subtract 32

    The ASCII codes for [a-z] are [97-122]
    The ASCII codes for [A-Z] are [65-90]

    The real solution is to mask off the 6th bit from AL to get rid of that 32. In doing so, if ever the input happens to be an uppercase character already, your program will still produce the desired uppercase output. Consider that a bonus.

    Next code will produce a .COM program. That's an easy program format where all the segment registers are equal to each other (CS==DS==ES==SS). The ORG 256 directive is mandatory.

    ORG 256
    
    Mainloop:
      mov dx, prompt
      mov ah, 09h       ; DOS.PrintString
      int 21h
    
      mov ah, 00h       ; BIOS.GetKey
      int 16h           ; -> AX
    
      and al, 11011111b ; UCase
      mov dl, al
      mov ah, 02h       ; DOS.PrintChar
      int 21h
      jmp Mainloop
    
    prompt db 13,10,"PLEASE ENTER A CHARACTER IN THE ALPHABET.$"