2014-01-13 39 views
-2

我的问题是它不会比较第一个数字是否大于第二个数字。它只执行在这里我打印我做了一个程序,只会在第一个数字大于第二个数字时才会添加两个数字,而不是比较

mov ah,9 
    lea dx,str1 
    int 21h   ;Write string at DS:DX to standard output 

    mov ah,1 
    int 21h   ;Read character from standard input into AL 
    sub al,30h  ;al = character - '0' 
    mov num1,al  ;num = character - '0' 

    mov ah,9 
    lea dx,str2 
    int 21h   ;Write string at DS:DX to standard output 

    mov ah,1 
    int 21h   ;Read character from standard input into AL 

    mov al,2 

    cmp num1,al  ;Is num1 greater than 2? 
    jg sum   ; yes, goto sum 
         ; no 
    mov ah,9 
    lea dx,str3 
    int 21h   ;Write string at DS:DX to standard output 
    jmp exit 


sum: 
    add num1,al  ;num1 = num1 + AL = num1 + 2 

    mov ah,9 
    lea dx,new 
    int 21h   ;Write string at DS:DX to standard output 

    mov ah,9 
    lea dx,num1 
    int 21h   ;Write string at DS:DX to standard output 
    jmp exit 

exit: 
+2

我没有看到实际的问题,我不知道你在编写什么语言,我认为你的文章的格式需要更多的工作。请修复这些问题,以便我们可以帮助您。 – Alexander

+0

我在汇编语言中使用条件语句..我的问题是,它不会比较第一个数字是否大于第二个数字.. –

+1

那么你有'cmp num1,al'' jg sum'在那里,所以CPU可能*是*比较的东西。然而,之前的指令是'mov al,2',所以你似乎将'num1'的值与'2'进行比较,这似乎与你希望代码执行的描述不符(比较第一个数字到第二个数字)。 –

回答

0

您写这

cmp num1,al  ;Is num1 greater than 2? 
jg sum   ; yes, goto sum 
        ; no 

您正在CMP声明的错意字符串的一部分

正确的意义和声明(FO R此之情况)是

cmp num1,al  ;Is 2 lesser than num1? 
    jl sum   ; yes, goto sum 
         ; no 

我已经修改您的代码,现在会是这样

org 100h 
    jmp start 
    str1 dw 'Provide Input',10,13,'$' 
    str2 dw 'Provide another input',10,13,'$' 
    str3 dw 'First is greater',10,13,'$' 
    new dw 'Second is greater',10,13,'$' 
    num1 db ? 
    start: 
    mov ah,9 

    lea dx,str1 
    int 21h   ;Write string at DS:DX to standard output 

    mov ah,1 
    int 21h   ;Read character from standard input into AL 
    sub al,30h  ;al = character - '0' 
    mov num1,al  ;num = character - '0' 

    mov ah,9 
    lea dx,str2 
    int 21h   ;Write string at DS:DX to standard output 

    mov ah,1 
    int 21h   ;Read character from standard input into AL 
    sub al,30h  ;al = character - '0' 

    cmp num1,al  ;Is al lesser than num1? 
    jl sum   ; yes, goto sum 
         ; no 
    mov ah,9 
    lea dx,str3 
    int 21h   ;Write string at DS:DX to standard output 
    jmp exit 


sum: 
    add num1,al  ;num1 = num1 + AL = num1 + al 

    mov ah,9 
    lea dx,new     
    int 21h   ;Write string at DS:DX to standard output 

exit: 
    MOV AH,4CH 
    INT 21H 

现在尝试用这种方法首先给输入2比5,看输出。现在再次运行并输入5以上的值2.当你自己运行时,你会明白更多。

相关问题