[ Pobierz całość w formacie PDF ]
level language has. How data is interpreted depends on what instruction is
used on the data. Whether the hex value FF is considered to represent a
signed -1 or a unsigned +255 depends on the programmer. The C language
30 CHAPTER 2. BASIC ASSEMBLY LANGUAGE
defines signed and unsigned integer types. This allows a C compiler to
determine the correct instructions to use with the data.
2.1.2 Sign extension
In assembly, all data has a specified size. It is not uncommon to need
to change the size of data to use it with other data. Decreasing size is the
easiest.
Decreasing size of data
To decrease the size of data, simply remove the more significant bits of
the data. Here s a trivial example:
mov ax, 0034h ; ax = 52 (stored in 16 bits)
mov cl, al ; cl = lower 8-bits of ax
Of course, if the number can not be represented correctly in the smaller
size, decreasing the size does not work. For example, ifAXwere 0134h (or
308 in decimal) then the above code would still setCLto 34h. This method
works with both signed and unsigned numbers. Consider signed numbers,
ifAXwas FFFFh (-1 as a word), thenCLwould be FFh (-1 as a byte).
However, note that this is not correct if the value inAXwas unsigned!
The rule for unsigned numbers is that all the bits being removed must
be 0 for the conversion to be correct. The rule for signed numbers is that
the bits being removed must be either all 1 s or all 0 s. In addition, the first
bit not being removed must have the same value as the removed bits. This
bit will be the new sign bit of the smaller value. It is important that it be
same as the original sign bit!
Increasing size of data
Increasing the size of data is more complicated than decreasing. Consider
the hex byte FF. If it is extended to a word, what value should the word
have? It depends on how FF is interpreted. If FF is a unsigned byte (255
in decimal), then the word should be 00FF; however, if it is a signed byte
(-1 in decimal), then the word should be FFFF.
In general, to extend an unsigned number, one makes all the new bits
of the expanded number 0. Thus, FF becomes 00FF. However, to extend
a signed number, one must extend the sign bit. This means that the new
bits become copies of the sign bit. Since the sign bit of FF is 1, the new
bits must also be all ones, to produce FFFF. If the signed number 5A (90
in decimal) was extended, the result would be 005A.
2.1. WORKING WITH INTEGERS 31
There are several instructions that the 80386 provides for extension of
numbers. Remember that the computer does not know whether a number is
signed or unsigned. It is up to the programmer to use the correct instruction.
For unsigned numbers, one can simply put zeros in the upper bits using
aMOVinstruction. For example, to extend the byte in AL to an unsigned
word in AX:
mov ah, 0 ; zero out upper 8-bits
However, it is not possible to use aMOVinstruction to convert the unsigned
word in AX to an unsigned double word in EAX. Why not? There is no way
to specify the upper 16 bits of EAX in aMOV. The 80386 solves this problem
by providing a new instructionMOVZX. This instruction has two operands.
The destination (first operand) must be a 16 or 32 bit register. The source
(second operand) may be an 8 or 16 bit register or a byte or word of memory.
The other restriction is that the destination must be larger than the source.
(Most instructions require the source and destination to be the same size.)
Here are some examples:
movzx eax, ax ; extends ax into eax
movzx eax, al ; extends al into eax
movzx ax, al ; extends al into ax
movzx ebx, ax ; extends ax into ebx
For signed numbers, there is no easy way to use theMOVinstruction for
any case. The 8086 provided several instructions to extend signed numbers.
TheCBW(Convert Byte to Word) instruction sign extends the AL register
into AX. The operands are implicit. TheCWD(Convert Word to Double
word) instruction sign extends AX into DX:AX. The notation DX:AX means
to think of the DX and AX registers as one 32 bit register with the upper
16 bits in DX and the lower bits in AX. (Remember that the 8086 did not
have any 32 bit registers!) The 80386 added several new instructions. The
CWDE(Convert Word to Double word Extended) instruction sign extends
AX into EAX. TheCDQ(Convert Double word to Quad word) instruction
sign extends EAX into EDX:EAX (64 bits!). Finally, theMOVSXinstruction
works likeMOVZXexcept it uses the rules for signed numbers.
Application to C programming
[ Pobierz całość w formacie PDF ]