Simple puts() function in x64 assembly

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I'm trying to write as simple I/O library in x64 using linux syscalls



section .text
strlen:
xor rdx, rdx
.loop:
cmp [rsi + rdx], 0
je .exit
inc rdx
jmp .loop
.exit:
ret ; value in rdx

puts:
; string passed through rsi
mov rax, 1 ; WRITE syscall
mov rdi, 1 ; fd for stdout
call strlen
syscall









share|improve this question



























    up vote
    2
    down vote

    favorite












    I'm trying to write as simple I/O library in x64 using linux syscalls



    section .text
    strlen:
    xor rdx, rdx
    .loop:
    cmp [rsi + rdx], 0
    je .exit
    inc rdx
    jmp .loop
    .exit:
    ret ; value in rdx

    puts:
    ; string passed through rsi
    mov rax, 1 ; WRITE syscall
    mov rdi, 1 ; fd for stdout
    call strlen
    syscall









    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I'm trying to write as simple I/O library in x64 using linux syscalls



      section .text
      strlen:
      xor rdx, rdx
      .loop:
      cmp [rsi + rdx], 0
      je .exit
      inc rdx
      jmp .loop
      .exit:
      ret ; value in rdx

      puts:
      ; string passed through rsi
      mov rax, 1 ; WRITE syscall
      mov rdi, 1 ; fd for stdout
      call strlen
      syscall









      share|improve this question















      I'm trying to write as simple I/O library in x64 using linux syscalls



      section .text
      strlen:
      xor rdx, rdx
      .loop:
      cmp [rsi + rdx], 0
      je .exit
      inc rdx
      jmp .loop
      .exit:
      ret ; value in rdx

      puts:
      ; string passed through rsi
      mov rax, 1 ; WRITE syscall
      mov rdi, 1 ; fd for stdout
      call strlen
      syscall






      strings io linux assembly x86






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 16 mins ago

























      asked 4 hours ago









      Meme myself and a very creepy

      514




      514




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          Your loop uses 2 jumps (je/jmp) on every iteration! Jumping is expensive, so a solution that needs only 1 jump (jne) will be more effective.



          strlen:
          xor rdx, rdx
          dec rdx ; This compensates for the INC that is happening first.
          .next:
          inc rdx
          cmp byte [rsi + rdx], 0
          jne .next
          ret



          Do keep things logically together. There's no point in setting RAX before the call to strlen.



          puts:
          ; string passed through rsi
          call strlen ; Result is in RDX

          mov rax, 1
          syscall





          share|improve this answer




















          • Would it be best to make an generalization of this and cal it fputs then have puts just mov rdi, 1 and call puts?
            – Meme myself and a very creepy
            19 mins ago











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          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%2f206381%2fsimple-puts-function-in-x64-assembly%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote













          Your loop uses 2 jumps (je/jmp) on every iteration! Jumping is expensive, so a solution that needs only 1 jump (jne) will be more effective.



          strlen:
          xor rdx, rdx
          dec rdx ; This compensates for the INC that is happening first.
          .next:
          inc rdx
          cmp byte [rsi + rdx], 0
          jne .next
          ret



          Do keep things logically together. There's no point in setting RAX before the call to strlen.



          puts:
          ; string passed through rsi
          call strlen ; Result is in RDX

          mov rax, 1
          syscall





          share|improve this answer




















          • Would it be best to make an generalization of this and cal it fputs then have puts just mov rdi, 1 and call puts?
            – Meme myself and a very creepy
            19 mins ago















          up vote
          2
          down vote













          Your loop uses 2 jumps (je/jmp) on every iteration! Jumping is expensive, so a solution that needs only 1 jump (jne) will be more effective.



          strlen:
          xor rdx, rdx
          dec rdx ; This compensates for the INC that is happening first.
          .next:
          inc rdx
          cmp byte [rsi + rdx], 0
          jne .next
          ret



          Do keep things logically together. There's no point in setting RAX before the call to strlen.



          puts:
          ; string passed through rsi
          call strlen ; Result is in RDX

          mov rax, 1
          syscall





          share|improve this answer




















          • Would it be best to make an generalization of this and cal it fputs then have puts just mov rdi, 1 and call puts?
            – Meme myself and a very creepy
            19 mins ago













          up vote
          2
          down vote










          up vote
          2
          down vote









          Your loop uses 2 jumps (je/jmp) on every iteration! Jumping is expensive, so a solution that needs only 1 jump (jne) will be more effective.



          strlen:
          xor rdx, rdx
          dec rdx ; This compensates for the INC that is happening first.
          .next:
          inc rdx
          cmp byte [rsi + rdx], 0
          jne .next
          ret



          Do keep things logically together. There's no point in setting RAX before the call to strlen.



          puts:
          ; string passed through rsi
          call strlen ; Result is in RDX

          mov rax, 1
          syscall





          share|improve this answer












          Your loop uses 2 jumps (je/jmp) on every iteration! Jumping is expensive, so a solution that needs only 1 jump (jne) will be more effective.



          strlen:
          xor rdx, rdx
          dec rdx ; This compensates for the INC that is happening first.
          .next:
          inc rdx
          cmp byte [rsi + rdx], 0
          jne .next
          ret



          Do keep things logically together. There's no point in setting RAX before the call to strlen.



          puts:
          ; string passed through rsi
          call strlen ; Result is in RDX

          mov rax, 1
          syscall






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 3 hours ago









          Fifoernik

          30027




          30027











          • Would it be best to make an generalization of this and cal it fputs then have puts just mov rdi, 1 and call puts?
            – Meme myself and a very creepy
            19 mins ago

















          • Would it be best to make an generalization of this and cal it fputs then have puts just mov rdi, 1 and call puts?
            – Meme myself and a very creepy
            19 mins ago
















          Would it be best to make an generalization of this and cal it fputs then have puts just mov rdi, 1 and call puts?
          – Meme myself and a very creepy
          19 mins ago





          Would it be best to make an generalization of this and cal it fputs then have puts just mov rdi, 1 and call puts?
          – Meme myself and a very creepy
          19 mins ago


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f206381%2fsimple-puts-function-in-x64-assembly%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          What does second last employer means? [closed]

          Installing NextGIS Connect into QGIS 3?

          One-line joke