0

Please can someone help me, I have been researching this for the past few weeks now and I have not found a solution to my problem.

I am trying to determine whether a number entered by a user is a multiple of three or not. The range of numbers is between 1-9.

Below is the code that I am working on:

mov ah,01   
int 21h
mov bl,al
mov bl,3
div bl
cmp ah,0 ;Checks to see if remainder is zero
je e ;if it is not, go to e else execute code below
                
    mov dx, var1
    mov ah,09
    int 21h
    jmp exit
            
e:
    mov dx, var2
    mov ah,09
    int 21h
    jmp exit
        
exit:
    mov ah,4ch
    int 21h

I know that I need to convert my input into hexadecimal, I have tried to use the below line for that but it does not help, when I output the values to be able to see them, I get symbols like a smiley :

 add ch,48
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    basically a duplicate of [NASM Assembly convert input to integer?](https://stackoverflow.com/q/19309749), but that's 32-bit code. There's probably a 16-bit version somewhere that includes input via DOS or BIOS calls. (Fun fact: for divisibility by 9, you could use the math trick of summing the base-10 digits (casting out 9s), instead of multiplying and adding to create a binary integer representation of the number. Either way, at no point is actual hex involved; you don't want to break the number up into 4-bit chunks.) – Peter Cordes Oct 20 '20 at 14:52

1 Answers1

1

I am trying to determine whether a number entered by a user is a multiple of three or not. The range of numbers is between 1-9.

mov ah,01   
int 21h

The input that you get from this DOS function is a character represented by an ASCII code. If you've used the keyboard keys 1 through 9, you will have received in the AL register a number ranging from 49 to 57.
To arrive at the values depicted on those keys you just need to subtract 48:

sub al, 48

To check if the number is a multiple of 3, you can indeed divide by 3 and inspect the remainder. But the byte-sized division expects the number that want to divide in the AX register and that's something that you've neglected!

mov ah, 0  ; From AL to AX (could have used CBW instruction here)
mov bl, 3
div bl     ; Divides AX by BL -> Quotient in AL, Remainder in AH

Rest is like you did it...

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • Thank you very much for the help, I used the code that you have provided for conversion initially but I guess I did not use it in the correct context with the code that followed. – Kaneez F.Areff Oct 25 '20 at 14:25