Debugging with an assemble program with GDB












1















Here is my question:



I wrote a piece of code of assemble. It could read a file, transform the content to the uppercase and print the outputs in a newfile.



I complie and link the assemble code with:




as -gstabs read-files.s -o read-files.o



ld read-files.o -o read-files




And a test like "./read-files input-file output-file" works well.



But what if I want to debug this piece of code with gdb? I tried, but:



when I set the breakpoint and args of target code in gdb with:




(gdb) b *_start+1



(gdb) run test-file TEST-FILE




It will end with a segmentfault immediately.



Can I really debug this code like what I just stated aboved? Thanks



And the assemble code is here:



 .section .data
.equ SYS_OPEN, 5
.equ SYS_WRITE, 4
.equ SYS_READ, 3
.equ SYS_CLOSE, 6
.equ SYS_EXIT, 1
.equ O_RDONLY, 0
.equ O_CREAT_WRONLY_TRUNC, 03101
.equ STDIN, 0
.equ STDOUT, 1
.equ STDERR, 2
.equ LINUX_SYSCALL, 0x80
.equ END_OF_FILE, 0
.equ NUMBER_ARGUMENTS, 2

.section .bss
.equ BUFFER_SIZE, 500
.lcomm BUFFER_DATA, BUFFER_SIZE

.section .text
.equ ST_SIZE_RESERVE, 8
.equ ST_FD_IN, -4
.equ ST_FD_OUT, -8
.equ ST_ARGC, 0
.equ ST_ARGV_0, 4
.equ ST_ARGV_1, 8
.equ ST_ARGV_2, 12

.globl _start
_start:
movl %esp, %ebp
subl $ST_SIZE_RESERVE, %esp

open_files:
open_fd_in:
movl $SYS_OPEN, %eax
movl ST_ARGV_1(%ebp), %ebx
movl $O_RDONLY, %ecx
movl $0666, %edx
int $LINUX_SYSCALL

store_fd_in:
movl %eax, ST_FD_IN(%ebp)

open_fd_out:
movl $SYS_OPEN, %eax
movl ST_ARGV_2(%ebp), %ebx
movl $O_CREAT_WRONLY_TRUNC, %ecx
movl $0666, %edx
int $LINUX_SYSCALL

store_fd_out:
movl %eax, ST_FD_OUT(%ebp)

read_loop_begin:
movl $SYS_READ, %eax
movl ST_FD_IN(%ebp), %ebx
movl $BUFFER_DATA, %ecx
movl $BUFFER_SIZE, %edx
int $LINUX_SYSCALL

cmpl $END_OF_FILE, %eax
jle end_loop

continue_read_loop:
pushl $BUFFER_DATA
pushl %eax
call convert_to_upper
popl %eax
addl $4, %esp

movl %eax, %edx
movl $SYS_WRITE, %eax
movl ST_FD_OUT(%ebp), %ebx
movl $BUFFER_DATA, %ecx
int $LINUX_SYSCALL

jmp read_loop_begin

end_loop:
movl $SYS_CLOSE, %eax
movl ST_FD_OUT(%ebp), %ebx
int $LINUX_SYSCALL

movl $SYS_CLOSE, %eax
movl ST_FD_IN(%ebp), %ebx
int $LINUX_SYSCALL

movl $SYS_EXIT, %eax
movl $0, %ebx
int $LINUX_SYSCALL

.equ LOWERCASE_A, 'a'
.equ LOWERCASE_Z, 'z'
.equ UPPER_CONVERSION, 'A' - 'a'
.equ ST_BUFFER_LEN, 8
.equ ST_BUFFER, 12

convert_to_upper:
pushl %ebp
movl %esp, %ebp

movl ST_BUFFER(%ebp), %eax
movl ST_BUFFER_LEN(%ebp), %ebx
movl $0, %edi

cmpl $0, %ebx
je end_convert_loop

convert_loop:
movb (%eax, %edi, 1), %cl
cmpb $LOWERCASE_A, %cl
jl next_byte
cmpb $LOWERCASE_Z, %cl
jg next_byte

addb $UPPER_CONVERSION, %cl
movb %cl, (%eax, %edi, 1)

next_byte:
incl %edi
cmpl %edi, %ebx
jne convert_loop

end_convert_loop:
movl %ebp, %esp
popl %ebp
ret









share|improve this question





























    1















    Here is my question:



    I wrote a piece of code of assemble. It could read a file, transform the content to the uppercase and print the outputs in a newfile.



    I complie and link the assemble code with:




    as -gstabs read-files.s -o read-files.o



    ld read-files.o -o read-files




    And a test like "./read-files input-file output-file" works well.



    But what if I want to debug this piece of code with gdb? I tried, but:



    when I set the breakpoint and args of target code in gdb with:




    (gdb) b *_start+1



    (gdb) run test-file TEST-FILE




    It will end with a segmentfault immediately.



    Can I really debug this code like what I just stated aboved? Thanks



    And the assemble code is here:



     .section .data
    .equ SYS_OPEN, 5
    .equ SYS_WRITE, 4
    .equ SYS_READ, 3
    .equ SYS_CLOSE, 6
    .equ SYS_EXIT, 1
    .equ O_RDONLY, 0
    .equ O_CREAT_WRONLY_TRUNC, 03101
    .equ STDIN, 0
    .equ STDOUT, 1
    .equ STDERR, 2
    .equ LINUX_SYSCALL, 0x80
    .equ END_OF_FILE, 0
    .equ NUMBER_ARGUMENTS, 2

    .section .bss
    .equ BUFFER_SIZE, 500
    .lcomm BUFFER_DATA, BUFFER_SIZE

    .section .text
    .equ ST_SIZE_RESERVE, 8
    .equ ST_FD_IN, -4
    .equ ST_FD_OUT, -8
    .equ ST_ARGC, 0
    .equ ST_ARGV_0, 4
    .equ ST_ARGV_1, 8
    .equ ST_ARGV_2, 12

    .globl _start
    _start:
    movl %esp, %ebp
    subl $ST_SIZE_RESERVE, %esp

    open_files:
    open_fd_in:
    movl $SYS_OPEN, %eax
    movl ST_ARGV_1(%ebp), %ebx
    movl $O_RDONLY, %ecx
    movl $0666, %edx
    int $LINUX_SYSCALL

    store_fd_in:
    movl %eax, ST_FD_IN(%ebp)

    open_fd_out:
    movl $SYS_OPEN, %eax
    movl ST_ARGV_2(%ebp), %ebx
    movl $O_CREAT_WRONLY_TRUNC, %ecx
    movl $0666, %edx
    int $LINUX_SYSCALL

    store_fd_out:
    movl %eax, ST_FD_OUT(%ebp)

    read_loop_begin:
    movl $SYS_READ, %eax
    movl ST_FD_IN(%ebp), %ebx
    movl $BUFFER_DATA, %ecx
    movl $BUFFER_SIZE, %edx
    int $LINUX_SYSCALL

    cmpl $END_OF_FILE, %eax
    jle end_loop

    continue_read_loop:
    pushl $BUFFER_DATA
    pushl %eax
    call convert_to_upper
    popl %eax
    addl $4, %esp

    movl %eax, %edx
    movl $SYS_WRITE, %eax
    movl ST_FD_OUT(%ebp), %ebx
    movl $BUFFER_DATA, %ecx
    int $LINUX_SYSCALL

    jmp read_loop_begin

    end_loop:
    movl $SYS_CLOSE, %eax
    movl ST_FD_OUT(%ebp), %ebx
    int $LINUX_SYSCALL

    movl $SYS_CLOSE, %eax
    movl ST_FD_IN(%ebp), %ebx
    int $LINUX_SYSCALL

    movl $SYS_EXIT, %eax
    movl $0, %ebx
    int $LINUX_SYSCALL

    .equ LOWERCASE_A, 'a'
    .equ LOWERCASE_Z, 'z'
    .equ UPPER_CONVERSION, 'A' - 'a'
    .equ ST_BUFFER_LEN, 8
    .equ ST_BUFFER, 12

    convert_to_upper:
    pushl %ebp
    movl %esp, %ebp

    movl ST_BUFFER(%ebp), %eax
    movl ST_BUFFER_LEN(%ebp), %ebx
    movl $0, %edi

    cmpl $0, %ebx
    je end_convert_loop

    convert_loop:
    movb (%eax, %edi, 1), %cl
    cmpb $LOWERCASE_A, %cl
    jl next_byte
    cmpb $LOWERCASE_Z, %cl
    jg next_byte

    addb $UPPER_CONVERSION, %cl
    movb %cl, (%eax, %edi, 1)

    next_byte:
    incl %edi
    cmpl %edi, %ebx
    jne convert_loop

    end_convert_loop:
    movl %ebp, %esp
    popl %ebp
    ret









    share|improve this question



























      1












      1








      1


      0






      Here is my question:



      I wrote a piece of code of assemble. It could read a file, transform the content to the uppercase and print the outputs in a newfile.



      I complie and link the assemble code with:




      as -gstabs read-files.s -o read-files.o



      ld read-files.o -o read-files




      And a test like "./read-files input-file output-file" works well.



      But what if I want to debug this piece of code with gdb? I tried, but:



      when I set the breakpoint and args of target code in gdb with:




      (gdb) b *_start+1



      (gdb) run test-file TEST-FILE




      It will end with a segmentfault immediately.



      Can I really debug this code like what I just stated aboved? Thanks



      And the assemble code is here:



       .section .data
      .equ SYS_OPEN, 5
      .equ SYS_WRITE, 4
      .equ SYS_READ, 3
      .equ SYS_CLOSE, 6
      .equ SYS_EXIT, 1
      .equ O_RDONLY, 0
      .equ O_CREAT_WRONLY_TRUNC, 03101
      .equ STDIN, 0
      .equ STDOUT, 1
      .equ STDERR, 2
      .equ LINUX_SYSCALL, 0x80
      .equ END_OF_FILE, 0
      .equ NUMBER_ARGUMENTS, 2

      .section .bss
      .equ BUFFER_SIZE, 500
      .lcomm BUFFER_DATA, BUFFER_SIZE

      .section .text
      .equ ST_SIZE_RESERVE, 8
      .equ ST_FD_IN, -4
      .equ ST_FD_OUT, -8
      .equ ST_ARGC, 0
      .equ ST_ARGV_0, 4
      .equ ST_ARGV_1, 8
      .equ ST_ARGV_2, 12

      .globl _start
      _start:
      movl %esp, %ebp
      subl $ST_SIZE_RESERVE, %esp

      open_files:
      open_fd_in:
      movl $SYS_OPEN, %eax
      movl ST_ARGV_1(%ebp), %ebx
      movl $O_RDONLY, %ecx
      movl $0666, %edx
      int $LINUX_SYSCALL

      store_fd_in:
      movl %eax, ST_FD_IN(%ebp)

      open_fd_out:
      movl $SYS_OPEN, %eax
      movl ST_ARGV_2(%ebp), %ebx
      movl $O_CREAT_WRONLY_TRUNC, %ecx
      movl $0666, %edx
      int $LINUX_SYSCALL

      store_fd_out:
      movl %eax, ST_FD_OUT(%ebp)

      read_loop_begin:
      movl $SYS_READ, %eax
      movl ST_FD_IN(%ebp), %ebx
      movl $BUFFER_DATA, %ecx
      movl $BUFFER_SIZE, %edx
      int $LINUX_SYSCALL

      cmpl $END_OF_FILE, %eax
      jle end_loop

      continue_read_loop:
      pushl $BUFFER_DATA
      pushl %eax
      call convert_to_upper
      popl %eax
      addl $4, %esp

      movl %eax, %edx
      movl $SYS_WRITE, %eax
      movl ST_FD_OUT(%ebp), %ebx
      movl $BUFFER_DATA, %ecx
      int $LINUX_SYSCALL

      jmp read_loop_begin

      end_loop:
      movl $SYS_CLOSE, %eax
      movl ST_FD_OUT(%ebp), %ebx
      int $LINUX_SYSCALL

      movl $SYS_CLOSE, %eax
      movl ST_FD_IN(%ebp), %ebx
      int $LINUX_SYSCALL

      movl $SYS_EXIT, %eax
      movl $0, %ebx
      int $LINUX_SYSCALL

      .equ LOWERCASE_A, 'a'
      .equ LOWERCASE_Z, 'z'
      .equ UPPER_CONVERSION, 'A' - 'a'
      .equ ST_BUFFER_LEN, 8
      .equ ST_BUFFER, 12

      convert_to_upper:
      pushl %ebp
      movl %esp, %ebp

      movl ST_BUFFER(%ebp), %eax
      movl ST_BUFFER_LEN(%ebp), %ebx
      movl $0, %edi

      cmpl $0, %ebx
      je end_convert_loop

      convert_loop:
      movb (%eax, %edi, 1), %cl
      cmpb $LOWERCASE_A, %cl
      jl next_byte
      cmpb $LOWERCASE_Z, %cl
      jg next_byte

      addb $UPPER_CONVERSION, %cl
      movb %cl, (%eax, %edi, 1)

      next_byte:
      incl %edi
      cmpl %edi, %ebx
      jne convert_loop

      end_convert_loop:
      movl %ebp, %esp
      popl %ebp
      ret









      share|improve this question
















      Here is my question:



      I wrote a piece of code of assemble. It could read a file, transform the content to the uppercase and print the outputs in a newfile.



      I complie and link the assemble code with:




      as -gstabs read-files.s -o read-files.o



      ld read-files.o -o read-files




      And a test like "./read-files input-file output-file" works well.



      But what if I want to debug this piece of code with gdb? I tried, but:



      when I set the breakpoint and args of target code in gdb with:




      (gdb) b *_start+1



      (gdb) run test-file TEST-FILE




      It will end with a segmentfault immediately.



      Can I really debug this code like what I just stated aboved? Thanks



      And the assemble code is here:



       .section .data
      .equ SYS_OPEN, 5
      .equ SYS_WRITE, 4
      .equ SYS_READ, 3
      .equ SYS_CLOSE, 6
      .equ SYS_EXIT, 1
      .equ O_RDONLY, 0
      .equ O_CREAT_WRONLY_TRUNC, 03101
      .equ STDIN, 0
      .equ STDOUT, 1
      .equ STDERR, 2
      .equ LINUX_SYSCALL, 0x80
      .equ END_OF_FILE, 0
      .equ NUMBER_ARGUMENTS, 2

      .section .bss
      .equ BUFFER_SIZE, 500
      .lcomm BUFFER_DATA, BUFFER_SIZE

      .section .text
      .equ ST_SIZE_RESERVE, 8
      .equ ST_FD_IN, -4
      .equ ST_FD_OUT, -8
      .equ ST_ARGC, 0
      .equ ST_ARGV_0, 4
      .equ ST_ARGV_1, 8
      .equ ST_ARGV_2, 12

      .globl _start
      _start:
      movl %esp, %ebp
      subl $ST_SIZE_RESERVE, %esp

      open_files:
      open_fd_in:
      movl $SYS_OPEN, %eax
      movl ST_ARGV_1(%ebp), %ebx
      movl $O_RDONLY, %ecx
      movl $0666, %edx
      int $LINUX_SYSCALL

      store_fd_in:
      movl %eax, ST_FD_IN(%ebp)

      open_fd_out:
      movl $SYS_OPEN, %eax
      movl ST_ARGV_2(%ebp), %ebx
      movl $O_CREAT_WRONLY_TRUNC, %ecx
      movl $0666, %edx
      int $LINUX_SYSCALL

      store_fd_out:
      movl %eax, ST_FD_OUT(%ebp)

      read_loop_begin:
      movl $SYS_READ, %eax
      movl ST_FD_IN(%ebp), %ebx
      movl $BUFFER_DATA, %ecx
      movl $BUFFER_SIZE, %edx
      int $LINUX_SYSCALL

      cmpl $END_OF_FILE, %eax
      jle end_loop

      continue_read_loop:
      pushl $BUFFER_DATA
      pushl %eax
      call convert_to_upper
      popl %eax
      addl $4, %esp

      movl %eax, %edx
      movl $SYS_WRITE, %eax
      movl ST_FD_OUT(%ebp), %ebx
      movl $BUFFER_DATA, %ecx
      int $LINUX_SYSCALL

      jmp read_loop_begin

      end_loop:
      movl $SYS_CLOSE, %eax
      movl ST_FD_OUT(%ebp), %ebx
      int $LINUX_SYSCALL

      movl $SYS_CLOSE, %eax
      movl ST_FD_IN(%ebp), %ebx
      int $LINUX_SYSCALL

      movl $SYS_EXIT, %eax
      movl $0, %ebx
      int $LINUX_SYSCALL

      .equ LOWERCASE_A, 'a'
      .equ LOWERCASE_Z, 'z'
      .equ UPPER_CONVERSION, 'A' - 'a'
      .equ ST_BUFFER_LEN, 8
      .equ ST_BUFFER, 12

      convert_to_upper:
      pushl %ebp
      movl %esp, %ebp

      movl ST_BUFFER(%ebp), %eax
      movl ST_BUFFER_LEN(%ebp), %ebx
      movl $0, %edi

      cmpl $0, %ebx
      je end_convert_loop

      convert_loop:
      movb (%eax, %edi, 1), %cl
      cmpb $LOWERCASE_A, %cl
      jl next_byte
      cmpb $LOWERCASE_Z, %cl
      jg next_byte

      addb $UPPER_CONVERSION, %cl
      movb %cl, (%eax, %edi, 1)

      next_byte:
      incl %edi
      cmpl %edi, %ebx
      jne convert_loop

      end_convert_loop:
      movl %ebp, %esp
      popl %ebp
      ret






      gdb assemble






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 17:56







      user6362462

















      asked Nov 24 '18 at 17:54









      user6362462user6362462

      62




      62
























          0






          active

          oldest

          votes











          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%2f53460912%2fdebugging-with-an-assemble-program-with-gdb%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460912%2fdebugging-with-an-assemble-program-with-gdb%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)