assemblyx86-16emu8086

How can I limit input number?


I want to limit the number to be lower than or equal to 65535 when I input number. When I calculate sum of two numbers in asssembly, emu8086 can not calculate sum of number higher than 65535, so I want to limit input to lower than or equal to 65535.

.model small
.stack 100h
.data
 tb1 db 10,13,"Input first number: $"
 tb2 db 10,13,"Input second number: $"
 tb3 db 10,13,"Sum of two number:$"
 tb4 db 10,13,"Invalid number. Please input again: $"
 
 x dw ?
 y dw ?
 z dw ?
 t dw ?
 k dw ?
 
.code
main proc
    mov ax,@data
    mov ds,ax
    
    ;input first number
    mov ah,9
    lea dx,tb1
    int 21h
    
    call input_number
    mov ax,x
    mov z,ax
    
    ;input second number
    mov ah,9
    lea dx,tb2
    int 21h
    
    call input_number
    mov ax,x
    mov t,ax
    
    ;in tong 2 so
    mov ah,9
    lea dx,tb3
    int 21h
    
    mov ax,x
    add ax,t
    jc overflow_number
    
    overflow_number:
        mov ax,z
        xor cx,cx
        xor dx,dx
        mov bx,10
        div bx
        
        mov x,ax
        mov k,dx
        
        mov ax,t
        xor cx,cx
        xor dx,dx
        mov bx,10
        div bx
        
        add x,ax ;cong 2 so sau khi chia 10
        add dx,k ;cong so du
        mov k,dx
        cmp k,9
        jl show
        cmp k,9
        jg reminder
        
    reminder:
        mov ax,k
        xor cx,cx
        xor dx,dx
        mov bx,10
        div bx
        add ax,dx
        inc x
        mov k,ax
        dec k
    
    show:
        mov ax,x
        mov x,ax
        call print
        mov ax,k
        mov x,ax
        call print    
    
    mov ah,4ch
    int 21h

main endp
;ham con

;input function
input_number proc
    mov x,0
    mov y,0
    mov bx,10
    mov cx,5
    
        input:
            mov ah,1
            int 21h
     
            cmp al,13
            je sumbit
            
            cmp al,30h
            jb invalid_number
            cmp al,39h
            ja invalid_number
            
            sub al,30h
            xor ah,ah
            mov y,ax
            mov ax,x
            mul bx
            add ax,y
            mov x,ax
            loop input    
            
            
        sumbit:
            ret
            
        invalid_number:
            mov ah,9
            lea dx,tb4
            int 21h
            jmp main proc


input_number endp

;print function

print proc
    mov ax,x
    mov bx,10
    mov cx,0
    
        divide:
            mov dx,0
            div bx 
            push dx 
            inc cx 
            cmp ax,0 
            je hienthi 
            jmp divide 
                      
        hienthi:      
            pop dx
            add dl,30h
            mov ah,2
            int 21h
            dec cx
            cmp cx,0
            jne hienthi
            ret
    
    
        
print endp


end main

Solution

  • mov ax,x
    add ax,t
    jc overflow_number
    
    overflow_number:
    

    This is a problematic part of you program!


    Confining the input to the range [0,65535] is easy:

    input_number proc
        mov  x, 0
        mov  cx, 5
      input:
        mov  ah, 01h
        int  21h           ; -> AL
        cmp  al, 13
        je   sumbit
        sub  al, 30h
        cmp  al, 9
        ja   invalid_number ; Not a decimal digit
        xor  ah, ah
        mov  bx, ax         ; -> BX is NewDigit
        mov  ax, 10
        mul  x
        jc   invalid_number ; Out of range
        add  ax, bx
        jc   invalid_number ; Out of range
        mov  x, ax
        loop input    
      sumbit:
        ret
      invalid_number:
        mov  dx, OFFSET tb4
        mov  ah, 09h
        int  21h
        pop  ax             ; Forget `call input_number`
        jmp  main           ; (*)
    input_number endp
    

    (*) Does emu8086 accept jmp main proc ?