0

I'm using NASM on my 64 bit linux machine, I'm trying to simply:

  1. take an array of characters (digits)
  2. convert them to integers
  3. print them as integers

The issue is after I call movzx rsi, [array+rcx], the value of rcx has changed (I learned this through debugging with gdb). Why is this?

My code:

array db "012345678"
fmt db "%d", 10, 0
...
...
    ; convert chars to integers
    mov rcx, 9
L1:
    dec rcx
    sub [array+rcx], byte "0"
    jnz L1

    ; print as integers
    mov rcx, 0
L2:
    mov rdi, fmt
    movzx rsi, byte [array+rcx] ; rcx changes after this
    xor rax, rax
    call printf

    inc rcx
    cmp rcx, 8
    jle L2
    ret

But it keeps spitting out 2s endlessly.

if I preserve rcx in the stack, or use a different register for counting, it works. Is movzx suppose to destroy rcx?

also bonus question, feel free to disreguard, am I iterating through the array correctly?

rosghub
  • 8,924
  • 4
  • 24
  • 37
  • 6
    `printf` is allowed to change RCX. – Ross Ridge Oct 30 '16 at 02:41
  • 3
    Try using `layout reg` and single-stepping with `stepi` (or `si`). (or `ni` to step over calls, instead of into them). You'll see that MOVZX only writes its destination, and that RCX is changing during printf, which presumably clobbers all the call-clobbered registers (as defined by the calling convention). (see the bottom of the [x86 tag wiki](http://www.felixcloutier.com/x86/) for tips on using gdb. – Peter Cordes Oct 30 '16 at 02:47
  • ah that helped a lot. You guys seem to be right, it appears to be printf changing rcx. thanks for the info – rosghub Oct 30 '16 at 03:28

0 Answers0