Displaying an array in MASM columns

I am running a program that asks the user to enter the number of composite numbers to display. Ex. if the user enters 10, the program will display the first 10 composite numbers.

The problem I am running into is that my program prints all the values ​​in one long column and I need the output to be displayed with 10 composite numbers per line with at least three spaces in between. Here is the code:

INCLUDE Irvine32.inc


.data

userInt1    DWORD   ?           ;integer to be entered by user
userInt2    DWORD   ?           ;integer to be entered by user
compNums DWORD 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30 
          DWORD 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46, 48, 49, 50, 51, 52, 54 
          DWORD 55, 56, 57, 58, 60, 62, 63, 64, 65, 66, 68, 69, 70, 72, 74, 75, 76, 77 
          DWORD 78, 80, 81, 82, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99 
          DWORD 100, 102, 104, 105, 106, 108, 110, 111, 112, 114, 115, 116, 117, 118, 119 
          DWORD 120, 121, 122, 123, 124, 125, 126, 128, 129, 130, 132, 133, 134, 135, 136 
          DWORD 138, 140, 141, 142, 143, 144, 145, 146, 147, 148, 150, 152, 153, 154, 155, 156 
          DWORD 158, 159, 160, 161, 162, 164, 165, 166, 168, 169, 170, 171, 172, 174, 175
          DWORD  176, 177, 178, 180, 182, 183, 184, 185, 186, 187, 188, 189, 190, 192, 194 
          DWORD  195, 196, 198, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 212 
          DWORD  213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 224, 225, 226, 228, 230 
          DWORD  231, 232, 234, 235, 236, 237, 238, 240, 242, 243, 244, 245, 246, 247, 248
          DWORD   249, 250, 252, 253, 254, 255, 256, 258, 259, 260, 261, 262, 264, 265, 266 
          DWORD   267, 268, 270, 272, 273, 274, 275, 276, 278, 279, 280, 282, 284, 285, 286, 287 
          DWORD   288, 289, 290, 291, 292, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304 
          DWORD   305, 306, 308, 309, 310, 312, 314, 315, 316, 318, 319, 320, 321, 322, 323, 324 
          DWORD   325, 326, 327, 328, 329, 330, 332, 333, 334, 335, 336, 338, 339, 340, 341, 342 
          DWORD   343, 344, 345, 346, 348, 350, 351, 352, 354, 355, 356, 357, 358, 360, 361, 362, 363 
          DWORD   364, 365, 366, 368, 369, 370, 371, 372, 374, 375, 376, 377, 378, 380, 381, 382, 384 
          DWORD   385, 386, 387, 388, 390, 391, 392, 393, 394, 395, 396, 398, 399, 400, 402, 403
          DWORD   404, 405, 406, 407, 408, 410, 411, 412, 413, 414, 415, 416, 417, 418, 420, 422
          DWORD   423, 424, 425, 426, 427, 428, 429, 430, 432, 434, 435, 436, 437, 438, 440, 441
          DWORD   442, 444, 445, 446, 447, 448, 450, 451, 452, 453, 454, 455, 456, 458, 459, 460
          DWORD   462, 464, 465, 466, 468, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 480
          DWORD   481, 482, 483, 484, 485, 486, 488, 489, 490, 492, 493, 494, 495
intro_1     BYTE    "Composite Numbers  by. Eric Walters" , 0
prompt_1    BYTE    "Enter the number of composite numbers you would like to see. ", 0
prompt_2    BYTE    "I'll accept orders for up to 400 composites. ", 0
prompt_3    BYTE    "Enter the number of composites to display [1 .. 400]: ", 0
prompt_4    BYTE    "Test", 0
prompt_6    BYTE    "Results certified by Eric Walters.",0
prompt_7    BYTE    "Goodbye, ", 0
outOfRange  BYTE    "Out of range. Try again.",0
goodBye     BYTE    "Impressed? Bye! ", 0
upperLevel  DWORD   400         ;the largest integer allowed
lowerLevel  DWORD   1           ;the smallest integer allowed
newArray    DWORD   400 DUP(?)


.code
main PROC

MOV eax, 0 
MOV ebx, 0
MOV ecx, 0
MOV edx, 0


; Introduction  
    Introduction:   
        mov     edx, OFFSET intro_1
        call    WriteString
        call    CrLf
        call    CrLf
        mov     edx, OFFSET prompt_1
        call    WriteString
        call    CrLf
        mov     edx, OFFSET prompt_2
        call    WriteString
        call    CrLf
        call    CrLf

; getUserData
    getUserData:
        mov     edx, OFFSET prompt_3
        call    WriteString
        call    ReadInt
        mov     userInt1, eax
        mov     ebx, OFFSET compNums

    ;validate   
        cmp     eax, upperLevel
        ja      option1
        cmp     eax, lowerLevel
        jb      option1
        jmp     showComposites

    option1 :
        mov     edx, OFFSET outOfRange
        call    WriteString
        call    crlf
        jmp     getUserData

    showComposites :
        mov ecx, userInt1
        cmp ecx, 0

        isComposite:
            mov eax, [ebx]
            call WriteDec
            call crlf
            add ebx, 4
            loop isComposite

farewell :
        mov     edx, OFFSET prompt_6
        call    WriteString
        call    crlf
        mov     edx, OFFSET prompt_7
        call    WriteString
        call    crlf


    exit        ; exit to operating system
main ENDP

END main

      

Any suggestions on how I can print 10 items on a line with 3 spaces in between?

I have implemented the following code:

showComposites :
        mov ecx, userInt1
        jecxz   farewell
        mov edx, 10

    isComposite:
        mov eax, [ebx]
        call WriteDec
        dec edx
        jnz Spaces
    linebreak:
        call crlf
        mov edx, 10
        jmp Next

    Spaces:
        cmp ecx, 1
        je  linebreak
        mov edx, OFFSET space3
        call WriteString

    Next:
        add ebx, 4
        loop isComposite

      

And the result didn't break until a new line until the 17th element, not the 10th.

I must be missing something, because after 10 elements the unlock does not happen.

UPDATE: Problem fixed. I changed the register of the counter from edx to esi and it works correctly.

+3


source to share


2 answers


cmp ecx, 0

      

You don't seem to be doing anything with the result of this comparison.



To solve the problem you are having, you can enter a counter that you initialize with a value of 10. Each time you do CRLF output, you reset that counter to 10.

showComposites :
    mov ecx, userInt1
    jecxz farewell
    mov edx, 10
isComposite:
    mov eax, [ebx]
    call WriteDec
    dec edx
    jnz Spaces
CRLF:
    call crlf
    mov edx, 10
    jmp Next
Spaces:
    cmp ecx, 1  ;No 3 spaces needed behind the last number!
    je CRLF
    ... Ouput 3 spaces here
Next:
     add ebx, 4
     loop isComposite

farewell :

      

+1


source


Could you,

Semi-pseudocode:



Divide UserInt1 by 10
If Quotient > 0 OuterLoopCount=Quotient
   OutterLoop:
   Save OuterLoopCount
   Initialize InnnerLoopCount=10
   InnerLoop:
   WriteDec & 3 Spaces
   Add Array, TYPE
   Loop InnerLoop
   CRLF
   Restore OuterLoopCount
   Loop OuterLoop
Else Here   
Move Remainder = Loop Count
ZeroQuotient:
WriteDec & 3 Spaces
Add array, TYPE
Loop ZeroQuotient
CRLF
...

      

0


source







All Articles