简体   繁体   English

NASM - 循环中的分段错误

[英]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. 我有两个类似的循环,在其中我写一个地址到eax然后从eax写入数据段。 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). db声明一个字节 (8位),这不足以容纳eax (32位)。 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. 但是第二次写入会将1个字节进一步覆盖到未分配的空间(通过它的外观),这可能足以使它崩溃,即使第一次写入没有导致崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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