Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Assembly 装配strlen输入探针_Assembly_Nasm - Fatal编程技术网

Assembly 装配strlen输入探针

Assembly 装配strlen输入探针,assembly,nasm,Assembly,Nasm,我对x86汇编编程非常陌生,对其复杂性一无所知。 假设我在节.bss下声明了一个变量 name resb 20 我想从用户那里得到一个名称输入: ; gets name mov eax, 3 mov ebx, 0 mov ecx, name int 80h 我只是想知道通过stdin输入的长度是否存储在其中一个寄存器中?包括回车在内算吗?长度值存储在al中,我想是吧?还是bl?如果是的话,我可以这样储存吗 mov byte[nameLen], al 其中nameLen是在.bss节下声明的

我对x86汇编编程非常陌生,对其复杂性一无所知。 假设我在节.bss下声明了一个变量

name resb 20
我想从用户那里得到一个名称输入:

; gets name
mov eax, 3
mov ebx, 0
mov ecx, name
int 80h
我只是想知道通过stdin输入的长度是否存储在其中一个寄存器中?包括回车在内算吗?长度值存储在al中,我想是吧?还是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。

这里有两个简单易懂的示例,介绍如何在汇编中使用
stdin
stdout

  • 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\u read
函数返回读取的字符数,因此,调用该函数后,该数字在
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

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

但是读取后如何检索输入字符串的长度?任何输入字符串都可以具有用户指定的任意长度。当我将字符串重新打印到stdout时,我希望有那个长度。不过解释得很详细。谢谢我用这个例子更新了我的帖子,以检索read.yep中的字符数,这就解决了问题!这一行
mov-edx,eax
让我免了很多麻烦。现在我知道读取的字节存储在eax寄存器中。谢谢实际上,它是存储在
eax
:)中的每个调用函数的返回。