Search code examples
assemblyx86tasm

TASM outputs result of multiplication as ascii symbol, how to convert to integer


The purpose of this program made in TASM is to multiply two single digit numbers and write the result on the screen. What it does it actually multiply, but the result is shown as ascii symbol (i checked with this site http://chexed.com/ComputerTips/asciicodes.php and the results are correct). I cant get it to show result as integer, especially when result is two digit number.

.model small
.stack 

.data

msgA DB "Input 1st number: $"
msgB DB 10, 13, "Input 2nd number $"
msgC DB 10, 13, 10, 13, "Result: $"
msgD DB 10, 13, 10, 13, "Error, retry", 10, 13, 10, 13, "$"

.code

MOV AX, @DATA MOV DS, AX

jmp start num1 DB ? num2 DB ? result Dw ? start: mov ah, 09 mov dx, offset msgA int 21h mov ah, 01 int 21h mov num1, al mov ah, 09 mov dx, offset msgB int 21h mov ah, 01 int 21h mov num2, al mov al,num1 sub al,'0' mov bl,num2 sub bl,'0' mul bl add ax,'0' mov result, ax sub result, 48 mov ah, 09 mov dx, offset msgC int 21h mov ah, 02 mov dx, result int 21h mov ax, 4c00h int 21h end


Solution

  • You'll have to convert the integer result to a string, which you then can print using int 21h / ah = 9.

    A simple way of doing the conversion is as follows (I'll let you do the translation to TASM-syntax x86 assembly):

    ax = the value to convert
    si = &buffer[9];     // buffer is an array of at least 10 bytes
    buffer[9] = '$';     // DOS string terminator
    do {
        ax /= 10;
        si--;            // the buffer is filled from right to left
        *si = dl + '0';  // place the remainder + '0' in the buffer
    } while (ax != 0);
    dx = si;             // dx now points to the first character of the string