How to count matches using compare + je?

僤鯓⒐⒋嵵緔 提交于 2021-02-05 09:23:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!