It was necessary to display a string a specified number of times. Obviously, you need to do this using a loop, but the question is: why put ecx from the stack and get it later? Why not work without it?

Here is this piece of code:

 LoopPrint: push ecx invoke WriteConsoleA, hStdout, offset text, sizeof text, 0, 0 pop ecx dec ecx jnz LoopPrint 
  • one
    Well, I WriteConsoleA writes to this register without saving it. See the calling convention that this function uses, and use a different registermymedia

1 answer 1

MSDN, Article x64 Software Conventions / Register Usage . If the table does not explicitly indicate that the register "must be kept by the called object", then it is impossible to rely on its value, which was before the call to the function call.

 Регистр статус Применение RAX Временный Регистр возвращаемого значения RCX Временный Первый целочисленный аргумент RDX Временный Второй целочисленный аргумент ... RDI Постоянный Должен сохраняться вызываемым объектом RSI Постоянный Должен сохраняться вызываемым объектом RBX Постоянный Должен сохраняться вызываемым объектом RBP Постоянный Может использоваться как указатель кадра. Должен сохраняться вызываемым объектом RSP Постоянный Указатель стека ... 

For 32-bit and 16-bit architectures, there are slight differences, but in any case, the safety of the ecx ( cx ) ecx during the operation of the function is not guaranteed.

  • You can, for example, use the rbx / ebx register for a loop instead of ecx (the called function should store the rbx value) if the loop statement is not used. That is, the word "volatile" in this case means that the register value is not saved by the called function and therefore it is necessary to protect such registers yourself. - jfs
  • @jfs, I don’t remember offhand, but there seemed to be no requirements for saving ebx / bx in 32/16. And in the 64-bit version it is already ABI. - PinkTux
  • I specifically brought the link, so as not to count on the memory. It says which registers the function can change (the functions are required to save ebx according to the documentation). - jfs
  • @jfs, I don’t see anything about saving ebx from your link. Sorry, I see: "Functions must preserve all registers, except for eax, ecx, and edx" - PinkTux