Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.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 如何计算汇编代码的位数?_Assembly - Fatal编程技术网

Assembly 如何计算汇编代码的位数?

Assembly 如何计算汇编代码的位数?,assembly,Assembly,假设我有一个用汇编语言编写的程序,它从用户那里获取一个输入句子(数字和字母的组合),下一行将显示句子中的小写字母数。同时显示句子中的位数 我的问题是:如何制作计数指令来计数数字和字母 我假设您指的是x86程序集,字符串以null结尾 mov eax, STRING_VARIABLE xor ebx, ebx xor ecx, ecx .loop: mov dl, [eax] cmp dl, 0 jz .end cmp dl, '0' jb .notdigit cmp d

假设我有一个用汇编语言编写的程序,它从用户那里获取一个输入句子(数字和字母的组合),下一行将显示句子中的小写字母数。同时显示句子中的位数


我的问题是:如何制作计数指令来计数数字和字母

我假设您指的是x86程序集,字符串以null结尾

mov eax, STRING_VARIABLE
xor ebx, ebx
xor ecx, ecx
.loop:
  mov dl, [eax]
  cmp dl, 0
  jz .end

  cmp dl, '0'
  jb .notdigit
  cmp dl, '9'
  ja .notdigit
  inc ecx
  jmp .notlowercase
  .notdigit:
  cmp dl, 'a'
  jb .notlowercase
  cmp dl, 'z'
  ja .notlowercase
  inc ecx
  .notlowercase:

  inc eax
  jmp .loop
.end:
; ebx contains the lowercase letter count
; ecx contains the digit count

如果它是一个Pascal字符串,字符串长度作为第一个字节,您将修改如下:

mov eax, STRING_VARIABLE
xor ebx, ebx  ; A tiny bit quicker and shorter than mov ebx,0
xor ecx, ecx
mov dh,[eax]  ; dh is loop counter based on string length
inc eax       ; move onto the string data
.loop:
  cmp dh,0
  jz .end
  .
  .
  .
.notlowercase:
  dec dh
  jmp .loop
.end:

我认为梅尔达德对正在努力实现的目标有着大致的了解

不过,只有一些观察结果--

在“inc ecx”之后跳转到.notlowercase可以节省几个周期,可能是一个疏忽--

我认为最后一个inc-ecx应该是inc-ebx

对上/下测试稍加改动,因为这只是字母/数字字符,在.notdigit标签之后,小写测试可以替换为

.notdigit:
    and   dl, 0x20
    jz   .notlowercase
    inc   ebx
.notlowercase:

只要我的2美分——:)

这也会给Unicode字符串带来不正确的结果。考虑到UTF-16,您需要将eax增加2。如果需要使用高字节集计算字符数,也需要考虑这一点