Search code examples
loopsassemblyx86dos

(Assembly Lanaguage) Need assistance with diamond pattern


I am currently working on creating a diamond pattern using assembly language in visual studio with asterisks (*). Currently, I'm working on creating the top half of the diamond pattern. The top half of my diamond is 10 columns long; what I'm trying to figure out is how to print an additonal 2 asterisks per column in my "print_star" loop function. (For exmaple column 2 of my diamond would print 2 more *'s, column 3 of my diamond would print 4 more *'s and etc.) So my question how do I do I add +2 more asterisks in a row per column in my print_star function.

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

How do I add 2 more asterisks char in my program for each column of my diamond? (ex: I need to output 2 more *'s in my second column of my diamond pattern and need to output 4 more *'s in my third column and etc. up to my tenth row.


Solution

  • You should write a loop to control the amount of star printed for each row. You will need to add two for the top half of the diamond and subtract 2 for the bottom half of the diamond. I literally just did this project for my computer org class. From the work you have shown you need to print one star at the top and drop down to the next coordinates and print 3 stars for the second row so on until you reach the middle of your diamond(depending how big you want it to be)