简体   繁体   中英

NASM - Segmentation fault in loop

i got two similar loops, in which i write an address to eax and then from eax to a data-segment. The first first loop is working, the second loop returns a segfault. Why is the second loop wrong and the first not!?

section .data
n1: db 1
n2: db 1

[...]

n_1_1:
mov eax,one
mov [n1],eax
jmp DIG2

 n_2_1:
 mov eax,one
 mov [n2],eax        ; segfault
 jmp DISP2

db declares a byte (8 bits), which isn't enough to hold eax (32 bits). Declare every variable so that it has enough space to hold the largest value you will try to write to / read from it.

For example:

; declare n1 and n2 as doublewords with initial values of 1
n1: dd 1
n2: dd 1   

As for why one of them crashes and the other doesn't; it's hard to say from the code you've shown and not knowing the execution environment. But the second write overwrites 1 byte further into unallocated space (by the looks of it), which could be enough to make it crash even though the first write didn't cause a crash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM