0

I am able to assembly, but when I step through with gdb, it crashes when I try to mov the value in the array to the register

I am assembling with nasm -f elf32 test.asm And I am linking with ld -m elf_i386 test.o

section data
    list:
        dw 7, 9, 3, 0
section text
global    _start
          _start:
        
push ebp                               ; prologue
mov ebp, esp                           ; prologue

        mov edx, 1                     ; i = 1
        mov ecx, [edx * 4 + list]      ; ecx = list[i]

        loop:
        mov ebx, [edx * 4 + list]
        cmp ebx,0                      ; if (list[i] = 0)
        jz end                         ; exit loop
        cmp ecx,ebx                    ; ecx - list[i]
        jl if_greater                  ; ecx - list[i] = <0
        inc edx                        ; i++
        jmp loop
        
        if_greater:                    ; if condition block
        mov ecx, [edx * 4 + list]      ; new highest value
        inc edx                        ; i++
        jmp loop

        end: 

        mov esp, ebp                  ; epilogue
        pop ebp                       ; epilogue

I have added updated code. I left the original code. My goal now is to find the highest pair in list, but am unsuccessful.


section data
    list:
        dw 7, 9, 3, 0
section text
global    _start
          _start:
        
push ebp                               ; prologue
mov ebp, esp                           ; prologue

        mov edx, 1                     ; i = 1
        mov ecx, [edx * 4 + list]      ; ecx = list[i]

        loop:
        mov ebx, [edx * 4 + list]
        cmp ebx,0                      ; if (list[i] = 0)
        jz end                         ; exit loop
        cmp ecx,ebx                    ; ecx - list[i]
        jl if_greater                  ; ecx - list[i] = <0
        inc edx                        ; i++
        jmp loop
        
        if_greater:                    ; if condition block
        mov ecx, [edx * 4 + list]      ; new highest value
        inc edx                        ; i++
        jmp loop

        end: 
        mov eax, 1
        xor ebx, ebx
        int 0x80

        mov esp, ebp                  ; epilogue
        pop ebp                       ; epilogue

update 2: I have deleted both the prologue and the epilogue. I also changed edx to 0


section data
    list:
        dd 7, 9, 3, 0
section text
global    _start
          _start:
        

        mov edx, 0                     ; i = 0
        mov ecx, [edx * 4 + list]      ; ecx = list[i]

        loop:
        mov ebx, [edx * 4 + list]
        cmp ebx,0                      ; if (list[i] = 0)
        jz end                         ; exit loop
        cmp ecx,ebx                    ; ecx - list[i]
        jl if_greater                  ; ecx - list[i] = <0
        inc edx                        ; i++
        jmp loop
        
        if_greater:                    ; if condition block
        mov ecx, [edx * 4 + list]      ; new highest value
        inc edx                        ; i++
        jmp loop

        end: 
        mov eax, 1
        xor ebx, ebx
        int 0x80

krenman
  • 1
  • 1
  • 2
    `list: dw 7, 9, 3, 0` your list elements are words, i.e. 16-bit values, 2 bytes each. But all the rest of your code is written as though they are 32-bit dwords, 4 bytes. Maybe you meant `dd` here? – Nate Eldredge Aug 10 '21 at 16:54
  • 1
    Also, your program doesn't end. The machine keeps executing even when you run out of instructions, so it executes garbage. You need to invoke an exit system call to terminate the program before it crashes. Try at the end: `mov eax, 1 / xor ebx, ebx / int 0x80`, assuming this is Linux. – Nate Eldredge Aug 10 '21 at 16:57
  • @NateEldredge I had mistakenly thought that dw was 32 bit. Thank you! Any reason why you `xor ebx. ebx`. Why not just `mov ebx, 0` – krenman Aug 10 '21 at 18:07
  • 1
    `mov ebx, 0` is more bytes, and `xor ebx, ebx` is the "zeroing idiom" that x86 CPUs optimize heavily. See https://stackoverflow.com/questions/33666617/what-is-the-best-way-to-set-a-register-to-zero-in-x86-assembly-xor-mov-or-and – Nate Eldredge Aug 10 '21 at 18:12
  • @NateEldredge Thank you! It's no longer crashing. Although, it's no running properly. I am looking for the highest value in the list. The updated code is in the post – krenman Aug 10 '21 at 18:53
  • Please explain what goes wrong, and what you did to observe the problem. Btw, you probably want to start edx at 0 instead of 1, otherwise you skip the first element (your address computations implicitly make the array "zero-based"). – Nate Eldredge Aug 10 '21 at 18:57
  • Also it is pointless to put the epilogue after the `int 0x80` since it is never reached; the `int 0x80` terminates the program. (Indeed the prologue and epilogue are both pointless to have at all, since you never do anything with `ebp`.) – Nate Eldredge Aug 10 '21 at 18:59
  • Are you looking for the highest **value** in the list, or the highest **pair**? Please clarify. – Nate Eldredge Aug 10 '21 at 19:01
  • 1
    @krenman: Now that you have a different problem, have you used a debugger to try to solve it yourself? Single-step the code and look at register values. (This would also have let you notice your original bug that you were loading pairs of 16-bit values from registers having hex values like 0x00090007). e.g. see the bottom of https://stackoverflow.com/tags/x86/info for Linux GDB tips. – Peter Cordes Aug 10 '21 at 19:06
  • 1
    Your code still has `dw` instead of `dd`. – Nate Eldredge Aug 10 '21 at 19:22
  • @PeterCordes When I run gdb on the program, I set a breakpoint at start, but when I step through, I receive the error `Single stepping until exit from function loop, which has no line number information.` and I can no longer continue debugging. I tried assembling with the debugging information using the -g flag `nasm -f elf32 -g test.asm` but still no luck – krenman Aug 11 '21 at 12:31
  • Use `si` to step by instructions, not source lines. Like I said, see the bottom of https://stackoverflow.com/tags/x86/info for GDB asm debugging tips. One of the key bolded things is to use `stepi`. – Peter Cordes Aug 11 '21 at 12:44
  • @PeterCordes Thank you! Consider the question solved! – krenman Aug 11 '21 at 14:06

0 Answers0