assembly to compare two numbers

前端 未结 8 1941
情书的邮戳
情书的邮戳 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:
    

提交回复
热议问题