NASM Segmentation fault when modifying a variable that should be in the read-write .data section (section .data doesn't work without a space?)

余生长醉 提交于 2021-02-15 07:40:23

问题


I'm having an issue with a program I'm writing in NASM using SASM, I'm using a variable as a counter and once I modified it and try to to save the new value at the used address in memory I get a segmentation fault. Here are the bits of code concerning the variable:

section.data
p_count DW 0

section.text
global CMAIN
CMAIN:
    mov ebp, esp; for correct debugging
    mov bx, [p_count]
    inc bx
    mov [p_count], bx

    ret

The program stops running when it arrives at the last line here. Anyone has an idea what the problem could be?


回答1:


You forgot the space in section.data (and .text), so everything went into the read-only .text section by default.

section.data is parsed as a label, like foo.bar: would be, not a section directive. The colon at the end of a label is optional when the label name isn't also a valid instruction mnemonic like loop:

The opposite error (valid section .data but buggy section.text) leads to putting your code into the .data, which gets linked into non-executable memory pages. In that case you'd segfault on code-fetch from the first instruction!


You should have gotten a warning from NASM like this:

warning: label alone on a line without a colon might be in error [-w+orphan-labels]

If your NASM didn't warn, use a newer version where it's on by default,
or run NASM with -Worphan-labels to enable that useful warning.



来源:https://stackoverflow.com/questions/31644669/any-ideas-why-this-is-crashing-after-calling-scanf-in-the-readint-function-nasm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!