Test whether a number is a Harshad Number











up vote
3
down vote

favorite












A little code I wrote that will check if the entered 4-digit number is Harshad or not (Harshad number is one which divided by the sum of its digits will leave no remainder, special Harshad is the same but will also work with flipped sum of digits, example - 1729/19 (19 is the sum of digits) and when flipped 1729/91 will leave no remainder as well).



I would love to see how it could become better (I'm still new so was limited with the commands I could've used).



; Harshad.asm - check if input from user (4 digits, 1000-9999) is a Harshad number and if so, check if it's a "special" Harshad number
;
.MODEL SMALL
.STACK 100h
.DATA
RequestStr DB 'Enter a 4 digit number (1000-9999):',13,10,'$'
IsHarshad DB ' is a Harshad number.',13,10,'$'
SpecialHarshad DB 'It is also a special Harshad number.',13,10,'$'
NotHarshad DB ' is not a Harshad number',13,10,'$'
IncorrectInput DB 13,10,'Input is incorrect.',13,10,'$'
Num DW ? ;Will fit a "word" size (16 bits)
DigitSum DW ? ;Sum of digits
TEN DW 10
TENbyte DB 10
Temp DB ? ;Used to check if number is also special Harshad during the div process
Temp2 DB ? ;Used with special Harshad div process
;
.CODE
MOV AX,@DATA ;DS can be written to only through a register
MOV DS,AX ;Set DS to point to data segment
MOV AH,9 ;Set print option for INT 21h
MOV DX,OFFSET RequestStr ;Set DS:DX to point to RequestString
INT 21h ;Print RequestStr
;
NumberInput:
;First digit
MOV AH,1 ;Set scanning (input) option for INT 21h
INT 21h ;Scan first digit
MOV DX,0
SUB AL,'0' ;Converting from ascii value to numeral value
CMP AL,1 ;First digit must be between 1 and 9 in order for the number to be of 4 digits
JB WrongInput ;Otherwise jump to WrongInput label
CMP AL,9
JA WrongInput
MOV AH,0
MOV DigitSum,AX ;Store only first digit's value at the variable DigitSum
MUL TEN ;Multiply AX by 10
MOV Num,AX
;Second digit
MOV AX,0
MOV AH,1
INT 21h
SUB AL,'0'
CMP AL,0
JB WrongInput
CMP AL,9
JA WrongInput
MOV AH,0
ADD DigitSum,AX ;Add only second's digit value to DigitSum
ADD Num,AX ;Add AX's value (which has been multiplied by 10 with the first digit) to Num variable
MOV AX,0
MOV AX,Num ;Move new Num's value to AX to multiply it by 10
MUL TEN
MOV Num,AX
;Third digit
MOV AX,0
MOV AH,1
INT 21h
SUB AL,'0'
CMP AL,0
JB WrongInput
CMP AL,9
JA WrongInput
MOV AH,0
ADD DigitSum,AX
ADD Num,AX
MOV AX,0
MOV AX,Num
MUL TEN
MOV Num,AX
;Forth digit
MOV AX,0
MOV AH,1
INT 21h
SUB AL,'0'
CMP AL,0
JB WrongInput
CMP AL,9
JA WrongInput
MOV AH,0
ADD DigitSum,AX ;Now DigitSum contains the sum of each of the 4 digits in the number
ADD Num,AX ;Num contains full 4 digits number
JMP CheckHarshad
WrongInput:
MOV AH,9
MOV DX,OFFSET IncorrectInput
INT 21h
JMP CodeEnd
CheckHarshad:
MOV AX,0
MOV DX,0
MOV AX,Num
DIV DigitSum ;Number will be stored in AX and the remainder in DX
CMP DX,0 ;Check if there is remainder or not
JE CheckSpecialHarshad
MOV AH,9
MOV DX,OFFSET NotHarshad
INT 21h
JMP CodeEnd
CheckSpecialHarshad:
MOV AH,9
MOV DX, OFFSET IsHarshad
INT 21h
MOV AX,0
MOV AX,DigitSum
DIV TENbyte
MOV Temp,AL
MOV Temp2,AH
MOV AX,0
MOV AL,Temp2
MUL TENbyte
ADD AL,Temp
MOV Temp2,AL ;Temp2 now has the DigitSum number flipped
MOV AX,0
MOV AX,Num
DIV Temp2
CMP AH,0
JNE CodeEnd
MOV DX,OFFSET SpecialHarshad
MOV AH,9
INT 21h
CodeEnd:
MOV AH,4Ch
INT 21h
END









share|improve this question









New contributor




S.Arkab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    3
    down vote

    favorite












    A little code I wrote that will check if the entered 4-digit number is Harshad or not (Harshad number is one which divided by the sum of its digits will leave no remainder, special Harshad is the same but will also work with flipped sum of digits, example - 1729/19 (19 is the sum of digits) and when flipped 1729/91 will leave no remainder as well).



    I would love to see how it could become better (I'm still new so was limited with the commands I could've used).



    ; Harshad.asm - check if input from user (4 digits, 1000-9999) is a Harshad number and if so, check if it's a "special" Harshad number
    ;
    .MODEL SMALL
    .STACK 100h
    .DATA
    RequestStr DB 'Enter a 4 digit number (1000-9999):',13,10,'$'
    IsHarshad DB ' is a Harshad number.',13,10,'$'
    SpecialHarshad DB 'It is also a special Harshad number.',13,10,'$'
    NotHarshad DB ' is not a Harshad number',13,10,'$'
    IncorrectInput DB 13,10,'Input is incorrect.',13,10,'$'
    Num DW ? ;Will fit a "word" size (16 bits)
    DigitSum DW ? ;Sum of digits
    TEN DW 10
    TENbyte DB 10
    Temp DB ? ;Used to check if number is also special Harshad during the div process
    Temp2 DB ? ;Used with special Harshad div process
    ;
    .CODE
    MOV AX,@DATA ;DS can be written to only through a register
    MOV DS,AX ;Set DS to point to data segment
    MOV AH,9 ;Set print option for INT 21h
    MOV DX,OFFSET RequestStr ;Set DS:DX to point to RequestString
    INT 21h ;Print RequestStr
    ;
    NumberInput:
    ;First digit
    MOV AH,1 ;Set scanning (input) option for INT 21h
    INT 21h ;Scan first digit
    MOV DX,0
    SUB AL,'0' ;Converting from ascii value to numeral value
    CMP AL,1 ;First digit must be between 1 and 9 in order for the number to be of 4 digits
    JB WrongInput ;Otherwise jump to WrongInput label
    CMP AL,9
    JA WrongInput
    MOV AH,0
    MOV DigitSum,AX ;Store only first digit's value at the variable DigitSum
    MUL TEN ;Multiply AX by 10
    MOV Num,AX
    ;Second digit
    MOV AX,0
    MOV AH,1
    INT 21h
    SUB AL,'0'
    CMP AL,0
    JB WrongInput
    CMP AL,9
    JA WrongInput
    MOV AH,0
    ADD DigitSum,AX ;Add only second's digit value to DigitSum
    ADD Num,AX ;Add AX's value (which has been multiplied by 10 with the first digit) to Num variable
    MOV AX,0
    MOV AX,Num ;Move new Num's value to AX to multiply it by 10
    MUL TEN
    MOV Num,AX
    ;Third digit
    MOV AX,0
    MOV AH,1
    INT 21h
    SUB AL,'0'
    CMP AL,0
    JB WrongInput
    CMP AL,9
    JA WrongInput
    MOV AH,0
    ADD DigitSum,AX
    ADD Num,AX
    MOV AX,0
    MOV AX,Num
    MUL TEN
    MOV Num,AX
    ;Forth digit
    MOV AX,0
    MOV AH,1
    INT 21h
    SUB AL,'0'
    CMP AL,0
    JB WrongInput
    CMP AL,9
    JA WrongInput
    MOV AH,0
    ADD DigitSum,AX ;Now DigitSum contains the sum of each of the 4 digits in the number
    ADD Num,AX ;Num contains full 4 digits number
    JMP CheckHarshad
    WrongInput:
    MOV AH,9
    MOV DX,OFFSET IncorrectInput
    INT 21h
    JMP CodeEnd
    CheckHarshad:
    MOV AX,0
    MOV DX,0
    MOV AX,Num
    DIV DigitSum ;Number will be stored in AX and the remainder in DX
    CMP DX,0 ;Check if there is remainder or not
    JE CheckSpecialHarshad
    MOV AH,9
    MOV DX,OFFSET NotHarshad
    INT 21h
    JMP CodeEnd
    CheckSpecialHarshad:
    MOV AH,9
    MOV DX, OFFSET IsHarshad
    INT 21h
    MOV AX,0
    MOV AX,DigitSum
    DIV TENbyte
    MOV Temp,AL
    MOV Temp2,AH
    MOV AX,0
    MOV AL,Temp2
    MUL TENbyte
    ADD AL,Temp
    MOV Temp2,AL ;Temp2 now has the DigitSum number flipped
    MOV AX,0
    MOV AX,Num
    DIV Temp2
    CMP AH,0
    JNE CodeEnd
    MOV DX,OFFSET SpecialHarshad
    MOV AH,9
    INT 21h
    CodeEnd:
    MOV AH,4Ch
    INT 21h
    END









    share|improve this question









    New contributor




    S.Arkab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      A little code I wrote that will check if the entered 4-digit number is Harshad or not (Harshad number is one which divided by the sum of its digits will leave no remainder, special Harshad is the same but will also work with flipped sum of digits, example - 1729/19 (19 is the sum of digits) and when flipped 1729/91 will leave no remainder as well).



      I would love to see how it could become better (I'm still new so was limited with the commands I could've used).



      ; Harshad.asm - check if input from user (4 digits, 1000-9999) is a Harshad number and if so, check if it's a "special" Harshad number
      ;
      .MODEL SMALL
      .STACK 100h
      .DATA
      RequestStr DB 'Enter a 4 digit number (1000-9999):',13,10,'$'
      IsHarshad DB ' is a Harshad number.',13,10,'$'
      SpecialHarshad DB 'It is also a special Harshad number.',13,10,'$'
      NotHarshad DB ' is not a Harshad number',13,10,'$'
      IncorrectInput DB 13,10,'Input is incorrect.',13,10,'$'
      Num DW ? ;Will fit a "word" size (16 bits)
      DigitSum DW ? ;Sum of digits
      TEN DW 10
      TENbyte DB 10
      Temp DB ? ;Used to check if number is also special Harshad during the div process
      Temp2 DB ? ;Used with special Harshad div process
      ;
      .CODE
      MOV AX,@DATA ;DS can be written to only through a register
      MOV DS,AX ;Set DS to point to data segment
      MOV AH,9 ;Set print option for INT 21h
      MOV DX,OFFSET RequestStr ;Set DS:DX to point to RequestString
      INT 21h ;Print RequestStr
      ;
      NumberInput:
      ;First digit
      MOV AH,1 ;Set scanning (input) option for INT 21h
      INT 21h ;Scan first digit
      MOV DX,0
      SUB AL,'0' ;Converting from ascii value to numeral value
      CMP AL,1 ;First digit must be between 1 and 9 in order for the number to be of 4 digits
      JB WrongInput ;Otherwise jump to WrongInput label
      CMP AL,9
      JA WrongInput
      MOV AH,0
      MOV DigitSum,AX ;Store only first digit's value at the variable DigitSum
      MUL TEN ;Multiply AX by 10
      MOV Num,AX
      ;Second digit
      MOV AX,0
      MOV AH,1
      INT 21h
      SUB AL,'0'
      CMP AL,0
      JB WrongInput
      CMP AL,9
      JA WrongInput
      MOV AH,0
      ADD DigitSum,AX ;Add only second's digit value to DigitSum
      ADD Num,AX ;Add AX's value (which has been multiplied by 10 with the first digit) to Num variable
      MOV AX,0
      MOV AX,Num ;Move new Num's value to AX to multiply it by 10
      MUL TEN
      MOV Num,AX
      ;Third digit
      MOV AX,0
      MOV AH,1
      INT 21h
      SUB AL,'0'
      CMP AL,0
      JB WrongInput
      CMP AL,9
      JA WrongInput
      MOV AH,0
      ADD DigitSum,AX
      ADD Num,AX
      MOV AX,0
      MOV AX,Num
      MUL TEN
      MOV Num,AX
      ;Forth digit
      MOV AX,0
      MOV AH,1
      INT 21h
      SUB AL,'0'
      CMP AL,0
      JB WrongInput
      CMP AL,9
      JA WrongInput
      MOV AH,0
      ADD DigitSum,AX ;Now DigitSum contains the sum of each of the 4 digits in the number
      ADD Num,AX ;Num contains full 4 digits number
      JMP CheckHarshad
      WrongInput:
      MOV AH,9
      MOV DX,OFFSET IncorrectInput
      INT 21h
      JMP CodeEnd
      CheckHarshad:
      MOV AX,0
      MOV DX,0
      MOV AX,Num
      DIV DigitSum ;Number will be stored in AX and the remainder in DX
      CMP DX,0 ;Check if there is remainder or not
      JE CheckSpecialHarshad
      MOV AH,9
      MOV DX,OFFSET NotHarshad
      INT 21h
      JMP CodeEnd
      CheckSpecialHarshad:
      MOV AH,9
      MOV DX, OFFSET IsHarshad
      INT 21h
      MOV AX,0
      MOV AX,DigitSum
      DIV TENbyte
      MOV Temp,AL
      MOV Temp2,AH
      MOV AX,0
      MOV AL,Temp2
      MUL TENbyte
      ADD AL,Temp
      MOV Temp2,AL ;Temp2 now has the DigitSum number flipped
      MOV AX,0
      MOV AX,Num
      DIV Temp2
      CMP AH,0
      JNE CodeEnd
      MOV DX,OFFSET SpecialHarshad
      MOV AH,9
      INT 21h
      CodeEnd:
      MOV AH,4Ch
      INT 21h
      END









      share|improve this question









      New contributor




      S.Arkab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      A little code I wrote that will check if the entered 4-digit number is Harshad or not (Harshad number is one which divided by the sum of its digits will leave no remainder, special Harshad is the same but will also work with flipped sum of digits, example - 1729/19 (19 is the sum of digits) and when flipped 1729/91 will leave no remainder as well).



      I would love to see how it could become better (I'm still new so was limited with the commands I could've used).



      ; Harshad.asm - check if input from user (4 digits, 1000-9999) is a Harshad number and if so, check if it's a "special" Harshad number
      ;
      .MODEL SMALL
      .STACK 100h
      .DATA
      RequestStr DB 'Enter a 4 digit number (1000-9999):',13,10,'$'
      IsHarshad DB ' is a Harshad number.',13,10,'$'
      SpecialHarshad DB 'It is also a special Harshad number.',13,10,'$'
      NotHarshad DB ' is not a Harshad number',13,10,'$'
      IncorrectInput DB 13,10,'Input is incorrect.',13,10,'$'
      Num DW ? ;Will fit a "word" size (16 bits)
      DigitSum DW ? ;Sum of digits
      TEN DW 10
      TENbyte DB 10
      Temp DB ? ;Used to check if number is also special Harshad during the div process
      Temp2 DB ? ;Used with special Harshad div process
      ;
      .CODE
      MOV AX,@DATA ;DS can be written to only through a register
      MOV DS,AX ;Set DS to point to data segment
      MOV AH,9 ;Set print option for INT 21h
      MOV DX,OFFSET RequestStr ;Set DS:DX to point to RequestString
      INT 21h ;Print RequestStr
      ;
      NumberInput:
      ;First digit
      MOV AH,1 ;Set scanning (input) option for INT 21h
      INT 21h ;Scan first digit
      MOV DX,0
      SUB AL,'0' ;Converting from ascii value to numeral value
      CMP AL,1 ;First digit must be between 1 and 9 in order for the number to be of 4 digits
      JB WrongInput ;Otherwise jump to WrongInput label
      CMP AL,9
      JA WrongInput
      MOV AH,0
      MOV DigitSum,AX ;Store only first digit's value at the variable DigitSum
      MUL TEN ;Multiply AX by 10
      MOV Num,AX
      ;Second digit
      MOV AX,0
      MOV AH,1
      INT 21h
      SUB AL,'0'
      CMP AL,0
      JB WrongInput
      CMP AL,9
      JA WrongInput
      MOV AH,0
      ADD DigitSum,AX ;Add only second's digit value to DigitSum
      ADD Num,AX ;Add AX's value (which has been multiplied by 10 with the first digit) to Num variable
      MOV AX,0
      MOV AX,Num ;Move new Num's value to AX to multiply it by 10
      MUL TEN
      MOV Num,AX
      ;Third digit
      MOV AX,0
      MOV AH,1
      INT 21h
      SUB AL,'0'
      CMP AL,0
      JB WrongInput
      CMP AL,9
      JA WrongInput
      MOV AH,0
      ADD DigitSum,AX
      ADD Num,AX
      MOV AX,0
      MOV AX,Num
      MUL TEN
      MOV Num,AX
      ;Forth digit
      MOV AX,0
      MOV AH,1
      INT 21h
      SUB AL,'0'
      CMP AL,0
      JB WrongInput
      CMP AL,9
      JA WrongInput
      MOV AH,0
      ADD DigitSum,AX ;Now DigitSum contains the sum of each of the 4 digits in the number
      ADD Num,AX ;Num contains full 4 digits number
      JMP CheckHarshad
      WrongInput:
      MOV AH,9
      MOV DX,OFFSET IncorrectInput
      INT 21h
      JMP CodeEnd
      CheckHarshad:
      MOV AX,0
      MOV DX,0
      MOV AX,Num
      DIV DigitSum ;Number will be stored in AX and the remainder in DX
      CMP DX,0 ;Check if there is remainder or not
      JE CheckSpecialHarshad
      MOV AH,9
      MOV DX,OFFSET NotHarshad
      INT 21h
      JMP CodeEnd
      CheckSpecialHarshad:
      MOV AH,9
      MOV DX, OFFSET IsHarshad
      INT 21h
      MOV AX,0
      MOV AX,DigitSum
      DIV TENbyte
      MOV Temp,AL
      MOV Temp2,AH
      MOV AX,0
      MOV AL,Temp2
      MUL TENbyte
      ADD AL,Temp
      MOV Temp2,AL ;Temp2 now has the DigitSum number flipped
      MOV AX,0
      MOV AX,Num
      DIV Temp2
      CMP AH,0
      JNE CodeEnd
      MOV DX,OFFSET SpecialHarshad
      MOV AH,9
      INT 21h
      CodeEnd:
      MOV AH,4Ch
      INT 21h
      END






      integer assembly x86






      share|improve this question









      New contributor




      S.Arkab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      S.Arkab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited yesterday









      Toby Speight

      22.8k537109




      22.8k537109






      New contributor




      S.Arkab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked yesterday









      S.Arkab

      161




      161




      New contributor




      S.Arkab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      S.Arkab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      S.Arkab is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote














          Written for NASM




          Your program is a little hard to follow, mostly due to poor formatting. There is a lot of unnecessary duplication too and the user has no opportunity for correcting mistakes. The following prologue will address these initial criteria;




          1. Prompt operator for what is required.

          2. Use a function that allows the user to correct mistakes.

          3. Test that entry is 4 characters long and the first digit is not zero.


                org   0x100

          DOS equ 21H
          SHOW equ 9
          BUFFERED equ 10 ; Buffered input
          OUTCHR equ 2
          GETCHR equ 8


          Begin: mov ah, SHOW ; Function 9 display string terminated with '$'
          mov dx, Prompt
          int DOS

          mov ah, BUFFERED ; Function 10 Get buffered input
          mov dx, Entry
          int DOS

          ; Test that your criteria of 4 character entry and first digit not zero has been met.

          cld ; Make sure SI auto-increments
          mov si, dx
          inc si ; Point to number of characters entered
          lodsb
          or al, al
          jz Err ; You might want to put some sort of prompting text here
          cmp al, 4
          jnz Err
          cmp byte [si], '1'
          jae Evaluate

          Err: mov ah, SHOW ; Function 9 again
          mov dx, ErrTxt
          int DOS
          ret ; Return to DOS


          You do have these elements in your program, but I think you'll agree when there are in one place it's a lot easier to recognize which error testing mechanism has been implemented. Notice too, formatting is monumentally important, especially in ASSM.



          Now we are ready to do the calculations and because we're not going to be using any DOS functions, there is a little more freedom in which registers we can use and for what. The criteria now becomes, establish a binary value of our entry and binary total of all digits.



          Evaluate:

          ; AX = Working register, CX = character count

          movzx cx, al ; Move character count into CX
          xor dx, dx ; DX will be binary representation of input
          mov bx, dx ; BL will be total of digits
          mov ax, dx

          .get: lodsb ; Read character from input string
          cmp al, '0'
          jb Err
          cmp al, '9'
          ja Err

          and ax, 1111b ; Could be SUB AL,'0' too.
          add bl, al ; Bump total of digits
          xchg ax, dx
          imul ax, 10
          add dx, ax
          loop .get

          ; Now DX = Binary equivalent and BL divisor

          xor ax, ax
          xchg ax, dx
          push ax ; Going to need this again to determine if Special
          idiv bx
          or dl, dl ; Determine if value is Harshad or not.


          From this point I'd pretty much be doing the same as you have except there is still no need to address anything in memory other than prompting text as everything you need is on the stack and BL. Let us know what you come up with then we'll compare it with what I have.






          share|improve this answer























          • I don't know some of the commands you used (but can guess what some might do, such as loop and xchg). I will study what you wrote and try to improve my code as you suggested. Thank you!
            – S.Arkab
            17 hours ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          S.Arkab is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208931%2ftest-whether-a-number-is-a-harshad-number%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote














          Written for NASM




          Your program is a little hard to follow, mostly due to poor formatting. There is a lot of unnecessary duplication too and the user has no opportunity for correcting mistakes. The following prologue will address these initial criteria;




          1. Prompt operator for what is required.

          2. Use a function that allows the user to correct mistakes.

          3. Test that entry is 4 characters long and the first digit is not zero.


                org   0x100

          DOS equ 21H
          SHOW equ 9
          BUFFERED equ 10 ; Buffered input
          OUTCHR equ 2
          GETCHR equ 8


          Begin: mov ah, SHOW ; Function 9 display string terminated with '$'
          mov dx, Prompt
          int DOS

          mov ah, BUFFERED ; Function 10 Get buffered input
          mov dx, Entry
          int DOS

          ; Test that your criteria of 4 character entry and first digit not zero has been met.

          cld ; Make sure SI auto-increments
          mov si, dx
          inc si ; Point to number of characters entered
          lodsb
          or al, al
          jz Err ; You might want to put some sort of prompting text here
          cmp al, 4
          jnz Err
          cmp byte [si], '1'
          jae Evaluate

          Err: mov ah, SHOW ; Function 9 again
          mov dx, ErrTxt
          int DOS
          ret ; Return to DOS


          You do have these elements in your program, but I think you'll agree when there are in one place it's a lot easier to recognize which error testing mechanism has been implemented. Notice too, formatting is monumentally important, especially in ASSM.



          Now we are ready to do the calculations and because we're not going to be using any DOS functions, there is a little more freedom in which registers we can use and for what. The criteria now becomes, establish a binary value of our entry and binary total of all digits.



          Evaluate:

          ; AX = Working register, CX = character count

          movzx cx, al ; Move character count into CX
          xor dx, dx ; DX will be binary representation of input
          mov bx, dx ; BL will be total of digits
          mov ax, dx

          .get: lodsb ; Read character from input string
          cmp al, '0'
          jb Err
          cmp al, '9'
          ja Err

          and ax, 1111b ; Could be SUB AL,'0' too.
          add bl, al ; Bump total of digits
          xchg ax, dx
          imul ax, 10
          add dx, ax
          loop .get

          ; Now DX = Binary equivalent and BL divisor

          xor ax, ax
          xchg ax, dx
          push ax ; Going to need this again to determine if Special
          idiv bx
          or dl, dl ; Determine if value is Harshad or not.


          From this point I'd pretty much be doing the same as you have except there is still no need to address anything in memory other than prompting text as everything you need is on the stack and BL. Let us know what you come up with then we'll compare it with what I have.






          share|improve this answer























          • I don't know some of the commands you used (but can guess what some might do, such as loop and xchg). I will study what you wrote and try to improve my code as you suggested. Thank you!
            – S.Arkab
            17 hours ago















          up vote
          1
          down vote














          Written for NASM




          Your program is a little hard to follow, mostly due to poor formatting. There is a lot of unnecessary duplication too and the user has no opportunity for correcting mistakes. The following prologue will address these initial criteria;




          1. Prompt operator for what is required.

          2. Use a function that allows the user to correct mistakes.

          3. Test that entry is 4 characters long and the first digit is not zero.


                org   0x100

          DOS equ 21H
          SHOW equ 9
          BUFFERED equ 10 ; Buffered input
          OUTCHR equ 2
          GETCHR equ 8


          Begin: mov ah, SHOW ; Function 9 display string terminated with '$'
          mov dx, Prompt
          int DOS

          mov ah, BUFFERED ; Function 10 Get buffered input
          mov dx, Entry
          int DOS

          ; Test that your criteria of 4 character entry and first digit not zero has been met.

          cld ; Make sure SI auto-increments
          mov si, dx
          inc si ; Point to number of characters entered
          lodsb
          or al, al
          jz Err ; You might want to put some sort of prompting text here
          cmp al, 4
          jnz Err
          cmp byte [si], '1'
          jae Evaluate

          Err: mov ah, SHOW ; Function 9 again
          mov dx, ErrTxt
          int DOS
          ret ; Return to DOS


          You do have these elements in your program, but I think you'll agree when there are in one place it's a lot easier to recognize which error testing mechanism has been implemented. Notice too, formatting is monumentally important, especially in ASSM.



          Now we are ready to do the calculations and because we're not going to be using any DOS functions, there is a little more freedom in which registers we can use and for what. The criteria now becomes, establish a binary value of our entry and binary total of all digits.



          Evaluate:

          ; AX = Working register, CX = character count

          movzx cx, al ; Move character count into CX
          xor dx, dx ; DX will be binary representation of input
          mov bx, dx ; BL will be total of digits
          mov ax, dx

          .get: lodsb ; Read character from input string
          cmp al, '0'
          jb Err
          cmp al, '9'
          ja Err

          and ax, 1111b ; Could be SUB AL,'0' too.
          add bl, al ; Bump total of digits
          xchg ax, dx
          imul ax, 10
          add dx, ax
          loop .get

          ; Now DX = Binary equivalent and BL divisor

          xor ax, ax
          xchg ax, dx
          push ax ; Going to need this again to determine if Special
          idiv bx
          or dl, dl ; Determine if value is Harshad or not.


          From this point I'd pretty much be doing the same as you have except there is still no need to address anything in memory other than prompting text as everything you need is on the stack and BL. Let us know what you come up with then we'll compare it with what I have.






          share|improve this answer























          • I don't know some of the commands you used (but can guess what some might do, such as loop and xchg). I will study what you wrote and try to improve my code as you suggested. Thank you!
            – S.Arkab
            17 hours ago













          up vote
          1
          down vote










          up vote
          1
          down vote










          Written for NASM




          Your program is a little hard to follow, mostly due to poor formatting. There is a lot of unnecessary duplication too and the user has no opportunity for correcting mistakes. The following prologue will address these initial criteria;




          1. Prompt operator for what is required.

          2. Use a function that allows the user to correct mistakes.

          3. Test that entry is 4 characters long and the first digit is not zero.


                org   0x100

          DOS equ 21H
          SHOW equ 9
          BUFFERED equ 10 ; Buffered input
          OUTCHR equ 2
          GETCHR equ 8


          Begin: mov ah, SHOW ; Function 9 display string terminated with '$'
          mov dx, Prompt
          int DOS

          mov ah, BUFFERED ; Function 10 Get buffered input
          mov dx, Entry
          int DOS

          ; Test that your criteria of 4 character entry and first digit not zero has been met.

          cld ; Make sure SI auto-increments
          mov si, dx
          inc si ; Point to number of characters entered
          lodsb
          or al, al
          jz Err ; You might want to put some sort of prompting text here
          cmp al, 4
          jnz Err
          cmp byte [si], '1'
          jae Evaluate

          Err: mov ah, SHOW ; Function 9 again
          mov dx, ErrTxt
          int DOS
          ret ; Return to DOS


          You do have these elements in your program, but I think you'll agree when there are in one place it's a lot easier to recognize which error testing mechanism has been implemented. Notice too, formatting is monumentally important, especially in ASSM.



          Now we are ready to do the calculations and because we're not going to be using any DOS functions, there is a little more freedom in which registers we can use and for what. The criteria now becomes, establish a binary value of our entry and binary total of all digits.



          Evaluate:

          ; AX = Working register, CX = character count

          movzx cx, al ; Move character count into CX
          xor dx, dx ; DX will be binary representation of input
          mov bx, dx ; BL will be total of digits
          mov ax, dx

          .get: lodsb ; Read character from input string
          cmp al, '0'
          jb Err
          cmp al, '9'
          ja Err

          and ax, 1111b ; Could be SUB AL,'0' too.
          add bl, al ; Bump total of digits
          xchg ax, dx
          imul ax, 10
          add dx, ax
          loop .get

          ; Now DX = Binary equivalent and BL divisor

          xor ax, ax
          xchg ax, dx
          push ax ; Going to need this again to determine if Special
          idiv bx
          or dl, dl ; Determine if value is Harshad or not.


          From this point I'd pretty much be doing the same as you have except there is still no need to address anything in memory other than prompting text as everything you need is on the stack and BL. Let us know what you come up with then we'll compare it with what I have.






          share|improve this answer















          Written for NASM




          Your program is a little hard to follow, mostly due to poor formatting. There is a lot of unnecessary duplication too and the user has no opportunity for correcting mistakes. The following prologue will address these initial criteria;




          1. Prompt operator for what is required.

          2. Use a function that allows the user to correct mistakes.

          3. Test that entry is 4 characters long and the first digit is not zero.


                org   0x100

          DOS equ 21H
          SHOW equ 9
          BUFFERED equ 10 ; Buffered input
          OUTCHR equ 2
          GETCHR equ 8


          Begin: mov ah, SHOW ; Function 9 display string terminated with '$'
          mov dx, Prompt
          int DOS

          mov ah, BUFFERED ; Function 10 Get buffered input
          mov dx, Entry
          int DOS

          ; Test that your criteria of 4 character entry and first digit not zero has been met.

          cld ; Make sure SI auto-increments
          mov si, dx
          inc si ; Point to number of characters entered
          lodsb
          or al, al
          jz Err ; You might want to put some sort of prompting text here
          cmp al, 4
          jnz Err
          cmp byte [si], '1'
          jae Evaluate

          Err: mov ah, SHOW ; Function 9 again
          mov dx, ErrTxt
          int DOS
          ret ; Return to DOS


          You do have these elements in your program, but I think you'll agree when there are in one place it's a lot easier to recognize which error testing mechanism has been implemented. Notice too, formatting is monumentally important, especially in ASSM.



          Now we are ready to do the calculations and because we're not going to be using any DOS functions, there is a little more freedom in which registers we can use and for what. The criteria now becomes, establish a binary value of our entry and binary total of all digits.



          Evaluate:

          ; AX = Working register, CX = character count

          movzx cx, al ; Move character count into CX
          xor dx, dx ; DX will be binary representation of input
          mov bx, dx ; BL will be total of digits
          mov ax, dx

          .get: lodsb ; Read character from input string
          cmp al, '0'
          jb Err
          cmp al, '9'
          ja Err

          and ax, 1111b ; Could be SUB AL,'0' too.
          add bl, al ; Bump total of digits
          xchg ax, dx
          imul ax, 10
          add dx, ax
          loop .get

          ; Now DX = Binary equivalent and BL divisor

          xor ax, ax
          xchg ax, dx
          push ax ; Going to need this again to determine if Special
          idiv bx
          or dl, dl ; Determine if value is Harshad or not.


          From this point I'd pretty much be doing the same as you have except there is still no need to address anything in memory other than prompting text as everything you need is on the stack and BL. Let us know what you come up with then we'll compare it with what I have.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 17 hours ago









          Toby Speight

          22.8k537109




          22.8k537109










          answered 18 hours ago









          Shift_Left

          49139




          49139












          • I don't know some of the commands you used (but can guess what some might do, such as loop and xchg). I will study what you wrote and try to improve my code as you suggested. Thank you!
            – S.Arkab
            17 hours ago


















          • I don't know some of the commands you used (but can guess what some might do, such as loop and xchg). I will study what you wrote and try to improve my code as you suggested. Thank you!
            – S.Arkab
            17 hours ago
















          I don't know some of the commands you used (but can guess what some might do, such as loop and xchg). I will study what you wrote and try to improve my code as you suggested. Thank you!
          – S.Arkab
          17 hours ago




          I don't know some of the commands you used (but can guess what some might do, such as loop and xchg). I will study what you wrote and try to improve my code as you suggested. Thank you!
          – S.Arkab
          17 hours ago










          S.Arkab is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          S.Arkab is a new contributor. Be nice, and check out our Code of Conduct.













          S.Arkab is a new contributor. Be nice, and check out our Code of Conduct.












          S.Arkab is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f208931%2ftest-whether-a-number-is-a-harshad-number%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Scott Moir

          Souastre

          Morgemoulin