Increment ecx in assembly language
assembly, x86
Solution
repeat:
xor ecx, ecx
push ecx ; just to print
push FORMATDBG ; just to print
call printf ; just to print
add esp, 8 ; add the spaces
inc ecx ; increment ecx
cmp ecx, 0 ; compare ecx to zero
ja repeat ; if not equal to zero loop again
`xor ecx, ecx` sets `ecx` to zero. I'm not sure if you know this. You probably do not want it to happen on each iteration. Furthermore your loop condition `ja repeat` currently causes a loop only if `ecx > 0` which is probably not what you wanted (or is it?).
One last thing, `printf` probably trashes `ecx` (I am assuming `cdecl` or `stdcall`). Read up on call conventions (not sure what compiler/OS you are on) and see which registers are guaranteed to be preserved in function calls.
In terms of your code, you probably wanted something closer to this:
xor ebx, ebx
repeat:
push ebx ; just to print
push FORMATDBG ; just to print
call printf ; just to print
add esp, 8 ; add the spaces
inc ebx ; increment ecx
cmp ebx, 0 ; compare ecx to zero
ja repeat ; if not equal to zero loop again
This will not cause an infinite loop though. When `ebx` reaches its maximum value, its value will wrap around back to 0 which will cause the loop condition (`ebx>0`) to evaluate to false and your loop to be exited.
Problem
I need to increment a number so that the code will increment forever, but it stays zero. Here is my code: ``` section .data FORMAT: db '%c', 0 FORMATDBG: db '%d', 10, 0 EQUAL: db "is equal", 10, 0 repeat: push ecx ; just to print push FORMATDBG ; just to print call printf ; just to print add esp, 8 ; add the spaces inc ecx ; increment ecx cmp ecx, 0 ; compare ecx to zero ja repeat ; if not equal to zero loop again ```