4

So I am working on an x86 Assembly program for Linux using NASM. This program basically asks the user for their name and their favorite color. After doing this and storing the two strings in variables declared in the .bss section, the program prints "No way name of user, favorite color is my favorite color, too!

The problem I am having is that there are enormous spaces in the output because I do not know how long the string was that the user entered, only the length that I declared the buffer to be.

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

The code for what I am doing is above. Does anyone have a good method for finding the length of the entered string, or of taking in a character at a time to find out the length of the string?

Thank you in advance.

nmagerko
  • 6,586
  • 12
  • 46
  • 71
  • `int 80` is `int 0x50`. You want `int 0x80`. (possible canonical duplicate for that: [Assembler sysTime giving error on executing](https://stackoverflow.com/a/39412096)) – Peter Cordes Jul 28 '21 at 22:07
  • This is one of the most useful sample assembly programs I've found so far. Very helpful in learning to write useful programs! – Rick Henderson Jan 25 '22 at 00:50

1 Answers1

2

After int80 in getname returns, EAX will contain the number of bytes actually read, or a negative error indication.

You should

  1. check for error return
  2. store the return value, as it gives you the length of input

Equivalent code in 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
}
Employed Russian
  • 199,314
  • 34
  • 295
  • 362
  • I have tried, but for some reason I always get the return value of whatever I declared the variable to be in .bss.... am I doing something wrong? – nmagerko Oct 23 '11 at 18:14
  • @nmagerko: are you saying that after int 80, EAX doesn't have the length of the input string? – 龚元程 Oct 24 '11 at 14:33