Asking about how to made a number in AX display in DOS screen.

Started by dungdoan, February 09, 2009, 03:59:43 PM

Previous topic - Next topic

dungdoan

I use masm32, and I did install Link56 package to translate 16bit in Dos. My program is below:

.MODEL  SMALL
.STACK 100h
.DATA
    A       DW      2
    B       DW      5
    sum     DW      ?
.CODE
        mov AX,@DATA
        mov DS,AX

        mov AX,A
        add AX,B
        mov sum,AX
             
        mov AH,4Ch
        int 21h
END

When I made progressing of assembling in DOS, it does not have any errors. It has a warning:
LINK: warning L4038: program has no starting address. But it is not matter. Then I run this program. It does not display sum's number in AX that I am expecting. Can you help me explain why it can not show the result in AX reg?
Thank you :)

MichaelW

To display a value you must add code to convert the value to a ASCII character code or sequence of ASCII character codes, and then call one of the display functions.

I'll assume you wish to display the value in decimal. For values that require only a single decimal digit (the values 0 to 9) the conversion is simple. Just add the ASCII character code for the decimal digit '0' (which is 48d or 30h) to the value. This works because the ASCII character codes were assigned in order by the value of the digits, 30h='0', 31h='1',...,39h='9'. You can then display the result as a digit, using the Print Character function. For example, assuming the value is in AL:

add al, 30h
mov dl, al  ; character code in DL
mov ah, 5   ; Print Character function
int 21h


For values that require more than one digit the process is a little more complex. One easy to understand method is to extract the digits in reverse order by repeatedly dividing the value by 10, continuing until the quotient is zero. For each division the remainder is the value of the extracted digit. For example, starting with the value 123:

123 / 10 = 12 remainder 3
12 / 10 = 1 remainder 2
1 / 10 = 0 remainder 1

To display the digits in the normal order, they must be reversed. The stack provides a convenient way to do this. You simply push the remainder on the stack after each division. Since the stack is a Last In First Out structure, once the divisions are complete the digits can be popped from the stack in the correct order. To know how many digits to pop, you must keep a count of the number of digits pushed. Once you have a digit value, it can be converted to an ASCII character code and displayed as described above. I'll leave the coding to you.
eschew obfuscation

dedndave


First, the assembly error:

.MODEL  SMALL
.STACK 100h
.DATA
    A       DW      2
    B       DW      5
    sum     DW      ?
.CODE

start:
        mov AX,@DATA
        mov DS,AX

        mov AX,A
        add AX,B
        mov sum,AX
             
        mov AH,4Ch
        int 21h
END start

you reference the entry point in the END directive

next, it will not display the "sum" value unless you tell it to do so
you could run this program under debug to observe the register change
or display it like Michael suggested, depending on the size of the result