0

I have a following code, running on the jdoodle.com:

section .text

global _start

_start:
    mov     eax, [array]
    mov     ebx, [array+1]
    add     eax, ebx
    mov     [sum], eax
    jmp     .printthis
    mov     eax, 1
    int     0x80
    ret

.printthis:
    mov     ecx, sum
    mov     edx, 1
    mov     ebx, 1
    mov     eax, 4
    int     0x80
    ret

section .data
    array   db 1,2,3,4,5

segment .bss
    sum     resb 1

After compilation I have the following error:

Command terminated by signal 11

Note:
I've tried

mov eax, [array+1*4]

and even

mov eax, [array*4] ; which is an error, obvious, but I can't understand why

Now to the question
I have some programming experience, but started learning assembly recently. This error, 11 seg fault, gives me a little. I would appreciate if you share your knowledge on this topic or recommend some good material to read.

Thanks!

Futman
  • 3
  • 3
  • 2
    Your variables are bytes but you use dword registers. Fix one or the other. Also note that `write` syscall will not convert your number to text. `jmp .printthis` should be `call .printthis` (that is likely the cause of the fault, the `ret` has nowhere to return to) – Jester Jan 26 '21 at 13:18
  • At which instruction does the crash occur? You mention that you've tried modifying one of the `mov`s, but have you verified that that's actually the instruction that triggers the crash? Side note, you've only reserved one byte of space for `sum` but you write 4 bytes (`eax`) to it. – Michael Jan 26 '21 at 13:20
  • Thanks! It makes sense. – Futman Jan 26 '21 at 13:22
  • @Jester, @Michael Guys, thank you so much! I've changed `mov eax, [array+1]` to it's right representation when working with Words - `mov eax, [array+1*4]`, and now it works. As well it seems like I need to learn about `jmp` and `call` alongside conversion from number to string. – Futman Jan 26 '21 at 13:35
  • 1
    `[array+1*4]` accesses more bytes than `array` contains. What is this program actually supposed to do? – Nate Eldredge Jan 26 '21 at 14:09
  • `jmp` to a `ret` in a non-function like `_start` is sort of a duplicate of [Nasm segmentation fault on RET in \_start](https://stackoverflow.com/q/19760002) and/or [What if there is no return statement in a CALLed block of code in assembly programs](https://stackoverflow.com/q/41205054). – Peter Cordes Jan 26 '21 at 19:12

0 Answers0