简体   繁体   中英

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 

I`m interested how to reduce this amount of cmp using interval and ASCII codes for numerals. Thanks.

ASCII codes are numbers. When you write '0', the assembler transforms it to 30h = 48d. As you see in this ASCII table the letters '0' to '9' are represented by the consecutive numbers 30h..39h. So you can reverse your check: If al is below '0' or al is above '9', then goto error . You need only two comparisons:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM