装配中的 CMP 和 jmp 变化

CMP and jmp variations in assembly

 cmp al,'0'
 je true
 cmp al,'1'
 je true
 cmp al,'2'
 je true
 cmp al,'3'
 je true
 cmp al,'4'
 je true
 cmp al,'5'
 je true
 cmp al,'6'
 je true
 cmp al,'7'
 je true
 cmp al,'8'
 je true
 cmp al,'9'
 je true
 jne error 

我很感兴趣如何使用数字的间隔和 ASCII 代码来减少 cmp 的数量。 谢谢。

A​​SCII 码是数字。当您写入“0”时,汇编器会将其转换为 30h = 48d。正如您在 this ASCII table 中看到的,字母“0”到“9”由连续的数字 30h..39h 表示。因此,您可以撤销检查:如果 al 低于“0”或 al 高于“9”,则转到 error。你只需要两个比较:

cmp al,'0'
jb error      ; jump if below
cmp al,'9'
ja error      ; jump if above
true: