[英]Assembly - (NASM 32-bit) Printing a triangle of stars not working properly
EDIT: This problem is already solved. 编辑:这个问题已经解决。 Many thanks to mbratch.
非常感谢mbratch。
My code outputs: 我的代码输出:
But it should display this: 但是它应该显示以下内容:
I think problem is in the innerloops but I can't fix it, it works properly on the first loop but not on the succeeding ones. 我认为问题出在内部循环中,但我无法解决,它可以在第一个循环中正常运行,但不能在后续循环中正常运行。
Here's my code: 这是我的代码:
innerloop1: ;;for(j=n-i;j>0;j--)
mov bl, [i]
sub byte [num], bl
mov dl, [num]
mov byte [j], dl
cmp byte [j], 0
jle printStar
mov eax, 4
mov ebx, 1
mov ecx, space
mov edx, spaceLen
int 80h
dec dl
jmp innerloop1
printStar:
mov eax, 4
mov ebx, 1
mov ecx, star
mov edx, starLen
int 80h
innerloop2: ;;for(k=0;k<(2*i)-1;k++)
mov al, [i]
mul byte [two]
dec al
cmp byte [k], al
jge printMe
mov eax, 4
mov ebx, 1
mov ecx, space
mov edx, spaceLen
int 80h
inc byte [k]
jmp innerloop2
printMe:
mov eax, 4
mov ebx, 1
mov ecx, star
mov edx, starLen
int 80h
mov eax, 4
mov ebx, 1
mov ecx, newLine
mov edx, newLineLen
int 80h
inc byte [i]
jmp outerloop
printSpace:
mov eax, 4
mov ebx, 1
mov ecx, space
mov edx, spaceLen
int 80h
There are lots of inefficiencies in your code and it could be much more clearly and concisely written. 您的代码中有很多低效率的地方,可以更清楚,简洁地编写它。 However, I'll just address the areas that are causing a functional problem.
但是,我将仅解决引起功能问题的区域。
There are a couple of problems with innerloop1
. innerloop1
有两个问题。 You are modifying [num]
every time through the loop. 您每次通过循环都在修改
[num]
。 Instead, you want to do it prior to the loop as an initializer for j
. 相反,您想在循环之前作为
j
的初始化器执行此操作。 Secondly, you are counting on the value of dl
being intact through the execution of the loop, but your mov edx, spaceLen
clobbers it, as might the call to int 80h
. 其次,您希望通过执行循环来保持
dl
的值不变,但是mov edx, spaceLen
破坏它,就像对int 80h
的调用一样。 So you can correct it by this: 因此,您可以通过以下方式纠正它:
mov dl, [num] ; INITIALIZE j=n-i
sub dl, byte [i]
innerloop1: ;;for(j=n-i;j>0;j--)
; REMOVED modification of 'num' here
mov byte [j], dl
cmp byte [j], 0
jle printStar
mov eax, 4
mov ebx, 1
mov ecx, space
push dx ; SAVE dx
mov edx, spaceLen
int 80h
pop dx ; RESTORE dx
dec dl
jmp innerloop1
In your second inner loop ( innerloop2
) you are relying on the pre-initialized value of k
every time you enter the loop, which is no longer valid after the first time the loop is encountered.. So you must initialize it each time: 在您的第二个内部循环(
innerloop2
)中,您每次进入循环时都依赖于k
的预初始化值,该值在第一次遇到循环后不再有效。因此,您每次都必须对其进行初始化:
mov byte [k], 0 ; INITIALIZE k=0
innerloop2: ;;for(k=0;k<(2*i)-1;k++)
mov al, [i]
mul byte [two]
dec al
cmp byte [k], al
jge printMe
This all makes the code work. 所有这些使代码正常工作。 Some additional comments:
一些其他评论:
num
for what was n
but also defined a value n
, which was a little confusing. num
表示n
但还定义了一个值n
,这有点令人困惑。 for
loops, try to maintain a consistent approach to doing them every time. for
循环)时,请尝试保持每次执行时的一致方法。 It will reduce the chance of errors.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.