What's the difference between MOV, MOVZ, MOVN and MOVK in ARMv8 assembly?











up vote
0
down vote

favorite












I was using a disassembler when I came across MOVZ and was a bit confused, since I had only used MOV before.



The ARMv8 ISA manual explains of course all the details, and that MOV is an alias for the other three depending on the context, but maybe someone can provide some rationale here, and give concrete examples to speedup the learning process.










share|improve this question






















  • What's your manual? infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0802a/… describes the differences in quite a short way, so I'm kind of confused what you are looking for.
    – domen
    Nov 13 at 8:19










  • @domen developer.arm.com/docs/ddi0487/latest/… seems a bit better. But I'm guessing it must be there somewhere.
    – Ciro Santilli
    Nov 13 at 9:42












  • I think I don't understand your question. movz - Move shifted 16-bit immediate to register. Then: movn - Move inverse of shifted 16-bit immediate to register. How much shorter do you want it?
    – domen
    Nov 13 at 10:00










  • @domen one could explain the encoding of immediate to new users I think, and show why we need the three encoding types.
    – Ciro Santilli
    Nov 13 at 10:36










  • @Close voters: can you explain rationale? There are thousands of assembly questions on SO, so it seems like a well established topic. You should likely open a meta post instead to decide once and for all for all of them instead of close-voting one by one.
    – Ciro Santilli
    Nov 16 at 7:01

















up vote
0
down vote

favorite












I was using a disassembler when I came across MOVZ and was a bit confused, since I had only used MOV before.



The ARMv8 ISA manual explains of course all the details, and that MOV is an alias for the other three depending on the context, but maybe someone can provide some rationale here, and give concrete examples to speedup the learning process.










share|improve this question






















  • What's your manual? infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0802a/… describes the differences in quite a short way, so I'm kind of confused what you are looking for.
    – domen
    Nov 13 at 8:19










  • @domen developer.arm.com/docs/ddi0487/latest/… seems a bit better. But I'm guessing it must be there somewhere.
    – Ciro Santilli
    Nov 13 at 9:42












  • I think I don't understand your question. movz - Move shifted 16-bit immediate to register. Then: movn - Move inverse of shifted 16-bit immediate to register. How much shorter do you want it?
    – domen
    Nov 13 at 10:00










  • @domen one could explain the encoding of immediate to new users I think, and show why we need the three encoding types.
    – Ciro Santilli
    Nov 13 at 10:36










  • @Close voters: can you explain rationale? There are thousands of assembly questions on SO, so it seems like a well established topic. You should likely open a meta post instead to decide once and for all for all of them instead of close-voting one by one.
    – Ciro Santilli
    Nov 16 at 7:01















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I was using a disassembler when I came across MOVZ and was a bit confused, since I had only used MOV before.



The ARMv8 ISA manual explains of course all the details, and that MOV is an alias for the other three depending on the context, but maybe someone can provide some rationale here, and give concrete examples to speedup the learning process.










share|improve this question













I was using a disassembler when I came across MOVZ and was a bit confused, since I had only used MOV before.



The ARMv8 ISA manual explains of course all the details, and that MOV is an alias for the other three depending on the context, but maybe someone can provide some rationale here, and give concrete examples to speedup the learning process.







arm armv8






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 18:34









Ciro Santilli

52214




52214












  • What's your manual? infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0802a/… describes the differences in quite a short way, so I'm kind of confused what you are looking for.
    – domen
    Nov 13 at 8:19










  • @domen developer.arm.com/docs/ddi0487/latest/… seems a bit better. But I'm guessing it must be there somewhere.
    – Ciro Santilli
    Nov 13 at 9:42












  • I think I don't understand your question. movz - Move shifted 16-bit immediate to register. Then: movn - Move inverse of shifted 16-bit immediate to register. How much shorter do you want it?
    – domen
    Nov 13 at 10:00










  • @domen one could explain the encoding of immediate to new users I think, and show why we need the three encoding types.
    – Ciro Santilli
    Nov 13 at 10:36










  • @Close voters: can you explain rationale? There are thousands of assembly questions on SO, so it seems like a well established topic. You should likely open a meta post instead to decide once and for all for all of them instead of close-voting one by one.
    – Ciro Santilli
    Nov 16 at 7:01




















  • What's your manual? infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0802a/… describes the differences in quite a short way, so I'm kind of confused what you are looking for.
    – domen
    Nov 13 at 8:19










  • @domen developer.arm.com/docs/ddi0487/latest/… seems a bit better. But I'm guessing it must be there somewhere.
    – Ciro Santilli
    Nov 13 at 9:42












  • I think I don't understand your question. movz - Move shifted 16-bit immediate to register. Then: movn - Move inverse of shifted 16-bit immediate to register. How much shorter do you want it?
    – domen
    Nov 13 at 10:00










  • @domen one could explain the encoding of immediate to new users I think, and show why we need the three encoding types.
    – Ciro Santilli
    Nov 13 at 10:36










  • @Close voters: can you explain rationale? There are thousands of assembly questions on SO, so it seems like a well established topic. You should likely open a meta post instead to decide once and for all for all of them instead of close-voting one by one.
    – Ciro Santilli
    Nov 16 at 7:01


















What's your manual? infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0802a/… describes the differences in quite a short way, so I'm kind of confused what you are looking for.
– domen
Nov 13 at 8:19




What's your manual? infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0802a/… describes the differences in quite a short way, so I'm kind of confused what you are looking for.
– domen
Nov 13 at 8:19












@domen developer.arm.com/docs/ddi0487/latest/… seems a bit better. But I'm guessing it must be there somewhere.
– Ciro Santilli
Nov 13 at 9:42






@domen developer.arm.com/docs/ddi0487/latest/… seems a bit better. But I'm guessing it must be there somewhere.
– Ciro Santilli
Nov 13 at 9:42














I think I don't understand your question. movz - Move shifted 16-bit immediate to register. Then: movn - Move inverse of shifted 16-bit immediate to register. How much shorter do you want it?
– domen
Nov 13 at 10:00




I think I don't understand your question. movz - Move shifted 16-bit immediate to register. Then: movn - Move inverse of shifted 16-bit immediate to register. How much shorter do you want it?
– domen
Nov 13 at 10:00












@domen one could explain the encoding of immediate to new users I think, and show why we need the three encoding types.
– Ciro Santilli
Nov 13 at 10:36




@domen one could explain the encoding of immediate to new users I think, and show why we need the three encoding types.
– Ciro Santilli
Nov 13 at 10:36












@Close voters: can you explain rationale? There are thousands of assembly questions on SO, so it seems like a well established topic. You should likely open a meta post instead to decide once and for all for all of them instead of close-voting one by one.
– Ciro Santilli
Nov 16 at 7:01






@Close voters: can you explain rationale? There are thousands of assembly questions on SO, so it seems like a well established topic. You should likely open a meta post instead to decide once and for all for all of them instead of close-voting one by one.
– Ciro Santilli
Nov 16 at 7:01














1 Answer
1






active

oldest

votes

















up vote
2
down vote













MOV



This instruction can take many forms, depending on the value needed to be moved. Changes if the value is an register or an immediate. If it is in a register then it produces an ORR instruction (ORR <Xd>, XZR, <Wm>)* and if it is using the SP (Stack Pointer) it produces an ADD (ADD <Xd|XSP>, <Xn|XSP>, #0)*. If you are moving an immediate, then is using one of the MOVZ, MOVK or MOVN instruction.



*These are the 64-bit prototypes.



MOVZ and MOVK



These two are sometimes used one after the other. And they are used to move immediates.
MOVZ moves an immediate value (16-bit value) to a register, and all the other bits outside the immediate value are set to Zero. The immediate can be shift to the left 0, 16, 32 or 48. MOVK moves and immediate value but leaves the other bits of the register untouched. For example, let's say you need to move this value 0x7fb7fb1f88 to register X0. First, you will move the first 16 bits (bit 0 to 15) with a MOVZ instruction, so the rest of the register is set to zero. And then you will move the second 16 bits (bit 16 to 31) with a MOVK instruction, so the value moved before (the first 16 bits) remains in the register, and you do the same with the other resting bits.



      instruction                     value of x0
mov x0, #0x1f88 | 0x1f88
movk x0, #0xb7fb, lsl #16 | 0xb7fb1f88
movk x0, #0x7f, lsl #32 | 0x7fb7fb1f88


MOVN



MOVN is usually used for moving bitmasks, let's say you want to move the bitmask 0xffffffff0000ffff to x0, then you will move 0xffff shift to the left 16, that will make the value 0x00000000fffff0000, but if you negate this value it becomes 0xffffffff0000ffff.



Here is an example:



      instruction                     value of x0
MOVN x0, 0xFFFF, lsl 16 | 0xffffffff0000ffff





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',
    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%2f53268118%2fwhats-the-difference-between-mov-movz-movn-and-movk-in-armv8-assembly%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
    2
    down vote













    MOV



    This instruction can take many forms, depending on the value needed to be moved. Changes if the value is an register or an immediate. If it is in a register then it produces an ORR instruction (ORR <Xd>, XZR, <Wm>)* and if it is using the SP (Stack Pointer) it produces an ADD (ADD <Xd|XSP>, <Xn|XSP>, #0)*. If you are moving an immediate, then is using one of the MOVZ, MOVK or MOVN instruction.



    *These are the 64-bit prototypes.



    MOVZ and MOVK



    These two are sometimes used one after the other. And they are used to move immediates.
    MOVZ moves an immediate value (16-bit value) to a register, and all the other bits outside the immediate value are set to Zero. The immediate can be shift to the left 0, 16, 32 or 48. MOVK moves and immediate value but leaves the other bits of the register untouched. For example, let's say you need to move this value 0x7fb7fb1f88 to register X0. First, you will move the first 16 bits (bit 0 to 15) with a MOVZ instruction, so the rest of the register is set to zero. And then you will move the second 16 bits (bit 16 to 31) with a MOVK instruction, so the value moved before (the first 16 bits) remains in the register, and you do the same with the other resting bits.



          instruction                     value of x0
    mov x0, #0x1f88 | 0x1f88
    movk x0, #0xb7fb, lsl #16 | 0xb7fb1f88
    movk x0, #0x7f, lsl #32 | 0x7fb7fb1f88


    MOVN



    MOVN is usually used for moving bitmasks, let's say you want to move the bitmask 0xffffffff0000ffff to x0, then you will move 0xffff shift to the left 16, that will make the value 0x00000000fffff0000, but if you negate this value it becomes 0xffffffff0000ffff.



    Here is an example:



          instruction                     value of x0
    MOVN x0, 0xFFFF, lsl 16 | 0xffffffff0000ffff





    share|improve this answer

























      up vote
      2
      down vote













      MOV



      This instruction can take many forms, depending on the value needed to be moved. Changes if the value is an register or an immediate. If it is in a register then it produces an ORR instruction (ORR <Xd>, XZR, <Wm>)* and if it is using the SP (Stack Pointer) it produces an ADD (ADD <Xd|XSP>, <Xn|XSP>, #0)*. If you are moving an immediate, then is using one of the MOVZ, MOVK or MOVN instruction.



      *These are the 64-bit prototypes.



      MOVZ and MOVK



      These two are sometimes used one after the other. And they are used to move immediates.
      MOVZ moves an immediate value (16-bit value) to a register, and all the other bits outside the immediate value are set to Zero. The immediate can be shift to the left 0, 16, 32 or 48. MOVK moves and immediate value but leaves the other bits of the register untouched. For example, let's say you need to move this value 0x7fb7fb1f88 to register X0. First, you will move the first 16 bits (bit 0 to 15) with a MOVZ instruction, so the rest of the register is set to zero. And then you will move the second 16 bits (bit 16 to 31) with a MOVK instruction, so the value moved before (the first 16 bits) remains in the register, and you do the same with the other resting bits.



            instruction                     value of x0
      mov x0, #0x1f88 | 0x1f88
      movk x0, #0xb7fb, lsl #16 | 0xb7fb1f88
      movk x0, #0x7f, lsl #32 | 0x7fb7fb1f88


      MOVN



      MOVN is usually used for moving bitmasks, let's say you want to move the bitmask 0xffffffff0000ffff to x0, then you will move 0xffff shift to the left 16, that will make the value 0x00000000fffff0000, but if you negate this value it becomes 0xffffffff0000ffff.



      Here is an example:



            instruction                     value of x0
      MOVN x0, 0xFFFF, lsl 16 | 0xffffffff0000ffff





      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        MOV



        This instruction can take many forms, depending on the value needed to be moved. Changes if the value is an register or an immediate. If it is in a register then it produces an ORR instruction (ORR <Xd>, XZR, <Wm>)* and if it is using the SP (Stack Pointer) it produces an ADD (ADD <Xd|XSP>, <Xn|XSP>, #0)*. If you are moving an immediate, then is using one of the MOVZ, MOVK or MOVN instruction.



        *These are the 64-bit prototypes.



        MOVZ and MOVK



        These two are sometimes used one after the other. And they are used to move immediates.
        MOVZ moves an immediate value (16-bit value) to a register, and all the other bits outside the immediate value are set to Zero. The immediate can be shift to the left 0, 16, 32 or 48. MOVK moves and immediate value but leaves the other bits of the register untouched. For example, let's say you need to move this value 0x7fb7fb1f88 to register X0. First, you will move the first 16 bits (bit 0 to 15) with a MOVZ instruction, so the rest of the register is set to zero. And then you will move the second 16 bits (bit 16 to 31) with a MOVK instruction, so the value moved before (the first 16 bits) remains in the register, and you do the same with the other resting bits.



              instruction                     value of x0
        mov x0, #0x1f88 | 0x1f88
        movk x0, #0xb7fb, lsl #16 | 0xb7fb1f88
        movk x0, #0x7f, lsl #32 | 0x7fb7fb1f88


        MOVN



        MOVN is usually used for moving bitmasks, let's say you want to move the bitmask 0xffffffff0000ffff to x0, then you will move 0xffff shift to the left 16, that will make the value 0x00000000fffff0000, but if you negate this value it becomes 0xffffffff0000ffff.



        Here is an example:



              instruction                     value of x0
        MOVN x0, 0xFFFF, lsl 16 | 0xffffffff0000ffff





        share|improve this answer












        MOV



        This instruction can take many forms, depending on the value needed to be moved. Changes if the value is an register or an immediate. If it is in a register then it produces an ORR instruction (ORR <Xd>, XZR, <Wm>)* and if it is using the SP (Stack Pointer) it produces an ADD (ADD <Xd|XSP>, <Xn|XSP>, #0)*. If you are moving an immediate, then is using one of the MOVZ, MOVK or MOVN instruction.



        *These are the 64-bit prototypes.



        MOVZ and MOVK



        These two are sometimes used one after the other. And they are used to move immediates.
        MOVZ moves an immediate value (16-bit value) to a register, and all the other bits outside the immediate value are set to Zero. The immediate can be shift to the left 0, 16, 32 or 48. MOVK moves and immediate value but leaves the other bits of the register untouched. For example, let's say you need to move this value 0x7fb7fb1f88 to register X0. First, you will move the first 16 bits (bit 0 to 15) with a MOVZ instruction, so the rest of the register is set to zero. And then you will move the second 16 bits (bit 16 to 31) with a MOVK instruction, so the value moved before (the first 16 bits) remains in the register, and you do the same with the other resting bits.



              instruction                     value of x0
        mov x0, #0x1f88 | 0x1f88
        movk x0, #0xb7fb, lsl #16 | 0xb7fb1f88
        movk x0, #0x7f, lsl #32 | 0x7fb7fb1f88


        MOVN



        MOVN is usually used for moving bitmasks, let's say you want to move the bitmask 0xffffffff0000ffff to x0, then you will move 0xffff shift to the left 16, that will make the value 0x00000000fffff0000, but if you negate this value it becomes 0xffffffff0000ffff.



        Here is an example:



              instruction                     value of x0
        MOVN x0, 0xFFFF, lsl 16 | 0xffffffff0000ffff






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 at 11:50









        Guillermo

        40336




        40336






























            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%2f53268118%2fwhats-the-difference-between-mov-movz-movn-and-movk-in-armv8-assembly%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

            Guess what letter conforming each word

            Port of Spain

            Run scheduled task as local user group (not BUILTIN)