汇编语言 从键盘输入一系列以$为结束符的字符串,然后对其中的非数字字符计数,并显示出计数结果

思路:

调用mov ah, 01h和int 21h接收键盘输入的字符,将输入的字符接收至al寄存器

注意:键盘输入的为字符的ASCII码

因此,字符1-9转化为ASCII码需要增加30h,即30h-39h

在不断接收字符的循环过程中,我们只需要判断:

1. 若输入的字符为'$',即ASCII码为24,即cmp dl, 24h范围0,则jz exit

2. 若输入的字符在0-9之间即在小于30h或大于39h的范围内,则分别jl count_up和jg count_up。其中,count_up使计数器加一并继续循环

3. 否则continue,继续循环

 

汇编代码如下:

;从键盘输入一系列以$为结束符的字符串,然后对其中的非数字字符计数,并显示出计数结果
dataseg segment
    buff db 50 Dup(?)
    cnt dw 0
    mess db 'The number of the character', '13', '10', '$'

    character label byte
    max db 2
    act db ?
    mon db 2 dup(?)

dataseg ends


program segment
	assume cs:program,ds:dataseg
main proc far
start:
    push ds 
    sub ax, ax
    sub bx, bx
    push bx
    push ax
    mov ax, dataseg
    mov ds, ax
    mov bl, 0
input:
;输入ASCII码
	lea dx,character
	mov ah,0ah
	int 21h
    cmp act, 0
    je exit
    mov al, mon
    cmp al, 24h
    jz exit
    cmp al, 30h
    jl count_up
    cmp al, 39h
    jg count_up
    loop input


count_up:
    inc bl
    ;输出计数器的内容
    call crlf;输出换行回车
    push dx
    push ax
    mov al, bl
    sub al, 30h
    mov dl, al
    mov ah, 02h
    int 21h
    pop ax
    pop dx
    call crlf;输出换行回车
    loop input

crlf proc near
    push dx
    mov dl,13
	mov ah,02
	int 21h
	mov dl,10  
	mov ah,02
	int 21h
    pop dx
    ret
crlf endp

exit:
    mov ax, 4c00h
    int 21h
    ret
program ends
main endp
    end start

 

你可能感兴趣的:(汇编语言)