?? chap03.html
字號:
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css">
<title>03 TAJGA FASM Tutorial</title>
</head>
<body>
<center><b>CHAPTER 3 - Labels & Addresses & Variables</b></center><br><br>
Okay, let's get to variables. In previous chapter i wrote that variable is
general term for space which stores some value. Registers are variables for
example. But there is limited number of registers (VERY limited, some 8 + few
special), and this is nearly always not enough. For this reason memory (RAM -
random access memory) is used.<br><br>
<b>NOTE:</b> when someone says "variable" he almost always means memory
variable.
<br><br><br>
<b>3.1. Labels</b>
<br>
Problem is that you have to know WHERE in memory is some value stored.
Position in memory (called "address") is given by number. But it is quite hard
to remember this number (address) for every variable.
<blockquote class="term">
term: <b>address</b><br>
number which gives position in memory
</blockquote>
Another problem with addresses is that when you change your program, address
can be changed too, and so you would have to correct this number everywhere
where it is used. For this reason addresses are represented by "labels".
Label is just some word (not string, it is not enclosed in apostrophes),
which, in your program, represents address in memory. When you compile your
program, compiler will replace label with proper address. Label consists of
alphabet characters ("a" to "z", "A" to "Z") numbers ("0" to "9"), underscores
("_") and dots ("."). But first character of label can't be number or dot.
Label also can't have same name as directive or instruction (instruction
mnemonics). Labels are case sensitive in FASM ('a' is NOT same as 'A').
Example of labels:
<br><br>
<table align=center width=70% border=0 bgcolor=#7F91EC cellspacing=1 cellpadding=0>
<tr align=center bgcolor=#CBCFD8><td><code>name </code></td><td>is label</td></tr>
<tr align=center bgcolor=#CBCFD8><td><code>a </code></td><td>is label</td></tr>
<tr align=center bgcolor=#CBCFD8><td><code>A </code></td><td>is label, different from "a"</td></tr>
<tr align=center bgcolor=#CBCFD8><td><code>name2 </code></td><td>is label</td></tr>
<tr align=center bgcolor=#CBCFD8><td><code>name.NAME2 </code></td><td>is label</td></tr>
<tr align=center bgcolor=#CBCFD8><td><code>name._NAME2</code></td><td>is label</td></tr>
<tr align=center bgcolor=#CBCFD8><td><code>_name </code></td><td>is label</td></tr>
<tr align=center bgcolor=#CBCFD8><td><code>_ </code></td><td>is label</td></tr>
<tr align=center bgcolor=#CBCFD8><td><code>.name </code></td><td>is not label, because is starts with dot (labels starting with
dot have special meaning in FASM, which you will learn later)</td></tr>
<tr align=center bgcolor=#CBCFD8><td><code>1 </code></td><td>is not label because it starts with number</td></tr>
<tr align=center bgcolor=#CBCFD8><td><code>1st_name </code></td><td>is not label for same reason</td></tr>
<tr align=center bgcolor=#CBCFD8><td><code>name1 name2</code></td><td>is not label, because it contains space</td></tr>
<tr align=center bgcolor=#CBCFD8><td><code>mov </code></td><td>is not label, because "mov" is instruction mnemonics</td></tr>
</td></tr></table>
<br>
<blockquote class="term">
term: <b>label</b><br>
Placeholder for some address, eg. placeholder for some number, because address is number. In FASM you can
use label same way as any other number (not really, but it doesn't matter for you too much now).
</blockquote>
You can define label using directive "label". This directive should be
followed with label itself (label name). For example:
<br><br>
<table align=center width=70% border=0 bgcolor=#7F91EC cellspacing=1 cellpadding=0>
<tr align=center bgcolor=#CBCFD8><td width=120><code>label name</code></td><td>is label definition, it defines label "name" </td></tr>
<tr align=center bgcolor=#CBCFD8><td width=120><code>label _name</code></td><td>is label definition, it defines label "_name"</td></tr>
<tr align=center bgcolor=#CBCFD8><td width=120><code>label label</code></td><td>is not label definition, because "label" can't be name of label
as decribed in previous paragraph
</td></tr></table>
<br>
this will define label that will represent address of data defined behind it<br><br>
<blockquote class="term">
directive: <b>label</b>
</blockquote>
<blockquote class="term">
<b>label definition</b><br>
directive <b>label</b> followed by label name
</blockquote>
Shorter way to define label is just writing label name followed by colon
(<code>:</code>)
<blockquote class="code"><pre>
name<font color=#333399>:</font>
_name<font color=#333399>:</font>
</pre></blockquote>
but we won't use this way for some time.
<br><br><br>
<b>3.2. Variable definition</b>
<br>
Now how we can return to problem with variables: how to define variable in
memory. Program you create (compiled program, in machine code) is loaded to
memory at execution time, where processor executes it instruction by
instruction. Look at this program:
<blockquote class="code"><pre>
org <font color=#339933>256</font>
mov al<font color=#333399>,</font><font color=#339933>10</font>
db <font color=#bb0000>'this is a string'</font>
int <font color=#339933>20h</font>
</pre></blockquote>
This program will probably crash, because after processor executes <code>mov
al,10</code> then it reaches string. But in program there is no difference between
string and instructions in machine code. Both are translated into array of numeric
values (bytes). There is no way processor can differ whether numeric value is
translation of string or translation of instruction. In this example,
processor will execute instructions whose numeric representation (in machine
code) is same as ASCII representation of string "this is a string".
Now look at this:
<blockquote class="code"><pre>
org <font color=#339933>256</font>
mov al<font color=#333399>,</font><font color=#339933>10</font>
int <font color=#339933>20h</font>
db <font color=#bb0000>'this is a string'</font>
</pre></blockquote>
This program will not crash, because before reaching bytes defined by string
processor reaches instruction <code>int 20h</code>, which ends execution of program. So
bytes defined with string will not be executed, it will just take some space.
This is way how you can define variable - define some data at place where
processor won't try to execute it (behind <code>int 20h</code> in this case).<br><br>
So code with byte-sized variable of value 105
<blockquote class="code"><pre>
org <font color=#339933>256</font>
mov al<font color=#333399>,</font><font color=#339933>10</font>
int <font color=#339933>20h</font>
db <font color=#339933>105</font>
</pre></blockquote>
Last line defines byte variable containing 105.<br><br>
Now how to access variable? First we must know address of variable. For this
we can use label (described above, reread it if you have forgotten)
<blockquote class="code"><pre>
org <font color=#339933>256</font>
mov al<font color=#333399>,</font><font color=#339933>10</font>
int <font color=#339933>20h</font>
label my_first_variable
db <font color=#339933>105</font>
</pre></blockquote>
So we already know address of variable. It is represented by label
<code>my_first_variable</code>. Now how to access it? You may think it is, for example
<blockquote class="code"><pre>
mov al<font color=#333399>,</font>my_first_variable
</pre></blockquote>
but no! Remember i told that label (<code>my_first_variable</code> in this
case) stands for address of variable. So this instruction will move address of
variable to <code>al</code> register, not variable's contents. To access
contents of variable (or contents of any memory location) you must enclose it's
address in brackets (<code>[</code> and <code>]</code>). So to access contents
of our variable, and copy it's value to <code>al</code> we use
<blockquote class="code"><pre>
mov al<font color=#333399>,[</font>my_first_variable<font color=#333399>]</font>
</pre></blockquote>
Now we will define two variables:
<blockquote class="code"><pre>
org <font color=#339933>256</font>
<font color=#333399><</font>some instructions<font color=#333399>></font>
int <font color=#339933>20h</font>
label variable1
db <font color=#339933>100</font>
label variable2
db <font color=#339933>200</font>
</pre></blockquote>
So to copy value of <code>variable1</code> to <code>al</code> we use
<blockquote class="code"><pre>
mov al<font color=#333399>,[</font>variable1<font color=#333399>]</font>
</pre></blockquote>
To copy <code>al</code> to <code>variable1</code> use
<blockquote class="code"><pre>
mov <font color=#333399>[</font>variable1<font color=#333399>],</font>al
</pre></blockquote>
To set value of <code>variable1</code> (exact: to set value of variable which is stored
at address represented by <code>variable1</code>) to 10 we could try
<blockquote class="code"><pre>
mov <font color=#333399>[</font>variable1<font color=#333399>],</font><font color=#339933>10</font>
</pre></blockquote>
but this will cause error (try it if you want). Problem is that you know that
you are changing variable at address <code>variable1</code> to <code>10</code>.
But what is size of variable? In previous two cases byte-size could be
determined because you used <code>al</code> register which is byte sized, so
compiler decided that variable at <code>variable1</code> is byte sized too,
because you can't move between operands with different sizes. But in this case,
value 10 can be of any size, so it can't decide size of memory variable. To
solve this we use "size operators". We will talk about two size operators for
now: <code>byte</code> and <code>word</code>. You can put size operator before
instruction operand when accessing it to let compiler know what the variable
size is:
<blockquote class="code"><pre>
mov byte <font color=#333399>[</font>variable1<font color=#333399>],</font><font color=#339933>10</font>
</pre></blockquote>
Another way to make this is
<blockquote class="code"><pre>
mov <font color=#333399>[</font>variable1<font color=#333399>],</font> byte <font color=#339933>10</font>
</pre></blockquote>
in this case compiler knows that moved value 10 is byte sized so it decides
that variable is byte-sized too (because we can move byte sized value only to
byte sized variable).<br><br>
But it would be hard to always remember and always write size of variable when
you access it. For this reason you can assign size of variable to label when
you define it. Just write size operator behind label name in definition:
<blockquote class="code"><pre>
label variable1 byte
db <font color=#339933>100</font>
</pre></blockquote>
or
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -