📄 chapter 3 numbers, characters and strings -- valvano.htm
字号:
<TBODY>
<TR>
<TD vAlign=top width="25%"><U><FONT size=2>type</FONT></U></TD>
<TD vAlign=top width="25%"><U><FONT size=2>range</FONT></U></TD>
<TD vAlign=top width="25%"><U><FONT size=2>precision</FONT></U></TD>
<TD vAlign=top width="25%"><U><FONT size=2>examples</FONT></U></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>unsigned char</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>0 to 255</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>8 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>0 10 123</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>char</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-127 to 127</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>8 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-123 0 10 +10</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>unsigned int</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>0 to 65535U</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>0 2000 2000U 50000U</FONT> </TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>int</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-32767 to 32767</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-1000 0 1000 +20000</FONT> </TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>unsigned short</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>0 to 65535U</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>0 2000 2000U 50000U</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>short</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-32767 to 32767</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-1000 0 1000 +20000</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>long</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-2147483647L to
2147483647L</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>32 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-1234567L 0L
1234567L</FONT></TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 3-10. The range of decimal numbers.</ADDRESS>
<P>Because the 6811 and 6812 microcomputers are most efficient for 16 bit data
(and not 32 bit data), the <B>unsigned int</B> and <B>int</B> data types are 16
bits. On the other hand, on a x86-based machine, the <B>unsigned int</B> and
<B>int</B> data types are 32 bits. In order to make your software more
compatible with other machines, it is preferable to use the <B>short</B> type
when needing 16 bit data and the <B>long</B> type for 32 bit data.</P>
<P>
<TABLE cellSpacing=0 cellPadding=0 width=360 border=0>
<TBODY>
<TR>
<TD vAlign=top width="25%"><U><FONT size=2>type</FONT></U></TD>
<TD vAlign=top width="25%"><U><FONT size=2>6811/6812</FONT></U></TD>
<TD vAlign=top width="25%"><U><FONT size=2>x86</FONT></U></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>unsigned char</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>8 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>8 bits</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>char</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>8 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>8 bits</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>unsigned int</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>32 bits</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>int</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>32 bits</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>unsigned short</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>short</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>long</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>32 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>32
bits</FONT></TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 3-11. Differences between a 6811/6812 and an x86</ADDRESS>
<P>Since the 6811 and 6812 microcomputers do not have direct support of 32-bit
numbers, the use of long data types should be minimized. On the other hand, a
careful observation of the code generated yields the fact that these compilers
are more efficient with 16 bit numbers than with 8 bit numbers.</P>
<P>Decimal numbers are reduced to their two's complement or unsigned binary
equivalent and stored as 8/16/32-bit binary values. </P>
<P><A name=DECIMALEXAMPLE></A>The manner in which decimal literals are treated
depends on the context. For example</P>
<DIR>
<P><CODE>short I;<BR>unsigned short J;<BR>char K;<BR>unsigned char L;<BR>long
M;<BR>void main(void){
<BR> I=97; /* 16 bits 0x0061
*/<BR> J=97; /* 16 bits 0x0061
*/<BR> K=97; /* 8 bits 0x61
*/<BR> L=97; /* 8 bits 0x61
*/<BR> M=97; /* 32 bits 0x00000061
*/}</CODE></P></DIR>
<P>The 6812 code generated by the ICC12 compiler is as follows</P>
<DIR>
<P><CODE> .area text <BR>_main::
<BR> pshx <BR> tfr s,x
<BR> movw #97,_I ;16
bits<BR> movw #97,_J ;16 bits
<BR> movb #97,_K ;8 bits
<BR> movb #97,_L ;8 bits
<BR> ldy #L2 <BR> jsr __ly2reg ;32
bits<BR> ldy #_M <BR> jsr __lreg2y
<BR> tfr x,s <BR> pulx
<BR> rts <BR> .area bss <BR>_M::
.blkb 4 <BR>_L:: .blkb 1 <BR>_K:: .blkb 1 <BR>_J:: .blkb 2 <BR>_I:: .blkb 2
<BR> .area text <BR>L2: .word 0,97</CODE></P></DIR>
<P>The 6812 code generated by the Hiware compiler is much more efficient when
dealing with 32 bit long integers</P>
<DIR>
<P><CODE> LDAB #97<BR> CLRA
<BR> STD I<BR> STD
J<BR> STAB K<BR> STAB
L<BR> STD M:2<BR> CLRB
<BR> STD M<BR> RTS
<BR></CODE></P></DIR>
<P><B><I><FONT face=Helvetica,Arial><A name=OCTAL></A>Octal
Numbers</FONT></I></B></P>
<P>If a sequence of digits begins with a leading <B>0</B>(zero) it is
interpreted as an octal value. There are only eight octal digits, 0 through 7.
As with decimal numbers, octal numbers are converted to their binary equivalent
in 8-bit or 16-bit words. The range of an octal number depends on the data type
as shown in the following table.</P>
<P>
<TABLE cellSpacing=0 cellPadding=0 width=480 border=0>
<TBODY>
<TR>
<TD vAlign=top width="25%"><U><FONT size=2>type</FONT></U></TD>
<TD vAlign=top width="25%"><U><FONT size=2>range</FONT></U></TD>
<TD vAlign=top width="25%"><U><FONT size=2>precision</FONT></U></TD>
<TD vAlign=top width="25%"><U><FONT size=2>examples</FONT></U></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>unsigned char</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>0 to 0377</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>8 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>0 010 0123</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>char</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-0200 to 0177</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>8 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-0123 0 010 +010</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>unsigned int</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>0 to 0177777</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>0 02000 0150000U</FONT> </TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>int</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-077777 to 077777</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-01000 0 01000 +020000</FONT>
</TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>unsigned short</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>0 to 0177777</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>0 02000 0150000U</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>short</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-077777 to 077777</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>16 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-01000 0 01000
+020000</FONT></TD></TR>
<TR>
<TD vAlign=top width="25%"><FONT size=2>long</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-017777777777L to
017777777777L</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>32 bits</FONT></TD>
<TD vAlign=top width="25%"><FONT size=2>-01234567L 0L
01234567L</FONT></TD></TR></TBODY></TABLE></P>
<ADDRESS>Table 3-12. The range of octal numbers.</ADDRESS>
<P>Notice that the octal values 0 through 07 are equivalent to the decimal
values 0 through 7. One of the advantages of this format is that it is very easy
to convert back and forth between octal and binary. Each octal digit maps
directly to/from 3 binary digits.</P>
<P><B><I><FONT face=Helvetica,Arial><A name=HEX></A>Hexadecimal
Numbers</FONT></I></B></P>
<P>The hexadecimal number system uses base 16 as opposed to our regular decimal
number system that uses base 10. Like the octal format, the hexadecimal format
is also a convenient mechanism for us humans to represent binary information,
because it is extremely simple for us to convert back and forth between binary
and hexadecimal. A <I>nibble </I>is defined as 4 binary bits. Each value of the
4-bit nibble is mapped into a unique hex digit.</P>
<P>
<TABLE cellSpacing=0 cellPadding=0 width=349 border=0>
<TBODY>
<TR>
<TD vAlign=top width="41%"><FONT size=2>Hex Digit</FONT> </TD>
<TD vAlign=top width="28%"><FONT size=2>Decimal Value</FONT> </TD>
<TD vAlign=top width="31%"><FONT size=2>Binary Value</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>0</FONT></TD>
<TD vAlign=top width="28%"><FONT size=2>0</FONT></TD>
<TD vAlign=top width="31%"><FONT size=2>0000</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>1</FONT></TD>
<TD vAlign=top width="28%"><FONT size=2>1</FONT></TD>
<TD vAlign=top width="31%"><FONT size=2>0001</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>2</FONT></TD>
<TD vAlign=top width="28%"><FONT size=2>2</FONT></TD>
<TD vAlign=top width="31%"><FONT size=2>0010</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>3</FONT></TD>
<TD vAlign=top width="28%"><FONT size=2>3</FONT></TD>
<TD vAlign=top width="31%"><FONT size=2>0011</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>4</FONT></TD>
<TD vAlign=top width="28%"><FONT size=2>4</FONT></TD>
<TD vAlign=top width="31%"><FONT size=2>0100</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>5</FONT></TD>
<TD vAlign=top width="28%"><FONT size=2>5</FONT></TD>
<TD vAlign=top width="31%"><FONT size=2>0101</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>6</FONT></TD>
<TD vAlign=top width="28%"><FONT size=2>6</FONT></TD>
<TD vAlign=top width="31%"><FONT size=2>0110</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>7</FONT></TD>
<TD vAlign=top width="28%"><FONT size=2>7</FONT></TD>
<TD vAlign=top width="31%"><FONT size=2>0111</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>8</FONT></TD>
<TD vAlign=top width="28%"><FONT size=2>8</FONT></TD>
<TD vAlign=top width="31%"><FONT size=2>1000</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>9</FONT></TD>
<TD vAlign=top width="28%"><FONT size=2>9</FONT></TD>
<TD vAlign=top width="31%"><FONT size=2>1001</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>A or a</FONT> </TD>
<TD vAlign=top width="28%"><FONT size=2>10</FONT> </TD>
<TD vAlign=top width="31%"><FONT size=2>1010</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>B or b</FONT> </TD>
<TD vAlign=top width="28%"><FONT size=2>11</FONT> </TD>
<TD vAlign=top width="31%"><FONT size=2>1011</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>C or c</FONT> </TD>
<TD vAlign=top width="28%"><FONT size=2>12</FONT> </TD>
<TD vAlign=top width="31%"><FONT size=2>1100</FONT> </TD></TR>
<TR>
<TD vAlign=top width="41%"><FONT size=2>D or d</FONT> </TD>
<TD vAlign=top width="28%"><FONT size=2>13</FONT> </TD>
<TD vAlign=top width="31%"><FONT size=2>1101</FONT> </TD></TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -