汇编语言——统计一个字符串中的大写字母、小写字母、数字和其他字符的个数,并显示...

;统计字符串中大写字母、小写字母、数字、其他字符的个数
DATAS SEGMENT
  buf db '12ADdf#gh592HKL*','$'
  tp1 db 0;大写字母个数
  tp2 db 0;小写字母个数
  tp3 db 0;数字的个数
  tp4 db 0;其他字符的个数

  str1 db 'the number of big is:','$'
  str2 db 'the number of small is:','$'
  str3 db 'the number of number is:','$'
  str4 db 'the number of other is:','$'
  str5 db 0dH,0aH,'$';换行

DATAS ENDS

STACKS SEGMENT
  ;此处输入堆栈段代码
STACKS ENDS

CODES SEGMENT
  ASSUME CS:CODES,DS:DATAS,SS:STACKS
  START:
    MOV AX,DATAS
    MOV DS,AX

    lea si, buf
    mov cx, 16;设置循环次数

     again:
      ;字符串结尾,结束程序
      cmp byte ptr[si],'&'
      je exit

      ;0-9
      cmp byte ptr[si],30h;小于30,其他字符加1
      jb L1
      cmp byte ptr[si],39h;大于39进一步比较
      jbe L2

      cmp byte ptr[si],41h
      jb L1
      cmp byte ptr[si],5AH
      jbe L3

      cmp byte ptr[si],61h
      jb L1
      cmp byte ptr[si],7AH
      jbe L4


    L1:
      inc tp4
      jmp L5
    L2:
      inc tp3
      jmp L5
    L3:
      inc tp1
      jmp L5
    L4:
      inc tp2
      jmp L5
    L5:
      add si,1
      loop again



    ;显示大写字母
    lea dx,str1
    mov ah,09h
    int 21h

    mov bl,tp1
    call disp ;调用子程序

    mov ah,09h
    lea dx,str5
    int 21h

    ;显示小写字母
    lea dx,str2
    mov ah,09h
    int 21h

    mov bl,tp2
    call disp ;调用子程序

    mov ah,09h
    lea dx,str5
    int 21h

    ;显示数字
    lea dx,str3
    mov ah,09h
    int 21h

    mov bl,tp3
    call disp ;调用子程序

    mov ah,09h
    lea dx,str5
    int 21h

    ;显示其他
    lea dx,str4
    mov ah,09h
    int 21h

    mov bl,tp4
    call disp ;调用子程序

    mov ah,09h
    lea dx,str5
    int 21h


    exit:
      MOV AH,4CH
      INT 21H

  disp PROC ;显示BX中的数
    mov ch,4
    roll:

      mov cl,4
      rol bx,cl
      mov dl,bl
      and dl,0fh
      cmp dl,9
      jbe next1
      add dl,07h
    next1:

       add dl,30h
      mov ah,02h
      int 21h
      dec ch
      jnz roll
    RET
  disp ENDP



CODES ENDS
  END START

 

 

我的CSDN博客地址:http://blog.csdn.net/qw963895582/article 

转载于:https://www.cnblogs.com/123qw/p/4621342.html

  • 6
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是汇编语言实现字符串统计的代码: ```asm section .data str db 'Hello, World! 123',0 len equ $-str section .bss uc resb 1 lc resb 1 num resb 1 section .text global _start _start: mov ecx, len ; 将字符串长度保存到 ecx 寄存器 mov esi, str ; 将字符串地址保存到 esi 寄存器 mov eax, 0 ; 将计数器清零 mov ebx, 0 ; 将计数器清零 mov edx, 0 ; 将计数器清零 loop_start: cmp byte [esi], 0 ; 判断是否到字符串末尾 je end_loop cmp byte [esi], 'A' ; 判断是否为大写字母 jb check_lower cmp byte [esi], 'Z' ja check_lower inc eax ; 大写字母计数器加一 jmp next_char check_lower: cmp byte [esi], 'a' ; 判断是否为小写字母 jb check_number cmp byte [esi], 'z' ja check_number inc ebx ; 小写字母计数器加一 jmp next_char check_number: cmp byte [esi], '0' ; 判断是否为数字 jb next_char cmp byte [esi], '9' ja next_char inc edx ; 数字计数器加一 next_char: inc esi ; 指针指向下一个字符 loop loop_start ; 继续处理下一个字符 end_loop: mov [uc], al ; 保存大写字母计数器的值 mov [lc], bl ; 保存小写字母计数器的值 mov [num], dl ; 保存数字计数器的值 ; 输出结果 mov eax, 4 mov ebx, 1 mov ecx, uc mov edx, 1 int 0x80 mov eax, 4 mov ebx, 1 mov ecx, lc mov edx, 1 int 0x80 mov eax, 4 mov ebx, 1 mov ecx, num mov edx, 1 int 0x80 ; 退出程序 mov eax, 1 xor ebx, ebx int 0x80 ``` 这段代码定义了一个字符串 `str`,并且使用 `ecx` 和 `esi` 寄存器分别保存了字符串长度和地址。然后,使用 `eax`、`ebx`、`edx` 寄存器分别作为大写字母小写字母数字的计数器。 接下来,使用 `loop` 指令和标签 `loop_start` 来循环处理字符串的每个字符。在每次循环,使用 `cmp` 指令判断当前字符是否为大写字母小写字母数字,并且根据判断结果分别增加相应的计数器。最后,将计数器的值保存到 `uc`、`lc`、`num` 变量,并且使用 `int 0x80` 指令将结果输出到屏幕上。 最后,使用 `int 0x80` 指令退出程序。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值