2016-11-03 146 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一)被告知,如果[esi] == 0运行65000次,因为在第一次迭代cx已经比0

更大的没错,外部功能可能会破坏它,以及。这里需要了解的一个主要问题是他们的calling convention。从它的外观(通过堆栈传递第一个参数),您的CX内容在返回时几乎注定要失败:几乎所有通过堆栈传递所有内容的约定都假定CX被调用者保存。

+0

您是否对我需要更改以解决此问题有任何建议;我对汇编程序相当陌生。 – Jurdun

+1

@Jurdun使用'DI'而不是'CX'和'MOV DI,-1'来代替'MOV CX,0'。 – hidefromkgb

+0

@Jurdun:另请参阅[x86标签wiki](http://stackoverflow.com/tags/x86/info)了解大量链接,其中包括一些解释函数调用约定中的调用保留寄存器和调用封装寄存器的内容。 (又名ABI) –