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?
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?
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.
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.
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:
JLT
and JMP
operators? –
Exciting JMP
jumps unconditionally, without checking the sign bit like JLT
does, so it does "more"... –
Irbm 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.
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.
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
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
© 2022 - 2024 — McMap. All rights reserved.