我目前正在尝试用 MASM 汇编语言编写一个程序,该程序将根据用户输入的整数值打印出一系列直角三角形。例如,如果用户输入的是 3,则程序将输出:
*
* *
* * *
-
* * *
* *
*
-
*
* *
* * *
-
* * *
* *
*
我已经弄清楚了除第三个三角形之外的所有三角形。我遇到了spaces1
三角形 3 以下的无限循环。程序将正确打印出三角形,直到它达到比用户整数值小一的值,然后它会无限循环。我已经研究这个问题一段时间了,但我不知道如何修复这个错误。这是我的代码:
org 100h
.model small
.stack 16
.data
; question the user is asked
input db "Enter the size for the triangles between 3 and 9, or 0 to quit $"
size dw ?
.code
main proc
mov AX, @data ; move location of data segment into AX
mov DS, AX ; points ds register at data segment
prompt:
mov AH, 09h ; opcode to print string
lea DX, input ; string to print out
int 21h
mov AH, 01h ; opcode to read in single char
int 21h
cmp AL, '0' ; check to see if user wants to quit
je exit
sub AL, '0' ; convert user input to literal value
mov AH, 0 ; blank out top half of AX
mov size, AX ; make copy of user input as literal value
mov CX, AX
mov BX, 1 ; sets up BX register for star counter loop
call crlf
; First Triangle
lines1:
push CX ; store the outer (lines) loop
mov CX, BX ; the number of stars to print out
stars1:
mov AH, 02h ; opcode to print string
mov DL, '*' ; char to print out
int 21h
loop stars1 ; end loop
call crlf
inc BX ; increment BX (stars)
pop CX ; recover value from CX for the outer lines loop
loop lines1 ; end loop
call crlf
mov CX, size ; reload CX with copy of size for next triangle
mov BX, 1
; Second Triangle
lines2:
push CX ; store the outer (lines) loop
stars2:
mov AH, 02h ; opcode to print single char
mov DL, '*' ; char to print out
int 21h
loop stars2 ; end loop
call crlf
pop CX ; recover value for outer loop
loop lines2 ; end loop
call crlf ; call crlf proc
mov CX, size ; reload CX with copy of size for next triangle
mov BX, 1 ; set up BX for star loop
; Third Triangle
lines3:
push CX ; Store the outer loop counter
mov CX, size ; Calculate spaces needed
sub CX, BX ; Adjust CX for the number of spaces
spaces1:
mov AH, 02h ; Opcode to print single char
mov DL, ' ' ; Print a space
int 21h
loop spaces1 ; End loop for spaces
mov CX, BX ; Set number of stars for this line
stars3:
mov AH, 02h ; Opcode to print single char
mov DL, '*' ; Print a star
int 21h
loop stars3 ; End loop for stars
call crlf ; Move to the next line
inc BX ; Increment the number of stars for the next line
pop CX ; Restore outer loop counter
loop lines3 ; End outer loop
call crlf ; call crlf proc
mov CX, size ; set up CX for fourth triangle
mov BX, 1
; Fourth Triangle
lines4:
push CX ; store copy of CX for outer loop
mov CX, BX ; set up number of spaces for spaces loop
spaces2:
mov AH, 02h ; opcode to print char
mov DL, ' ' ; char to print out
int 21h
loop spaces2 ; end loop
pop CX ; recover CX for stars loop
push CX ; for outer lines loop
stars4:
mov AH, 02h ; opcode to print char
mov DL, '*' ; char to print out
int 21h
loop stars4 ; end loop
call crlf
inc BX ; increment BX for next line
pop CX ; recover value for outer lines loop
loop lines4
call crlf ; call crlf proc
mov CX, size ; set up CX for fourth triangle
mov BX, 1
exit:
mov AX, 4C00h
int 21h
main endp
crlf proc
mov AH, 02h ; opcode to print single char
mov DL, 13 ; ASCII carriage return
int 21h
mov DL, 10 ; ASCII line feed
int 21h
ret
crlf endp
end main
这是问题的屏幕截图。在程序中,它在第 92 行和第 95 行之间循环。spaces1
有人能给我指出正确的方向吗?谢谢!:)
第 3 个和第 4 个三角形都有一条线,无需插入空格。您需要添加一个合适的旁路,绕过空间1和空间 2循环。
对于第 4 个三角形,设置 BX=0,因为第一行没有空格: