assembly - 如何处理 x86 ASM 中的未知长度输入?

标签 assembly x86 gnu

所以我的大部分代码都在工作,但我无法弄清楚如何准确处理输入句子长度未知的事实。我是组装新手,这有点令人困惑。

(现在我已将其设置为好像已知长度为三个字符,但显然我需要更改它。)

.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!    

基本上,我想知道的是如何让代码适用于任何句子,而不仅仅是一个 3 个字符长的句子?

最佳答案

您只需要为 input_str 分配一个更长的缓冲区。并读取有效读入的文本量,在 read 系统调用之后的 eax 中找到。

换句话说,您需要决定您可以接受的最大长度并将代码更改为如下所示:
注意:静态分配这样的短字符串是可以的,当然,如果您需要一个大缓冲区(比如从文件中获取数据),您可以改为动态分配缓冲区)。同样,对于键盘输入,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.

关于assembly - 如何处理 x86 ASM 中的未知长度输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15445579/

相关文章:

c++ - 如何优化非线性方程组的解?

c - 模拟 printf 堆栈弹出

linux - (GNU) 使用变量查找 -mtime

c# - "Unable to find a version of runtime to run this application"适用于 64 位 Windows 上的 32 位应用程序

c - GCC 如何初始化自动变量?它们保证为 0 吗?

c - -wrap 选项的 GNU 链接器通配符

assembly - 在代码执行时将未对齐写入机器代码中的立即操作数是否安全?

assembly - 我什么时候应该单独使用 AESIMC,而不是使用 AESDEC

c - 硬件处理器计数器重置不正确