How to work with unknown input length in x86 ASM?

So, most of the code works for me, but I can’t figure out exactly how to deal with the fact that the input sentence has an unknown length. I am new to assembly and all this is a bit confusing.

(Now I set it up as if the length was known as three characters, but obviously I need to change it).

.data       
input_msg:  .ascii "Enter a random sentence: "
input_msg_len:  .long 25
input_str:  .ascii  "???" # 3rd should get newline  
count:      .long 0 
newline:    .long 10    

.text               
.global _start          
_start:             

# prompt for input
    mov $4, %eax    # prompt for input
    mov $1, %ebx
    mov $input_msg, %ecx
    mov input_msg_len, %edx
    int $0x80
# get input
    mov $3, %eax    # 3 to request "read"
    mov $0, %ebx    # 0 is "console" (keyboard)
    mov $input_str, %ecx # input buffer addr
    mov $3, %edx    # number of symbols typed in
    int $0x80       # Go do the service!

again1:
    mov $input_str, %ecx    
    add count, %ecx # count is offset from input_str beginning

    mov $4, %eax    # to write
    mov $1, %ebx    # to console display
    mov $1, %edx    # 1 byte to write
    int $0x80   # Do it!

    push    %ecx        # push onto stack   

    incl    count   # increment count

    cmp $3, count   # compare lengths
    jnz again1     # jmp again if not 0 (no difference)

    mov $0, %edi    # use edi as loop counter

    mov $4, %eax    # print out msg
    mov $1, %ebx    # etc.
    mov $1, %edx    # length
    int $0x80       # OS, serve!

again2:     
    pop %ecx    

    mov $4, %eax    # print out msg
    mov $1, %ebx    # etc.
    mov $1, %edx    # length
    int $0x80       # OS, serve!        

    inc %edi    # increment edi 
    cmp count, %edi # compare lengths
    jnz again2  # jmp again if not 0 (no difference)

# print newline
    mov $4, %eax    # print out msg
    mov $1, %ebx    # etc.
    mov $newline, %ecx  # addr
    mov $1, %edx    # length
    int $0x80       # OS, serve!
# exit
    mov $1, %eax    # exit
    int $0x80       # OS, serve!    

Basically, I want to know how to make the code work for any sentence, not just three characters?

+5
source share
2 answers

You just need to allocate a longer buffer for input_strand read the amount of effectively loaded text found in eax after reading syscall.

, , , :
: , , , , (, ), ). , 132, , .

...
input_str:  db  132 dup(?)  # 132 bytes buffer for input string
input_str_len: .long        # length of the string effectively read from user
...
# get input
    mov $3, %eax    # 3 to request "read"
    mov $0, %ebx    # 0 is "console" (keyboard)
    mov $input_str, %ecx # input buffer addr
    mov $131, %edx    # _Max_ number of bytes accepted in input_str
    int $0x80       # Go do the service!

    move %eax, $input_str_len    # save nb of bytes effectively read
...
    #you can then use input_str_len to control when to exit processing loop etc.
+1

... sys_brk %ebx= 0. "break" - . 4k sys_brk . sys_read . 4k ( %eax sys_read), "break" sys_brk, ... . "" ...

- "" ! " ". , sys_read ( ) , (0xA). , %edx , . 3- . "abcls". , , "ls" . , "rm" - ! sys_read , %eax %edx, . %eax= %edx ( ), LF (0xA), . , sys_read , LF. , ""...

Nasm, , AT & T...:)

0

All Articles