从用户处获取新消息。验证消息以确保它以大写字母开头,并以句点('。'),问号('?')或感叹号('!')结尾。如果输入无效,请向用户显示错误消息以拒绝输入(请改用先前的消息)。否则,如果有效,请替换原始消息。
我检查了第一个有效的字符,现在要检查字符串的结尾,我将输入字符的长度加到输入减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
答案 0 :(得分:2)
考虑简化您的代码,同时不要更改用户输入。 SYSCALL已经返回您输入的字符数,RSI不变。
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
现在,尚未更改前导字符,并通过不必要地设置其他指针来节省了一些字节。