MASM 列中的数组输出

Array output in columns MASM

我是 运行 一个要求用户输入要显示的合数个数的程序。前任。如果用户输入 10,程序将显示前 10 个合数。

我遇到的问题是我的程序在一个长列中打印所有值,我需要让输出显示为每行 10 个合数,它们之间至少有 3 个空格。这是代码:

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

关于如何每行打印 10 个元素并在它们之间留出 3 个空格,有什么建议吗?

我实现了以下代码:

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

直到第 17 个元素而不是第 10 个元素,输出才换行。

我一定是漏掉了什么,因为在 10 个元素之后没有换行。

更新:修复了这个问题。我将计数器寄存器从 edx 更改为 esi,它工作正常。

你可以,

半伪代码:

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
...
cmp ecx, 0

你似乎没有对这个比较的结果做任何事情。

要解决您遇到的问题,您可以引入一个用值 10 初始化的计数器。每次输出 CRLF 时,都会将此计数器重置为 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 :