3

After every 5 elements the next element should be printed on the next line. I am not allowed to use a string of empty spaces for the space between the elements. I tried using GoToXy, but I am having trouble. All elements in the first row have 5 spaces between them, starting from the second row all the elements need to be aligned to the one above it.

Desired Output:

94     2     67     57     7
40     58    48     73     94
16     77    88     16

Output from my code: (Everything is printed on the same line with 5 spaces between them.)

94     2     67     57     7     40     58    48     73     94     16   77    88     16

My code is:

INCLUDE Irvine32.inc

.data
count = 14
array WORD count DUP(?)

.code
main PROC

    push OFFSET array
    push COUNT
    call ArrayFill
    call DisplayArray

    exit

main ENDP

;-----------------------------------------------------------------------------------------
ArrayFill PROC  
    push    ebp
    mov ebp,esp
    pushad              ; save registers
    mov esi,[ebp+12]    ; offset of array
    mov ecx,[ebp+8]     ; array size
    cmp ecx,0           ; ECX == 0?
    je  L2              ; yes: skip over loop

L1:
    mov eax, 100            
    call    RandomRange ; from the link library
    mov [esi],ax
    add esi,TYPE WORD
    loop    L1

L2: popad               ; restore registers
    pop ebp
    ret 8               ; clean up the stack
ArrayFill ENDP

;---------------------------------------------------------------------------------------
DisplayArray PROC

mov eax, 0
mov esi, 0
mov ecx, COUNT
mov dh, 0
call GoToXy
mov dl, 5

L1:
    mov ax, array[esi * TYPE array]
    call WriteDec
    call GoToXy
    add dl, 5
    inc esi
    loop L1
    call CrlF
ret
DisplayArray ENDP
END main
Michael Petch
  • 46,082
  • 8
  • 107
  • 198

1 Answers1

4

Tab characters auto-align on most consoles, so printing a tab character (ascii 9) will align to an 8-character column format automatically.

A tab character should be printed before printing the next decimal value from the array when dl equals 1, 2, or 3 within your L1 loop. By not printing a tab when dl is 0, the first column will stay left-justified.

When dl is 4 in your L1 loop (4 represents the fifth value for each row), output a newline (crlf) after printing and then reset dl to 0 for the next 5. Resetting dl allows you to use simple comparisons, since you only have 5 possible values to check.

GoToXy seems to be interfering with your CrlF, although without the code for either of those I cannot tell in what way. Using the approach I have outlined, you won't need GoToXy at all, but you will still need CrlF, so I suggest that you verify it actually works.

Matt Jordan
  • 2,133
  • 9
  • 10