pls show me how to add two decimal numbers and display the total

I need help in adding two decimal numbers and displaying the total. for now, i just get a sample program that can add numbers range 0 to 4 and view the total, but for numbers bigger than 5, the total is diplayed as ASCII characters. how to display the actual value in decimal instead of ASCII characters? i'm using Microsoft (R) Macro Assembler Version 6.15.8803. Thank You.

MODEL SMALL
.STACK 64
.DATA

.CODE

main proc far
mov ax, @Data ; Define current data segment
mov ds, ax

mov ah, 01 ; read digit as ascii char from keyboard into al
int 21h

sub al, 30h ; convert to number

add al, al

mov DL, AL
add DL, 30h ; conver number to ascii char
mov ah, 02h
int 21h


mov ax, 4c00H
int 21H

main endp
END main


Comments

  • Dividing by 10 is the key to printing a value in decimal.
    Here's a Nasm includeable procedure, you can alter it for Masm with
    WD PROC
    ENDP
    stuff, the rest of the code looks like it should work in Masm.

    [code]
    ; ------------------ WD = WRITDIGT for testing programs ------------
    ; Writes a Decimal digit at the cursor & a CRLF after
    ; Call with, AX=digit to print
    ; Returns, nothing
    ;
    align 16
    WD:
    PUSH AX
    PUSH BX
    PUSH CX
    PUSH DX
    XOR CX,CX ; clear count register for push count
    MOV BX,10
    NZ: XOR DX,DX ; clear the register to push
    DIV BX ; divide DX:AX by BX=10
    ADD DX,48 ; make remainder number a printable ASCII value
    PUSH DX ; put least siginificant number (remainder) on stack
    OR AX,AX ; check to see if AX=number is divided to 0 yet
    LOOPNE NZ ; get another digit? count the pushes
    MOV AH,2 ; function 2 for interupt 21h write digit
    NEG CX ; two's compliment, reverse CX
    WRID: POP DX ; get digit to print, last pushed=most significant
    INT 21h ; print ascii interupt 21h ( function 2 in AH )
    LOOP WRID ; deincrement CX, write another, if CX=0 we done
    MOV AH,2
    MOV DL,13 ; CR carriage return
    INT 21h
    MOV DL,10 ; LF line feed
    INT 21h
    POP DX
    POP CX
    POP BX
    POP AX
    RET
    [/code]

  • Here is a better way without ints

    ;shows an integer value in a register with color
    ;to put a space when using more than two of this macro use add di,2
    ;before using showintx functions don't forget to initialize di and text video
    ; shows an integer at the current cursor position and
    ; moves cursor to the end of number
    ; eax = integer value
    ; ecx = base
    ; bp = color
    ; edi = screen start position to show
    SHOWINT PROC
    PUSH EAX ; save some important regs
    PUSH EBX
    PUSH ECX
    PUSH EDX

    MOV EAX, EAX ; value to print in eax
    ; xor ecx, ecx ; make sure hi word of ecx is 0
    MOV ECX, ECX ; base (divisor) in cx

    CALL SHOWINTPRINT32_1 ; start the recursion
    JMP SHOWINTPRINT32_3 ; jmp to end

    SHOWINTPRINT32_1:
    XOR EDX, EDX ; clear dividend
    DIV ECX ; divide out to find digit
    PUSH EDX ; save it
    CMP EAX, 0 ; are we done yet?
    JE SHOWINTPRINT32_2 ; jmp if so
    CALL SHOWINTPRINT32_1 ; otherwise call ourselves again

    SHOWINTPRINT32_2:
    ; showx color ; print whatever's in dl and return

    POP EDX ; get the last dividend
    ADD DL, 30H ; convert to number 0-9
    CMP DL, 39H ; is it 9?
    JLE SHOWINTPRINTX_1 ; jmp if greater
    ADD DL, 7 ; convert to character a-f

    SHOWINTPRINTX_1:
    MOV AX,BP
    SHL AX,8
    CLD
    MOV AL,DL
    MOV ES:[DI],AX
    ADD DI,2
    ; move cursor
    MOV AX,DI
    MOV DX,0
    MOV BX,160 ;TOTALSCREENWIDTH
    DIV BX
    MOV BX,DX
    MOV DH,AL
    SHR BX,1
    MOV DL,BL
    MOV AH,02H
    MOV BH,0
    ; int 10h
    RETN ; undo recursion

    SHOWINTPRINT32_3:
    POP EDX ; restore what we saved
    POP ECX
    POP EBX
    POP EAX
    RETN
    SHOWINT ENDP


    usage:
    MOV AX,0003H ; text video
    INT 10H


    mov eax,1123 ; integer value
    mov ecx,10 ; base
    mov bp,31 ; color
    mov edi,320 ; screen start position to show
    call showint

    for more take a look at geocities.com/macrotechonline


    : Dividing by 10 is the key to printing a value in decimal.
    : Here's a Nasm includeable procedure, you can alter it for Masm with
    : WD PROC
    : ENDP
    : stuff, the rest of the code looks like it should work in Masm.
    :
    : [code]
    : ; ------------------ WD = WRITDIGT for testing programs ------------
    : ; Writes a Decimal digit at the cursor & a CRLF after
    : ; Call with, AX=digit to print
    : ; Returns, nothing
    : ;
    : align 16
    : WD:
    : PUSH AX
    : PUSH BX
    : PUSH CX
    : PUSH DX
    : XOR CX,CX ; clear count register for push count
    : MOV BX,10
    : NZ: XOR DX,DX ; clear the register to push
    : DIV BX ; divide DX:AX by BX=10
    : ADD DX,48 ; make remainder number a printable ASCII value
    : PUSH DX ; put least siginificant number (remainder) on stack
    : OR AX,AX ; check to see if AX=number is divided to 0 yet
    : LOOPNE NZ ; get another digit? count the pushes
    : MOV AH,2 ; function 2 for interupt 21h write digit
    : NEG CX ; two's compliment, reverse CX
    : WRID: POP DX ; get digit to print, last pushed=most significant
    : INT 21h ; print ascii interupt 21h ( function 2 in AH )
    : LOOP WRID ; deincrement CX, write another, if CX=0 we done
    : MOV AH,2
    : MOV DL,13 ; CR carriage return
    : INT 21h
    : MOV DL,10 ; LF line feed
    : INT 21h
    : POP DX
    : POP CX
    : POP BX
    : POP AX
    : RET
    : [/code]
    :
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories