assembly to compare two numbers
Asked Answered
J

8

22

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?

Jacqulynjactation answered 14/7, 2009 at 4:13 Comment(1)
Assembler Syntax and Machine Code depend on the machine architecture you're using...Abrahamsen
F
23

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

Fortyish answered 7/9, 2013 at 7:2 Comment(1)
Thanks, straight forward, showing equal greater, less, also just saw in your comments for greater than equal and less than equalAllometry
P
12

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.

Parament answered 14/7, 2009 at 4:18 Comment(1)
dont forget to do CMP register1, register2 before doing the jump. MOV AX, 1; MOVE BX, 2; CMP AX, BX; JLE somewhere..Study
E
9

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.

Extrinsic answered 14/7, 2009 at 4:20 Comment(3)
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?Pomfrey
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.Extrinsic
ah okay! Thanks for the explanation. That's makes what I wanna do easier than I thought it would bePomfrey
C
7

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:
Copycat answered 14/7, 2009 at 4:19 Comment(2)
Is there any performance difference between these JLT and JMP operators?Exciting
Well, JMP jumps unconditionally, without checking the sign bit like JLT does, so it does "more"...Irbm
R
4

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.

Ralph answered 14/7, 2009 at 4:19 Comment(0)
N
1

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.

Nisan answered 14/7, 2009 at 4:41 Comment(0)
A
1

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

Ammonium answered 30/11, 2016 at 20:30 Comment(0)
R
-3
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 
Rockhampton answered 7/9, 2014 at 6:18 Comment(1)
This does not really answer this specific question.Prize

© 2022 - 2024 — McMap. All rights reserved.