<< Chapter < Page | Chapter >> Page > |
Two's-complement notation is an efficient way of representing signed numbers in microprocessors. It offers theadvantage that addition and subtraction can be done with ordinary unsigned operations. When a number is written intwo's complement notation, the most significant bit of the number represents its sign: 0 means that the number ispositive, and 1 means the number is negative. A positive number written in two's-complement notation is the same as thenumber written in unsigned notation (although the most significant bit must be zero). A negative number can bewritten in two's complement notation by inverting all of the bits of its absolute value, then adding one to the result.
Consider the following four-bit two's complement numbers (in binary form):
The maximum number that can be represented with a -bit two's-complement notation is , and the minimum number that can be represented is . The maximum integer that can be represented in a 16-bit memory register is 32767, and the minimum integer is-32768.
The DSP microprocessor is a 16-bit integer processor with some extra support for fractional arithmetic. Fractional arithmetic turns out to be very useful for DSP programming, since it frees us from worries about overflow onmultiplies. (Two 16-bit numbers, multiplied together, can require 32 bits for the result. Two 16-bit fixed-pointfractional numbers also require 32 bits for the result, but the 32-bit result can be rounded into 16 bits while onlyintroducing an error of approximately .) For this reason, we will be using fixed-point fractional representation to describe filter taps and inputs throughout this course.
Unfortunately, the assembler and debugger we are using do not recognize this fractional fixed-point representation. For thisreason, when you are using the assembler or debugger, you will see decimal values (ranging from -32768 to 32767) on screeninstead of the fraction being represented. The conversion is simple; the fractional number being represented is simply thedecimal value shown divided by 32768. This allows us to represent numbers between -1 and .
When we multiply using this representation, an extra shift left is required. Consider the two examplesbelow:
fractional | |
decimal | |
hex |
fractional | |
decimal | |
hex |
You may wish touse the MATLAB commands
hex2dec
and
dec2hex
.
When we do the multiplication, we are primarily interested inthe top 16 bits of the result, since these are the data that
are actually used when we store the result back into memoryand send it out to the digital-to-analog converter. (The
entire result is actually stored in the accumulator, sorounding errors do not accumulate when we do a sequence of
multiply-accumulate operations in the accumulators.) As theexample above shows, the top 16 bits of the result of
multiplying the fixed point fractional numbers together ishalf the expected fractional result. The extra left shift
multiplies the result by two, giving us the correct finalproduct.
The left-shift requirement can alternatively be explained by way of decimal place alignment. Remember that when wemultiply decimal numbers, we first multiply them ignoring the decimal points, then put the decimal point back in the laststep. The decimal point is placed so that the total number of digits right of the decimal point in the multiplier andmultiplicand is equal to the number of digits right of the decimal point in their product. The same applies here; the"decimal point" is to the right of the leftmost (sign) bit, and there are 15 bits (digits) to the right of this point. Sothere are a total of 30 bits to the right of the decimal in the source. But if we do not shift the result, there are 31bits to the right of the decimal in the 32-bit result. So we shift the number to the left by one bit, which effectivelyreduces the number of bits right of the decimal to 30.
Before the numbers are multiplied by the ALU, each term is
sign-extended generating a 17-bit number from the
16-bit input. Because the examples presented above are allpositive, the effect of this sign extension is simply adding
an extra "0" bit at the top of the register(
fractional | |
decimal | |
hex |
Note that even after the result is left-shifted by one bit following the multiply, the top bit of the result is still"0", implying that the result is incorrectly interpreted as a positive number.
To correct this problem, the ALU sign-extends negative multipliers and multiplicands by placing a "1" instead of a"0" in the added bit. This is called sign extension because the sign bit is "extended" to the left another place, addingan extra bit to the left of the number without changing the number's value.
fractional | |
hex |
Although the top bit of this result is still "0", after the
final 1-bit left-shift the result is
E000 000h
which is a negative number (the top bit is "1"). To check the
final answer, we can negate the product using the two'scomplement method described above. After flipping all of the
bits we have
1FFF FFFFh
, and adding one yields
2000 0000h
, which equals 0.25 when interpreted as
an 32 bit fractional number.
Notification Switch
Would you like to follow the 'Dsp laboratory with ti tms320c54x' conversation and receive update notifications?