MIPS jump and BNE address calculation











up vote
1
down vote

favorite












Suppose the program counter (PC) is set to 0x20000000.Is it possible to use the jump (j) MIPS assembly instruction to set the PC to the address 0x40000000? Is it possible to use the branch-on-equal (beq) MIPS assembly instruction to set the PC to this same address?










share|improve this question









New contributor




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
























    up vote
    1
    down vote

    favorite












    Suppose the program counter (PC) is set to 0x20000000.Is it possible to use the jump (j) MIPS assembly instruction to set the PC to the address 0x40000000? Is it possible to use the branch-on-equal (beq) MIPS assembly instruction to set the PC to this same address?










    share|improve this question









    New contributor




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






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      Suppose the program counter (PC) is set to 0x20000000.Is it possible to use the jump (j) MIPS assembly instruction to set the PC to the address 0x40000000? Is it possible to use the branch-on-equal (beq) MIPS assembly instruction to set the PC to this same address?










      share|improve this question









      New contributor




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











      Suppose the program counter (PC) is set to 0x20000000.Is it possible to use the jump (j) MIPS assembly instruction to set the PC to the address 0x40000000? Is it possible to use the branch-on-equal (beq) MIPS assembly instruction to set the PC to this same address?







      mips cpu-architecture mips32






      share|improve this question









      New contributor




      Hasin Sadique 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




      Hasin Sadique 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 Nov 21 at 16:50









      Peter Cordes

      116k16176302




      116k16176302






      New contributor




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









      asked Nov 21 at 13:44









      Hasin Sadique

      61




      61




      New contributor




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





      New contributor





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






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
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote













          I believe you could use jr to jump to a 32bit address



          li $t0, 0x40000000
          jr $t0


          Similarly you could use a branch and combine it with the above to perform this jump.



          The reason why BNE is not suited for this is that it only operates on 16-bit offsets due to how instructions are encoded.



          0001 01ss ssst tttt iiii iiii iiii iiii represents a BNE instruction where sssss and ttttt are the compared registers and iiii iiii iiii iiii the 16-bit offset (twos-complement to allow backwards offset as well).
          This means that the 0x20000000 offset cannot be expressed in the 16-bits provided by this encoding.



          The jump instruction behaves a bit differently in the way it uses the current PC to calculate the destination address. This is done by concatenating the first 6 bits of the current PC (which would be 0010 00 in this case) together with the destination stored in the immediate part of the encoding (which would be 26-bits filled with 0). Thus the resulting address can only be 0x20000000.



          Jump-Register (JR) on the other hand allows to jump to full 32-bit addresses since it uses a register for the destination address and is not bound by the aforementioned instruction limitations.






          share|improve this answer










          New contributor




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














          • 1




            Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
            – Jevgeni Geurtsen
            Nov 21 at 14:16






          • 1




            Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
            – Minn
            Nov 21 at 17:11






          • 1




            Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
            – Jevgeni Geurtsen
            Nov 21 at 18:21


















          up vote
          1
          down vote













          The documentation states the following in the programming notes section of the BEQ-instruction:




          Description: if rs = rt then branch An 18-bit signed offset (the
          16-bit offset field shifted left 2 bits) is added to the address of
          the instruction following the branch (not the branch itself), in the
          branch delay slot, to form a PC-relative effective target address.



          Programming Notes:
          With the 18-bit signed instruction offset, the
          conditional branch range is ± 128 Kbytes. Use jump (J) or jump
          register (JR) instructions to branch to addresses outside this range.




          So you cannot use the BEQ instruction to jump to 0x40000000 (starting from 0x20000000 it would have to jump over 0x20000000 = 536870912b = 536871kb). Thus you must use the jump register instructions to jump to 0x40000000.






          share|improve this answer





















            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
            });


            }
            });






            Hasin Sadique 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%2fstackoverflow.com%2fquestions%2f53413461%2fmips-jump-and-bne-address-calculation%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            I believe you could use jr to jump to a 32bit address



            li $t0, 0x40000000
            jr $t0


            Similarly you could use a branch and combine it with the above to perform this jump.



            The reason why BNE is not suited for this is that it only operates on 16-bit offsets due to how instructions are encoded.



            0001 01ss ssst tttt iiii iiii iiii iiii represents a BNE instruction where sssss and ttttt are the compared registers and iiii iiii iiii iiii the 16-bit offset (twos-complement to allow backwards offset as well).
            This means that the 0x20000000 offset cannot be expressed in the 16-bits provided by this encoding.



            The jump instruction behaves a bit differently in the way it uses the current PC to calculate the destination address. This is done by concatenating the first 6 bits of the current PC (which would be 0010 00 in this case) together with the destination stored in the immediate part of the encoding (which would be 26-bits filled with 0). Thus the resulting address can only be 0x20000000.



            Jump-Register (JR) on the other hand allows to jump to full 32-bit addresses since it uses a register for the destination address and is not bound by the aforementioned instruction limitations.






            share|improve this answer










            New contributor




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














            • 1




              Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
              – Jevgeni Geurtsen
              Nov 21 at 14:16






            • 1




              Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
              – Minn
              Nov 21 at 17:11






            • 1




              Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
              – Jevgeni Geurtsen
              Nov 21 at 18:21















            up vote
            3
            down vote













            I believe you could use jr to jump to a 32bit address



            li $t0, 0x40000000
            jr $t0


            Similarly you could use a branch and combine it with the above to perform this jump.



            The reason why BNE is not suited for this is that it only operates on 16-bit offsets due to how instructions are encoded.



            0001 01ss ssst tttt iiii iiii iiii iiii represents a BNE instruction where sssss and ttttt are the compared registers and iiii iiii iiii iiii the 16-bit offset (twos-complement to allow backwards offset as well).
            This means that the 0x20000000 offset cannot be expressed in the 16-bits provided by this encoding.



            The jump instruction behaves a bit differently in the way it uses the current PC to calculate the destination address. This is done by concatenating the first 6 bits of the current PC (which would be 0010 00 in this case) together with the destination stored in the immediate part of the encoding (which would be 26-bits filled with 0). Thus the resulting address can only be 0x20000000.



            Jump-Register (JR) on the other hand allows to jump to full 32-bit addresses since it uses a register for the destination address and is not bound by the aforementioned instruction limitations.






            share|improve this answer










            New contributor




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














            • 1




              Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
              – Jevgeni Geurtsen
              Nov 21 at 14:16






            • 1




              Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
              – Minn
              Nov 21 at 17:11






            • 1




              Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
              – Jevgeni Geurtsen
              Nov 21 at 18:21













            up vote
            3
            down vote










            up vote
            3
            down vote









            I believe you could use jr to jump to a 32bit address



            li $t0, 0x40000000
            jr $t0


            Similarly you could use a branch and combine it with the above to perform this jump.



            The reason why BNE is not suited for this is that it only operates on 16-bit offsets due to how instructions are encoded.



            0001 01ss ssst tttt iiii iiii iiii iiii represents a BNE instruction where sssss and ttttt are the compared registers and iiii iiii iiii iiii the 16-bit offset (twos-complement to allow backwards offset as well).
            This means that the 0x20000000 offset cannot be expressed in the 16-bits provided by this encoding.



            The jump instruction behaves a bit differently in the way it uses the current PC to calculate the destination address. This is done by concatenating the first 6 bits of the current PC (which would be 0010 00 in this case) together with the destination stored in the immediate part of the encoding (which would be 26-bits filled with 0). Thus the resulting address can only be 0x20000000.



            Jump-Register (JR) on the other hand allows to jump to full 32-bit addresses since it uses a register for the destination address and is not bound by the aforementioned instruction limitations.






            share|improve this answer










            New contributor




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









            I believe you could use jr to jump to a 32bit address



            li $t0, 0x40000000
            jr $t0


            Similarly you could use a branch and combine it with the above to perform this jump.



            The reason why BNE is not suited for this is that it only operates on 16-bit offsets due to how instructions are encoded.



            0001 01ss ssst tttt iiii iiii iiii iiii represents a BNE instruction where sssss and ttttt are the compared registers and iiii iiii iiii iiii the 16-bit offset (twos-complement to allow backwards offset as well).
            This means that the 0x20000000 offset cannot be expressed in the 16-bits provided by this encoding.



            The jump instruction behaves a bit differently in the way it uses the current PC to calculate the destination address. This is done by concatenating the first 6 bits of the current PC (which would be 0010 00 in this case) together with the destination stored in the immediate part of the encoding (which would be 26-bits filled with 0). Thus the resulting address can only be 0x20000000.



            Jump-Register (JR) on the other hand allows to jump to full 32-bit addresses since it uses a register for the destination address and is not bound by the aforementioned instruction limitations.







            share|improve this answer










            New contributor




            Minn 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 answer



            share|improve this answer








            edited Nov 21 at 17:57





















            New contributor




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









            answered Nov 21 at 13:49









            Minn

            948




            948




            New contributor




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





            New contributor





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






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








            • 1




              Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
              – Jevgeni Geurtsen
              Nov 21 at 14:16






            • 1




              Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
              – Minn
              Nov 21 at 17:11






            • 1




              Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
              – Jevgeni Geurtsen
              Nov 21 at 18:21














            • 1




              Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
              – Jevgeni Geurtsen
              Nov 21 at 14:16






            • 1




              Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
              – Minn
              Nov 21 at 17:11






            • 1




              Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
              – Jevgeni Geurtsen
              Nov 21 at 18:21








            1




            1




            Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
            – Jevgeni Geurtsen
            Nov 21 at 14:16




            Thanks for posting the solution :) Maybe you could elaborate more on why the jump instruction must be used instead of the branch jump instruction?
            – Jevgeni Geurtsen
            Nov 21 at 14:16




            1




            1




            Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
            – Minn
            Nov 21 at 17:11




            Sure thing, I just had to look up some specifications about instruction formats first. I hope this is now sufficient.
            – Minn
            Nov 21 at 17:11




            1




            1




            Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
            – Jevgeni Geurtsen
            Nov 21 at 18:21




            Awesome edit Minn! The answer itself is usually enough, but some clarification is nice to have for further reference and new readers with little knowledge about MIPS. I learned some things while reading your explanation. Keep up the good work!
            – Jevgeni Geurtsen
            Nov 21 at 18:21












            up vote
            1
            down vote













            The documentation states the following in the programming notes section of the BEQ-instruction:




            Description: if rs = rt then branch An 18-bit signed offset (the
            16-bit offset field shifted left 2 bits) is added to the address of
            the instruction following the branch (not the branch itself), in the
            branch delay slot, to form a PC-relative effective target address.



            Programming Notes:
            With the 18-bit signed instruction offset, the
            conditional branch range is ± 128 Kbytes. Use jump (J) or jump
            register (JR) instructions to branch to addresses outside this range.




            So you cannot use the BEQ instruction to jump to 0x40000000 (starting from 0x20000000 it would have to jump over 0x20000000 = 536870912b = 536871kb). Thus you must use the jump register instructions to jump to 0x40000000.






            share|improve this answer

























              up vote
              1
              down vote













              The documentation states the following in the programming notes section of the BEQ-instruction:




              Description: if rs = rt then branch An 18-bit signed offset (the
              16-bit offset field shifted left 2 bits) is added to the address of
              the instruction following the branch (not the branch itself), in the
              branch delay slot, to form a PC-relative effective target address.



              Programming Notes:
              With the 18-bit signed instruction offset, the
              conditional branch range is ± 128 Kbytes. Use jump (J) or jump
              register (JR) instructions to branch to addresses outside this range.




              So you cannot use the BEQ instruction to jump to 0x40000000 (starting from 0x20000000 it would have to jump over 0x20000000 = 536870912b = 536871kb). Thus you must use the jump register instructions to jump to 0x40000000.






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote









                The documentation states the following in the programming notes section of the BEQ-instruction:




                Description: if rs = rt then branch An 18-bit signed offset (the
                16-bit offset field shifted left 2 bits) is added to the address of
                the instruction following the branch (not the branch itself), in the
                branch delay slot, to form a PC-relative effective target address.



                Programming Notes:
                With the 18-bit signed instruction offset, the
                conditional branch range is ± 128 Kbytes. Use jump (J) or jump
                register (JR) instructions to branch to addresses outside this range.




                So you cannot use the BEQ instruction to jump to 0x40000000 (starting from 0x20000000 it would have to jump over 0x20000000 = 536870912b = 536871kb). Thus you must use the jump register instructions to jump to 0x40000000.






                share|improve this answer












                The documentation states the following in the programming notes section of the BEQ-instruction:




                Description: if rs = rt then branch An 18-bit signed offset (the
                16-bit offset field shifted left 2 bits) is added to the address of
                the instruction following the branch (not the branch itself), in the
                branch delay slot, to form a PC-relative effective target address.



                Programming Notes:
                With the 18-bit signed instruction offset, the
                conditional branch range is ± 128 Kbytes. Use jump (J) or jump
                register (JR) instructions to branch to addresses outside this range.




                So you cannot use the BEQ instruction to jump to 0x40000000 (starting from 0x20000000 it would have to jump over 0x20000000 = 536870912b = 536871kb). Thus you must use the jump register instructions to jump to 0x40000000.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 21 at 13:57









                Jevgeni Geurtsen

                2,54641231




                2,54641231






















                    Hasin Sadique is a new contributor. Be nice, and check out our Code of Conduct.










                     

                    draft saved


                    draft discarded


















                    Hasin Sadique is a new contributor. Be nice, and check out our Code of Conduct.













                    Hasin Sadique is a new contributor. Be nice, and check out our Code of Conduct.












                    Hasin Sadique 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%2fstackoverflow.com%2fquestions%2f53413461%2fmips-jump-and-bne-address-calculation%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

                    Contact image not getting when fetch all contact list from iPhone by CNContact

                    count number of partitions of a set with n elements into k subsets

                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks