问候, 我正在linux下编写NASM,我遇到了以下问题:我必须编写一个简单的程序,用户必须输入字符串,并将其传递给函数返回它的大小: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字节[EB x],0h,因为当我启动程序时,会在str_input之后打印出分段错误。我没有看到任何错误,但也许我误以为地址。调用func
前将
你在用ebx做什么? [ebx]意味着访问内存中的数据,其地址在ebx ?,我没有看到你在ebx中加载地址..如果我错了,请纠正我的错误 – Zimbabao 2011-03-27 17:36:00
你是如何编译的?什么命令行? – BlackBear 2011-03-27 17:37:42
@津巴布奥:是的,他在调用func之前加载ebx – BlackBear 2011-03-27 17:38:15