如何检查用户输入的结尾是否以“?”结尾”。“”!“

问题描述 投票:0回答:1

从用户处获得新消息。验证消息以确保它以大写字母开头,并以句点('。'),问号('?')或感叹号('!')结尾。如果输入无效,请向用户显示错误消息以拒绝输入(请改用先前的消息)。否则,如果有效,请替换原始消息。

我检查了第一个有效的字符,现在要检查字符串的末尾,我将输入字符的长度加到输入的负-1处,但这会改变用户输入的第一个字符的值。

section .data
        text1 db "ENTER TEXT: "
        len equ $ - text1
        text2 db "THIS IS WHAT YOU ENTERED: "
        text3 db "invalid message, keeping curent "


section .bss
        character resb 255

section .text
        global main

main:
        mov r11, text2

        mov rax, 1
        mov rdi, 1
        mov rsi, text1
        mov rdx, 12
        syscall
        call validate


validate:

         mov rax, 0
        mov rdi, 0
        mov rsi, character
        mov rdx, 10
        syscall                                                                                                                                                                
        mov r8, rax

        mov rdi, character
        mov al, [character]
        mov ah, 'A'
        cmp al, ah
        jl invalid

        cmp al, 90
        jg invalid

        add rax, r8             ;                                                                                                                                                                           
        sub rax, 1

        cmp rax, '!'
        je valid
        cmp rax, '.'
        je valid
        cmp rax, '?'
        je valid
valid:

        mov r11, character

        mov rax, 1
        mov rdi, 1
        mov rsi, r11
        mov rdx, r8
        syscall
        jmp exit

invalid:

        mov rax, 1
        mov rdi, 1
        mov rsi, text3
        mov rdx, 12
        syscall
        jmp exit

exit:

        mov rax, 60
        xor rdi, rdi
        syscall
assembly x86-64 nasm
1个回答
1
投票
mov cl, [rsi] ; RSI already points to first character cmp cl, 'A' jb invalid cmp cl, 'Z' ja invalid mov al, [rsi+rax-2] ; RAX = # of characters entered cmp al, '.' jz valid cmp al, '?' jz valid cmp al, '!' jnz Error

现在,尚未更改前导字符,并且通过不必要地设置其他指针来节省一些字节。

© www.soinside.com 2019 - 2024. All rights reserved.