When I compile my assembly file (code below) I get the message Error: invalid instruction suffix for `mov' pointing to my movw instruction line. Now, I've Googled a bit and still don't understand this. My CPU is 64-bit, but since I'm clearly telling gcc to generate 32-bit code shouldn't movw be a valid instruction to move the lower 16 bits of a register? I'm copying the example almost verbatim from my book at hand.
remnux@remnux:~/Assembly$ gcc -o zx zx.s -gstabs -m32 zx.s: Assembler messages: zx.s:10: Error: invalid instruction suffix for `mov'
.code32
.section .data
myString:
.asciz "eax is now %d\n"
.section .text
.globl main
main:
movl $9999, %ebx
movl $3, %eax
movw %ax, %ebx
pushl %ebx
pushl %eax
pushl %ebx
pushl $myString
call printf
add $8, %esp
popl %eax
popl %ebx
movzx %ax, %ebx
pushl %ebx
pushl %eax
pushl %ebx
pushl $myString
call printf
add $8, %esp
call printf
call exit
movw
with a 32-bit%ebx
-- that can't work. – Chris Doddmovzx %ax, %ebx
. This is available in ia32 (386 and later). – EOFmovw %ax, %bx
-- the instruction size and all the register sizes must match, or the assembler will flag it as an error. – Chris Doddmov %eax, %ebx
is shorter and faster thanmovw %ax, %bx
. It avoids a partial write of ebx. Only use 16bit instructions when you actually need truncation to 16bits (e.g. a 16bit store of the low 16.) Often you can still use wider add/sub/mul instructions – Peter Cordes