Arashigor
Arashigor

Reputation: 171

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.

Upvotes: 1

Views: 1213

Answers (1)

rkhb
rkhb

Reputation: 14409

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:

Upvotes: 4

Related Questions