问题
I am writing a code that counts how many words are in a string. How can I increase a register using je?
For example:
cmp a[bx+1],00h
je inc cx
回答1:
je is a conditional jump. Unlike ARM, x86 can't directly predicate another single instruction based on an arbitrary condition. There's no single machine instruction that can do anything like je inc cx
or ARM-style inceq cx
.
Instead you need to build the logic yourself by conditionally branching over other instruction(s).
If you want to increase a register if two numbers compare equal, try something like this:
cmp a[bx + 1], 00h ; compare numbers
jne .noteq ; if they are different, skip
inc cx ; the increment
.noteq:
A branch-free option is possible if you have a 386-compatible CPU. It requires an extra register:
xor ax, ax ; clear register
cmp a[bx + 1], 00h ; compare numbers
sete al ; set al = 1 if the numbers are equal
add cx, ax ; increment cx if numbers are equal
PPro compatible CPUs have cmovcc and fcmovcc. Along with setcc (386), jcc (8086), and loopcc (8086), those are x86's only condition-checking instructions. (Conditions bits are stored in the FLAGS register where you could access them directly, but usually that's less convenient.)
来源:https://stackoverflow.com/questions/57718517/how-to-count-matches-using-compare-je