0

I am trying to print the loop count inside my loop. However, I am getting an error trying to move %cx to %rdi:

Printing %cx which holds the value of the loop count:

movw    %cx, %rsi
movq    $stringNumber, %rdi
xorq    %rax, %rax
call    printf

This is what I have in the .data section:

.stringNumber: .asciz  "Number: %d\n"

I am getting this error:

Error: unsupported instruction `mov'

I think it has to do with the way I am trying to move %cx into %rsi. I don't know what I am doing wrong though.

Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102
  • 2
    `cx` is 16 bits, `rsi` is 64 of which `%d` only uses 32.. You need to convert the sizes, e.g. `movzwl %cx, %esi` for a zero extension. Note that `printf` is allowed to destroy your `cx` among other registers so be sure to preserve it if you need it later. PS: of course you should not be using `cx` as counter to start with - use `ecx` instead. – Jester Oct 27 '19 at 23:47
  • Thanks so much for the input. I am quite new to assembly so I am trying to understand it better. Why should I use ecx for the loop count rather than cx? @Jester – Tom el Safadi Oct 27 '19 at 23:50
  • 2
    It's easier on the cpu, because when writing to `cx` it has to preserve the top 48 bits causing partial register dependency stalls. Using `ecx` zeroes the unused top bits automatically. – Jester Oct 27 '19 at 23:54
  • 1
    Makes sense, I changed it in my code. Thanks for the info! @Jester – Tom el Safadi Oct 27 '19 at 23:55
  • 1
    and instructions operating on 32-bit registers are generally smaller than 16-bit instructions due to the lack of size prefix – phuclv Oct 28 '19 at 00:16
  • 1
    [The advantages of using 32bit registers/instructions in x86-64](//stackoverflow.com/q/38303333) - 32-bit is the default operand-size and doesn't have any partial-register performance problems. – Peter Cordes Oct 28 '19 at 03:10

0 Answers0