如何使用 compare + je 计算匹配项? [英] How to count matches using compare + je?

查看:20
本文介绍了如何使用 compare + je 计算匹配项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个代码来计算一个字符串中有多少个单词.如何使用 je 增加寄存器?

I am writing a code that counts how many words are in a string. How can I increase a register using je?

例如:

cmp a[bx+1],00h
je inc cx

推荐答案

je 是有条件的跳转.与 ARM 不同,x86 不能根据任意条件直接预测另一条指令.没有任何一条机器指令可以像 je inc cx 或 ARM 风格的 inceq cx 那样做任何事情.

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:

如果您有一个 386 兼容的 CPU,则可以选择无分支选项.它需要一个额外的寄存器:

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 兼容 CPU 具有 cmovccfcmovcc.与 setcc (386) 一起,jcc (8086) 和 loopcc (8086),这些是 x86 唯一的条件检查指令.(条件位存储在 FLAGS 寄存器中,您可以在其中直接访问它们,但这通常不太方便.)


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.)

这篇关于如何使用 compare + je 计算匹配项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆