1

I have made a code for finding out all the factors of a given number in 8086 assembly language. But the problem is I am getting wrong output if the factors are more than 9. For example: when the input is 54 I am getting the correct result but when the input is 72 i am getting wrong output . How will i show the output if the result is greater than 9?

Here is my code :

.MODEL SMALL
.STACK 100

.DATA

NUMBER DW 72

.CODE

MAIN PROC  

MOV AX,@DATA
MOV DS,AX 

MOV AX,NUMBER    ; storing the dividend
MOV BX,1         ; storing the divisor (starting from 1)
MOV CX,0         ; initially all factors are zero

WHILE_:   
XOR DX,DX       ;clearing the bit fields
DIV BX
CMP DX,0        ; checking if remainder is 0
JE CHECK  

MOV AX,NUMBER
INC BX           ;incrementing divisor
CMP BX,AX        ; checking if dividend is less then divisor
JL WHILE_        ;
JMP END_         ; if so then ends


CHECK:
CMP AX,BX
JG INC_          ;jump if divisor is smaller than dividend
JE INC2_         ;jump if divisor= dividend
JMP END_


INC_:
ADD CX,2            ; incrementing by 2 as we are getting to factors: divisor and the quotient
MOV AX,NUMBER       
INC BX             ;incrementing divisor
CMP BX,AX          ;checking if dividend is less then divisor
JL WHILE_:
JMP END_   

INC2_:


INC CX      ; incrementing counter single time as we are getting two same factors

JMP END_ 


END_: 
CMP CX,10
JL PRINT1:
JMP PRINT2


PRINT1: 
ADD CX,48   ; printing decimal value
MOV AH,2
MOV DX,CX
INT 21H   

MOV AH,4CH
INT 21H

PRINT2:
ADD CX,48   ; printing decimal value
MOV AH,2
MOV DX,CX
INT 21H 


MOV AH,4CH
INT 21H


MAIN ENDP

END MAIN 

RET

How will I improve this code? Is there any way to show the output is this case?

Michael
  • 57,169
  • 9
  • 80
  • 125
  • just a simple search gives you lots of duplicates http://stackoverflow.com/questions/17664108/printing-3-digits-in-assembly http://stackoverflow.com/questions/15621258/assembly-printing-ascii-number http://stackoverflow.com/questions/9113060/print-decimal-in-8086-emulator – phuclv Sep 03 '14 at 04:01

1 Answers1

2

It's been long enough that I'm not going to try to address your Int 21h calls themselves. However, I see your failure anyway:

Both of your print functions are absolutely identical and print only one digit--if that digit is actually greater than 9 it runs past the numbers into the characters that follow--IIRC some punctuation, then the uppercase letters.

You need to split the digits apart and send each one to the screen--you'll need a couple of calls if all you're interested in is two-digit numbers, if they get long it's better to construct a general purpose routine.

Personally I would never use INC_ or INC2_ as label names--they look too much like the INC command. Don't be afraid to use longer label names, readability is a lot more important than the number of keys you have to hit.

Loren Pechtel
  • 8,945
  • 3
  • 33
  • 45
  • Can u provide me a sample code to print a number greater than 9 in 8086 assembly? @Loren Pechtel –  Sep 03 '14 at 03:53
  • @user3435851 It's been too long (I haven't written any assembly code this century) and this is obviously a homework problem anyway. – Loren Pechtel Sep 03 '14 at 17:10