1

I need to read and print the numbers stored in two variables (I'm doing a menu program) but when I print them it prints the small letters from the ASCII table ( like a,b,c instead of 2,3,4). I know I'm supposed to substract 48 and multiply but I didn't find a great solution on how to implement that transformation in my program. I'm using Assembly x86, TASM.

This is the code from reading the inputs:

scriere:

mov dx, offset prompt1
   mov ah, 9
   int 21h

        input_x:

    mov bx, offset x ; point BX to the start of the array x
    mov cx, 10 ; set the counter to the number of elements in the array x

    input_loop1:
        mov ah, 01h ; function to read a single character
        int 21h ; call DOS function

        mov [bx], al ; store the character in the current array element
        inc bx ; move to the next array element

        loop input_loop1 ; repeat until counter reaches 0

    jmp input_y


        input_y:

mov dx, offset prompt2
mov ah, 9
int 21h

    mov bx, offset y ; point BX to the start of the array y
    mov cx, 10 ; set the counter to the number of elements in the array y

    input_loop2:
        mov ah, 01h ; function to read a single character
        int 21h ; call DOS function

        mov [bx], al ; store the character in the current array element
        inc bx ; move to the next array element

        loop input_loop2 ; repeat until counter reaches 0
    jmp bucla 

and the problem I think comes from my printing code:

afisare:

 mov dx, offset prompt3
   mov ah, 9
   int 21h

  print_x:
    mov bx, offset x ; point BX to the start of the array x
    mov cx, 10 ; set the counter to the number of elements in the array x

    print_loop1:
        mov dl, [bx] ; move the value of the current array element to DL
        add dl, 48 ; convert the number to its ASCII equivalent
       
        mov ah, 02h ; function to print a single character
        int 21h ; call DOS function

        inc bx ; move to the next array element
        loop print_loop1 ; repeat until counter reaches 0

    jmp print_y


  print_y:

 mov dx, offset prompt4
   mov ah, 9
   int 21h

    mov bx, offset y ; point BX to the start of the array y
    mov cx, 10 ; set the counter to the number of elements in the array y

    print_loop2:
        mov dl, [bx] ; move the value of the current array element to DL
        add dl, 48 ; convert the number to its ASCII equivalent

        mov ah, 02h ; function to print a single character
        int 21h ; call DOS function

        inc bx ; move to the next array element
        loop print_loop2 ; repeat until counter reaches 0

jmp bucla ; return to main loop
  • 1
    adding/subtracting `0'` to convert between an integer and the ASCII code for the digit that represents it only works for numbers from 0..9. See [Displaying numbers with DOS](https://stackoverflow.com/q/45904075) for printing larger numbers. – Peter Cordes Jan 15 '23 at 14:50

1 Answers1

0

How do I actually print the numbers I'm storing in the variables X and Y?

You have stored the ASCII representation of your numbers in the X and Y variables. Therefore printing the numbers does not require any conversion. Just remove the lines add dl, 48 from the afisare code.

But if you did want to convert the inputted numbers from text to integer, then read about it in Inputting multi-radix multi-digit signed numbers with DOS.

For displaying such integers read Displaying numbers with DOS.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • When I remove the "add dl, 48" lines it doesn't print anything anymore.. I think I need to do the operation when I substract 48 and multiply it with another number to convert the number I write to the ASCII representation for 1, 2, 3 etc. but I don't know how to implement it because It doesn't work for both. – purplemagictimes Jan 15 '23 at 16:19
  • @purplemagictimes If **nothing** gets printed anymore then you are doing something else to the variables X and Y between *scriere* and *afisare*. What *you* call "the operation when I substract 48 and multiply it with another number to convert the number" is described in the first link I gave. Read it and **learn from it**. You are not supposed to find a ready-made solution, but you can steal the ideas that I provided through the linked posts. And if *your* implementation then has problems, return to this forum with your technical question... – Sep Roland Jan 15 '23 at 16:57