assembly to compare two numbers

前端 未结 8 1901
情书的邮戳
情书的邮戳 2020-12-10 02:35

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 bi

相关标签:
8条回答
  • 2020-12-10 02:53

    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:
    
    0 讨论(0)
  • 2020-12-10 02:54

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

    0 讨论(0)
  • 2020-12-10 02:56
    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 
    
    0 讨论(0)
  • 2020-12-10 03:00

    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.

    0 讨论(0)
  • 2020-12-10 03:03

    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.

    0 讨论(0)
  • 2020-12-10 03:07

    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
    

    0 讨论(0)
提交回复
热议问题