0

I'm writing bubble sort in x86 assembly. I used DDD to figure out registers status and stack info.

I steped through the program, and I ran into the error 0x00000000004000e7 in ?? (). I want to figure out why this happened.

This is my bubble_sort.s

.section .text
.global _start

_start:
movq $0, %rsi           #i = 0;
movq $0, %rdi           #j = 0;
movq $4, %rbx           #N = 4;

subq $40,%rsp           
movq $4, (%rsp)         #a[0] = 4;
movq $3, 4(%rsp)        #a[1] = 3;
movq $2, 8(%rsp)        #a[2] = 2;
movq $1, 12(%rsp)       #a[3] = 1;

subq $1, %rbx           #N = N - 1
cmpq %rsi, %rbx         #i - (N-1)
jge .DONE1
.LOOP1:
    movq %rbx, %r15     #%rdx: N - 1
    subq %rsi, %r15     #%rbx: N-1-i
    cmpq %rdi, %r15     #if(j < N-1-i)
    jge .DONE2
    .LOOP2:
        movq (%rsp, %rdi, 4), %r8       
        xchg %r8, 1(%rsp, %rdi, 4)      
        movq %r8, (%rsp, %rdi, 4)       
        addq $1, %rdi   #j = j+1
    cmpq %rdi, %r15     #compare j with N-1-i
    jl .LOOP2
    .DONE2:
    addq $1, %rsi
cmpq %rsi, %rbx         #i - (N-1)
jl .LOOP1
.DONE1:

I stepped through. When I run movq %rbx, %r15 #%rdx: N - 1, ddd showed me 0x00000000004000e7 in ?? ().

Then I run the next line, ddd showed me Cannot find bounds of current function.

I am puzzled, and I have not figure out why this happened. Could you please tell me what mistake I have made?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Y.A. Cao
  • 1
  • 1
  • 1
    **That's not an error, that's the current location where execution stopped**. (for a fault or for single-step / breakpoint.) Your debugger should see that you're in `_start`, because you do have `.global _start` to export that symbol even if you build without debug symbols. (`.L` labels are "local" and don't show up in the object file, so `.LOOP2` won't be there, but `.DONE2` should be.) What commands did you use to build an executable? Did you maybe use `strip`, or `ld -s` when linking? That would of course remove the symbols gdb looks for to find the start of a function. – Peter Cordes Apr 18 '19 at 09:11
  • @PeterCordes It could be that the debugger trusts the `size` directives; since `_start` has no size assigned to it, this might confuse the debugger. – fuz Apr 18 '19 at 09:15
  • 1
    The `Cannot find bounds of current function` might be from trying to step by source line, instead of the equivalent of the GDB `stepi` / `si` command, which steps by 1 asm instruction. Again a symbol of missing symbols (or maybe debug info). Did you build this with `gcc -no-pie -nostdlib bubble_sort.s`? Try adding `-g` to that. (Also don't forget to put an exit system call at the bottom of your file.) – Peter Cordes Apr 18 '19 at 09:16
  • 1
    @fuz: well possibly, but I've never needed a `size` directive for regular GDB in a terminal to treat `_start` as a symbol when debugging a quick thrown-together test case, as long as I have a `.globl` symbol. The OP should at least have been seeing `... in _start` even if `next` didn't work. – Peter Cordes Apr 18 '19 at 09:19
  • @PeterCordes Thank you for editing my question. I used `as -g -o my_bubble_sort.o my_bubble_sort.s` and `gcc -nostartfiles -g -o my_bubble_sort my_bubble_sort.o` to generate the executable. And I tried `gcc -no-pie -nostdlib -g bubble_sort.s`, but error `/usr/bin/ld: waring: cannot find the project sysbol _start; default 0000000000400144` appeared. – Y.A. Cao Apr 18 '19 at 13:43
  • Then your question is not a [mcve]. I copy-pasted the code from your question into `start.s`, and `gcc -no-pie -nostdlib start.s` assembled + linked it with no warnings, and has symbols in GDB. You have a `.global _start` directive and a `_start:` label, so that warning makes no sense. (And again, that's *not an error*. It's a warning. But it's one you shouldn't be getting, unless your actual text file has some hidden characters or is different from your question.) Also, `gcc -nostartfiles` should warn the same as `gcc -nostdlib` if the object file is missing an exported `_start` symbol. – Peter Cordes Apr 18 '19 at 13:50
  • And BTW, you don't need to link libc when you don't use it. `-nostdlib` leaves out libraries as well as CRT start files. – Peter Cordes Apr 18 '19 at 13:51
  • To make this a [mcve], edit your question with code we can copy/paste and commands to build it. (Or if copy/paste is impossible, shell commands like `cat > sort.s < – Peter Cordes Apr 18 '19 at 13:54
  • @PeterCordes I'm sorry to have troubled you. I'm a green hand to stack overflow. I will pay attention to what you have said next time I ask a question. Thank you very much. – Y.A. Cao Apr 18 '19 at 15:27

0 Answers0