assembly to compare two numbers

159,236

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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:

Solution 5

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.

Share:
159,236
Alex Gordon
Author by

Alex Gordon

Check out my YouTube channel with videos on Azure development.

Updated on July 21, 2022

Comments

  • Alex Gordon
    Alex Gordon almost 2 years

    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?

  • Jimmy Chandra
    Jimmy Chandra almost 15 years
    dont forget to do CMP register1, register2 before doing the jump. MOV AX, 1; MOVE BX, 2; CMP AX, BX; JLE somewhere..
  • CTS_AE
    CTS_AE over 10 years
    Thanks, straight forward, showing equal greater, less, also just saw in your comments for greater than equal and less than equal
  • Unihedron
    Unihedron over 9 years
    This does not really answer this specific question.
  • Bagherani
    Bagherani almost 8 years
    Is there any performance difference between these JLT and JMP operators?
  • Mahmud Ahmad
    Mahmud Ahmad over 7 years
    What would an assembler generally do after the comparison? Would it return a value, that is usable, and/or store a value in a certain register?
  • Ira Baxter
    Ira Baxter over 7 years
    When the CMP instruction executes, on most machines it produces a result e.g., "less", "greater", "equal" and put it in a special "conditions" register. The programmer normally writes a CMP instruction, followed by "JMP on condition" (e.g, "JE" or "jmp equal") which inspects the conditions register and causes program flow to change to the tarfet of the jmp instruction if the condition is true. The assembler does have to do anything; it is the responsibility of the program to write the instructions one after another. The assembler merely converts each instruction source line into binary.
  • Mahmud Ahmad
    Mahmud Ahmad over 7 years
    ah okay! Thanks for the explanation. That's makes what I wanna do easier than I thought it would be