📄 chap07.html
字号:
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<title>07 TAJGA FASM Tutorial</title>
</head>
<body>
<center><b>CHAPTER 7 - Arithmetic instructions, more on flags</b></center><br><br>
In this chapter you will learn how to perform basic math operations in
assembly language. Then you will get deeper into how processor does it and
thus learn more on flags.
<br><br><br>
<a name="1"></a>
<b>7.1. Addition and substraction</b><br>
Simplest case of addition is addition of one, called "incrementing". For
example, if we increment variable holding value 5, it will contain 6 etc.
<br><br>
Instruction which performs incrementing is <code>inc</code> (obvious why). It
has one operand, which tells what should be incremented (eg. to what will 1 be
added). Operand can be register or memory variable. It can't be constant, of
course, because such instruction, even if it would exist, wouldn't have any
effect. Example of incrementing:
<blockquote class="code"><pre>
mov ax<font color=#333399>,</font><font color=#339933>5</font>
inc ax <font color=#777777>;increment (add 1 to) value in ax</font>
<font color=#777777>;here ax holds value 6</font>
</pre></blockquote>
<br>
I think it is clear (it isn't, you'll see later why).
<br><br>
Substracting of value 1 is called "decrementing". Decrementing is opposite of incrementing. Instruction which
performs decrementing is <code>dec</code>. Example:
<blockquote class="code"><pre>
mov ax<font color=#333399>,</font><font color=#339933>5</font>
inc ax <font color=#777777>;increment (add 1 to) value in ax</font>
<font color=#777777>;here ax holds value 6</font>
dec ax <font color=#777777>;decrement (substract 1 from) value in ax</font>
<font color=#777777>;here ax holds value 5 again</font>
</pre></blockquote>
<blockquote class="term">
terms: <b>incrementing</b> (adding 1), <b>decrementing</b> (substracting 0)<br>
instructions: <b>inc</b>, <b>dec</b>
</blockquote>
If you wan't to add or substract more than 1, you can use more
<code>inc</code>s or <code>dec</code>s, but that is quite ugly way, requires
more typing, and code is big and slow. So there is instruction which can add
any value, this instruction is <code>add</code>. It takes two arguments, first
one is destination eg. value to which will be added, and second is value to be
added. Argument types are same as for <code>mov</code>, first can be register
or memory variable, second can be constant, register or memory variable (if
first one isn't memory variable, remember - one instruction can't access two
memory locations). Example:
<blockquote class="code"><pre>
mov ax<font color=#333399>,</font><font color=#339933>5</font>
add ax<font color=#333399>,</font><font color=#339933>5</font>
<font color=#777777>;here ax contains 10</font>
</pre></blockquote>
<br>
Another example:
<blockquote class="code"><pre>
mov ax<font color=#333399>,</font><font color=#339933>5</font>
mov bx<font color=#333399>,</font><font color=#339933>5</font>
add bx<font color=#333399>,[</font>five<font color=#333399>]</font>
add ax<font color=#333399>,</font>bx
<font color=#777777>;here ax contains 15, bx contains 10</font>
five dw <font color=#339933>5</font>
</pre></blockquote>
<br>
Instruction for substracting is <code>sub</code>. It is exact opposite of
<code>add</code>, everything is same for it as for <code>add</code>.
<blockquote class="code"><pre>
mov ax<font color=#333399>,</font><font color=#339933>15</font>
mov bx<font color=#333399>,</font><font color=#339933>10</font>
sub bx<font color=#333399>,[</font>five<font color=#333399>]</font>
sub ax<font color=#333399>,</font>bx
<font color=#777777>;here ax contains 10, bx contains 5</font>
five dw <font color=#339933>5</font>
</pre></blockquote>
<br><br><br>
<a name="2"></a>
<b>7.2. Overflows</b>
<br>
There are some cases with addition and substraction that I haven't already
mentioned. For example if you try to add 10 to byte sized variable containing
250 (biggest number byte sized variable can hold is 255). In such case, we say
that <code>overflow</code> has occured.
<br><br>
But question is, what happens with result of operation that has overflown. When
upper limit of variable is crossed, then result of operation will be rest of
value to be added. We can say the operation will be "wrapped" from max value to
minimal value. For example: <br>
byte 255 + 1 = 0<br>
byte 255 + 2 = 1<br>
byte 254 + 3 = 1<br>
byte 250 + 10 = 5<br>
byte 255 + 255 = 254<br>
word 65535 + 1 = 0<br>
word 65535 + 65535 = 65534<br>
etc.
<br><br>
There is also other case, when result of operation falls below lower limit
(which is 0 for any size of variable). In this case result of operation will be
wrapped from lower limit to upper limit. This case is called
<code>underflow</code>. For example:<br>
byte 0 - 1 = 255<br>
byte 0 - 255 = 1<br>
byte 254 - 254 = 0<br>
byte 254 - 255 = 255<br>
etc.
<br><br>
<b>NOTE:</b> Word <code>oveflow</code> is usually used for both
<code>overflow</code> and <code>underflow</code>.
<blockquote class=term>
Terms: <b>Overflow</b>, <b>Underflow</b>
</blockquote>
We also need to know how to check if overflow has occured after performing
operation to prevent bugs. For this, flags are used. I mentioned flags in
<a href=chap05.html#3>chapter 5.3</a>. We used flags for checking results of
comparison at conditional jumps, and I also said that there shouldn't be any
instrcutions between comparison and jumps, because many instructions change
flags (of course you can place instruction there if you are sure it won't
change any needed flag). Arithmetic instructions <code>add</code> and
<code>sub</code> use one bit of flags called CF (carry flag). If overflow
occurs, then they set it to 1, otherwise they set it to 0. You can test carry
flag with conditional jumps <code>jc</code> and <code>jnc</code> (see
<a href=chap05.html#3>chapter 5.3</a> about conditional jumps). <code>jc</code> jumps
if carry flag is set, <code>jnc</code> jumps if carry flag is not set.
<br><br>
Here is example of testing overflows:
<blockquote class="code"><pre>
add ax<font color=#333399>,</font>bx
jc overflow
no_overflow<font color=#333399>:</font>
sub cx<font color=#333399>,</font>dx
jc underflow
no_underflow<font color=#333399>:</font>
</pre></blockquote>
<blockquote class="term">
<b>carry flag (CF)</b> - ont bit (flag) of "flags" register<br>
conditional jump instructions: <b>jc</b>, <b>jnc</b>
</blockquote>
<br><br><br>
<a name="3"></a>
<b>7.3. Zero Flag</b>
<br>
Instructions <code>inc</code> and <code>dec</code> doesn't set CF, so you
can't test overflow using CF with them. But there is another rule that can be
used to prevent overflow with <code>inc</code> and <code>dec</code>. This rule
is that when result of operation is zero, then flag called "zero flag" (ZF) is
set. This flag is tested with <code>jz</code> (jump if zero flag is set) and
<code>jnz</code> (jump if zero flag is clear) conditional jump instructions.
<br><br>
With this you can create loops, eg. repeat some part of code several times.<br>
For example code
<blockquote class="code"><pre>
org <font color=#339933>256</font>
mov cx<font color=#333399>,</font><font color=#339933>5</font>
here<font color=#333399>:</font>
mov dl<font color=#333399>,<font color=#bb0000>'a'</font></font>
mov ah<font color=#333399>,</font><font color=#339933>2</font>
int <font color=#339933>21h</font>
dec cx
jnz here
int <font color=#339933>20</font>h
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -