0

I'm trying to figure out how to print the char inputted by the user in assembly language. (Let's pretend that I already have the lowercase value and I want to print it on uppercase.)

lowercase  DB  "Input a letter: $"
uppercase  DB  "The uppercase equivalent of letter <the lowercase letter the user inputted> is: $"

In contrast, if it's written in c++, it would be:

cout << "The uppercase equivalent of letter" << lowercase << "is: " << endl"

How should I do it?

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
hatzuramen
  • 33
  • 3
  • 2
    Since you don't have `printf` in emu8086, you'll have to either store a `char` into a string and print the whole string, or use multiple separate print calls like you're doing in C++. (`operator<<` on a stream returns the stream, it's just multiple separate member-function calls on the `std::cout` object, using overloaded operator syntax.) – Peter Cordes Oct 22 '21 at 11:03

1 Answers1

3

The preferred solution would be to insert "the lowercase letter the user inputted" in the output string. I see you already $-terminated the string so it can get printed all at once using the DOS.PrintString function 09h.

  • Define the uppercase string with a placeholding question mark (or any other character that you like):

      uppercase  db "The uppercase equivalent of letter ? is: $"
                                                        ^
                                              placeholder
    
  • For ease of addressing the placeholder, you can write the string on 2 lines, so you can put a label in front of the placeholder:

      uppercase  db "The uppercase equivalent of letter "
      uppercase_ db "? is: $"
                     ^
           placeholder
    
  • Replace the placeholder by the lowercase letter from user input:

      mov     [uppercase_], al      ; AL is lowercase
    
  • Print the string with DOS.PrintString function 09h:

      mov     dx, offset uppercase
      mov     ah, 09h
      int     21h
    

What about displaying the capitalized letter?

No need to output it separately. The simplest solution is to also include it in the output string.

  • Just add a second placeholder right before the terminating $ character. Instead of splitting off a third line we can easily establish an offset by counting characters (The 2 placeholders are 6 characters apart):

      uppercase  db "The uppercase equivalent of letter "
      uppercase_ db "? is: ?$"
                     ^     ^
           placeholder1    placeholder2
    
  • Replace both placeholders. At the ellipsis ... you must convert from lowercase to uppercase!

      mov     [uppercase_], al      ; AL is lowercase
      ...
      mov     [uppercase_ + 6], al  ; AL is uppercase
    
  • Print the string with DOS.PrintString function 09h.

      mov     dx, offset uppercase
      mov     ah, 09h
      int     21h
    

For some background information about displaying text read this Q/A Displaying characters with DOS or BIOS. It has examples of printing strings of text character per character.

Bringing it more in line with what cout does

The above solution no longer holds true if the datum that needs to overwrite the placeholder is of varying lengths. Using a 5-character placeholder, consider:

db 'Your donation of       € is much appreciated.$'

If the donated amount has 5 digits this will print out just fine:

Your donation of 20000 € is much appreciated.

but if the donated amount is small, it's no longer that nice to look out:

Your donation of     5 € is much appreciated.

In this scenario we'll have to use the different approach of outputting the accompanying texts separately from the number that goes in between.

ThankYou1 db 'Your donation of $'
ThankYou2 db ' € is much appreciated.$'

...

mov     dx, offset ThankYou1
mov     ah, 09h
int     21h

mov     ax, [amount]
call    DisplayNumber

mov     dx, offset ThankYou2
mov     ah, 09h
int     21h

For ideas about how to write the DisplayNumber routine, see Displaying numbers with DOS.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • It's important to note that the method used to print text is entirely dependent on the hardware. What works for DOS isn't guaranteed to work on other computers. If you're programming on the WonderSwan (it's like a Game Boy except it runs on x86-16) for example you won't be able to use the ```int 21h``` method. Luckily if you're sticking to home computers the vast majority of them have firmware calls like ```int 21h``` to help you out. – puppydrum64 Nov 02 '21 at 13:41