\documentstyle[art11,fullpage]{article}
\title{X}
\author{}
\date{\today}
%\input{abbrev.tex}
\input{comm}

\begin{document}

{\Huge

\bi
\item 8-bit Numbers
\item []
\item The Negative of N, written as $-N_2$, is computed from 
	\[ 2^8 - N_2  = 1\;0000\;0000_2 - N_2 \]
\item[] 
\item Example:
	\[ \begin{array}{l r}
		N_2 = 0000\; 0011_2 &  \\
		-N_2 = &	1\;0000\;0000_2 \\
		&	- 0000\;0011_2 \\
		& 	\rule{1.5in}{0.5mm} \\
		-N_2 = &	 1111\;1101_2
	   \end{array}
	 \]
\item[]
\item {\bf Definition:} The negative of N is called the {\bf two's complement
of N} and is denoted by  {\bf 2comp(N)}
\ei

\newpage

\bi
\item \fbox{Math Tip:}
\item When calculating $2comp(N_2)$ use this ``shortcut''
\item 8-Bits
	\[	\begin{array}{l c l}
		2comp(N_2)	& =	& 2^8 - N_2 \\
				& =	& ((2^8 - 1) - N_2) + 1 \\
				& = 	& (1111 \; 1111 - N_2 ) + 1
		\end{array}
	\]
\item This is a shortcut because \[1111\; 1111 - N_2\] can be calculated by
simply replacing each $0$ by $1$, and each $1$ with $0$ in $N_2$. (You don't
have to worry so much about borrows).
\item Two Steps in Calculating 2comp(N)
	\be
	\item Complement $N_2$ ; also called $1comp(N_2)$
	\item Add 1
	\ee
\ei

\newpage

\fbox{N-bit Two's Complement Numbers}
\be
\item Two's complement n-bit numbers are binary numbers of length n. Bits are
indexed from 0 to $n-1$. For each bit $d_i$, the subscript corresponds to the
power of 2 in the binary expansion.
%
\item {\bf Range: } \( -2^{n-1} \;  \ldots  \; (2^{n-1}  - 1) \)
%
\item {\bf Sign:}MSB = Sign bit; $1 \rightarrow -ive, \;\;\; 0 \rightarrow
+ive$  
%
\item For an n-bit number M, Negative of M is 2comp(M)
	\be 
	\item 2comp(M) = $2^n - M$
	\item Shortcut: 2comp(M) = 1Comp(M) + 1
	\ee
%
\item $\forall$ values of M, $M + 2comp(M) = 0$
	\bi
	\item Except for $-2^{n-1}$, $2comp(-2^{n-1}) = -2^{n-1}$ !!!!!!
	\ei
%
\newpage
\item {\bf Magnitude:} The magnitude of a negative number in two's complement
representation is hidden. You know it is negative because the MSB  =  1, but
what is its magnitude? 
	\bi
	\item Magnitude of a negative number is calculated by finding its two's
complement: 
		\bi 
		\item example: $M = 10001011_2$
		\item M is negative, because MSB = 1
		\item What is M's magnitude;  M is -ive what?
		\item $2comp(M) = 01110100_2 + 1 = 01110101_2 = 117_{10}$
		\item Therefore : M is $-117_{10}$
		\ei
	\ei
\ee

\newpage

\bi
\item []\fbox{2's complement Hex numbers}
\item We can compactly write binary numbers in HEX
\ei
\begin{verbatim}
Size = 8 bits
2's complement         Decimal value           HEX
0111 1111                  127                  7F
0111 1110                  126                  7E
0111 1101                  125                  7D
.....                    .....                .....
0000 0011                    3                  03
0000 0010                    2                  02
0000 0001                    1                  01
0000 0000                    0                  00
1111 1111                   -1                  FF
1111 1110                   -2                  FE
1111 1101                   -3                  FD
.....                    .....                 .....
1000 0010                 -126                  82
1000 0001                 -127                  81
1000 0000                 -128                  80
\end{verbatim}

\newpage

{\LARGE
\bi
\item[] {\Huge 2's complement Hex numbers} \ldots cont'd
%
\item[] 16-bit numbers
\item example 1: 2comp(06FD)
\[
\begin{array}{l c r}
	\begin{array}{r}
	2^{16} - 06FD \\
	\rule{1.5in}{0.9mm} \\
	10000 \\
	-06FD \\
	\rule{1.2in}{0.5mm} \\
	\\
	\\
	\\
	 F903 \\
	\rule{1.5in}{0.9mm} \\
	\end{array} &
	\mbox{\hspace{0.4in}OR\hspace{0.4in}} &
	\begin{array}{r}
	\mbox{SHORTCUT}\\
	\rule{1.5in}{0.9mm} \\
	 FFFF \\
	-06FD \\
	\rule{1.2in}{0.5mm} \\
	 F902 \\
	+0001 \\
	\rule{1.2in}{0.5mm} \\
	 F903 \\
	\rule{1.5in}{0.9mm} \\
	\end{array} 
\end{array} 
\]
\item[]
\item example 2: 2comp(903B)
\[
\begin{array}{l c r}
	\begin{array}{r}
	2^{16} - 903B \\
	\rule{1.5in}{0.9mm} \\
	10000 \\
	-903B \\
	\rule{1.2in}{0.5mm} \\
	\\
	\\
	\\
	 6FC5\\
	\rule{1.5in}{0.9mm} \\
	\end{array} &
	\mbox{\hspace{0.4in}OR\hspace{0.4in}} &
	\begin{array}{r}
	\mbox{SHORTCUT}\\
	\rule{1.5in}{0.9mm} \\
	 FFFF \\
	-903B \\
	\rule{1.2in}{0.5mm} \\
	 6FC4 \\
	+0001 \\
	\rule{1.2in}{0.5mm} \\
	 6FC5 \\
	\rule{1.5in}{0.9mm} \\
	\end{array} 
\end{array} 
\]
\ei
}  % end LARGE

\newpage

\bi
\item[] \fbox{Answers you should know}
\item[]
\item What's the two's complement representation of $-9C8$?
\item How many integers (positive and negative) can we represent in n bits
using the 2's complement representation?
\item Conversely, if we want to represent the integers between $-N \; to \;N$,
what's the minimum number of bits that we need?
\ei

\newpage
\bi
\item[] \fbox{SIGN EXTENSION}
\ei
\begin{verbatim}
Decimal    3-Bit     4-Bit         8-Bit
0             000        0000        0000 0000
1             001        0001        0000 0001
2             010        0010        0000 0010
3             011        0011        0000 0011

-1            111        1111        1111 1111
-2            110        1110        1111 1110
-3            101        1101        1111 1101
-4            100        1100        1111 1100
\end{verbatim}
\bi
\item {\bf Definition:}  Let X be a 2's complement number with fixed length M.
The sign extension of X with fixed length N $(N > M)$ replicates the
higher-order bit of X in its high-order (N - M) bits.
\ei

\newpage

\bi
\item \fbox{Examples}
\item {\bf Binary: }8-bit number to 16-bit number
	\be
	\item \(1010 \; 1100 \rightarrow 1111 \; 1111 \; 1010 \; 1100 \)
	\item \(0001 \;1100 \rightarrow 0000 \; 0000 \; 0001 \; 1100 \)
	\ee
\item {\bf Hex:} 8-bit to 16-bit
	\be
	\item \(9B \rightarrow FF \; 9B \)
	\item[] \(9B = 1001\; 1011\); MSB is 1; sign extension \(1111\;1111\;
1001 1011 = FF \; 9B\)
	\item \(5F \rightarrow 00 \; 5F \)
	\item \(7A \rightarrow 00 \; 7A \)
	\item \(87 \rightarrow FF \; 87 \)
	\item \(A5 \rightarrow FF \; A5 \)
	\item \(D6 \rightarrow FF \; D6 \)
	\item \(36 \rightarrow 00 \; 36 \)
	\item \(6C \rightarrow 00 \; 6C \)
	\ee 
\ei

\newpage

\bi
\item[]\fbox{Addition and Subtraction of 2's complement}
\item {\bf Addition Rule:} Add two terms using straight binary addition.
{\em DISCARD} any final carry.
\item {\bf Subtraction Rule:} \( N - M = N + 2comp(M) \)
\item[]
\item Addition
\ei

\[
\begin{array}{r r c r r}
	& -2 	& \hspace{1in}	& 	& 1111\;1110 \\
+	&  3	& 		& +	& 0000\;0011 \\
	& \rule{0.25in}{0.5mm}&	& 	& \rule{1.5in}{0.5mm} \\
	& 1	&		& carry = 1	& 0000\;0001 \\
	&	&		&		&	\\
	&	&		& result & 0000\;0001 \\
\end{array}
\]

\[
\begin{array}{r r c r r}
	& -1 	& \hspace{1in}	& 		& 1111\;1111 \\
+	& -1	& 		& +		& 1111\;1111 \\
	& \rule{0.25in}{0.5mm}&	& 		& \rule{1.5in}{0.5mm} \\
	& -2	&		& carry = 1	& 1111\;1110 \\
	&	&		&		&	\\
	&	&		& result 	& 1111\;1110 \\
\end{array}
\]

\newpage

\bi
\item Subtraction
\ei		
\[
\begin{array}{r r r r c r r}
	& 1 	&  & 1 & \hspace{1in}	& 		& 0000\;0001 \\
-	& 2	& +& -2&		& +		& 1111\;1110 \\
	& \rule{0.25in}{0.5mm}
                & \hspace{0.5in}  
		   & \rule{0.25in}{0.5mm}
		       & 		& 		& \rule{1.5in}{0.5mm}\\
	& -1	&  & -1& 		& carry = 0	& 1111\;1111 \\
	&	&  &   & 		&		&	\\
	&	&  &   & 		& result 	& 1111\;1111 \\
\end{array}
\]

\[
\begin{array}{r r r r c r r}
	& 8 	&  & 8 & \hspace{1in}	& 		& 0000\;1000 \\
-	& 7	& +& -7&		& +		& 1111\;1001 \\
	& \rule{0.25in}{0.5mm}
                & \hspace{0.5in}  
		   & \rule{0.25in}{0.5mm}
		       & 		& 		& \rule{1.5in}{0.5mm}\\
	& 1	&  &  1& 		& carry = 1	& 0000\;0001 \\
	&	&  &   & 		&		&	\\
	&	&  &   & 		& result 	& 0000\;0001 \\
\end{array}
\]

\newpage

\bi
\item[]\fbox{Two's complement Hex operations}
\item A5B6 + 3A94 = ?
\item 803E + A74B = ?
\item D67A + 80D7 = ?
\item[]
\item 30FC - 0011 = ?
\item 5F3C - A0F4 = ?
%
\ei

\newpage


\fbox{ {\bf OVERFLOW }}
\bi
\item Signed Overflow
\item \(3000_h + 6000_h = 9000_h\); (+ive $+$ +ive = -ive) ???
\item[] The sum of these +ive numbers exceeds the upper limit of 16bit 2's
complement positive numbers $7FFF_h \rightarrow $ {\bf Signed Overflow}
\item Unsigned Overflow
\item \(6000_h + D000_h = 3000_h\) Adding two +ive numbers results in a smaller
number ???
\item[] Sum exceeds $FFFF_h$ (largest 16bit unsigned number) $\rightarrow$
{\bf Unsigned Overflow}
\ei

\newpage

\bi
\item \underline{Checking for Signed Overflow}
	\bi
	\item Addition:
		\be
		\item OCCURS When sum of two numbers of {\em like} sign has a
		result with the Opposite sign. 
		\item Never occurs when adding two numbers of opposite sign
		\ee
	\item Subtraction: M - N = M + 2comp(N)
		\be
		\item -ive - +ive = +ive
		\item +ive - -ive = -ive
		\item Never Occurs when subtracting two numbers of the same
		      sign. 
		\ee
	\ei
\item [] 50A3 + 6A38 = 
\item [] 3826 - 7000 = 
\item [] 38A3 + A330 =
\item [] C839 - 7000 = 
\item NOTE: {\em The Microprocessor checks for signed overflow and flags it}.
You must be aware of this and insert code to respond to the possibility of
overflow!
\ei

\newpage

\bi
\item Signed overflow carry test
\item Two's complement numbers: M, N 
\item[] M + N produces signed overflow iff the carry {\bf into} the high-order
bit is {\bf not equal} to the carry {\bf out} of the high-order bit
\ei


\newpage

\fbox{Representing Characters}
\bi
\item {\bf ASCII:} American Standard Code for Information Interchange
\item EBCDIC: Extended Binary Coded Decimal Interchange Code (IBM)
\item ASCII: Fit in One byte but is a 7-bit code
\item $2^7 = 128_{10}$ characters
\item 95 printable characters
\item 33 control characters for communications and peripheral interface
controls 
\item A = $41_h$ ; Z = $5A_h$ ; a = $61_h$; z = $7A_h$
\item Adding $32_{10} = 20_h$  converts from Upper case to Lower case for the
letters of the alphabet
\ei
}  % end HUGE
\end{document}
