?? cintro.doc
字號:
An
Introduction
to
CCC
CC CC
CC CC
CC
CC CC
CC CC
CCC
Using
The MICRO-C Compiler
Revised: 27-Mar-90
Copyright 1989,1990 Dave Dunfield
All rights reserved
Intro to MICRO-C Page: 1
1. INTRODUCTION
Since releasing my MICRO-C compiler in 1988, I have received many
requests to include information on the 'C' language as part of that
package.
This manual is intended as a companion to the MICRO-C compiler,
and presents an introduction to the 'C' language for those who are
not already familiar with it. The language represented is that
portion of 'C' which is implemented by the MICRO-C compiler. Since
MICRO-C implements a subset of the 'C' programming language as
described by Kernighan and Ritchie (the original developers of the
language), you should have little difficulty using and learning a
full 'C' compiler once you have mastered it.
You should also refer to the MICRO-C technical manual entitled
"MICRO-C a compact 'C' compiler for small systems" for details on the
MICRO-C implementation.
'C' has many inter-relationships between its various constructs
and features. I have attempted to introduce them in a logical and
"building" manner, however it is not always possible to fully
describe each feature before it is mentioned in the description of
some other construct. For this reason, I suggest that you read this
text "lightly from cover to cover" at least once before you try to
fully understand each point.
This is a first draft of this document. In its present form, it is
not very easy reading, but does contain much useful information. I
will be improving and adding to it as I find the time.
Presented herein is a brief summary of the major features of the
'C' language as implemented in MICRO-C.
Intro to MICRO-C Page: 2
2. BACKGROUND INFORMATION
This section provides some detailed background information for the
novice, and may be skipped if you are already familier with the
basics of computer architecture and programming languages.
2.1 Computer Architecture
The basis of any computer system is its Central Processor Unit
(CPU) which controls the operation of all other parts of the
computer, by following a set of "instructions" which make up a
software "program". The program is stored in "memory" and directs
the CPU to read and write data to various "peripheral" devices
(Such as terminals, disks and printers), and to manipulate that
data in a matter which accomplishes the goals set out by the
author of the program.
Although there are a wide variety of CPUs available in modern
computers, they are all very similar, and feature the following
characteristics:
All data accessed by the CPU is represented by circuits which
may be either OFF or ON. This is represented by the digits 0 and
1. Since there are only two states (0 and 1), the computer may be
thought of as operating in a BASE 2 (Binary) number system, and
each individual data element is called a Binary digIT, or BIT.
This BASE 2 number system is used because it is much easier to
build and interface to electrical circuits which have only two
states (OFF and ON) than ones which have many states.
Since single BITs cannot represent much information,
manipulating large amounts of data at the BIT level would be a
very tedious chore. For this reason, modern CPUs access data as
groups of BITs. Usually the smallest group of data which can be
manipulated by a computer consists of 8 BITs, and is called a
BinarY TErm (BYTE).
Very small computers can often only access data a BYTE (8 bits)
at a time, while larger machines may be able to access and
manipulate data in larger groups called WORDS. The size of the
data group usually manipulated by a CPU is called its WORDSIZE,
and is expressed in a number of bits. This is almost always a
multiple of 8 bits, resulting in an even number of bytes. Thus, if
you hear a CPU or computer called a "16 bit" machine, you know
that it can access and manipulate 16 bits (2 bytes) of data at a
time. A "32 bit" machine would operate on 32 bits (4 bytes) of
data. In general, the larger the WORDSIZE of a CPU, the more data
it manipulates at one time, resulting in faster completion of a
given task.
The CPU has access to external "memory", which consists of many
thousands (and often millions) of WORDS of data. Up to one
complete word of data may be transferred between the CPU and
memory in one memory access.
Intro to MICRO-C Page: 3
Often it is known that each data element stored in memory will
not take up an entire word, and is it desirable to access memory
in smaller groups, to reduce the number of memory words required
to accomplish a particular task. For this reason, most modern CPUs
can access any single BYTE (8 bits) from a memory word. It should
be understood however, that accessing a single byte causes a full
memory access, and takes just as much time as accessing an entire
word.
In order to provide the programmer with a simple method of
specifying memory locations, each BYTE of memory is assigned an
ADDRESS, which is simply the number of BYTES from the beginning of
memory to the desired byte. Therefore, the first byte in memory
has address 0, the second byte byte in memory has address 1, the
third byte has address 2 etc. Thus, memory from the viewpoint of
the programmer may be considered as a simple array of BYTES,
beginning with an address of 0, and continuing with sequential
addresses up the memory size (in bytes) of the computer.
In addition to the external memory, a CPU has a small amount of
very fast internal memory which is organized into words called
"registers". Registers act as holding places to store the data
words which are to be manipulated. At least some of the registers
are internally connected to an Arithmetic Logic Unit (ALU), which
has the necessary electronics to perform basic operations such as
addition and subtraction on the data in those registers. The
result of the operation is also placed in a register, often one of
the registers containing the original data.
One special register called the Program Counter (PC) is used by
the CPU to follow the software program. It contains the address of
the next INSTRUCTION to be executed. At the beginning of an
INSTRUCTION CYCLE, the CPU reads the word of memory at the address
which is contained in the PC, and interprets the value contained
there as an operation to be performed, such as loading a register
from memory, storing a register into memory, or performing an
arithmetic operation on the registers. After performing that
operation, the CPU advances the PC to the next memory word before
beginning another instruction cycle. In this manner, all of the
instructions in a program are read, and the programmed operations
are carried out.
The CPU may also read from and increment the PC during the
execution of an instruction, in order to access data bytes which
are OPERANDS to the particular instruction being executed. Such
would be the case if you were instructing the CPU to load a
register with the contents of a particular memory address. The
data bytes following the instruction would contain the memory
address to be used.
Intro to MICRO-C Page: 4
There are a few instructions which direct the CPU to store a
new value in the PC, rather than advance it. These are called
JUMPS, and are used to cause the program to begin executing
instuctions at a different address. This can be used to create
LOOPS in the program where a sequence of instructions is executed
over and over again.
Some of the "jump" instructions will only store the new value
in the PC if certain conditions are met, such as the last ALU
operation resulted in zero, or did not result in zero. This allows
the program to alter its pattern of execution based on data
values.
For example, here is a program to count to 10 on a very simple
imaginary computer. It shows the use of IMMEDIATE operands to
instructions, which are shown by [PC+], and indicate that during
the execution of the instruction, the CPU reads the next value
from the address in the PC. The PC is advanced so that that value
will not be executed as another instruction when the first
instruction is finished:
Address Interpretation of instruction value
------------------------------------------------------
[0] Load [PC+] data byte into register1
[1] Data: 0
[2] Add [PC+] data byte to register1
[3] Data: 1
[4] Load [PC+] data byte into register2
[5] Data: 10
[6] Subtract register1 from register2
[7] Jump if result not zero to address in [PC+]
[8] Data: 2
[9] Halt CPU
Intro to MICRO-C Page: 5
2.2 Assembly Language
In the preceeding section, you have learned how a CPU executes
a program, and how a program may be coded in memory as a series of
instruction and data values. It should be obvious to you that
although you can create programs in this way, it would be a long
and tedious job to write a program of any size using only numeric
values.
Not only is it very hard to remember the hundreds of
instruction values which may be used to perform certain
operations, but managing the memory address which are coded as
operands to the instructions becomes a real headache. This is
particularily true when you change a portion of the program,
causing a change in the number of bytes of memory used by that
portion, and therefore changes all of the memory address of
instructions and data which follow.
To help ease the programming job, each of the CPU manufacturers
have defined an ASSEMBLY LANGUAGE for their CPU, which represents
each of the machine operations with a more meaningful name called
a NMEMONIC. Similar instructions may be grouped under the same
neumonic with the individual instruction values determined by the
operands. For example, it might be a completely different
instruction value which loads Register1 with a value than that
which loads register2. In assembly language you would use similar
statements such as:
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -