0

I'm making a program that converts multiple lowercase letter to its uppercase equivalent.

For example, the output I want would be:

Input a letter: asddf

The uppercase equivalent of letter asddf is ASDDF

But it only gets and print the last character like this:

Input a letter: asddf

The uppercase equivalent of letter fffff is FFFFF

How do i fix this? I am aware that my code is incorrect, but I don't know which part where I went wrong because I'm still new to the language. Thanks!

Here's my code:

  org 100h  

     LEA DX, lowercase           
     MOV AH, 9
     INT 21H
     
     MOV CX, 5
     
     MOV AH, 1                   
     
     @LOOP:
     INT 21H
     
     LOOP @LOOP

     MOV BL, AL                  

     LEA DX, uppercase          
     MOV AH, 9
     INT 21H        
     
     MOV DL, BH
     MOV AH, 02H   
     INT 21h       
     
     MOV CX, 5
      
     MOV DL, BL
     MOV AH, 02H
     
     @LOOP2:
     INT 21h
            
     LOOP @LOOP2 
            
     LEA DX, uppercase_          
     MOV AH, 9
     INT 21H
     
     SUB BL, 20H                 
             
     MOV CX, 5
             
     MOV AH, 2                    
     MOV DL, BL
     
     @LOOP3:
     INT 21H
     
     LOOP @LOOP3   
     

     MOV AH, 4CH                
     INT 21H    
     
   ret
   
   lowercase  db  "Input a letter: $"
   uppercase  db  0AH, 0DH, "The uppercase equivalent of letter$"
   uppercase_ db " is $"  
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
hatzuramen
  • 33
  • 3
  • 1
    *at once*? Like in parallel with multiple threads? If you want to [use SIMD to do 16 or 32 characters in parallel](https://stackoverflow.com/questions/735204/convert-a-string-in-c-to-upper-case/37151084#37151084), it's more effective to process multiple characters of the same string at once, then move on to the next string. If you just mean you have multiple strings and need to get them all done eventually on 8086 (which is single-threaded and doesn't have SIMD), loop over them. Anyway, seems your question title just doesn't match your description of the problem you're debugging. – Peter Cordes Nov 03 '21 at 10:43

1 Answers1

0

I am aware that my code is incorrect, but I don't know which part where I went wrong

  1. The input loop should store the characters somewhere
MOV CX, 5
MOV AH, 1                   
@LOOP:
INT 21H
                <<<<
LOOP @LOOP
  1. The first output loop should retrieve and use unmodified the characters that got stored
MOV CX, 5
MOV DL, BL
MOV AH, 02H
@LOOP2:
INT 21h         <<<<

LOOP @LOOP2
  1. The second output loop should retrieve and capitalize the characters that got stored
MOV CX, 5
MOV DL, BL
MOV AH, 02H
@LOOP3:
INT 21h         <<<<

LOOP @LOOP3

General solution

Add a line to your code that reads buffer db 5 dup(0). Then change all your loops like below:

  • This is a loop that stores:
  xor  bx, bx
@LOOP:
  
  ...
  
  mov  [buffer + bx], al      ; Store lowercase
  inc  bx
  cmp  bx, 5
  jb   @LOOP
  • This is a loop that retrieves
  xor  bx, bx
@LOOP:
  mov  dl, [buffer + bx]      ; Retrieve lowercase
  
  ...
  
  inc  bx
  cmp  bx, 5
  jb   @LOOP

Special solution

Since you've hardcoded the number of characters to be 5, you can use the same technique that I showed you in my previous answer. It will allow you to print everything at once:

uppercase  db  0AH, 0DH, "The uppercase equivalent of letter$"
uppercase_ db  "????? is ?????$"
;               ^        ^
;               | 9 characters apart
...

  xor  bx, bx
@LOOP:
  mov  ah, 01h                    ; DOS.GetCharacter
  int  21h                        ; -> AL
  mov  [uppercase_ + bx], al      ; Store lowercase
  sub  al, 32
  mov  [uppercase_ + bx + 9], al  ; Store uppercase
  inc  bx
  cmp  bx, 5
  jb   @LOOP

...  Print with DOS.PrintString 09h
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • Why use `sub` here? You can force upper-case without breaking already-uppercase characters with `and al, ~32` to clear the lower-case bit without changing numbers that already had it cleared. [What is the idea behind ^= 32, that converts lowercase letters to upper and vice versa?](https://stackoverflow.com/a/54585515) – Peter Cordes Nov 04 '21 at 00:30
  • @PeterCordes I think the real problem the OP has, is not so much the capitalization, but rather the use of memory for storage. Also, since their code is assuming all lowercase input and is not checking for numbers or punctuation marks, using `and` or `xor` could be confusing at this stage. But your link will be fine for people that are interested in the capitalization itself. – Sep Roland Nov 04 '21 at 00:45