2017-10-25 11 views
0

このプログラムはユーザー入力を受け取り、値が負か正かを判断します。私はユーザーが複数の数字をチェックすることができるようにforループを使いたい、この場合は3つの入力にループを設定する。私が困っているのは、ユーザーが「0」を入力して終了することです。 JSはJG & JLと同じことをやっています。私はちょうど2つのテストを1つのジャンプに統合するようにコメントしました。条件文を追加してゼロをチェックすると、少なくとも5バイトの "ジャンプ"エラーが発生します。助けてください。私は欠陥のあるコードをコメントアウトしました。ありがとう。クリス・スヌークが言ったように、問題は、ジャンプの遠すぎる長さがあるので'0'が入力されたループを終了するアセンブリ

.586 
.MODEL FLAT 

INCLUDE io.h 

.STACK 4096 

.DATA 
titleLbl BYTE "Is your number positive or negative ", 0 
formula BYTE "It's anyones guess, well no not really, cuz conditional statements.....", 0 
prompt BYTE "Please enter a number: (enter '0' to exit)", 0 
string BYTE 40 DUP (?) 
resultLbl BYTE "The number you entered is:", 0 
positiveLbl BYTE "Positive", 0 
negativeLbl BYTE "Negative", 0 
zeroLbl BYTE "You Entered '0'", 0 
zero_messageLbl BYTE "Exiting", 0 

.CODE 

_MainProc PROC 
output titleLbl, formula 

mov ecx, 3     ;the for loop will run 3 times 
forCount: input prompt, string, 40  ; prompt user for a number 
    atod string     ;convert to integer 
    cmp eax, 0      ;compare user input to value stored (0) 

    js negative 
    ;jg positive  ;jump conditional if input is greater than 0 
    ;jl negative  ;jump conditional if input is less than 0 
    ;jz zero   ;jump conditional if input is equal to 0 

    positive: ;condition if greater than; ouput 
     output resultLbl, positiveLbl ; output the even message 
      jmp exit      

    negative: ;condition if less than; ouput 
     output resultLbl, negativeLbl ; output the odd message 
      jmp exit 

    ;zero:  ;condition if equal to; ouput 
     ;output zeroLbl, zero_messageLbl 
      ;jmp exit 

    exit: ;quit jump    ;end if/else/if conditional 
loop forCount 
quit: 

    mov eax, 0   ;clear memory 
    ret 
_MainProc ENDP 
END 
+0

は、あなたが最初のフォールスルーパスに別の条件ジャンプを行うことができます1。 'jl negative '/' jz zero'はうまく動作するはずです。なぜなら、 'eax = 0'なら' jl'が取られないからです。または、[slow loop命令を使用する]と主張している場合(https://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently) )、ループの最後にゼロのチェックを入れ、 'loopnz'を使い、ループの先頭に' jl'を使います(フラグは 'cmp/loopnz'から設定します)。最初の反復では、cmp/loopnzのループにジャンプして、0で終了することができます。 –

+1

"ジャンプエラー"というエラーメッセージをコピー/ペーストします。おそらく 'ゼロ 'というシンボルがあるかもしれませんが、それ以外の場合は、' jz zero'のコメントを外すのになぜ問題があるのでしょうか? –

+1

エラー\t A2075 \tジャンプ先があまりにも遠く:7バイトで –

答えて

0

、あなたが書くことができます。

cmp eax, 0      ;compare user input to value stored (0) 

jle negative_or_zero 

;positive: ;here there is always a positive number; ouput 
    output resultLbl, positiveLbl ; output the even message 
     jmp exit      

negative_or_zero: 
jz zeroz      ;jump conditional if input is equal to 0 

negative: ;condition if less than; ouput 
    output resultLbl, negativeLbl ; output the odd message 
     jmp exit 

zeroz:  ;condition if equal to; ouput 
    ;output zeroLbl, zero_messageLbl 
     ;jmp exit 
関連する問題