2011-10-23 7 views
3

그래서 NASM을 사용하는 Linux 용 x86 어셈블리 프로그램에서 작업하고 있습니다. 이 프로그램은 기본적으로 사용자에게 이름과 좋아하는 색을 묻습니다. .bss라고 섹션에서 선언 된 변수에있는 두 개의 문자열을이 일을하고 저장 후 프로그램 인쇄 "사용자 수있는 방법 이름, 좋아하는 색깔도, 내 마음에 드는 색상입니다! 오전 데 문제가사용자 입력 수정 - x86 Linux 어셈블리

출력에 엄청난 공백이 있음을 나는 문자열은 사용자가, 내가 버퍼가 선언에만 길이를 입력했는지 얼마나 오래 모르기 때문에.

section .data 
    greet:  db 'Hello!', 0Ah, 'What is your name?', 0Ah ;simple greeting 
    greetL:  equ $-greet         ;greet length 
    colorQ:  db 'What is your favorite color?'   ;color question 
    colorL:  equ $-colorQ         ;colorQ length 
    suprise1: db 'No way '        
    suprise1L equ $-suprise1 
    suprise3: db ' is my favorite color, too!', 0Ah 

section .bss 
    name:  resb 20          ;user's name 
    color:  resb 15          ;user's color 

section .text 
    global _start 
_start: 

    greeting: 
     mov eax, 4 
     mov ebx, 1 
     mov ecx, greet 
     mov edx, greetL 
     int 80            ;print greet 

    getname: 
     mov eax, 3 
     mov ebx, 0 
     mov ecx, name 
     mov edx, 20 
     int 80            ;get name 

    askcolor: 
     ;asks the user's favorite color using colorQ 

    getcolor: 
     mov eax, 3 
     mov ebx, 0 
     mov ecx, name 
     mov edx, 20 
     int 80 

    thesuprise: 
     mov eax, 4 
     mov ebx, 1 
     mov ecx, suprise1 
     mov edx, suprise1L 
     int 80 

     mov eax, 4 
     mov ebx, 1 
     mov ecx, name 
     mov edx, 20 
     int 80 

     ;write the color 

     ;write the "suprise" 3 

     mov eax, 1 
     mov ebx, 0 
     int 80 

내가 뭐하는 거지에 대한 위의 코드입니다 누구든지 입력 된 문자열의 길이를 찾거나 문자를 한 번에 가져 와서 길이를 알아내는 좋은 방법이 있습니까? 전자 문자열?

미리 감사드립니다.

답변

1

int80에서 getname이 반환 된 후 EAX은 실제로 읽은 바이트 수 또는 음수 오류 표시를 포함합니다.

당신이해야 오류 리턴

  • 저장 반환 값에 대한

    1. 체크 그것은 당신에게 입력의 길이를주기 때문에 C에서

    동등한 코드 :

    char name[20]; 
    int rc; 
    
    rc = syscall(SYS_read, 0, name, 20-1); // leave space for terminating NUL 
    if (rc < 0) { 
        // handle error 
    } else { 
        name[rc] = '\0';      // NUL terminate the string 
    } 
    
  • +0

    I 시도했지만, 어떤 이유로 나는 .bss에 변수가 있다고 선언 한 값의 반환 값을 얻는다 .... 나는 somet을하고있다. 힌트가 틀렸어? – nmagerko

    +0

    @nmagerko : int 80 이후에는 EAX가 입력 문자열의 길이를 가지고 있지 않습니까? –

    +0

    ^예. 이게 말이 돼? – nmagerko

    관련 문제