user3435851
user3435851

Reputation:

Error in Finding all the factors of a given number

I have made a code for finding out all the factorials of a given number in 8086 assembly language. But the problem is I am getting wrong output. For example: when the input is 54 I am getting the result as 6 but the result should be 8. It's also giving errors for some other inputs. But I can't 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 produce the correct output( Like 8 for 54)

Upvotes: 3

Views: 1672

Answers (1)

Gunner
Gunner

Reputation: 5884

You need to zero out dx before the div

WHILE_:
; something here to zero dx
DIV BX
CMP DX,0
JE CHECK  

By fixing this, I get correct results for many numbers. There will still be a problem in your output if the number is greater than 9. Your code works with single digits, for more than one digit, (say for the factors of 72), you need to convert your resulting string to ASCII.

It also help to post code with out 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 catch errors. Plus you are posting 16 bit code, it is old and dead! Many of us haven't used it in many years!

Upvotes: 2

Related Questions