Search code examples
stringpointersassemblybuffereol

Move to end of string within buffer - Assembly Languge


I am trying to take in a string and then see if the last value in the string is an EOL character. I figured I would use the length of the string read in and then add it to the address of the buffer to find the last element. This does not seem to work.

Edit: I apologize that I did not include more information. Variables are defined as such:

  %define BUFLEN 256

  SECTION .bss                    ; uninitialized data section
    buf:    resb BUFLEN                     ; buffer for read
    newstr: resb BUFLEN                     ; converted string
    rlen:   resb 4   

Then a dos interrupt is called to accept a string from the user like so:

    ; read user input
    ;
    mov     eax, SYSCALL_READ       ; read function
    mov     ebx, STDIN              ; Arg 1: file descriptor
    mov     ecx, buf                ; Arg 2: address of buffer
    mov     edx, BUFLEN             ; Arg 3: buffer length
    int     080h

Then we go into our loop:

  test_endl:
    mov     ecx, [rlen]
    mov     esi, buf
    add     esi, ecx                ; i want to move 'rlen' bytes into buf
    mov     al, [esi]               ; to see what the last element is
    cmp     al, 10                  ; compare it to EOL
    jnz     L1_init
    dec     ecx                     ; and then decrease 'rlen' if it is an EOL
    mov     [rlen], ecx\

I am user NASM to compile and writing for an i386 machine.


Solution

  • Adding the length of the string to the address of the buffer gives access to the byte behind the string.

    Based on you saying that

    • you want to see if the last value in the string is an EOL character
    • you aim to decrease 'rlen' if it is an EOL (*)

    I conclude that you consider the possible EOL character part of the string as defined by its length rlen. If you don't then (*) doesn't make sense.

    Use mov al,[esi-1] to see what the last element is!

    test_endl:
      mov     ecx, [rlen]
      mov     esi, buf
      add     esi, ecx         ; i want to move 'rlen' bytes into buf
      mov     al, [esi-1]      ; to see what the last element is
      cmp     al, 10           ; compare it to EOL
      jnz     L1_init
      dec     ecx              ; and then decrease 'rlen' if it is an EOL
      mov     [rlen], ecx