Error while searching for all factors of a given number
I made a code to figure out all the factorials of a given number in 8086 assembler. But the problem is, I'm wrong. For example: when the input is 54, I get the result as 6, but the result should be 8. It also gives errors for some other inputs. But I cannot find out the problem in my code.
Here is my code:
.MODEL SMALL
.STACK 100
.DATA
NUMBER DW 54
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
MOV AX,NUMBER
MOV BX,1
MOV CX,0
WHILE_:
DIV BX
CMP DX,0
JE CHECK
MOV AX,NUMBER
INC BX
CMP BX,AX
JL WHILE_
JMP END_
CHECK:
CMP AX,BX
JG INC_
JE INC2_
JMP END_
INC_:
ADD CX,2
MOV AX,NUMBER
INC BX
CMP BX,AX
JL WHILE_:
JMP END_
INC2_:
INC CX
JMP END_
END_:
ADD CX,48
MOV AH,2
MOV DX,CX
INT 21H
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN
RET
What changes should I make to generate the correct output (e.g. 8 for 54)
You need to zero dx
beforediv
WHILE_:
; something here to zero dx
DIV BX
CMP DX,0
JE CHECK
By correcting this, I am getting correct results for many numbers. There will still be a problem in your release if the number is greater than 9. Your code works with single digits, for more than one digit (say, for coefficients 72), you need to convert the resulting string to ASCII.
It also helps in sending errors!
INC_:
ADD CX,2
MOV AX,NUMBER
INC BX
CMP BX,AX
JL WHILE_: ; <<<<< What is this colon?!?!
JMP END_
Learn to comment your code! This is important in assembly and will help you catch errors. Also, you submit 16 bit code, it's old and dead! Many of us haven't used it in years!
source to share