Sum of Digits & Number of Digits - Assembly Language - MASM


Number of Digits, Sum of Digits & Reverse of a Number
 

Program:
.model small
.stack 200h
.data
   msg1 db "Enter the number::$"
   msg2 db 'Sum of digits is::$'
   msg3 db 'No. of digits is::$'
   msg4 db 10,13,"Reverse of the given number is::$"
   newline DB 10,13, ' $'
   sum dw 0
   NO dW 0
   cnt dw 0
   arr db 20 dup(?)
.code
   print MACRO msg
   push ax
   push dx
   mov ah,09h
   mov dx,offset msg
   INT 21H
   pop dx
   pop ax
ENDM
.startup
   print msg1
   call readnumtoAX
   print msg4
   mov cx,ax
   mov BX,10
   mov si,offset arr
   mov di,offset arr
DV1:
   mov ax,cx
   mov dh,00
   mov dl,00
   div BX
   mov cx,ax
   inc NO
   mov al,dl
   mov [si],al
   mov ah,00
   call displayAX
   inc si
   add SUM,DX
   mov ax,cx
  cmp    AX,0
  jne DV1
  mov AX,NO
   print newline
   print msg3
   CALL displayAX
   MOV AX,SUM
   print newline
   print msg2
CALL displayAX
  print newline
   ;mov cx,NO
  ;dis:
   ; mov ah,00
   ; mov al,arr[di]
   ; CALL displayAX
   ;inc di
   ;loop dis
.EXIT
readnumtoAX PROC NEAR
   PUSH BX
   PUSH CX
   MOV CX,10
   MOV BX,00
back:
   MOV AH,01H
   INT 21H
   CMP AL,'0'
   JB skip
   CMP AL,'9'
   JA skip
   SUB AL,'0'
   PUSH AX
   MOV AX,BX
   MUL CX
   MOV BX,AX
   POP AX
   MOV AH,00
   ADD BX,AX
   JMP back
skip:
   MOV AX,BX
   POP CX
   POP BX
   RET
readnumtoAX ENDP
displayAX PROC NEAR
   PUSH DX
   PUSH CX
   PUSH BX
   PUSH AX
   MOV CX,0
   MOV BX,10
back1:
   MOV DX,0
   DIV BX
   PUSH DX
   INC CX
   OR AX,AX
   JNZ back1
back2:
   POP DX
   ADD DL,30H
   MOV AH,02H
   INT 21H
   LOOP back2
   POP AX
   POP BX
   POP CX
   POP DX
   RET
displayAX ENDP
END

Output:
F:\>unt
Enter the number::7835
Reverse of the given number is::5387
No. of digits is::4
Sum of digits is::23

1 comment:

  1. translation services Your article swept me away with its vast information and great writing. Thank you for sharing your views with such passion. I like your views.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...