memset movq给出段错误 [英] memset movq giving segfault
问题描述
我在movq(%rsi,%rcx)行出现段错误.
I am getting a segfault at the movq (%rsi, %rcx) line.
我知道您不能执行mem-> mem mov,所以我通过一个临时寄存器来完成它. (%rsi),%rcx,然后在循环%rcx,(%rdi)中.这是我的代码:
I know you can't do mem->mem mov, so I did it through a temporary register. (%rsi), %rcx, then in the loop %rcx, (%rdi). Here is my code:
experimentMemset: #memset(void *ptr, int value, size_t num)
#%rdi #%rsi #%rdx
movq %rdi, %rax #sets rax to the first pointer, to return later
.loop:
cmp $0, (%rdx) #see if num has reached 0
je .end
cmpb $0, (%rdi) #see if string has ended also
je .end
movq %rsi, %rdi #copies value into rdi
inc %rdi #increments pointer to traverse string
dec %rdx #decrements the count, aka num
jmp .loop
.end:
ret
推荐答案
如您所见,RDX拥有一个大小(一个整数),而不是一个指针.它是通过值而不是通过引用传递的.
As you discovered, RDX holds a size (an integer count), not a pointer. It's passed by value, not by reference.
cmp $0, (%rdx)
不是比较寄存器,而是比较它所指向的位置.看来%rdx用作计数器,所以您应该比较寄存器本身.
compares not the register, but the location pointed by it. It seems that %rdx is used as a counter, so you should compare the register itself.
test %rdx,%rdx
; je count_was_zero
还有其他错误,例如检查只写目标的内容是否为零,以及不将%sil
存储到(%rdi)
中.但这是在当前版本的问题中出现段错误的原因.
There are other bugs, like checking the contents of the write-only destination for zeros, and not storing %sil
into (%rdi)
. But this was the cause of the segfault in the current version of the question.
这篇关于memset movq给出段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!