Interpreting basic ARM instructions











up vote
1
down vote

favorite












I have been a set few questions, one of them is:




Which of these ARM instructions clears register r5, so that all of it's bits are set to ’0’?



and r5, r5, #0
eor r5, r5, r5
lsr r5, #32
sub r5, r5, r5



From my understanding sub r5, r5, r5 clears the register as it subtracts the number from itself. The and and eor ones clearly look wrong.



Does the lsr r5, #32 also clear the register? It shifts the r5 register by 32 bits, right? So, it makes sense for that instruction to clear the register too if it does.





Related to this, I also need to interpret this code:




What is the relationship between the contents of register r0 and register r1 when the following sequence of ARM instructions is executed?



    mov r0, #12
mov r1, #1
start:
cmp r0, #0
ble end
mul r1, r0, r1
sub r0, r0, #1
b start
end:



I am not entirely sure what the cmp r0, #0 does and if it changes the value of r0 in the end. I know that it compares the value.



So, from my understanding, after this code is run, mul r1, r0, r1 means that r1 is set to equal 12 as 1 * 12 = 12 (if the cmp r0, #0 doesn't affect the value of r0, which I don't know).



So, r1 is set to 12 - 1 = 11.



Can anyone clarify if I got the correct values for r0 (12) and r1 (11) after this code is run, and what exactly cmp r0, #0 and ble end does here and how it affects the register r0, if at all?










share|improve this question















migrated from unix.stackexchange.com Nov 23 at 11:26


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.



















    up vote
    1
    down vote

    favorite












    I have been a set few questions, one of them is:




    Which of these ARM instructions clears register r5, so that all of it's bits are set to ’0’?



    and r5, r5, #0
    eor r5, r5, r5
    lsr r5, #32
    sub r5, r5, r5



    From my understanding sub r5, r5, r5 clears the register as it subtracts the number from itself. The and and eor ones clearly look wrong.



    Does the lsr r5, #32 also clear the register? It shifts the r5 register by 32 bits, right? So, it makes sense for that instruction to clear the register too if it does.





    Related to this, I also need to interpret this code:




    What is the relationship between the contents of register r0 and register r1 when the following sequence of ARM instructions is executed?



        mov r0, #12
    mov r1, #1
    start:
    cmp r0, #0
    ble end
    mul r1, r0, r1
    sub r0, r0, #1
    b start
    end:



    I am not entirely sure what the cmp r0, #0 does and if it changes the value of r0 in the end. I know that it compares the value.



    So, from my understanding, after this code is run, mul r1, r0, r1 means that r1 is set to equal 12 as 1 * 12 = 12 (if the cmp r0, #0 doesn't affect the value of r0, which I don't know).



    So, r1 is set to 12 - 1 = 11.



    Can anyone clarify if I got the correct values for r0 (12) and r1 (11) after this code is run, and what exactly cmp r0, #0 and ble end does here and how it affects the register r0, if at all?










    share|improve this question















    migrated from unix.stackexchange.com Nov 23 at 11:26


    This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.

















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have been a set few questions, one of them is:




      Which of these ARM instructions clears register r5, so that all of it's bits are set to ’0’?



      and r5, r5, #0
      eor r5, r5, r5
      lsr r5, #32
      sub r5, r5, r5



      From my understanding sub r5, r5, r5 clears the register as it subtracts the number from itself. The and and eor ones clearly look wrong.



      Does the lsr r5, #32 also clear the register? It shifts the r5 register by 32 bits, right? So, it makes sense for that instruction to clear the register too if it does.





      Related to this, I also need to interpret this code:




      What is the relationship between the contents of register r0 and register r1 when the following sequence of ARM instructions is executed?



          mov r0, #12
      mov r1, #1
      start:
      cmp r0, #0
      ble end
      mul r1, r0, r1
      sub r0, r0, #1
      b start
      end:



      I am not entirely sure what the cmp r0, #0 does and if it changes the value of r0 in the end. I know that it compares the value.



      So, from my understanding, after this code is run, mul r1, r0, r1 means that r1 is set to equal 12 as 1 * 12 = 12 (if the cmp r0, #0 doesn't affect the value of r0, which I don't know).



      So, r1 is set to 12 - 1 = 11.



      Can anyone clarify if I got the correct values for r0 (12) and r1 (11) after this code is run, and what exactly cmp r0, #0 and ble end does here and how it affects the register r0, if at all?










      share|improve this question















      I have been a set few questions, one of them is:




      Which of these ARM instructions clears register r5, so that all of it's bits are set to ’0’?



      and r5, r5, #0
      eor r5, r5, r5
      lsr r5, #32
      sub r5, r5, r5



      From my understanding sub r5, r5, r5 clears the register as it subtracts the number from itself. The and and eor ones clearly look wrong.



      Does the lsr r5, #32 also clear the register? It shifts the r5 register by 32 bits, right? So, it makes sense for that instruction to clear the register too if it does.





      Related to this, I also need to interpret this code:




      What is the relationship between the contents of register r0 and register r1 when the following sequence of ARM instructions is executed?



          mov r0, #12
      mov r1, #1
      start:
      cmp r0, #0
      ble end
      mul r1, r0, r1
      sub r0, r0, #1
      b start
      end:



      I am not entirely sure what the cmp r0, #0 does and if it changes the value of r0 in the end. I know that it compares the value.



      So, from my understanding, after this code is run, mul r1, r0, r1 means that r1 is set to equal 12 as 1 * 12 = 12 (if the cmp r0, #0 doesn't affect the value of r0, which I don't know).



      So, r1 is set to 12 - 1 = 11.



      Can anyone clarify if I got the correct values for r0 (12) and r1 (11) after this code is run, and what exactly cmp r0, #0 and ble end does here and how it affects the register r0, if at all?







      linux arm assembly






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 at 15:50









      phuclv

      14.2k846208




      14.2k846208










      asked Nov 23 at 6:07









      Mandingo

      18219




      18219




      migrated from unix.stackexchange.com Nov 23 at 11:26


      This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.






      migrated from unix.stackexchange.com Nov 23 at 11:26


      This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.


























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          For the first part:




          Which of these ARM instructions clears register r5, so that all of its bits are set to ’0’?




          All of them!



          and r5, r5, #0


          That's the equivalent of r5 = r5 & 0, ANDing all bits with zero will clear the register.



          eor r5, r5, r5


          This is r5 = r5 ^ r5, the "exclusive OR" operation. XORing with itself also results in zero, since 1 ^ 1 is 0 too.



          lsr r5, #32


          As you mentioned, shifting all 32 bits right will zero them all out. The ARM specification even says for LSR:




          if the shift is 32, Rd is cleared, and the last bit shifted out remains in the C flag




          sub r5, r5, r5


          Again, r5 = r5 - r5 will zero it out.





          The code from the second part is doing this (in C code):



          int r0 = 12;
          int r1 = 1;
          while (r0 > 0)
          r1 *= r0--;


          So, at the end of the loop, r1 will have the product of the numbers 12, 11, 10... down to 1. In other words, this is calculating the factorial of r0 and storing it on r1.



          The cmp instruction is simply comparing r0 to the constant 0 and setting the flags, so that the ble (branch if less-or-equal) can act on it. The cmp instruction doesn't modify its operands. Both instructions together can be read as:



          if (r0 <= 0)
          goto end;





          share|improve this answer





















          • Thank you! I kind of get it now! :)
            – Mandingo
            Nov 23 at 7:19











          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',
          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%2f53445840%2finterpreting-basic-arm-instructions%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



          accepted










          For the first part:




          Which of these ARM instructions clears register r5, so that all of its bits are set to ’0’?




          All of them!



          and r5, r5, #0


          That's the equivalent of r5 = r5 & 0, ANDing all bits with zero will clear the register.



          eor r5, r5, r5


          This is r5 = r5 ^ r5, the "exclusive OR" operation. XORing with itself also results in zero, since 1 ^ 1 is 0 too.



          lsr r5, #32


          As you mentioned, shifting all 32 bits right will zero them all out. The ARM specification even says for LSR:




          if the shift is 32, Rd is cleared, and the last bit shifted out remains in the C flag




          sub r5, r5, r5


          Again, r5 = r5 - r5 will zero it out.





          The code from the second part is doing this (in C code):



          int r0 = 12;
          int r1 = 1;
          while (r0 > 0)
          r1 *= r0--;


          So, at the end of the loop, r1 will have the product of the numbers 12, 11, 10... down to 1. In other words, this is calculating the factorial of r0 and storing it on r1.



          The cmp instruction is simply comparing r0 to the constant 0 and setting the flags, so that the ble (branch if less-or-equal) can act on it. The cmp instruction doesn't modify its operands. Both instructions together can be read as:



          if (r0 <= 0)
          goto end;





          share|improve this answer





















          • Thank you! I kind of get it now! :)
            – Mandingo
            Nov 23 at 7:19















          up vote
          1
          down vote



          accepted










          For the first part:




          Which of these ARM instructions clears register r5, so that all of its bits are set to ’0’?




          All of them!



          and r5, r5, #0


          That's the equivalent of r5 = r5 & 0, ANDing all bits with zero will clear the register.



          eor r5, r5, r5


          This is r5 = r5 ^ r5, the "exclusive OR" operation. XORing with itself also results in zero, since 1 ^ 1 is 0 too.



          lsr r5, #32


          As you mentioned, shifting all 32 bits right will zero them all out. The ARM specification even says for LSR:




          if the shift is 32, Rd is cleared, and the last bit shifted out remains in the C flag




          sub r5, r5, r5


          Again, r5 = r5 - r5 will zero it out.





          The code from the second part is doing this (in C code):



          int r0 = 12;
          int r1 = 1;
          while (r0 > 0)
          r1 *= r0--;


          So, at the end of the loop, r1 will have the product of the numbers 12, 11, 10... down to 1. In other words, this is calculating the factorial of r0 and storing it on r1.



          The cmp instruction is simply comparing r0 to the constant 0 and setting the flags, so that the ble (branch if less-or-equal) can act on it. The cmp instruction doesn't modify its operands. Both instructions together can be read as:



          if (r0 <= 0)
          goto end;





          share|improve this answer





















          • Thank you! I kind of get it now! :)
            – Mandingo
            Nov 23 at 7:19













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          For the first part:




          Which of these ARM instructions clears register r5, so that all of its bits are set to ’0’?




          All of them!



          and r5, r5, #0


          That's the equivalent of r5 = r5 & 0, ANDing all bits with zero will clear the register.



          eor r5, r5, r5


          This is r5 = r5 ^ r5, the "exclusive OR" operation. XORing with itself also results in zero, since 1 ^ 1 is 0 too.



          lsr r5, #32


          As you mentioned, shifting all 32 bits right will zero them all out. The ARM specification even says for LSR:




          if the shift is 32, Rd is cleared, and the last bit shifted out remains in the C flag




          sub r5, r5, r5


          Again, r5 = r5 - r5 will zero it out.





          The code from the second part is doing this (in C code):



          int r0 = 12;
          int r1 = 1;
          while (r0 > 0)
          r1 *= r0--;


          So, at the end of the loop, r1 will have the product of the numbers 12, 11, 10... down to 1. In other words, this is calculating the factorial of r0 and storing it on r1.



          The cmp instruction is simply comparing r0 to the constant 0 and setting the flags, so that the ble (branch if less-or-equal) can act on it. The cmp instruction doesn't modify its operands. Both instructions together can be read as:



          if (r0 <= 0)
          goto end;





          share|improve this answer












          For the first part:




          Which of these ARM instructions clears register r5, so that all of its bits are set to ’0’?




          All of them!



          and r5, r5, #0


          That's the equivalent of r5 = r5 & 0, ANDing all bits with zero will clear the register.



          eor r5, r5, r5


          This is r5 = r5 ^ r5, the "exclusive OR" operation. XORing with itself also results in zero, since 1 ^ 1 is 0 too.



          lsr r5, #32


          As you mentioned, shifting all 32 bits right will zero them all out. The ARM specification even says for LSR:




          if the shift is 32, Rd is cleared, and the last bit shifted out remains in the C flag




          sub r5, r5, r5


          Again, r5 = r5 - r5 will zero it out.





          The code from the second part is doing this (in C code):



          int r0 = 12;
          int r1 = 1;
          while (r0 > 0)
          r1 *= r0--;


          So, at the end of the loop, r1 will have the product of the numbers 12, 11, 10... down to 1. In other words, this is calculating the factorial of r0 and storing it on r1.



          The cmp instruction is simply comparing r0 to the constant 0 and setting the flags, so that the ble (branch if less-or-equal) can act on it. The cmp instruction doesn't modify its operands. Both instructions together can be read as:



          if (r0 <= 0)
          goto end;






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 at 6:31









          Filipe Brandenburger

          469110




          469110












          • Thank you! I kind of get it now! :)
            – Mandingo
            Nov 23 at 7:19


















          • Thank you! I kind of get it now! :)
            – Mandingo
            Nov 23 at 7:19
















          Thank you! I kind of get it now! :)
          – Mandingo
          Nov 23 at 7:19




          Thank you! I kind of get it now! :)
          – Mandingo
          Nov 23 at 7:19


















          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.





          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%2fstackoverflow.com%2fquestions%2f53445840%2finterpreting-basic-arm-instructions%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

          Morgemoulin

          Scott Moir

          Souastre