(Assembly Language) 需要钻石图案方面的帮助

时间:2021-03-25 10:48:34

标签: loops assembly x86 dos

我目前正在使用带有星号 (*) 的 Visual Studio 中的汇编语言创建菱形图案。目前,我正在制作菱形图案的上半部分。我的钻石的上半部分有 10 列长;我想弄清楚的是如何在我的“print_star”循环函数中每列打印一个额外的 2 个星号。 (例如,我的钻石的第 2 列会再打印 2 个 *,我的钻石的第 3 列会再打印 4 个 * 等等)所以我的问题是如何在每列的行中添加 +2 个星号在我的 print_star 函数中。

My Incomplete Top-half of Diamond

Desired Output for top half of diamond

  .386
.model flat,c
.stack 4096

include cisc225.inc

.data
star BYTE '*'

.code
main PROC

call Randomize  ; initializes the random number generator

mov al, star
mov dh, 1   ; moves text to row 1 (X-Coordinate)
mov dl, 40  ; moves text to column 40 (Y-Coordinate)
call GotoXY ; relocates cursor to coordinate

mov ecx, 10 ; counter-loop

call print_star

    call Readchar           ; Hold console window open until a key press
    call EndProgram         ; Terminates the program

main ENDP

;//////////////////////////////////////////////////////////////

print_star PROC

L1:
    mov eax,256     ; for random color value (foreground and background)
    call RandomRange ; Sets EAX to random color
    call SetTextColor   ; Sets foreground to a random color

    mov al, star ; move character to 8 bit register for display
call Writechar ; displays 8-bit character in al register.
inc dh ; moves 1 space down
dec dl ; moves 1 space to the left
call GotoXY ; relocates cursor to new coordinates


loop L1
    ret
    print_star ENDP
END

如何在我的程序中为钻石的每一列添加 2 个以上的星号字符? (例如:我需要在我的菱形图案的第二列中再输出 2 个 * 并且需要在我的第三列中再输出 4 个 * 等等,直到我的第十行。

1 个答案:

答案 0 :(得分:1)

您应该编写一个循环来控制每行打印的星号数量。您需要为钻石的上半部分添加两个,并为钻石的下半部分减去 2。我真的只是为我的计算机组织课程做了这个项目。根据您所展示的工作,您需要在顶部打印一颗星,然后下拉到下一个坐标,然后在第二行打印 3 颗星,依此类推,直到到达钻石的中间(取决于您希望它有多大)

相关问题