;These instructions require a simple convention of binary. In computers, numbers can either be "signed" or "unsigned." "Unsigned numbers" are always positive. However, you can sacrifice 1 bit and cut the number of positive numbers in half. You'll end up with 1 more negative number than positive numbers. Also, all the other bits get flipped for negative numbers. So for positive numbers, all is the same. However, -1 is 1...1, -2 is 1...10. Why ...? Because how many 1s is determined by the size of the thing.

include "macros.inc"
begin

        mov eax, -1
        mov ebx, -2
        mov ecx, -3
        mov edx, -4
        int3

;cwd "sign extends" ax to dx (convert word to dword). Meaning, if it's positive, dx gets set to zero. If it's negative, dx gets set to 1s.

        mov ax, 4
        cwd
        int3
        mov ax, -4
        cwd
        int3 ;Note, the high words are not affected.

;cdq does the same for 32bits instead of 16 (cwd is a relic from 16bit days before you could prefix registers with "e"). There are alot of instructions like this so that eax and edx can be used in tandem to provide math in bits higher than the cpu can presently otherwise handle (due to register size limits).

;cbw takes al and sign extends it throughout ax, and cwde sign extends ax through eax.

;movsx sign extends the source oprand into the destination oprand.
        mov al, -4
        movsx ecx, al

;movzx zero extends (ignores signing)

        movzx ebx, al
        int3

terminate
end

;Actually, i've forgotten most all these, actually. They're convenient, but there are other ways of zero extending. However, this was a nice refresher for me.


Get your own web kitty here!
┬ęCopyright 2010, 2011, 2012, 2013, 2014, 2017. All rights reserved.