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?
This question is related to
assembly
x86
compare
machine-code
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 :)
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.
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.
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.
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:
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
Source: Stackoverflow.com