?? chap06.html
字號:
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<title>06 TAJGA FASM Tutorial</title>
</head>
<body>
<center><b>CHAPTER 6 - Bit arithmetics</b></center><br><br>
This is what most tutorials usually start with. After reading this you will be
confused, it is normal, you will get this by usage. Return to this chapter
whenever needed. So let's go.<br><br>
<b>6.1. Encoding number in bits</b><br>
You know computer uses "bits", which are variables that can contain one of two
possible values, 0 or 1. When value of bit is 0, we say it is "clear", when it
is 1, we say it is "set"<br><br>
<blockquote class="term">
terms:<br>
<b>bit</b> - variable containing 0 or 1<br>
<b>clear bit</b> - bit containing 0<br>
<b>set bit</b> - bit containing 1<br>
</blockquote>
Now how can we store number in such bits. It is similar to storing word in two
bytes (chapter 4.2, reread it). One bit contains value 0 or 1, so number that
consists from only one bit can contain only values 0 and 1. When we add
another bit, we can still store 0 and 1 in first bit, but we have another bit
which now can hold 2*(0 or 1). Then another bit holds 4*(0 or 1), then 8*(0 or
1) etc.<br><br>
Like i said before, byte consists of 8 bits. So it can hold value<br>
<code>1*(0 or 1)+2*(0 or 1)+4*(0 or 1)+8*(0 or 1)+16*(0 or 1)+32*(0
or 1)+64*(0 or 1)+128*(0 or 1)</code> which is value from 0 (when all bits
are 0) to <code>1+2+4+8+16+32+64+128</code> = 255 (when all bits are
1). See it?<br><br>
It is similar for word, just with 16 bits instead of 8, check it yourself if
you wish.<br><br>
Now some terms: bit which holds 1*(0 or 1) is bit 0, next, which holds 2*(0 or
1) is bit 1, and so until bit 7 which holds 128*(0 or 1). So bits are
enumerated from 0, not from 1 as you would maybe await. bit 0 is called "low
order bit", highest bit (which holds greatest value) is "high order bit". For
example high order bit of some byte value is bit 7, and high order bit of word
value is bit 15.
<blockquote class="term">
terms: <b>low-order bit, high-order bit</b>
</blockquote>
<b>important:</b> bits are enumerated from 0, not from 1, so first bit is bit#0<br><br>
Number encoded this way (in bits) is called "binary number".<br><br><br>
<b>6.2. Binary constants</b><br>
You was using numeric constants before probably without deeper realizing you
are using them. Numeric constants were just numbers you wrote in source file
which were assembled into binary file. Numeric constants are for example "0",
"50", "-100", "123456".<br><br>
You used them here:
<blockquote class="code"><pre>
db <font color=#339933>5</font>
mov al<font color=#333399>,</font><font color=#339933>20</font>
cmp ax<font color=#333399>,</font><font color=#339933>5</font>
db <font color=#bb0000>'Some string'</font><font color=#333399>,</font><font color=#339933>0</font>
org <font color=#339933>256</font>
</pre></blockquote>
These numbers were decimal: ones which are used by (?) normal people.
Assembler then translated them to binary form. But sometimes you want to
specify directly binary numbers. Of course you dont have to hand-translate to
decimal, you can specify them directly binary. Here are some examples of
binary numbers: 0, 101011, 1101011, 11111, etc. To differ them from decimal
numbers, every binary number must end with "b" character, so "0b", "101011b",
"1101011b", "11111b" etc. Here first binary digit (first bit, first 0 or 1) is
high-order bit, last one is low-order bit. So if you write "1101", then bit 0
= 1, bit 1 = 0, bit 2 =1, bit 3 = 1.<br><br>
Example table:
<table align=center width=200 border=0 bgcolor=#7F91EC cellspacing=1 cellpadding=0 class="tut_tab">
<tr align=center><th>decimal</td><th>binary</td>
<tr align=center><td>0 </td><td> 0b</td></tr>
<tr align=center><td>1 </td><td> 1b</td></tr>
<tr align=center><td>2 </td><td> 10b</td></tr>
<tr align=center><td>3 </td><td> 11b</td></tr>
<tr align=center><td>4 </td><td> 100b</td></tr>
<tr align=center><td>5 </td><td> 101b</td></tr>
<tr align=center><td>6 </td><td> 110b</td></tr>
<tr align=center><td>7 </td><td> 111b</td></tr>
<tr align=center><td>10</td><td> 1010b</td></tr>
<tr align=center><td>15</td><td> 1111b</td></tr>
<tr align=center><td>16</td><td>10000b</td></tr>
</table><br>
I could learn you way how to translate number between decimal and binary
forms, but you wont need it now anyway, and all other tutorials are full of
such informations.<br><br>
Binary numeric constants are just another way to express some number. Writing
"5" is same as writing "101b". For example this will work too:
<blockquote class="code"><pre>
org <font color=#339933>100000000b</font>
mov ah<font color=#333399>,</font><font color=#339933>1001b</font>
mov dx<font color=#333399>,</font>string
int <font color=#339933>21h</font>
int <font color=#339933>20h</font>
string db <font color=#bb0000>'Hello world writen using binary constants'</font><font color=#333399>,</font><font color=#339933>0</font>
</pre></blockquote>
<code>org 100000000b</code> is same as <code>org 256</code>, and <code>mov ah,1001b</code>
is same as <code>mov ah,9</code>
<br><br><br>
<b>6.3. Bit operations</b><br>
Now let's think about what we can do with bit (which can hold value 0 or 1).
<br><br>
First, we can "set" it (set it's value to 1) or "clear" it (set it's value to
0). Then we can "flip" it's value (from 0 to 1, from 1 to 0). And that is
probably all. This operation is called "bit complement" too (0 is complement
of 1, 1 is complement of 0)<br><br>
(lack of english math terms here, somebody could help me)<br><br>
Now what we can do with 2 bits? If you think of bits as about boolean values,
which can be either true (1) or false (0). Now what operations can we make
with boolean values? If you programmed before you probably know this.<br><br>
First is <code>and</code> (like "a and b" where "a" and "b" are boolean values
:). When we have two boolean values, result of <code>and</code>ing them is true
only when they are both true, otherwise result if false. (Table will be below)<br><br>
Then comes <code>or</code>. You know, result of <code>or</code>ing two values is
true, when at least one of them is true. And last, least known, is <code>xor</code>,
which means "exclusive or" (the previous one was "inclusive or", but everyone calls it
just "or"). Result of <code>xor</code>ing is 1 when one operand is 1 and other is 0.
<br><br>
Here is the table:<br>
<table align=center width=320 border=0 bgcolor=#7F91EC cellspacing=1 cellpadding=0 class="tut_tab">
<tr align=center><th>A</th><th>B</th><th>A and B</th><th>A or B</th><th>A xor B</th></tr>
<tr align=center><td>0</td><td>0</td><td> 0 </td><td> 0 </td><td> 0 </td></tr>
<tr align=center><td>0</td><td>1</td><td> 0 </td><td> 1 </td><td> 1 </td></tr>
<tr align=center><td>1</td><td>0</td><td> 0 </td><td> 1 </td><td> 1 </td></tr>
<tr align=center><td>1</td><td>1</td><td> 1 </td><td> 1 </td><td> 0 </td></tr>
</table><br>
<b>NOTE:</b> There are 16 possible operations on two bits but we wont talk
about them all.<br><br>
Now interesting (?) part: In late times, processors designers didnt like
having lot of instructions on their processors. But as you see, we defined 3
operations for single bit and 3 for two bits. So they found (obvious) way to
achieve operations on single bit by using operations on two bits. To remind:
operations on single bit were setting it to 0, setting it to 1 and flipping
it's value (0->1, 1->0). How?<br><br>
First let's talk about clearing bit (setting it's value to 0). Note that
result of <code>and</code> is 0 whenever at least one of operands is 0. So if we
<code>and</code> any bit (0 or 1) with 0 we always get 0, when we <code>and</code>
with 1 bit will reamin unchanged. And this is what we wanted. It is similar with
setting bit (to 1). Result of <code>or</code>ing is 1 when at least one of
operands is 1. So <code>or</code>ing any bit with 1 will always produce 1,
<code>or</code>ing with 0 will leave bit unchanged.<br><br>
How to flip bit? Result of <code>xor</code>ing is 1 when one of operands is 1,
other 0. So <code>xor</code>ing any value with 1 will always produce value's
complement, <code>xor</code>ing with 0 will leave bit unchanged. This one is not
so obvious, so better look at it in table.<br><br><br>
<b>6.4. Binary operation instructions</b><br>
First of all. You know smallest register we have are 8 bit (byte) registers.
Also smallest part of memory we can access is byte (8 bits). For this reason
binary operation instruction we will use will operate on two 8 bit numbers
instead on two bits. What will be result? Bit 0 of result will be result of
operation between bit 0 of first argument and bit 0 of second argument. Bit
1 of result will be result of operation on bits 1 of argument etc. You 'll
see it.<br><br>
First operation will be "and". Example:
<blockquote class="code"><pre>
mov al<font color=#333399>,</font><font color=#339933>00010001b</font>
mov bl<font color=#333399>,</font><font color=#339933>00000001b</font>
and al<font color=#333399>,</font>bl
</pre></blockquote>
first we load <code>al</code> with 00010001b, so it's bits 0 and 4 contain 1,
other bits contain 0. Then we load <code>bl</code> with 00000001b so it's bit 0
contains 1, other contain 0. Now when we <code>and</code> <code>al</code> with
<code>bl</code> (this is how asm coder usually tells it), it works like some
<code>al = al and bl</code> would eg. result of <code>and</code>ing
<code>al</code> with <code>bl</code> is stored in <code>al</code>.<br><br>
So what is the result (in <code>al</code>)? Bit 0 of <code>al</code> contained
1 and was <code>and</code>ed with 1. "1 and 1" is 1, so bit 0 of <code>al</code>
will be 1. Bits 1 to 2 and 5 to 7 would be "0 and 0" which is 0. Bit 3 would
contain "0 and 1" which is 0 too. Bit 4 will contain "1 and 0" which is 0 again. So result would be 00000001b.
Better way to write previus code would be
<blockquote class="code"><pre>
mov al<font color=#333399>,</font><font color=#339933>00010001b</font>
and al<font color=#333399>,</font><font color=#339933>00001001b</font>
</pre></blockquote>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -