首页 > 解决方案 > (组装语言)需要菱形图案的帮助

问题描述

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

我不完整的钻石上半部分

钻石上半部分的期望输出

  .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 个 *,等等,直到我的第十行。

标签: loopsassemblyx86dos

解决方案


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


推荐阅读