Assembly NASM与al的奇怪行为
我尝试运行以下程序: C代码:Assembly NASM与al的奇怪行为,assembly,nasm,strchr,Assembly,Nasm,Strchr,我尝试运行以下程序: C代码: int main() { char *s1 = "hello"; printf("string : %s\n", strchr(s1, 'l')); } 汇编代码: global strchr section .text strchr: push rbp mov rbp, rsp strchr_loop: mov al, byte [rsi] ; My bug come
int main()
{
char *s1 = "hello";
printf("string : %s\n", strchr(s1, 'l'));
}
汇编代码:
global strchr
section .text
strchr:
push rbp
mov rbp, rsp
strchr_loop:
mov al, byte [rsi] ; My bug come from here
cmp byte [rdi], al ; and from here
je strchr_end
cmp byte[rdi], 0
jz strchr_nul
inc rdi
jmp strchr_loop
strchr_end:
mov rax, rdi
mov rsp, rbp
pop rbp
ret
strchr_nul:
mov rax, 0
mov rsp, rbp
pop rbp
ret
当我执行此操作时,我遇到了一个分段错误
但是当我用值'l'代替use al替换bug行时,程序正在运行
cmp byte [rdi], 'l' ; It's working
S1代表rdi,“l”代表rsi。当我在代码中手动输入“l”时,我的代码正在工作。您的第二个参数是
char
,但您的汇编代码似乎将其视为char*
。好的,所以我使用了sil,rsi的8位寄存器,现在它正在工作。谢谢您使用cmp字节[rdi],sil
(并删除mov al,[rsi]
)*我看你明白了:)