views:

173

answers:

3

I'm a novice programmer who is attempting assembly for the first time. Sorry in advance if this is an incredibly lame question.

I have a character stored in the EAX register, but I need to move it to my DL register. When I try: mov dl, eax I get an error C2443: operand size conflict. I know that the eax register is 32 bit while the dl is 8 bit... am I on to something?? How do I go about solving this.

+6  A: 

Try

xor edx,edx
mov dl, al

perhaps? The first instruction to zero out the 'un-neccessary' high order bits of edx (optional), then just move the low 8 from eax into edx.

As others have pointed out, movzx does this in one step. It's worth mentioning that along the same lines, if you had a signed value in al, you could use "movsx edx, al" to fill the high order bits of edx with a copy of the msb of al, thereby putting a signed 32 bit representation of al into edx.

JustJeff
That's right.The EAX register is 32 bits wide, whereas DL is only 8 bits.You need to move AL to DL, AL being a subset of EAX.
MPelletier
And xoring edx is just a darn good idea :) Nice and clean.
MPelletier
I don't understand the first instruction. Won't `xor edx,edx` zero out *all* of the bits in %edx?
JSBangs
@JS Bangs: Yes, that is usually what you'd want. The original question doesn't say anything about saving the rest of EDX, so to be safe it is best to zero it out. Otherwise there will be random stuff in there, and when you go to push it on the stack or something else, you will get unexpected results.
SoapBox
+2  A: 

If you just want to access the lower 8 bits of eax, then use al:

mov dl, al

You can access the lower 8 bits, 16 bits, or 32 bits of each general purpose register by changing the letters at the start or end. For register eax, using eax means use all 32 bits, ax is the lower 16 bits, and al is the lower 8 bits. The equivalent for ebx is ebx, bx and bl respectively, and so on.

Note that if you modify the lower 16 or 8 bits of a register, then the upper bits are unchanged. For instance, if you load all ones in eax, then load zero into al, then the lower 8 bits of eax will be zeroes, and the higher 24 bits will be ones.

mov eax, -1 ; eax is all ones
mov al, 0 ; higher 24 bits are ones, lower 8 bits are zeros
Michael Williamson
+8  A: 

What you want is probably:

movzx edx, al

This will copy al to dl and zero fill the rest of edx. This single instruction is equivalent these two instructions:

xor edx, edx 
mov dl, al
SoapBox
I prefer this. movzx takes care of all the trailing bits. Yay for some abstraction! (My enthusiasm should betray my lack of experience with assembly).
MPelletier