比较两个数字的汇编

发布于 2024-07-26 17:33:06 字数 102 浏览 1 评论 0原文

确定两个数字中哪一个更大的汇编语法是什么?

它的较低级别(机器代码)是什么? 我们还能再低一点吗? 一旦我们达到位级别,会发生什么? 它是如何用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

莫言歌 2024-08-02 17:33:06

在 TASM(x86 汇编)中,它可能如下所示:

cmp BL, BH
je EQUAL       ; BL = BH
jg GREATER     ; BL > BH
jmp LESS       ; BL < BH

在本例中,它比较我们临时存储在寄存器 B 的较高部分和较低部分中的两个 8 位数字。或者,您也可以考虑使用 jbe (如果 BL <= BH)或 jge/jae(如果 BL >= BH)。

希望有人觉得它有帮助:)

In TASM (x86 assembly) it can look like this:

cmp BL, BH
je EQUAL       ; BL = BH
jg GREATER     ; BL > BH
jmp LESS       ; BL < BH

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) or jge/jae (if BL >= BH).

Hopefully someone finds it helpful :)

血之狂魔 2024-08-02 17:33:06

首先调用 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.

苍风燃霜 2024-08-02 17:33:06

它因汇编程序而异。
大多数机器都提供寄存器,它们具有符号名称
像 R1 或 EAX(Intel x86)一样,并且有指令
诸如“CMP”之类的名称用于比较。 并进行比较
指令,有时你需要另一个操作数
一个寄存器,有时是一个文字。 通常是组装工
允许在指令右侧添加注释。

指令行如下所示:

<opcode>   <register> <operand>   ; comment

您的汇编程序可能会有所不同。

对于 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:

<opcode>   <register> <operand>   ; comment

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.

记忆で 2024-08-02 17:33:06

基本技术(在大多数现代系统上)是将两个数字相减,然后检查结果的符号位,即查看结果是否大于/等于/小于零。 在汇编代码中,您通常只是根据状态进行分支,而不是直接获取结果(放入寄存器中):

; Compare r1 and r2
    CMP $r1, $r2
    JLT lessthan
greater_or_equal:
    ; print "r1 >= r2" somehow
    JMP l1
lessthan:
    ; print "r1 < r2" somehow
l1:

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:

; Compare r1 and r2
    CMP $r1, $r2
    JLT lessthan
greater_or_equal:
    ; print "r1 >= r2" somehow
    JMP l1
lessthan:
    ; print "r1 < r2" somehow
l1:
浪漫之都 2024-08-02 17:33:06

这完全取决于您所讨论的处理器,但它往往采用以下形式:

cmp r1, r2
ble label7

换句话说,设置相关标志的比较指令,后跟取决于这些标志的条件分支。

这通常是您编程所需的最低值。 如果您正在编写汇编程序,您只需要了解它的机器语言;如果您正在构建处理器,您只需了解微代码和/或电路设计。

This depends entirely on the processor you're talking about but it tends to be of the form:

cmp r1, r2
ble label7

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.

老街孤人 2024-08-02 17:33:06

正如已经提到的,通常通过减法进行比较。
例如,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.

天荒地未老 2024-08-02 17:33:06

比较两个数字。 如果等于“Y”,则不等于则在屏幕上打印“否”“N”。 我用的是emu8086。 您可以使用 SUB 或 CMP 命令。

MOV AX,5h
MOV BX,5h
SUB AX,BX 
JZ EQUALS
JNZ NOTEQUALS

EQUALS:
MOV CL,'Y'
JMP PRINT

NOTEQUALS:
MOV CL,'N'

PRINT:
MOV AH,2
MOV DL,CL
INT 21H

RET

输入图片此处描述

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.

MOV AX,5h
MOV BX,5h
SUB AX,BX 
JZ EQUALS
JNZ NOTEQUALS

EQUALS:
MOV CL,'Y'
JMP PRINT

NOTEQUALS:
MOV CL,'N'

PRINT:
MOV AH,2
MOV DL,CL
INT 21H

RET

enter image description here

大海や 2024-08-02 17:33:06
input password program
.modle small
.stack 100h
.data
s pasword db 34
input pasword db "enter pasword","$"
valid db ?
invalid db?
.code
mov ax, @ data 
mov db, ax
mov ah,09h
mov dx, offest s pasword
int 21h
mov ah, 01h
cmp al, s pasword
je v
jmp nv
v:
mov ah, 09h
mov dx, offset valid 
int 21h
nv:
mov ah, 09h
mov dx, offset invalid 
int 21h
mov ah, 04ch 
int 21
end 
input password program
.modle small
.stack 100h
.data
s pasword db 34
input pasword db "enter pasword","$"
valid db ?
invalid db?
.code
mov ax, @ data 
mov db, ax
mov ah,09h
mov dx, offest s pasword
int 21h
mov ah, 01h
cmp al, s pasword
je v
jmp nv
v:
mov ah, 09h
mov dx, offset valid 
int 21h
nv:
mov ah, 09h
mov dx, offset invalid 
int 21h
mov ah, 04ch 
int 21
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文