2011-03-27 79 views
1

问候, 我正在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前将

+0

你在用ebx做什么? [ebx]意味着访问内存中的数据,其地址在ebx ?,我没有看到你在ebx中加载地址..如果我错了,请纠正我的错误 – Zimbabao 2011-03-27 17:36:00

+0

你是如何编译的?什么命令行? – BlackBear 2011-03-27 17:37:42

+0

@津巴布奥:是的,他在调用func之前加载ebx – BlackBear 2011-03-27 17:38:15

回答

0

试试这个:

mov ebx, offset string 

好像你正在使用Intel语法。在AT & T语法mov $string,%ebx将是正确的,因为常量总是立即的,但这不会发生在英特尔的

+0

在AT&T语法中它将是mov $ string,%ebx – ughoavgfhw 2011-03-27 18:36:45

+0

@ughoavgfhw:肯定 – BlackBear 2011-03-27 18:40:33

0

这是变得相当陈旧...但你的问题是,“str_input”不输入zero-终止字符串!