s

8086 program to find the factor of a single digit number edit button Edit

author
Murugan Andezuthu Dharmaratnam | calendar 01 April 2021 | 2383

Here is an 8086 assembly language program to find the factor of a single-digit number. Full explanation is given in the code below.

Sample Code

.model tiny
.code
org 100h


main proc near 

loop_readnumber:
    mov ah,09h
    mov dx,offset messageenternumber
    int 21h  
     
    
    mov ah,01h  ; dos function to read a character with echo from keyboard
                ; result (character entered) is stored in al
    int 21h     ; dos interrupt 21h
    
    
    cmp al,30h  ; check if input character is less then 0, 
                ; here we are validating user input to check if entered character is betwen 0 and 9
    jl loop_readnumber ; jump to display invalid character
 
    cmp al,39h  ; check if input character is great then 9
    jg loop_readnumber ; jump to display invalid character
    
    
    sub al,30h  ; subtract 30h character code of character 0 from input to get numeric value
    mov ah,00h  
    mov num,ax
    
    mov cx,ax
    mov bl,1
    mov si,offset factors2  ; memory location of factor2 for displaying the output
loop2:
    push cx
    mov ax,num
    div bl
    
    cmp ah,0    ; after division ah will have the reminder
    jg notfactor; If there is a reminder then then its not a factor 
              
    push bx     ; temp save bx          
    add bl,30h  ; have to add 30h to convert from number to ascii character   
    mov [si],bl ; store the number in factors2 memory location
    pop bx      ; restore the oreiginal value of bx pushed 
    inc si
    inc si
    

notfactor:
 
    
    inc bl
    pop cx
    loop loop2   
    
    mov ah,09h
    mov dx,offset factors
    int 21h
    
    mov ah,4ch      ; dos function 4ch to terminate and return to dos
    mov al,00       
    int 21h         ; dos interrupt 21h
    
endp
    messageenternumber db 0ah,0dh,'Please input a single digit number $'
    num dw 0;
    factors db 0ah,0dh,'The Factors are '
    factors2 db '          $'
end main