I am running a program that asks the user for an input of the number of composite numbers to display. Ex. if the user enters 10, the program would display the first 10 composite numbers.
The issue I am having is that my program prints all of the values in one long column and I need to have the output is supposed to be displayed with 10 composite numbers per line with at least 3 spaces between them. 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 elements per line with 3 spaces between them?
I 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 output didn't break to a new line until the 17th element as opposed to the 10th.
I must be missing something because the linebreak isn't happening after 10 elements.
UPDATE: Fixed the issue. I changed the counter register from edx to esi and it works properly.
cmp ecx, 0
You don't seem to do anything with the outcome of this comparison.
To solve the problem you are having you can introduce a counter that you initialize with the value 10. Each time that you do output a CRLF you reset this 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 :