NASM分段错误问题
问候, 我正在Linux下的NASM上编写,我遇到以下问题:我必须编写一个简单的程序,其中用户必须输入字符串,将其传递给返回其大小的函数:
代码是:
%include "macros.inc"
section .data
prompt1 db "Enter string: ",0
prompt1len equ $-prompt1
prompt2 db "The size of string is: ",0
prompt2len equ $-prompt2
section .bss
string resb 20
section .text
global _start
_start: str_output prompt1len,prompt1
str_input string
mov ebx,string
call func
str_output prompt2len,prompt2
output eax
exit 0
func:
xor eax,eax
et **cmp byte [ebx],0h**
je end
inc eax
inc ebx
jmp et
end dec eax
ret
这是宏文件:
%macro exit 1
mov eax,1
mov ebx,%1
int 80h
%endmacro
%macro int_input 1
mov eax,3
mov ebx,0
mov ecx,%1
mov edx,1
int 80h
and eax,0Fh
mov %1,eax
%endmacro
%macro str_input 1
mov eax,3
mov ebx,1
mov ecx,%1
mov edx,20
int 80h
% endmacro
%macro output 1
mov eax,%1
or eax,30h
mov %1,eax
mov eax,4
mov ebx,1
mov ecx,%1
mov edx,1
int 80h
%endmacro
%macro str_output 2+
mov eax,4
mov ebx,1
mov ecx,%2
mov edx,%1
int 80h
%endmacro
我调试了程序,问题出在指令 cmp byte [ebx],0h 中,因为当我启动程序时,它在 str_input 之后打印出分段错误。我没有发现任何问题,但也许我的地址有误。
Greetings,
I'm writing on NASM under linux, and I have the following problem:I have to write a simple program, in which user must enter string, pass it to function which returns it's size:
The code is:
%include "macros.inc"
section .data
prompt1 db "Enter string: ",0
prompt1len equ $-prompt1
prompt2 db "The size of string is: ",0
prompt2len equ $-prompt2
section .bss
string resb 20
section .text
global _start
_start: str_output prompt1len,prompt1
str_input string
mov ebx,string
call func
str_output prompt2len,prompt2
output eax
exit 0
func:
xor eax,eax
et **cmp byte [ebx],0h**
je end
inc eax
inc ebx
jmp et
end dec eax
ret
And here is the macro file:
%macro exit 1
mov eax,1
mov ebx,%1
int 80h
%endmacro
%macro int_input 1
mov eax,3
mov ebx,0
mov ecx,%1
mov edx,1
int 80h
and eax,0Fh
mov %1,eax
%endmacro
%macro str_input 1
mov eax,3
mov ebx,1
mov ecx,%1
mov edx,20
int 80h
% endmacro
%macro output 1
mov eax,%1
or eax,30h
mov %1,eax
mov eax,4
mov ebx,1
mov ecx,%1
mov edx,1
int 80h
%endmacro
%macro str_output 2+
mov eax,4
mov ebx,1
mov ecx,%2
mov edx,%1
int 80h
%endmacro
I debugged the program and the problem is in instruction cmp byte [ebx],0h ,because when I start the program it prints out segmentation fault, after str_input. I don't see anything wrong, but maybe I've mistake with the addressation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在调用
func
之前尝试一下:您似乎正在使用 intel 语法。在 AT&T 语法中
mov $string,%ebx
是正确的,因为常量总是立即的,但在 intel 中不会发生这种情况Try this in your before calling
func
:it seems you're using intel syntax. In AT&T syntax
mov $string,%ebx
would be correct because constants are always immediate, but this doesn't happen in intel's这变得很陈旧......但你的问题是“str_input”不输入以零结尾的字符串!
This is getting quite stale... but your problem is that "str_input" doesn't input a zero-terminated string!