【问题标题】:Assembly strlen input prob装配 strlen 输入问题
【发布时间】:2013-07-05 00:13:12
【问题描述】:

我是 x86 汇编编程的新手,不知道它的复杂性。 假设我在 section .bss

下声明了一个变量
name resb 20

我想从用户那里得到一个名字输入:

; gets name
mov eax, 3
mov ebx, 0
mov ecx, name
int 80h

我只是想知道通过标准输入的输入长度是否存储在其中一个寄存器中?它是否包括回车?长度值存储在我想?还是bl?如果是这样,我可以这样存储它吗?

mov byte[nameLen], al

nameLen 在 .bss 部分下声明如下

nameLen resb 1

我很想像这样重新打印字符串输入:

; excludes the carriage return from count
dec byte[nameLen]

mov eax, 4
mov ebx, 1
mov ecx, name
mov edx, nameLen
int 80h

请帮忙!谢谢!


我使用的是 x86 Ubuntu。

【问题讨论】:

    标签: assembly nasm


    【解决方案1】:

    这里有两个易于理解的示例,说明如何在汇编中使用stdinstdout

    • STDIN你是对的,输入长度存储在其中一个寄存器中:

      ; read a byte from stdin
      mov eax, 3           ; 3 is recognized by the system as meaning "read"
      mov ebx, 0           ; read from standard input
      mov ecx, name        ; address to pass to
      mov edx, 1           ; input length (one byte)
      int 0x80             ; call the kernel
      

      如果我没记错的话,stdin 不考虑回车。但是您应该对其进行测试以确定。

    • STDOUT你的实现是正确的,但我给你我的评论:

      ; print a byte to stdout
      mov eax, 4           ; the system interprets 4 as "write"
      mov ebx, 1           ; standard output (print to terminal)
      mov ecx, name        ; pointer to the value being passed
      mov edx, 1           ; length of output (in bytes)
      int 0x80             ; call the kernel
      

    我建议你尽可能多地评论你在汇编中所做的事情,因为很难回到你几个月前做的代码......

    编辑: 您可以检索使用 eax 寄存器读取的字符数。

    在 EAX 寄存器中返回整数值和内存地址。

    sys_read函数返回so读取的字符数,这个数字在函数调用后的eax中。

    这是一个使用eax的程序示例:

    section .data
            nameLen: db 20
    
    section .bss
            name:    resb 20
    
    section .text
            global _start
    
    _exit:
            mov eax, 1                ; exit
            mov ebx, 0                ; exit status
            int 80h
    
    _start:
            mov eax, 3                ; 3 is recognized by the system as meaning "read"
            mov ebx, 0                ; read from the standard input
            mov ecx, name             ; address to pass to
            mov edx, nameLen          ; input length
            int 80h
    
            cmp eax, 0                ; compare the returned value of the function with 0
            je  _exit                 ; jump to _exit if equal
    
            mov edx, eax              ; save the number of bytes read
                                      ; it will be passed to the write function
    
            mov eax, 4                ; the system interprets 4 as "write"
            mov ebx, 1                ; standard output (print to terminal)
            mov ecx, name             ; pointer to the value being passed
            int 80h
    
            jmp _start                ; Infinite loop to continue reading on the standard input
    

    这个简单的程序继续读取标准输入并在标准输出上打印结果。

    【讨论】:

    • 但是读取后如何获取输入字符串的长度呢?任何输入字符串都可以具有来自用户的任意长度。当我将字符串重新打印到标准输出时,我想要那个长度。虽然非常冗长的解释。谢谢!
    • 我用示例更新了我的帖子以检索读取的字符数。
    • 是的,解决了!这条线mov edx, eax 让我免于头疼。现在我知道读取的字节存储在 eax 寄存器中。谢谢! :D
    • 事实上,每个被调用函数的返回值都存储在eax :) 中。
    猜你喜欢
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 1970-01-01
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多