0
votes

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
1
You've got a 16-bit movw with a 32-bit %ebx -- that can't work.Chris Dodd
Hmm, the book has this example, and I was under the impression that it would only move to the lower 16 bits of register ebx. Example from the book: *****There is no guarantee that the upper part of the EBX register contains zeroes. To accomplish this, you must use two instructions: movl $0, %ebx movw %ax, %ebx****** So, you think this is a typo and they meant something like movw %ax, %bx?patricio2626
If your processor supports it, you can use movzx %ax, %ebx. This is available in ia32 (386 and later).EOF
@patricio2626: yes, that would be a typo in your book -- to do a 16-bit mov it needs to be movw %ax, %bx -- the instruction size and all the register sizes must match, or the assembler will flag it as an error.Chris Dodd
@patricio2626: note that mov %eax, %ebx is shorter and faster than movw %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 instructionsPeter Cordes

1 Answers

1
votes

Chris Dodd was right but posted a comment and not an answer. As each answer should have closure, I'm answering my own question.

I was using a 16-bit movw with a 32-bit %ebx and the example in the book was incorrect. movw will work with a 16-bit register such as %bx, or movl to move 32 bits into %ebx.