String routinesString BuildingIterating string characters and passing them to a BIOS APIString length calculation implementation in Assembly (NASM)Check whether string ends with stringConvert string array to string with separatorAssembly 8086 program to input a 16-bit signed number (in string form) and output its binary equivalentE820 display using X86 legacy boot sector or DOS 6.22 com fileMIPS assembly string to int functionPrinting binary string in assemblystring length in x64 assembly (fasm)

Are there any cons in using rounded corners for bar graphs?

Set theory with antielements?

Change the default Bookmarks Folder In Firefox

Physical Interpretation of an Overdamped Pendulum

Is there a word for returning to unpreparedness?

Why do aircraft leave cruising altitude long before landing just to circle?

Doesn't the speed of light limit imply the same electron can be annihilated twice?

Why won't the Republicans use a superdelegate system like the DNC in their nomination process?

Unconventional examples of mathematical modelling

How do I answer an interview question about how to handle a hard deadline I won't be able to meet?

Short comic about alien explorers visiting an abandoned world with giant statues that turn out to be alive but move very slowly

How to render "have ideas above his station" into German

Did Michelle Obama have a staff of 23; and Melania have a staff of 4?

How do I pass a "list of lists" as the argument to a function of the form F[x,y]?

What would it take to get a message to another star?

Knights and Knaves on a (Not So) Deserted Island

What does 〇〇〇〇 mean when combined with おじさん?

The space of cusp forms for GL_2 over F_q(T)

Has the speed of light ever been measured in vacuum?

Who owns content posted at Paizo.com forums?

global variant of csname…endcsname

Why do so many people play out of turn on the last lead?

Java methods to add and authenticate users in MySQL

Minimum population for language survival



String routines


String BuildingIterating string characters and passing them to a BIOS APIString length calculation implementation in Assembly (NASM)Check whether string ends with stringConvert string array to string with separatorAssembly 8086 program to input a 16-bit signed number (in string form) and output its binary equivalentE820 display using X86 legacy boot sector or DOS 6.22 com fileMIPS assembly string to int functionPrinting binary string in assemblystring length in x64 assembly (fasm)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4












$begingroup$


These are a few string routines (just the mem* ones). I've tried to optimize them the best I can without having them be too big, but I'm unsure if I've done a good job.



I'd prefer size over speed unless it's just a few bytes, in which case that would be fine. I would also prefer not to sacrifice simplicity for speed.



memchr.S (related):



.globl memchr
memchr:
mov %rdx, %rcx
movzbl %sil, %eax
repne scasb
lea -1(%rdi), %rax
test %rcx, %rcx
cmove %rcx, %rax
ret


memcmp.S:



.globl memcmp
memcmp:
mov %rdx, %rcx
repe cmpsb
movzbl -1(%rdi), %eax
movzbl -1(%rsi), %edx
sub %edx, %eax
ret


memcpy.S:



.globl memcpy
memcpy:
mov %rdx, %rcx
mov %rdi, %rax
rep movsb
ret


memmove.S:



.globl memmove
memmove:
mov %rdx, %rcx
mov %rdi, %rax
cmp %rdi, %rsi
jge 0f
dec %rdx
add %rdx, %rdi
add %rdx, %rsi
std
0: rep movsb
cld
ret


memrchr.S:



.globl memrchr
memrchr:
mov %rdx, %rcx
add %rdx, %rdi
movzbl %sil, %eax
std
repne scasb
cld
lea 1(%rdi), %rax
test %rcx, %rcx
cmove %rcx, %rax
ret


memset.S:



.globl memset
memset:
mov %rdx, %rcx
mov %rdi, %rdx
movzbl %sil, %eax
rep stosb
mov %rdx, %rax
ret


As usual for Stack Exchange sites, this code is released under CC/by-sa 3.0, but any future changes can be accessed here.










share|improve this question









$endgroup$




















    4












    $begingroup$


    These are a few string routines (just the mem* ones). I've tried to optimize them the best I can without having them be too big, but I'm unsure if I've done a good job.



    I'd prefer size over speed unless it's just a few bytes, in which case that would be fine. I would also prefer not to sacrifice simplicity for speed.



    memchr.S (related):



    .globl memchr
    memchr:
    mov %rdx, %rcx
    movzbl %sil, %eax
    repne scasb
    lea -1(%rdi), %rax
    test %rcx, %rcx
    cmove %rcx, %rax
    ret


    memcmp.S:



    .globl memcmp
    memcmp:
    mov %rdx, %rcx
    repe cmpsb
    movzbl -1(%rdi), %eax
    movzbl -1(%rsi), %edx
    sub %edx, %eax
    ret


    memcpy.S:



    .globl memcpy
    memcpy:
    mov %rdx, %rcx
    mov %rdi, %rax
    rep movsb
    ret


    memmove.S:



    .globl memmove
    memmove:
    mov %rdx, %rcx
    mov %rdi, %rax
    cmp %rdi, %rsi
    jge 0f
    dec %rdx
    add %rdx, %rdi
    add %rdx, %rsi
    std
    0: rep movsb
    cld
    ret


    memrchr.S:



    .globl memrchr
    memrchr:
    mov %rdx, %rcx
    add %rdx, %rdi
    movzbl %sil, %eax
    std
    repne scasb
    cld
    lea 1(%rdi), %rax
    test %rcx, %rcx
    cmove %rcx, %rax
    ret


    memset.S:



    .globl memset
    memset:
    mov %rdx, %rcx
    mov %rdi, %rdx
    movzbl %sil, %eax
    rep stosb
    mov %rdx, %rax
    ret


    As usual for Stack Exchange sites, this code is released under CC/by-sa 3.0, but any future changes can be accessed here.










    share|improve this question









    $endgroup$
















      4












      4








      4





      $begingroup$


      These are a few string routines (just the mem* ones). I've tried to optimize them the best I can without having them be too big, but I'm unsure if I've done a good job.



      I'd prefer size over speed unless it's just a few bytes, in which case that would be fine. I would also prefer not to sacrifice simplicity for speed.



      memchr.S (related):



      .globl memchr
      memchr:
      mov %rdx, %rcx
      movzbl %sil, %eax
      repne scasb
      lea -1(%rdi), %rax
      test %rcx, %rcx
      cmove %rcx, %rax
      ret


      memcmp.S:



      .globl memcmp
      memcmp:
      mov %rdx, %rcx
      repe cmpsb
      movzbl -1(%rdi), %eax
      movzbl -1(%rsi), %edx
      sub %edx, %eax
      ret


      memcpy.S:



      .globl memcpy
      memcpy:
      mov %rdx, %rcx
      mov %rdi, %rax
      rep movsb
      ret


      memmove.S:



      .globl memmove
      memmove:
      mov %rdx, %rcx
      mov %rdi, %rax
      cmp %rdi, %rsi
      jge 0f
      dec %rdx
      add %rdx, %rdi
      add %rdx, %rsi
      std
      0: rep movsb
      cld
      ret


      memrchr.S:



      .globl memrchr
      memrchr:
      mov %rdx, %rcx
      add %rdx, %rdi
      movzbl %sil, %eax
      std
      repne scasb
      cld
      lea 1(%rdi), %rax
      test %rcx, %rcx
      cmove %rcx, %rax
      ret


      memset.S:



      .globl memset
      memset:
      mov %rdx, %rcx
      mov %rdi, %rdx
      movzbl %sil, %eax
      rep stosb
      mov %rdx, %rax
      ret


      As usual for Stack Exchange sites, this code is released under CC/by-sa 3.0, but any future changes can be accessed here.










      share|improve this question









      $endgroup$




      These are a few string routines (just the mem* ones). I've tried to optimize them the best I can without having them be too big, but I'm unsure if I've done a good job.



      I'd prefer size over speed unless it's just a few bytes, in which case that would be fine. I would also prefer not to sacrifice simplicity for speed.



      memchr.S (related):



      .globl memchr
      memchr:
      mov %rdx, %rcx
      movzbl %sil, %eax
      repne scasb
      lea -1(%rdi), %rax
      test %rcx, %rcx
      cmove %rcx, %rax
      ret


      memcmp.S:



      .globl memcmp
      memcmp:
      mov %rdx, %rcx
      repe cmpsb
      movzbl -1(%rdi), %eax
      movzbl -1(%rsi), %edx
      sub %edx, %eax
      ret


      memcpy.S:



      .globl memcpy
      memcpy:
      mov %rdx, %rcx
      mov %rdi, %rax
      rep movsb
      ret


      memmove.S:



      .globl memmove
      memmove:
      mov %rdx, %rcx
      mov %rdi, %rax
      cmp %rdi, %rsi
      jge 0f
      dec %rdx
      add %rdx, %rdi
      add %rdx, %rsi
      std
      0: rep movsb
      cld
      ret


      memrchr.S:



      .globl memrchr
      memrchr:
      mov %rdx, %rcx
      add %rdx, %rdi
      movzbl %sil, %eax
      std
      repne scasb
      cld
      lea 1(%rdi), %rax
      test %rcx, %rcx
      cmove %rcx, %rax
      ret


      memset.S:



      .globl memset
      memset:
      mov %rdx, %rcx
      mov %rdi, %rdx
      movzbl %sil, %eax
      rep stosb
      mov %rdx, %rax
      ret


      As usual for Stack Exchange sites, this code is released under CC/by-sa 3.0, but any future changes can be accessed here.







      strings assembly x86






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 8 hours ago









      JL2210JL2210

      2059 bronze badges




      2059 bronze badges























          2 Answers
          2






          active

          oldest

          votes


















          2












          $begingroup$

          The code looks straight-forward and really optimized for size and simplicity.



          There's a small detail that I would change, though: replace cmove with cmovz, to make the code more expressive. It's not that "being equal" would be of any interest here, it's the zeroness of %ecx that is interesting.



          I like the omitted second jmp in memmove. It's obvious after thinking a few seconds about it.



          According to this quote it's ok to rely on the direction flag being always cleared.



          I still suggest to write a few unit tests to be on the safe side.








          share|improve this answer











          $endgroup$














          • $begingroup$
            See my answer for a bug that I found on my own (found by writing unit tests).
            $endgroup$
            – JL2210
            5 hours ago


















          2












          $begingroup$

          There's a bug in your code if memchr finds %sil in the last byte of %rdi; if %rcx tests to be zero and yet the byte has been found, it will incorrectly return zero.



          To fix that, do something like this:



          .globl memchr
          memchr:
          mov %rdx, %rcx
          movzbl %sil, %eax
          repne scasb
          sete %cl
          lea -1(%rdi), %rax
          test %cl, %cl
          cmovz %rcx, %rax
          ret


          The same applies to memrchr.






          share|improve this answer











          $endgroup$

















            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: "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',
            autoActivateHeartbeat: false,
            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
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f226285%2fstring-routines%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









            2












            $begingroup$

            The code looks straight-forward and really optimized for size and simplicity.



            There's a small detail that I would change, though: replace cmove with cmovz, to make the code more expressive. It's not that "being equal" would be of any interest here, it's the zeroness of %ecx that is interesting.



            I like the omitted second jmp in memmove. It's obvious after thinking a few seconds about it.



            According to this quote it's ok to rely on the direction flag being always cleared.



            I still suggest to write a few unit tests to be on the safe side.








            share|improve this answer











            $endgroup$














            • $begingroup$
              See my answer for a bug that I found on my own (found by writing unit tests).
              $endgroup$
              – JL2210
              5 hours ago















            2












            $begingroup$

            The code looks straight-forward and really optimized for size and simplicity.



            There's a small detail that I would change, though: replace cmove with cmovz, to make the code more expressive. It's not that "being equal" would be of any interest here, it's the zeroness of %ecx that is interesting.



            I like the omitted second jmp in memmove. It's obvious after thinking a few seconds about it.



            According to this quote it's ok to rely on the direction flag being always cleared.



            I still suggest to write a few unit tests to be on the safe side.








            share|improve this answer











            $endgroup$














            • $begingroup$
              See my answer for a bug that I found on my own (found by writing unit tests).
              $endgroup$
              – JL2210
              5 hours ago













            2












            2








            2





            $begingroup$

            The code looks straight-forward and really optimized for size and simplicity.



            There's a small detail that I would change, though: replace cmove with cmovz, to make the code more expressive. It's not that "being equal" would be of any interest here, it's the zeroness of %ecx that is interesting.



            I like the omitted second jmp in memmove. It's obvious after thinking a few seconds about it.



            According to this quote it's ok to rely on the direction flag being always cleared.



            I still suggest to write a few unit tests to be on the safe side.








            share|improve this answer











            $endgroup$



            The code looks straight-forward and really optimized for size and simplicity.



            There's a small detail that I would change, though: replace cmove with cmovz, to make the code more expressive. It's not that "being equal" would be of any interest here, it's the zeroness of %ecx that is interesting.



            I like the omitted second jmp in memmove. It's obvious after thinking a few seconds about it.



            According to this quote it's ok to rely on the direction flag being always cleared.



            I still suggest to write a few unit tests to be on the safe side.









            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 5 hours ago









            JL2210

            2059 bronze badges




            2059 bronze badges










            answered 7 hours ago









            Roland IlligRoland Illig

            14.7k2 gold badges23 silver badges56 bronze badges




            14.7k2 gold badges23 silver badges56 bronze badges














            • $begingroup$
              See my answer for a bug that I found on my own (found by writing unit tests).
              $endgroup$
              – JL2210
              5 hours ago
















            • $begingroup$
              See my answer for a bug that I found on my own (found by writing unit tests).
              $endgroup$
              – JL2210
              5 hours ago















            $begingroup$
            See my answer for a bug that I found on my own (found by writing unit tests).
            $endgroup$
            – JL2210
            5 hours ago




            $begingroup$
            See my answer for a bug that I found on my own (found by writing unit tests).
            $endgroup$
            – JL2210
            5 hours ago













            2












            $begingroup$

            There's a bug in your code if memchr finds %sil in the last byte of %rdi; if %rcx tests to be zero and yet the byte has been found, it will incorrectly return zero.



            To fix that, do something like this:



            .globl memchr
            memchr:
            mov %rdx, %rcx
            movzbl %sil, %eax
            repne scasb
            sete %cl
            lea -1(%rdi), %rax
            test %cl, %cl
            cmovz %rcx, %rax
            ret


            The same applies to memrchr.






            share|improve this answer











            $endgroup$



















              2












              $begingroup$

              There's a bug in your code if memchr finds %sil in the last byte of %rdi; if %rcx tests to be zero and yet the byte has been found, it will incorrectly return zero.



              To fix that, do something like this:



              .globl memchr
              memchr:
              mov %rdx, %rcx
              movzbl %sil, %eax
              repne scasb
              sete %cl
              lea -1(%rdi), %rax
              test %cl, %cl
              cmovz %rcx, %rax
              ret


              The same applies to memrchr.






              share|improve this answer











              $endgroup$

















                2












                2








                2





                $begingroup$

                There's a bug in your code if memchr finds %sil in the last byte of %rdi; if %rcx tests to be zero and yet the byte has been found, it will incorrectly return zero.



                To fix that, do something like this:



                .globl memchr
                memchr:
                mov %rdx, %rcx
                movzbl %sil, %eax
                repne scasb
                sete %cl
                lea -1(%rdi), %rax
                test %cl, %cl
                cmovz %rcx, %rax
                ret


                The same applies to memrchr.






                share|improve this answer











                $endgroup$



                There's a bug in your code if memchr finds %sil in the last byte of %rdi; if %rcx tests to be zero and yet the byte has been found, it will incorrectly return zero.



                To fix that, do something like this:



                .globl memchr
                memchr:
                mov %rdx, %rcx
                movzbl %sil, %eax
                repne scasb
                sete %cl
                lea -1(%rdi), %rax
                test %cl, %cl
                cmovz %rcx, %rax
                ret


                The same applies to memrchr.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 1 hour ago

























                answered 5 hours ago









                JL2210JL2210

                2059 bronze badges




                2059 bronze badges






























                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f226285%2fstring-routines%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

                    ParseJSON using SSJSUsing AMPscript with SSJS ActivitiesHow to resubscribe a user in Marketing cloud using SSJS?Pulling Subscriber Status from Lists using SSJSRetrieving Emails using SSJSProblem in updating DE using SSJSUsing SSJS to send single email in Marketing CloudError adding EmailSendDefinition using SSJS

                    Кампала Садржај Географија Географија Историја Становништво Привреда Партнерски градови Референце Спољашње везе Мени за навигацију0°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.340°11′ СГШ; 32°20′ ИГД / 0.18° СГШ; 32.34° ИГД / 0.18; 32.34МедијиПодациЗванични веб-сајту

                    19. јануар Садржај Догађаји Рођења Смрти Празници и дани сећања Види још Референце Мени за навигацијуу