prints only 7 and 11. I am just starting to learn assembler. Tell me, please, what is the matter? and yes, I also cannot figure out the output of the array for the time being, so I pass it directly from the register.

;программа ищет 10 простых чисел и выводит их на консоль .386 .model flat, stdcall option casemap:none include \masm32\include\windows.inc include \masm32\include\user32.inc ;wsprintf include \masm32\include\kernel32.inc includelib \masm32\lib\user32.lib ;wsprintf includelib \masm32\lib\kernel32.lib BSIZE equ 15 SSIZE equ 10 .data ifmt BYTE "%d",0 buf BYTE BSIZE dup(?) stdout DWORD ? cWritten DWORD ? crlf BYTE 0dh, 0ah .data? PrimeNumbers DWORD SSIZE dup(?) .code start: mov ebx, 3 ;begin from number 3 mov edi, 0 ;0 element of array mov ebp, 0 ;counter of primes nxtdig: mov edx, 0 ;edx:eax число mov eax, ebx mov ecx, ebx ;счетчик цикла sub ecx, 2 ;число проверок меньше проверяемого числа на 2 mov esi, 2 ;начинаем с деления на 2 nxtpr: div esi cmp edx, 0 ;сравниваем остаток с 0 jz skip ;да - идем к след.проверке mov edx, 0 ;no -> mov eax, ebx ;восстан число в edx:eax inc esi ;высчитываем след число для деления loop nxtpr ;проверка edx и конец итерации цикла mov PrimeNumbers[edi], ebx ;если не на что уже делить (ecx = 0) invoke GetStdHandle, STD_OUTPUT_HANDLE ;вывод числа на консоль mov stdout, ebx invoke wsprintf, ADDR buf, ADDR ifmt, ebx ;форматирование числа для вывода invoke WriteConsoleA, stdout, ADDR buf, ;вывод числа BSIZE, ADDR cWritten, NULL invoke WriteConsoleA, stdout, ADDR crlf, 2, ADDR cWritten, NULL ; вывод новая строка (числа в столбик) inc ebp ;увеличиваем счетчик простых чисел cmp ebp, SSIZE ;найдено ли нужное кол-во простых? (10) jz done ;да - выходим add edi, 4 ;нет - след элем массива skip: inc ebx ;увеличиваем - переходим jmp nxtdig ;к следующему числу для проверки done: invoke ExitProcess, 0 end start 
  • Did you use a debugger? - pavel
  • I try to use it) the fact is that the seven instead of the three is placed in eax after calling StdHandle. the problem, it seems to me, is precisely how my conclusion is organized. but this is only my humble assumption ... - al m
  • Sorry, about eax nonsense now was. I pass on ebx, which remains "uncorrupted." - al m

2 answers 2

In short, I am a scum. all we had to do was not to put it in stdout from ebx, but from EAX. so empirically, we found that this variable for output (and obviously also for input and for opening a file) can only be moved from it.

 invoke GetStdHandle, STD_OUTPUT_HANDLE ;вывод числа на консоль mov stdout, eax invoke wsprintf, ADDR buf, ADDR ifmt, ebx ;форматирование числа для вывода invoke WriteConsoleA, stdout, ADDR buf, ;вывод числа BSIZE, ADDR cWritten, NULL invoke WriteConsoleA, stdout, ADDR crlf, 2, ADDR cWritten, NULL ; вывод новая строка (числа в столбик) 

and the question remains for the knowledgeable: how to organize the output of the PrimeNumbers array? otherwise it is created, but not used, you can just as well not create it. put each element in EAX in advance? hardly.

    in my opinion the error is here

      sub ecx, 2 ;число проверок меньше проверяемого числа на 2 

    and should be = ecx / 2

    • I do not think that it is critical. - pavel
    • @santavital, yes, it's better and faster, but 7 will still appear there ... - al m