比较两个数字的汇编
确定两个数字中哪一个更大的汇编语法是什么?
它的较低级别(机器代码)是什么? 我们还能再低一点吗? 一旦我们达到位级别,会发生什么? 它是如何用0和1表示的?
What is the assembler syntax to determine which of two numbers is greater?
What is the lower level (machine code) for it? Can we go even lower? Once we get to the bit level, what happens? How is it represented in 0's and 1's?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
在 TASM(x86 汇编)中,它可能如下所示:
在本例中,它比较我们临时存储在寄存器 B 的较高部分和较低部分中的两个 8 位数字。或者,您也可以考虑使用
jbe
(如果 BL <= BH)或jge
/jae
(如果 BL >= BH)。希望有人觉得它有帮助:)
In TASM (x86 assembly) it can look like this:
in this case it compares two 8bit numbers that we temporarily store in the higher and the lower part of the register B. Alternatively you might also consider using
jbe
(if BL <= BH) orjge
/jae
(if BL >= BH).Hopefully someone finds it helpful :)
首先调用 CMP(比较)指令,然后调用以下指令之一:
jle - 如果小于或等于
则跳转到行
jge - 如果大于或等于则跳转到行
最低汇编器使用的是字节,而不是位(无论如何直接)。 如果您想了解位逻辑,您需要查看电路设计。
First a CMP (comparison) instruction is called then one of the following:
jle - jump to line if less than or equal to
jge - jump to line if greater than or equal to
The lowest assembler works with is bytes, not bits (directly anyway). If you want to know about bit logic you'll need to take a look at circuit design.
它因汇编程序而异。
大多数机器都提供寄存器,它们具有符号名称
像 R1 或 EAX(Intel x86)一样,并且有指令
诸如“CMP”之类的名称用于比较。 并进行比较
指令,有时你需要另一个操作数
一个寄存器,有时是一个文字。 通常是组装工
允许在指令右侧添加注释。
指令行如下所示:
您的汇编程序可能会有所不同。
对于 Microsoft X86 汇编器,您可以编写:
CMP EAX, 23 ; 将寄存器 EAX 与常量 23
或
CMP EAX, XYZ 进行比较; 将寄存器 EAX 与名为 XYZ 的内存位置的内容进行比较
通常,人们可以在操作数字段中写入复杂的“表达式”
使指令(如果有能力的话)能够寻址
记忆的方式多种多样。 但我认为这回答了你的问题。
It varies from assembler to assembler.
Most machines offer registers, which have symbolic names
like R1, or EAX (the Intel x86), and have instruction
names like "CMP" for compare. And for a compare
instruction, you need another operand, sometimes
a register, sometimes a literal. Often assemblers
allow comments to the right of instruction.
An instruction line looks like:
Your assembler may vary somewhat.
For the Microsoft X86 assembler, you can write:
CMP EAX, 23 ; compare register EAX with the constant 23
or
CMP EAX, XYZ ; compare register EAX with contents of memory location named XYZ
Often one can write complex "expressions" in the operand field
that enable the instruction, if it has the capability, to address
memory in variety of ways. But I think this answers your question.
基本技术(在大多数现代系统上)是将两个数字相减,然后检查结果的符号位,即查看结果是否大于/等于/小于零。 在汇编代码中,您通常只是根据状态进行分支,而不是直接获取结果(放入寄存器中):
The basic technique (on most modern systems) is to subtract the two numbers and then to check the sign bit of the result, i.e. see if the result is greater than/equal to/less than zero. In the assembly code instead of getting the result directly (into a register), you normally just branch depending on the state:
这完全取决于您所讨论的处理器,但它往往采用以下形式:
换句话说,设置相关标志的比较指令,后跟取决于这些标志的条件分支。
这通常是您编程所需的最低值。 如果您正在编写汇编程序,您只需要了解它的机器语言;如果您正在构建处理器,您只需了解微代码和/或电路设计。
This depends entirely on the processor you're talking about but it tends to be of the form:
In other words, a compare instruction to set the relevant flags, followed by a conditional branch depending on those flags.
This is generally as low as you need to get for programming. You only need to know the machine language for it if you're writing assemblers and you only need to know the microcode and/or circuit designs if you're building processors.
正如已经提到的,通常通过减法进行比较。
例如,X86 程序集/控制流。
在硬件级别,有特殊的数字电路用于进行计算,例如加法器。
As already mentioned, usually the comparison is done through subtraction.
For example, X86 Assembly/Control Flow.
At the hardware level there are special digital circuits for doing the calculations, like adders.
比较两个数字。 如果等于“Y”,则不等于则在屏幕上打印“否”“N”。 我用的是emu8086。 您可以使用 SUB 或 CMP 命令。
Compare two numbers. If it equals Yes "Y", it prints No "N" on the screen if it is not equal. I am using emu8086. You can use the SUB or CMP command.