我一直在尝试创建一个循环来根据用户的输入打印一个字符,但是,循环不会停止而是无限期地继续。
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 :(得分:2)
如果loop2
,内循环(从[esi] == 0
开始)被告知运行65k次,因为在第一次迭代时cx
已经大于0。
是的,外部功能也可能破坏它。这里需要知道的一个主要问题是他们calling convention。从它的外观(通过堆栈传递第一个参数),你的CX
内容在返回时几乎注定失败:几乎所有通过堆栈传递所有内容的约定都假设CX
被调用者保存。