Search code examples
assemblynasmx86-64movyasm

YASM: Instruction movsx refuses dword for operand size?


An assembly program I'm writing will not compile with the y assembler, citing:

error: invalid size for operand 2

On the following line:

movsx rbx, dword [rsi+4*rcx]    ; Copy double-word and sign extend.

However, I can't find any reason why dword should not work. I want to move a double-word (4 bytes) at address rsi+4*rcx into 8 byte register rbx. So there remain 32 bits to be "padded" after copying it into what is effectively ebx. If I change the size to byte, I get no error. But this is not what I want.

There is a question with a similar title here. However, the poster had forgotten to include any size operands whatsoever, and the answer to the question did not resolve my problem.

Edit: I've added the full program below in case the particular syntax I've copied here is not the culprit.

    segment .data

a:
    dd  1
    dd  3
    dd  0
    dd  1
    dd  7
    dd  9
    dd  5
    dd  2
b:
    dd  8
    dd  3
    dd  3
    dd  9
    dd  6
    dd  4
    dd  1
    dd  1

p   dq  0

    segment .text
    global main

main:
    xor rax, rax                    ; Set sum to 0.
    xor rcx, rcx                    ; Set counter to 0.
    lea rsi, [a]                    ; Set source 1.
    lea rdi, [b]                    ; Set source 2.

dot:
    movsx rbx, dword [rsi+4*rcx]    ; Copy in double-word.
    movsx rdx, dword [rdi+4*rcx]    ; Copy in other double-word.        
    imul rbx, rdx                   ; Multiply the two double-words.
    add rax, rbx                    ; Sum product so far.
    inc rcx
    cmp rcx, 8
    jz done
    jmp dot

done:
    mov [p], rax

    xor rax, rax
    ret

Solution

  • The assembler calls the desired instruction movsxd for some reason:

    movsxd rbx, dword [rsi+4*rcx]
    

    This should work.