我怎样才能画出一个直立的三角形星号又名星星?
How can I draw an upright triangle of asterisks aka stars?
我是一名计算机科学专业的本科生,我想知道如何使用 gotoxy x,y
命令更改此汇编代码以在输出中显示星星,如下图所示。
nextline macro
mov ah,2
mov dl,13
int 21h
mov dl,10
int 21h
endm
gotoxy macro x,y
mov dl,x
mov dh,y
mov bh,0
mov ah,2
int 10h
endm
.MODEL SMALL
.STACK 64
.DATA
msg1 DB 'Enter a number : $'
msg2 DB 'Wrong number!!$'
n db ?
x db 0
y db 0
.CODE
MAIN:
MOV AX, @DATA
MOV DS, AX
l3:
LEA DX, msg1
MOV AH, 9
INT 21H
MOV AH, 1
INT 21H
cmp al,'1'
jl l4
cmp al,'9'
jg l4
jmp l5
l4: nextline
LEA DX, msg2
MOV AH, 9
INT 21H
nextline
jmp l3
l5: sub al,'0'
mov n,al
mov cl,1
l1:
mov ch,1
l2:
mov al,n
sub al,ch
mov x,al
mov y,cl
gotoxy x,y
mov dl,'*'
mov ah,2
int 21h
inc ch
cmp ch,cl
jle l2
inc cl
cmp cl,n
jle l1
MOV AH, 4CH
INT 21H
END MAIN
如果你用你想在屏幕上看到的东西做一个 table 会有帮助:
row
indentation
asterisks
1
4
1
2
3
3
3
2
5
4
1
7
5
0
9
然后看看这一切的规律性以及如何产生知道 n=5
:
的数字
row
indentation
asterisks
1
4 = (n - 1)
1 = (1 + 1) - 1
2
3 = (n - 2)
3 = (2 + 2) - 1
3
2 = (n - 3)
5 = (3 + 3) - 1
4
1 = (n - 4)
7 = (4 + 4) - 1
5
0 = (n - 5)
9 = (5 + 5) - 1
row
n - row
row + row - 1
mov cl, 1 ; Row from 1 to 5
T1:
mov al, n ; Indentation AL = (n - row)
sub al, cl
mov x, al ; \
mov y, cl ; |
gotoxy x,y ; / This could easily become "GOTOXY AL, CL",
; no longer needing the x and y variables
mov dl, '*'
mov bl, cl ; Number of asterisks BL = (row + row - 1)
add bl, cl
dec bl
T2:
mov ah, 02h ; DOS.PrintCharacter
int 21h
dec bl
jnz T2
inc cl ; Next row
cmp cl, n
jbe T1
我是一名计算机科学专业的本科生,我想知道如何使用 gotoxy x,y
命令更改此汇编代码以在输出中显示星星,如下图所示。
nextline macro
mov ah,2
mov dl,13
int 21h
mov dl,10
int 21h
endm
gotoxy macro x,y
mov dl,x
mov dh,y
mov bh,0
mov ah,2
int 10h
endm
.MODEL SMALL
.STACK 64
.DATA
msg1 DB 'Enter a number : $'
msg2 DB 'Wrong number!!$'
n db ?
x db 0
y db 0
.CODE
MAIN:
MOV AX, @DATA
MOV DS, AX
l3:
LEA DX, msg1
MOV AH, 9
INT 21H
MOV AH, 1
INT 21H
cmp al,'1'
jl l4
cmp al,'9'
jg l4
jmp l5
l4: nextline
LEA DX, msg2
MOV AH, 9
INT 21H
nextline
jmp l3
l5: sub al,'0'
mov n,al
mov cl,1
l1:
mov ch,1
l2:
mov al,n
sub al,ch
mov x,al
mov y,cl
gotoxy x,y
mov dl,'*'
mov ah,2
int 21h
inc ch
cmp ch,cl
jle l2
inc cl
cmp cl,n
jle l1
MOV AH, 4CH
INT 21H
END MAIN
如果你用你想在屏幕上看到的东西做一个 table 会有帮助:
row | indentation | asterisks |
---|---|---|
1 | 4 | 1 |
2 | 3 | 3 |
3 | 2 | 5 |
4 | 1 | 7 |
5 | 0 | 9 |
然后看看这一切的规律性以及如何产生知道 n=5
:
row | indentation | asterisks |
---|---|---|
1 | 4 = (n - 1) | 1 = (1 + 1) - 1 |
2 | 3 = (n - 2) | 3 = (2 + 2) - 1 |
3 | 2 = (n - 3) | 5 = (3 + 3) - 1 |
4 | 1 = (n - 4) | 7 = (4 + 4) - 1 |
5 | 0 = (n - 5) | 9 = (5 + 5) - 1 |
row | n - row | row + row - 1 |
mov cl, 1 ; Row from 1 to 5
T1:
mov al, n ; Indentation AL = (n - row)
sub al, cl
mov x, al ; \
mov y, cl ; |
gotoxy x,y ; / This could easily become "GOTOXY AL, CL",
; no longer needing the x and y variables
mov dl, '*'
mov bl, cl ; Number of asterisks BL = (row + row - 1)
add bl, cl
dec bl
T2:
mov ah, 02h ; DOS.PrintCharacter
int 21h
dec bl
jnz T2
inc cl ; Next row
cmp cl, n
jbe T1