Search code examples
assemblytasmemu8086dosbox

Assembly 8086 - Display number in console and not the ASCII representation


I got an assignment where I have to add 2 numbers and display the result. I'm having problems with displaying them in console. Instead of displaying the number, it displays the representation of ascii[ for numbers that adding are greater then 9. For example: 8 + 9 = 17 and it prints the letter A.

How can I display the number and not the ascii?

Bellow is the code I'm working with.

    .model large

DATAS SEGMENT
    aV dw ?
    bV dw ?
    cV dw ?     ;,'$'
DATAS ENDS

STACKSEGMENT SEGMENT
    startStack label word
    dw 3 dup(0)
STACKSEGMENT ENDS

CODES SEGMENT
ASSUME CS:CODES, SS:STACKSEGMENT, DS:DATAS
START:
    MOV AX, SEG DATAS
    MOV DS, AX
    
    MOV AX, SEG STACKSEGMENT
    MOV SS, AX
    MOV AX, OFFSET startStack  
    MOV SP, AX

    xor ax,ax
    mov ah, 01h
    int 21h
    xor ah,ah
    sub al, 48   ; ASCII to DECIMAL or substract al 30h
    mov aV, ax
    xor ax,ax
    
    ; NEW LINE
    MOV DL, 10      
    MOV ah, 02h
    INT 21h
    
    mov ah, 01h
    int 21h 
    xor ah,ah
    sub al, 48   ; ASCII to DECIMAL or substract al 30h
    mov bV, ax
    
    MOV DL, 10      
    MOV ah, 02h
    INT 21h
    
    mov ax, aV
    push ax
    
    mov ax, bV
    push ax
    
    mov ax, cV
    push ax
    
    CALL FAR PTR mathcalc
    
    MOV BX, DS:[cV] ; cV
    add BX, 30H
    MOV AH,2
    MOV DL,BH
    INT 21H
    MOV AH,2
    MOV DL,BL
    INT 21H
    sub BX, 30H

    CALL ENDPROGRAM
    

mathcalc PROC FAR
    push BP
    mov bp,sp
    
    
    mov al, SS:[bp+10]
    
    ;========
    add al,SS:[bp+8]
    ;========
    
    xor ah,ah
    mov ss:[bp+6], ax
    xor dx,dx
    MOV dx, ss:[bp+6]
    mov DS:[cV], dx
    
    pop bp
    retf 2                          
ENDP    


ENDPROGRAM:
    MOV AX,4c00h
    INT 21h
CODES ENDS

END START

PS: I'm using DOSBox with TASM compiler.

Thank you, Kind regards, Armand


Solution

  • You would have to do the following in (pseudo code):

    reserve data with b bytes
    n = 1234 # Number to be printed
    b = address of data + b - 1
    while n > 0
        d = n mod 10
        n = n / 10
        c = 48 + d # 48 is ascii code for digit zero
        store c at b
        decrement b