⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chap06.html

📁 vid写的FASM向导
💻 HTML
📖 第 1 页 / 共 2 页
字号:

(i used <code>bl</code> in previous example only to reference second number
easier in text).<br><br>

Now <code>or</code>ing:

<blockquote class="code"><pre>
mov al<font color=#333399>,</font><font color=#339933>00010001b</font>
or  al<font color=#333399>,</font><font color=#339933>00001001b</font>
</pre></blockquote>

result will be 00011001b, due to <code>or</code> description (one subchapter
upwards). <br><br>

And <code>xor</code>ing:

<blockquote class="code"><pre>
mov al<font color=#333399>,</font><font color=#339933>00010001b</font>
xor al<font color=#333399>,</font><font color=#339933>00001001b</font>
</pre></blockquote>

result will be 00011000b. Bits <code>xor</code>ed with 0 will remain, bits
<code>xor</code>ed will 1 will be flipped (to their complement).

<blockquote class="term">
instructions: <b>and, or, xor</b>
</blockquote>

These instructions take same arguments as <code>mov</code> eg. first argument
can be memory variable or register, second can be memory variable, register or
constant. Both arguments must be of same size, only one of arguments can be
memory variable.<br><br>

<b>6.5. Testing bits.</b><br>
If you was programming before, you probably already know about boolean
variables (rarely called logical). They can hold two values, TRUE or FALSE.
You see that they can be finely stored in bit, 1 for TRUE, 0 for FALSE.

<blockquote class="term">
term: <b>boolean variable</b>
</blockquote>

Problem here is, that smallest data directly accessible is byte (8 bits). You
know, you can access byte register or byte memory variable, not bit. This is
really so, there are no instruction which just access one bit. (Of course
there are, you just don't need to know about them now :).<br>
But when you work with boolean variable you want to access only one bit, not
all 8 bits or more. There are some tricks to do this:<br><br>

Use only one bit of whole byte and leave other bits cleared. Thus if you want
to see if bit is 0, you just check if whole byte is equal to 0. If it is not,
then our bit is 1. Example:

<blockquote class="code"><pre>
cmp <font color=#333399>[</font>byte_boolean_varaible<font color=#333399>],</font><font color=#339933>0</font>
je byte_boolean_variable_is_false
jnz byte_boolean_varaible_is_true
</pre></blockquote>

where <code>byte_boolean_variable</code> is byte varaible with only one bit
used. If such variable is 0 then value is FALSE, otherwise value is TRUE.<br>
<code>byte_boolean_variable_is_***</code> are labels used for branching as
shown in previous chapter. By the way better "more assembly" way to do same as
previous is:

<blockquote class="code"><pre>
  cmp <font color=#333399>[</font>byte_boolean_varaible<font color=#333399>],</font><font color=#339933>0</font>
  je byte_boolean_variable_is_false
byte_boolean_varaible_is_true<font color=#333399>:</font>
  <font color=#333399>&lt;</font>here value is TRUE<font color=#333399>&gt;</font>
byte_boolean_varaible_is_false<font color=#333399>:</font>
  <font color=#333399>&lt;</font>here value is FALSE<font color=#333399>&gt;</font>
</pre></blockquote>

beacause in first version <code>jnz</code> conditonal jump would be always
taken, because instruction is executed only when <code>je</code> wasn't taken.
If you dont understand read previous chapter.<br><br>

But using this way there are 7 unused bits, and that is waste of space. (Not
for single variable, but surely for array of such variables). It is clear we
can "pack" 8 boolean varaibles into single byte (8 bits). Problem is only
setting and reading it.<br><br>

First, we'll set all 8 bits (boolean variables) using <code>mov</code>
instruction.

<blockquote class="code"><pre>
mov <font color=#333399>[</font>eight_booleans<font color=#333399>],</font><font color=#339933>00000000b</font>
</pre></blockquote>

this would set all variables to zero (clear them). If we want set some of them
to one, we just set bits in which they are stored.

<blockquote class="code"><pre>
mov <font color=#333399>[</font>eight_booleans<font color=#333399>],</font><font color=#339933>00010100b</font>
</pre></blockquote>

this will set variables in bits 2 and 4, and leave all ather clear.<br><br>

First, how to clear one bit and leave all other unmodified? We solved this
before, we can do this with "and"ing:

<blockquote class="code"><pre>
and <font color=#333399>[</font>eight_booleans<font color=#333399>],</font><font color=#339933>11110111b</font>
</pre></blockquote>

This will clear bit 3 (<code>and</code>ed with 0 so result will be 0), and leave
all other bits unchanged (<code>and</code>ed with 1 so will stay unchanged).
This will clear bits 3 and 5:

<blockquote class="code"><pre>
and <font color=#333399>[</font>eight_booleans<font color=#333399>],</font><font color=#339933>11010111b</font>
</pre></blockquote>

it should be clear if you comprehended chapter 6.4.<br><br>

Now how to set one of variables:

<blockquote class="code"><pre>
or <font color=#333399>[</font>eight_booleans<font color=#333399>],</font><font color=#339933>00001000b</font>
</pre></blockquote>

This sets bit 3 to 1 (<code>or</code>ing with 1 always gives 1) and leave other
unchanged (<code>or</code>ing with 0 leaves unchanged).<br><br>

And, of course, using <code>xor</code> we can flip bit(s):

<blockquote class="code"><pre>
xor <font color=#333399>[</font>eight_booleans<font color=#333399>],</font><font color=#339933>00001000b</font>
</pre></blockquote>

will flip bit 3 and leave others.<br><br>

This was just to remind you, now let's go to checking value of bit. Checking
value of bit is called "testing bit".

<blockquote class="term">
term: <b>testing bit</b>
</blockquote>

You often need to test value of some boolean variable and do something (jump
somewhere) if it is (or isn't) TRUE.  We did this with byte-sized boolean
variable using <code>cmp</code> instruction, but it is impossible to use
<code>cmp</code> for testing only one bit of byte. For this reason, there is
<code>test</code> instruction. It takes same arguments like <code>mov</code>,
<code>xor</code>, <code>and</code>, <code>cmp</code> etc. It <code>and</code>s
it's operands and if result of <code>and</code>ing is 0 then it sets flags so
that if result is zero then <code>je</code> will be taken, if result isn't zero
<code>je</code> won't be taken (and <code>jnz</code> would).

<blockquote class="code"><pre>
test arg1<font color=#333399>,</font>arg2
</pre></blockquote>

acts a little like

<blockquote class="code"><pre>
and arg1<font color=#333399>,</font>arg2
cmp arg1<font color=#333399>,</font><font color=#339933>0</font>
</pre></blockquote>

but it doesn't modify <code>arg1</code> and you use <code>jz</code> (jump if zero) and
<code>jnz</code> (jump if not zero) conditional jumps. <code>jz</code> jumps if result
of virtual <code>and</code>ing (testing) is zero. Similary, <code>jnz</code>
jumps, if result is not zero (eg. at least one tested bit is non-zero)

<b>NOTE:</b> In fact, <code>jz</code> is same instruction as <code>je</code> and <code>jnz</code>
is same as <code>jne</code>, so using <code>jz</code> is same as using <code>je</code> would be in
and/cmp example

<blockquote class="term">
instruction: <b>test</b>
</blockquote>

Some example of using <code>test</code>

<blockquote class="code"><pre>
test <font color=#333399>[</font>eight_booleans<font color=#333399>],</font><font color=#339933>00001000b</font>
jz bit_3_is_clear
bit_3_is_set<font color=#333399>:</font>
<font color=#333399>&lt;</font>...<font color=#333399>&gt;</font>
bit_3_is_clear<font color=#333399>:</font>
<font color=#333399>&lt;</font>...<font color=#333399>&gt;</font>
</pre></blockquote>

all bits but third of <code>eight_booleans</code> are <code>and</code>ed with
0 (but <code>eight_booleans</code> stays unmodified), which means they are
cleared, only value of third will remain. Then result of operation will be zero
(and <code>je</code> will taken) only if third bit is 0. If it is 1, result of
operation will be 00001000b, not 0 so <code>je</code> won't be taken.<br><br>

Now some little more dificult example:

<blockquote class="code"><pre>
test <font color=#333399>[</font>eight_booleans<font color=#333399>],</font><font color=#339933>00101000b</font>
je bits_3_and_5_clear
bits_3_and_5_not_both_clear<font color=#333399>:</font>
<font color=#333399>&lt;</font>...<font color=#333399>&gt;</font>
bits_3_and_5_clear<font color=#333399>:</font>
<font color=#333399>&lt;</font>...<font color=#333399>&gt;</font>
</pre></blockquote>

bits 3 and 5 of <code>eight_booleans</code> will remain, so result of operation
will be 0 (and <code>je</code> will be taken) only when both these bits are 0.
If at least one of these bits is 1 result won't be 0 (it can be 00001000b or
00100000b or 00101000b) and <code>jz</code> won't be taken. But testing two bits
at one time usually isn't used, at least not by beginners, i gave this example
just for better picture how <code>test</code> works.

</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -