2016-11-03 5 views
1

私は、ユーザーの入力に応じて文字を何回か印刷するループを作成しようとしていましたが、ループは停止せず、無限に続きます。ループは無期限に連続的にループします

mov esi, [items]   //Esi points to the first item - Calling data from the C code and assigning it to the esi source indexer, in this case being the users inputted number. 



    loop1: mov eax, [esi]  // moves the first number which is in esi to the eax register 
      push eax    // pushes it onto the stack so it can be used 
      call printInt  // Prints the integer in the eax register 



      push ','    // Prints a comma after the number inputted 
      call printChar 

      push ' '    // Prints a space 
      call printChar 

      mov cx, 0   // assigning the value of 0 to counter 
      loop2: 
        push '*'  // pushing the required character onto the stack 
        call printChar // printing the character 
        inc cx   // incrementing the counter by 1 
        cmp cx, [esi] // comparing the program counter with the users inputted number 
        jne loop2  // jumping back to the beginning of the loop if cx is not equal to the users input thus printing the character the same number of times as the users inputted number 

      call printNewLine 

      mov eax, [esi] 
      add esi, 4   // Now that's odd. Esi is pointing at an integer, and that's 4 bytes in size. 
      cmp eax, 0 

      jnz loop1 



     jmp finish   // We need to jump past the subroutine(s) that follow 
           // else the CPU will just carry on going. 

プログラムの入出力はCで制御されているので、Cで投稿にタグ付けされています。

プログラムの中で動作しない部分は、loop2から始まりjne loop2で終わるべきです。

ご協力いただきありがとうございます。

+0

私はCタグがここで正当化されているとは思わない。 –

+0

さて、私はそれを削除します。 – Jurdun

+4

'printChar'はおそらくあなたの' cx'カウンターを壊しています。デバッガの使い方を学ぶ。また、ABIのドキュメントを読んでください。 Cから呼び出された場合は、規約に従ってください。 – Jester

答えて

2

内部ループ(loop2から始まる1)は、最初の反復cxですでに大きな

0以上であるとはいので、外部関数は同様にそれを台無しにする、 [esi] == 0場合は65000回実行するように言われています。ここで知る必要がある主なものは、 calling conventionです。その外観(最初のパラメータをスタックに渡す)から、あなたの CXの内容はかなり戻ります。スタックを介してすべてを渡すほぼすべての規約は、 CXが呼び出し元を保存していると仮定しています。

+0

この問題を解決するために何を変更する必要があるかについてご意見はありますか?私はアセンブラをかなり新しくしています。 – Jurdun

+1

@Jurdunは、例えば 'CX'の代わりに' DI'を使い、 'MOV CX、0'の代わりに' MOV DI、-1'を使います。 – hidefromkgb

+0

@Jurdun:多くのリンクについては、[x86タグwiki](http://stackoverflow.com/tags/x86/info)を参照してください。関数呼び出し呼び出し規約では、呼び出し保護されたレジスタと呼び出しが競合したレジスタについて説明しています。 (別名ABI) –