What is the purpose of this function in assembly?












3














I am reading an assembly code, and I do not have the source code available. There is one function called next_text in the assembly that I am struggling to understand. Can anyone help me on this?



      next_text()

push %rbp
push %rbx
sub $0x108,%rsp
mov %rdi,%rbp
mov %esi,%ebx
jmp <next_text+45>
16 mov %rbp,%rdx
mov $0x100,%esi
mov %rsp,%rdi
callq <fgets@plt>
test %rax,%rax
jne <next_text+42>
callq <func>
42 sub $0x1,%ebx
45 test %ebx,%ebx
jg <next_text+16>
add $0x108,%rsp
pop %rbx
pop %rbp
retq


Here %ebx starts with certain large integer like 16000. My understanding of what this function is doing is it takes an argument which is the file stream. It then reads the file line by line. The maximum length of the line is 100 hex which is 256 characters. It then subtracts ebx by 1, and as long as the file is long enough, keeps on reading the file for 16000 lines, and at the end when ebx is 0, it exits. Is my understanding correct? Also, is the command sub $0x108,%rsp particularly important here, or is it just setting up the stack memory?










share|improve this question





























    3














    I am reading an assembly code, and I do not have the source code available. There is one function called next_text in the assembly that I am struggling to understand. Can anyone help me on this?



          next_text()

    push %rbp
    push %rbx
    sub $0x108,%rsp
    mov %rdi,%rbp
    mov %esi,%ebx
    jmp <next_text+45>
    16 mov %rbp,%rdx
    mov $0x100,%esi
    mov %rsp,%rdi
    callq <fgets@plt>
    test %rax,%rax
    jne <next_text+42>
    callq <func>
    42 sub $0x1,%ebx
    45 test %ebx,%ebx
    jg <next_text+16>
    add $0x108,%rsp
    pop %rbx
    pop %rbp
    retq


    Here %ebx starts with certain large integer like 16000. My understanding of what this function is doing is it takes an argument which is the file stream. It then reads the file line by line. The maximum length of the line is 100 hex which is 256 characters. It then subtracts ebx by 1, and as long as the file is long enough, keeps on reading the file for 16000 lines, and at the end when ebx is 0, it exits. Is my understanding correct? Also, is the command sub $0x108,%rsp particularly important here, or is it just setting up the stack memory?










    share|improve this question



























      3












      3








      3







      I am reading an assembly code, and I do not have the source code available. There is one function called next_text in the assembly that I am struggling to understand. Can anyone help me on this?



            next_text()

      push %rbp
      push %rbx
      sub $0x108,%rsp
      mov %rdi,%rbp
      mov %esi,%ebx
      jmp <next_text+45>
      16 mov %rbp,%rdx
      mov $0x100,%esi
      mov %rsp,%rdi
      callq <fgets@plt>
      test %rax,%rax
      jne <next_text+42>
      callq <func>
      42 sub $0x1,%ebx
      45 test %ebx,%ebx
      jg <next_text+16>
      add $0x108,%rsp
      pop %rbx
      pop %rbp
      retq


      Here %ebx starts with certain large integer like 16000. My understanding of what this function is doing is it takes an argument which is the file stream. It then reads the file line by line. The maximum length of the line is 100 hex which is 256 characters. It then subtracts ebx by 1, and as long as the file is long enough, keeps on reading the file for 16000 lines, and at the end when ebx is 0, it exits. Is my understanding correct? Also, is the command sub $0x108,%rsp particularly important here, or is it just setting up the stack memory?










      share|improve this question















      I am reading an assembly code, and I do not have the source code available. There is one function called next_text in the assembly that I am struggling to understand. Can anyone help me on this?



            next_text()

      push %rbp
      push %rbx
      sub $0x108,%rsp
      mov %rdi,%rbp
      mov %esi,%ebx
      jmp <next_text+45>
      16 mov %rbp,%rdx
      mov $0x100,%esi
      mov %rsp,%rdi
      callq <fgets@plt>
      test %rax,%rax
      jne <next_text+42>
      callq <func>
      42 sub $0x1,%ebx
      45 test %ebx,%ebx
      jg <next_text+16>
      add $0x108,%rsp
      pop %rbx
      pop %rbp
      retq


      Here %ebx starts with certain large integer like 16000. My understanding of what this function is doing is it takes an argument which is the file stream. It then reads the file line by line. The maximum length of the line is 100 hex which is 256 characters. It then subtracts ebx by 1, and as long as the file is long enough, keeps on reading the file for 16000 lines, and at the end when ebx is 0, it exits. Is my understanding correct? Also, is the command sub $0x108,%rsp particularly important here, or is it just setting up the stack memory?







      c assembly x86-64






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 at 2:50









      melpomene

      58.4k54489




      58.4k54489










      asked Nov 23 at 2:46









      dipankar

      214




      214
























          1 Answer
          1






          active

          oldest

          votes


















          5














          Looks to me like it is this:



          void next_text(FILE *f, count) {
          char buf[256];
          while (count-- > 0) {
          if (fgets(buf, sizeof buf, f)) {
          func();
          }
          }
          }





          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%2f53440082%2fwhat-is-the-purpose-of-this-function-in-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









            5














            Looks to me like it is this:



            void next_text(FILE *f, count) {
            char buf[256];
            while (count-- > 0) {
            if (fgets(buf, sizeof buf, f)) {
            func();
            }
            }
            }





            share|improve this answer


























              5














              Looks to me like it is this:



              void next_text(FILE *f, count) {
              char buf[256];
              while (count-- > 0) {
              if (fgets(buf, sizeof buf, f)) {
              func();
              }
              }
              }





              share|improve this answer
























                5












                5








                5






                Looks to me like it is this:



                void next_text(FILE *f, count) {
                char buf[256];
                while (count-- > 0) {
                if (fgets(buf, sizeof buf, f)) {
                func();
                }
                }
                }





                share|improve this answer












                Looks to me like it is this:



                void next_text(FILE *f, count) {
                char buf[256];
                while (count-- > 0) {
                if (fgets(buf, sizeof buf, f)) {
                func();
                }
                }
                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 at 3:03









                mevets

                2,030618




                2,030618






























                    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%2f53440082%2fwhat-is-the-purpose-of-this-function-in-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

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

                    Calculate evaluation metrics using cross_val_predict sklearn

                    Insert data from modal to MySQL (multiple modal on website)