Round Register up to multiple of 8












0















I am trying to round a 64bit register up so it's a multiple of 8. So it ends with either an 8 or 0.



Is there an efficient way to do this, I am currently doing it as follows:



xor r9, r9
_R:
add r9, 08h
cmp r8, r9
ja _R
mov r8, r9









share|improve this question



























    0















    I am trying to round a 64bit register up so it's a multiple of 8. So it ends with either an 8 or 0.



    Is there an efficient way to do this, I am currently doing it as follows:



    xor r9, r9
    _R:
    add r9, 08h
    cmp r8, r9
    ja _R
    mov r8, r9









    share|improve this question

























      0












      0








      0








      I am trying to round a 64bit register up so it's a multiple of 8. So it ends with either an 8 or 0.



      Is there an efficient way to do this, I am currently doing it as follows:



      xor r9, r9
      _R:
      add r9, 08h
      cmp r8, r9
      ja _R
      mov r8, r9









      share|improve this question














      I am trying to round a 64bit register up so it's a multiple of 8. So it ends with either an 8 or 0.



      Is there an efficient way to do this, I am currently doing it as follows:



      xor r9, r9
      _R:
      add r9, 08h
      cmp r8, r9
      ja _R
      mov r8, r9






      x86-64 masm






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 1:59









      WillWill

      1256




      1256
























          1 Answer
          1






          active

          oldest

          votes


















          2














          To round a value up to a multiple of eight requires two statements and no loops:



          add r9, 7
          and r9, 0fffffffffffffff8H


          The first moves any value that's not already a multiple of eight into the "next highest section". The second rounds that down to a multiple of eight. So you'll get results like:



          orig  add  and
          0 7 0
          1 8 8
          : : :
          7 14 8
          8 15 8
          9 16 16


          Note that, if you want to round up even those numbers which are already multiples of eight (e.g., 8 -> 16), just add eight instead of seven.






          share|improve this answer


























          • I need it so that it always round up. With r9 = 11h, the and instruction results in r9 = 10h.

            – Will
            Nov 14 '18 at 2:10











          • @Will, suggest you try again, that's handled by the initial add: adding seven to 11h will give you 18h which will then and to 18h.

            – paxdiablo
            Nov 14 '18 at 2:12








          • 1





            Ahah yeah you're right, I was looking at the register window after the "add r9, 07h" and I saw 11h which was correctly round to 10h. The original value before the add was 0Ah. This works perfectly. I know there would be a smart way to do it! Thanks.

            – Will
            Nov 14 '18 at 2:14











          • Fun fact: you can write 0xfffffffffffffff8 as -8, which is portable to all the x86 assemblers. For MASM vs. GAS you have to choose between 0xfffffffffffffff8 and 0fffffffffffffff8H if you want to write it out that way.

            – Peter Cordes
            Nov 14 '18 at 2:55













          Your Answer






          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: "1"
          };
          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',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53292127%2fround-register-up-to-multiple-of-8%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









          2














          To round a value up to a multiple of eight requires two statements and no loops:



          add r9, 7
          and r9, 0fffffffffffffff8H


          The first moves any value that's not already a multiple of eight into the "next highest section". The second rounds that down to a multiple of eight. So you'll get results like:



          orig  add  and
          0 7 0
          1 8 8
          : : :
          7 14 8
          8 15 8
          9 16 16


          Note that, if you want to round up even those numbers which are already multiples of eight (e.g., 8 -> 16), just add eight instead of seven.






          share|improve this answer


























          • I need it so that it always round up. With r9 = 11h, the and instruction results in r9 = 10h.

            – Will
            Nov 14 '18 at 2:10











          • @Will, suggest you try again, that's handled by the initial add: adding seven to 11h will give you 18h which will then and to 18h.

            – paxdiablo
            Nov 14 '18 at 2:12








          • 1





            Ahah yeah you're right, I was looking at the register window after the "add r9, 07h" and I saw 11h which was correctly round to 10h. The original value before the add was 0Ah. This works perfectly. I know there would be a smart way to do it! Thanks.

            – Will
            Nov 14 '18 at 2:14











          • Fun fact: you can write 0xfffffffffffffff8 as -8, which is portable to all the x86 assemblers. For MASM vs. GAS you have to choose between 0xfffffffffffffff8 and 0fffffffffffffff8H if you want to write it out that way.

            – Peter Cordes
            Nov 14 '18 at 2:55


















          2














          To round a value up to a multiple of eight requires two statements and no loops:



          add r9, 7
          and r9, 0fffffffffffffff8H


          The first moves any value that's not already a multiple of eight into the "next highest section". The second rounds that down to a multiple of eight. So you'll get results like:



          orig  add  and
          0 7 0
          1 8 8
          : : :
          7 14 8
          8 15 8
          9 16 16


          Note that, if you want to round up even those numbers which are already multiples of eight (e.g., 8 -> 16), just add eight instead of seven.






          share|improve this answer


























          • I need it so that it always round up. With r9 = 11h, the and instruction results in r9 = 10h.

            – Will
            Nov 14 '18 at 2:10











          • @Will, suggest you try again, that's handled by the initial add: adding seven to 11h will give you 18h which will then and to 18h.

            – paxdiablo
            Nov 14 '18 at 2:12








          • 1





            Ahah yeah you're right, I was looking at the register window after the "add r9, 07h" and I saw 11h which was correctly round to 10h. The original value before the add was 0Ah. This works perfectly. I know there would be a smart way to do it! Thanks.

            – Will
            Nov 14 '18 at 2:14











          • Fun fact: you can write 0xfffffffffffffff8 as -8, which is portable to all the x86 assemblers. For MASM vs. GAS you have to choose between 0xfffffffffffffff8 and 0fffffffffffffff8H if you want to write it out that way.

            – Peter Cordes
            Nov 14 '18 at 2:55
















          2












          2








          2







          To round a value up to a multiple of eight requires two statements and no loops:



          add r9, 7
          and r9, 0fffffffffffffff8H


          The first moves any value that's not already a multiple of eight into the "next highest section". The second rounds that down to a multiple of eight. So you'll get results like:



          orig  add  and
          0 7 0
          1 8 8
          : : :
          7 14 8
          8 15 8
          9 16 16


          Note that, if you want to round up even those numbers which are already multiples of eight (e.g., 8 -> 16), just add eight instead of seven.






          share|improve this answer















          To round a value up to a multiple of eight requires two statements and no loops:



          add r9, 7
          and r9, 0fffffffffffffff8H


          The first moves any value that's not already a multiple of eight into the "next highest section". The second rounds that down to a multiple of eight. So you'll get results like:



          orig  add  and
          0 7 0
          1 8 8
          : : :
          7 14 8
          8 15 8
          9 16 16


          Note that, if you want to round up even those numbers which are already multiples of eight (e.g., 8 -> 16), just add eight instead of seven.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 2:54









          Peter Cordes

          123k17186314




          123k17186314










          answered Nov 14 '18 at 2:06









          paxdiablopaxdiablo

          632k17012451669




          632k17012451669













          • I need it so that it always round up. With r9 = 11h, the and instruction results in r9 = 10h.

            – Will
            Nov 14 '18 at 2:10











          • @Will, suggest you try again, that's handled by the initial add: adding seven to 11h will give you 18h which will then and to 18h.

            – paxdiablo
            Nov 14 '18 at 2:12








          • 1





            Ahah yeah you're right, I was looking at the register window after the "add r9, 07h" and I saw 11h which was correctly round to 10h. The original value before the add was 0Ah. This works perfectly. I know there would be a smart way to do it! Thanks.

            – Will
            Nov 14 '18 at 2:14











          • Fun fact: you can write 0xfffffffffffffff8 as -8, which is portable to all the x86 assemblers. For MASM vs. GAS you have to choose between 0xfffffffffffffff8 and 0fffffffffffffff8H if you want to write it out that way.

            – Peter Cordes
            Nov 14 '18 at 2:55





















          • I need it so that it always round up. With r9 = 11h, the and instruction results in r9 = 10h.

            – Will
            Nov 14 '18 at 2:10











          • @Will, suggest you try again, that's handled by the initial add: adding seven to 11h will give you 18h which will then and to 18h.

            – paxdiablo
            Nov 14 '18 at 2:12








          • 1





            Ahah yeah you're right, I was looking at the register window after the "add r9, 07h" and I saw 11h which was correctly round to 10h. The original value before the add was 0Ah. This works perfectly. I know there would be a smart way to do it! Thanks.

            – Will
            Nov 14 '18 at 2:14











          • Fun fact: you can write 0xfffffffffffffff8 as -8, which is portable to all the x86 assemblers. For MASM vs. GAS you have to choose between 0xfffffffffffffff8 and 0fffffffffffffff8H if you want to write it out that way.

            – Peter Cordes
            Nov 14 '18 at 2:55



















          I need it so that it always round up. With r9 = 11h, the and instruction results in r9 = 10h.

          – Will
          Nov 14 '18 at 2:10





          I need it so that it always round up. With r9 = 11h, the and instruction results in r9 = 10h.

          – Will
          Nov 14 '18 at 2:10













          @Will, suggest you try again, that's handled by the initial add: adding seven to 11h will give you 18h which will then and to 18h.

          – paxdiablo
          Nov 14 '18 at 2:12







          @Will, suggest you try again, that's handled by the initial add: adding seven to 11h will give you 18h which will then and to 18h.

          – paxdiablo
          Nov 14 '18 at 2:12






          1




          1





          Ahah yeah you're right, I was looking at the register window after the "add r9, 07h" and I saw 11h which was correctly round to 10h. The original value before the add was 0Ah. This works perfectly. I know there would be a smart way to do it! Thanks.

          – Will
          Nov 14 '18 at 2:14





          Ahah yeah you're right, I was looking at the register window after the "add r9, 07h" and I saw 11h which was correctly round to 10h. The original value before the add was 0Ah. This works perfectly. I know there would be a smart way to do it! Thanks.

          – Will
          Nov 14 '18 at 2:14













          Fun fact: you can write 0xfffffffffffffff8 as -8, which is portable to all the x86 assemblers. For MASM vs. GAS you have to choose between 0xfffffffffffffff8 and 0fffffffffffffff8H if you want to write it out that way.

          – Peter Cordes
          Nov 14 '18 at 2:55







          Fun fact: you can write 0xfffffffffffffff8 as -8, which is portable to all the x86 assemblers. For MASM vs. GAS you have to choose between 0xfffffffffffffff8 and 0fffffffffffffff8H if you want to write it out that way.

          – Peter Cordes
          Nov 14 '18 at 2:55




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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%2fstackoverflow.com%2fquestions%2f53292127%2fround-register-up-to-multiple-of-8%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

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python