在此循环的第二次迭代中发生段错误移动 (%eax)、%ebx?
Segfault moving (%eax), %ebx on the second iteration of this loop?
我是汇编新手,我正在尝试一次打印一个字符串的一个字符,到目前为止,我已经做到了。
.equ STDOUT,1
.equ WRITE,4
.equ EXIT,1
char_string:
.asciz "hello, world"
.text
.globl _start
_start:
movl $char_string, %eax
call print_str
movl $EXIT, %eax
int [=10=]X80
print_str:
mov (%eax), %ebx
movl $WRITE, %eax
movl $STDOUT, %ebx
movl $char_string, %ecx
movl , %edx
int [=10=]x80
inc %eax
cmpl [=10=], %ebx
jne print_str
je out_of_loop
out_of_loop:
ret
但是,当我尝试编译时,我在该行遇到了分段错误
move (%eax), %ebx
这有什么问题?我该如何解决?我正在尝试将字符串的尖字符移动到 %ebx 以进行打印,然后我增加 eax 以移动到字符串中的下一个字符。
直接导致崩溃的原因是eax
作为系统调用的return值。但是,您的代码在其他方面也是错误的。我评论了你的代码:
print_str:
mov (%eax), %ebx # loads 4 bytes not 1
movl $WRITE, %eax
movl $STDOUT, %ebx # overwrites ebx you loaded
movl $char_string, %ecx # uses the starting address instead of iterating
movl , %edx
int [=10=]x80
inc %eax # eax is return value of system call by now
cmpl [=10=], %ebx # ebx is $STDOUT, see above
jne print_str
je out_of_loop # makes no sense to jump to next instruction
out_of_loop:
ret
可能的解决方案是:
print_str:
mov %eax, %ecx # address of char to print
movl $STDOUT, %ebx
movl , %edx
print_str_loop:
cmpb [=11=], (%ecx) # test for terminating zero byte
je out_of_loop
movl $WRITE, %eax # reload eax as it is return value from a previous iteration
int [=11=]x80
inc %ecx # point to next character
jmp print_str_loop
out_of_loop:
ret
我是汇编新手,我正在尝试一次打印一个字符串的一个字符,到目前为止,我已经做到了。
.equ STDOUT,1
.equ WRITE,4
.equ EXIT,1
char_string:
.asciz "hello, world"
.text
.globl _start
_start:
movl $char_string, %eax
call print_str
movl $EXIT, %eax
int [=10=]X80
print_str:
mov (%eax), %ebx
movl $WRITE, %eax
movl $STDOUT, %ebx
movl $char_string, %ecx
movl , %edx
int [=10=]x80
inc %eax
cmpl [=10=], %ebx
jne print_str
je out_of_loop
out_of_loop:
ret
但是,当我尝试编译时,我在该行遇到了分段错误
move (%eax), %ebx
这有什么问题?我该如何解决?我正在尝试将字符串的尖字符移动到 %ebx 以进行打印,然后我增加 eax 以移动到字符串中的下一个字符。
直接导致崩溃的原因是eax
作为系统调用的return值。但是,您的代码在其他方面也是错误的。我评论了你的代码:
print_str:
mov (%eax), %ebx # loads 4 bytes not 1
movl $WRITE, %eax
movl $STDOUT, %ebx # overwrites ebx you loaded
movl $char_string, %ecx # uses the starting address instead of iterating
movl , %edx
int [=10=]x80
inc %eax # eax is return value of system call by now
cmpl [=10=], %ebx # ebx is $STDOUT, see above
jne print_str
je out_of_loop # makes no sense to jump to next instruction
out_of_loop:
ret
可能的解决方案是:
print_str:
mov %eax, %ecx # address of char to print
movl $STDOUT, %ebx
movl , %edx
print_str_loop:
cmpb [=11=], (%ecx) # test for terminating zero byte
je out_of_loop
movl $WRITE, %eax # reload eax as it is return value from a previous iteration
int [=11=]x80
inc %ecx # point to next character
jmp print_str_loop
out_of_loop:
ret