2011-03-27 2 views
1

인사말,리눅스에서 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 후에 segmentation fault를 출력하기 때문이다. 나는 틀린 것을 보지 못했지만 어쩌면 그 연설과 실수를했을 것이다.

+0

ebx로 무엇을하고 있습니까?. [ebx]는 ebx에있는 주소의 메모리에있는 데이터에 액세스하는 것을 의미합니다. ebx에서 주소를로드하는 것을 보지 못합니다. 내가 잘못하면 올바른 주소가 표시됩니다. – Zimbabao

+0

어떻게 컴파일합니까? 무슨 명령 줄? – BlackBear

+0

@Zimbabao : 예, func – BlackBear

답변

0

func를 호출하기 전에 당신이 시도 : 당신이 인텔 구문을 사용하고있는 것 같다

mov ebx, offset string 

. AT &에서 T 문법 mov $string,%ebx은 정확합니다. 왜냐하면 상수는 항상 즉각적인 것이기 때문에 이것은 인텔에서 발생하지 않습니다.

+0

AT & T 구문에서 mov $ string, % ebx – ughoavgfhw

+0

@ughoavgfhw : 확실합니다. – BlackBear

0

이것은 꽤 오래되었습니다 ...하지만 문제는 "str_input" 종료 된 문자열!