CMP声明没有给出正确的结果

时间:2013-10-10 16:56:58

标签: assembly x86 masm irvine32

TITLE Allowance


INCLUDE irvine32.inc

.DATA
 welcomeMessage BYTE "Welcome To Allowance Calculator", 0 ;
 inputMessage BYTE "Please Enter Salary", 0 ;
 salary WORD ?
 allowance WORD ?
 total WORD ?

 ten WORD 10
 five WORD 5
 hundred WORD 100
 limit WORD 2D

 great BYTE "I'm in greater"

.CODE
main PROC
   MOV EAX, 0
   MOV EBX, 0
   MOV ECX, 0
   MOV EDX, 0
   Sub limit, 1D

   Call DumpRegs

   MOV EDX, offset welcomeMessage
   Call WriteString 

   MOV EDX, offset inputMessage
   Call WriteString
   Call ReadDec
   MOV salary, Ax

   cmp Ax, 100D
   Call DumpRegs
   Call WriteInt

   JLE lesser
   JG greater

   greater: 
    MOV EDX, offset great
    Call WriteString
    MOV EAX, 0
    MOV Ax, salary
    MUL ten
    DIV hundred

    MOV allowance, Ax
    Call WriteInt
    JMP end1

    lesser:
        MOV EAX, 0
        MOV Ax, salary
        MUL five
        DIV hundred

        MOV allowance, Ax
        Call WriteInt
        JMP end1

    end1:
        exit
main ENDP

结束主要

当我使用CMP命令进行比较时,它只带我去JGE ..无论价值是什么......对此有什么解决方案吗?

我也尝试使用JE并给予相同的输入,但即便如此,它也让我更加贴上标签。

1 个答案:

答案 0 :(得分:3)

cmp指令修改标志,跳转指令在标志状态下做出决定。这些标志可能会被这些call(仅适用于WriteInt)修改:

Cmp Ax, 100D
Call DumpRegs
Call WriteInt
JLE lesser
JG greater

jlejg的行为会出乎意料。您可以在使用pushf进行呼叫之前存储标记状态,然后使用popf将其恢复,例如:

Cmp Ax, 100D
pushf
Call DumpRegs
Call WriteInt
popf
JLE lesser
JG greater