China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-24 11:25
中国DOS联盟论坛 » 网络日志(Blog) » 1. Expansion of articles in DOS learning for easy reading. (20160203) View 51,768 Replies 75
Original Poster Posted 2016-02-02 23:09 ·  中国 海南 移动
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
Contents:
1 How was the first C language compiler written? 2nd floor
2 Let's write a simple interpreter together 3rd floor
3 Teach you step by step to make a C language compiler 4th floor
4 Learning low - level programming: write a C language compiler by hand 5th floor
5 Homemade compiler: parser 6th floor
6 Homemade compiler: lexer 7th floor
7 cucu: an easy - to - understand compiler 8th floor
8 kickout reposts Li Jiafang's "Hard Disk Partition Table Detailed Explanation" 9th floor (within the forum)
9 Incredible Physics - Dialogue with Michio Kaku 10th floor
10 LOLI Hell Nicholas' Will 11th floor
11 "About Time" 12th floor
12 Classic Software under Linux (the most comprehensive in history) 13th floor

-----------------------------------------------------------------------------------------Page 1-----------------------------------------------------------------------------------------Page 1

If you enter hell, please don't panic, because where you are, where will become heaven.
Fear is a path to try not to be afraid, and courage and curiosity are always on the way to accompany.
(The article all belongs to the original author, not personal original, of course, the individual has no original, all are reprinted)
------------------------------------------------------------------------------------------------------------
Product collection:
Invention of football kinetic energy power generation for energy - insufficient areas
http://business.sohu.com/20160722/n460421178.shtml
Conceptual human - powered pedal power generation exercise

The British company Billions in Change launched a fitness power generation bike
http://tech.163.com/15/1219/15/BB754ST500094OE0.html

[ Last edited by zzz19760225 on 2018 - 1 - 15 at 11:46 ]
Floor 2 Posted 2016-02-02 23:13 ·  中国 海南 移动
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
How was the first C language compiler written?

First, pay tribute to Dennis Ritchie, the father of the C language!

Almost all practical compilers/interpreters (collectively referred to as compilers) today are written in the C language. There are some languages such as Clojure, Jython, etc. that are based on the JVM or implemented in Java, and IronPython, etc. that are based on.NET. But Java and C# themselves also rely on C/C++ to be implemented, which is indirectly calling C. So measuring the portability of a certain high-level language is actually discussing the portability of ANSI/ISO C.

The C language is a very low-level language, and in many aspects it is similar to assembly language. In the book "Intel 32-bit Assembly Language Programming", it even introduces the method of manually translating simple C language into assembly. For system software like compilers, it is very natural to write them in the C language. Even a high-level language like Python still relies on the C language at the bottom (the example of Python is because Intel hackers are trying to make Python run without an operating system - actually, to remove the one-time C code on the BIOS). Now, students who have studied compiler principles and have some programming ability can implement a simple C-like language compiler.

But here's the problem: Have you ever thought about how the first C language compiler was written when everyone uses the C language or languages based on the C language to write compilers? This is not a "chicken and egg" problem...

Let's go back to the history of the C language: In 1970, Tomphson and Ritchie developed the B language based on BCPL (an interpreted language). In 1973, they successfully developed the current C language based on the B language. Before the C language was used as a system programming language, Tomphson also used the B language to write an operating system. It can be seen that before the C language was implemented, the B language could already be put into practical use. Therefore, the prototype of the first C language compiler could completely be written in the B language or a mixture of the B language and PDP assembly language. We now know that the efficiency of the B language is relatively low, but if it is all written in assembly language, not only is the development cycle long and the maintenance difficulty high, but more importantly, the portability necessary for a high-level programming language is lost. So the early C language compiler took a clever approach: first, use assembly language to write a compiler for a subset of the C language, and then use this subset to recursively complete the complete C language compiler. The detailed process is as follows:

First, create a subset that only has the most basic functions of the C language, denoted as the C0 language. The C0 language is simple enough that the compiler of the C0 can be directly written in assembly language. Relying on the existing functions of the C0, design a subset of the C language, C1, which is more complex than the C0 but still incomplete. The C0 belongs to the C1, and the C1 belongs to the C. Use the C0 to develop the compiler of the C1 language. On the basis of the C1, design another subset of the C language, the C2 language. The C2 language is more complex than the C1 but still not the complete C language. Develop the compiler of the C2 language... until CN. The CN is powerful enough, and then it is enough to develop the implementation of the complete C language compiler. As for how much N is here, it depends on the complexity of your target language (here is the C language) and the programming ability of the programmer - simply put, if at a certain subset stage, it is very convenient to use the existing functions to implement the C language, then you find N. The following figure illustrates this abstract process:

C language
CN language
……
C0 language
Assembly language
Machine language

Does this figure seem familiar? Yes, it's what we saw when talking about virtual machines. But here it's the CVM (C Language Virtual Machine). Each language can be independently implemented for compilation at each virtual layer, and except for the C language, the output of each layer will be used as the input of the next layer (the output of the last layer is the application program). This is like rolling a snowball. Using hands (assembly language) to combine a small handful of snow together and rolling it little by little forms a big snowball. This is probably what is meant by 0 gives birth to 1, 1 gives birth to C, and C gives birth to everything!

So how is this bold subset simplification method implemented and what is the theoretical basis? First, introduce a concept, "self-compile" (Self-Compile), that is, for some strongly typed programming languages with obvious self-bootstrapping properties (where each variable in the program must be declared a type before it can be used, such as the C language. Conversely, some scripting languages have no concept of type at all), they can rely on a limited small subset to realize the expression of themselves through a limited number of recursions. Such languages include C, Pascal, Ada, etc. As for why they can be self-compiled, please refer to "Compiler Principles" published by Tsinghua University Press, which implements a compiler for a subset of Pascal. In short, CS scientists have proved that the C language can theoretically implement a complete compiler through the above-mentioned CVM method. So how is it actually simplified?

The following are the keywords of C99:

auto enum restrict unsigned
break extern return void
case float short volatile
char for signed while
const goto sizeof _Bool
continue if static _Complex
default inline struct _Imaginary
do int switch
double long typedef
else register union
//Total 37

Take a close look, in fact, many of these keywords are to help the compiler with optimization, and some are used to limit the scope, linkage, or lifetime of variables and functions (functions don't have it). These don't need to be added in the early stage of compiler implementation. So we can remove auto, restrict, extern, volatile, const, sizeof, static, inline, register, typedef. Thus, a subset of C, the C3 language, is formed. The keywords of the C3 language are as follows:

enum unsigned
break return void
case float short
char for signed while
goto _Bool
continue if _Complex
default struct _Imaginary
do int switch
double long
else union
//Total 27

Think again, and find that there are many types and type modifiers in the C3 that don't need to be added all at once. For example, the three integer types, just implement int. Therefore, further remove these key words. They are: unsigned, float, short, char (char is int), signed, _Bool, _Complex, _Imaginary, long. Thus, the C2 language we get is formed. The keywords of the C2 language are as follows:

enum
break return void
case
for while
goto
continue if
default struct
do int switch
double
else union
//Total 18

Continuing to think, even the C2 language with 18 keywords still has many advanced places, such as composite data structures based on basic data types. Also, the operators are not written in our keyword list. The compound assignment operators -> operators, ++, -- and other too flexible expressions in the C language can also be completely removed at this time. Therefore, the keywords that can be removed are: enum, struct, union. Thus, we can get the keywords of the C1 language:

break return void
case
for while
goto
continue if
default
do int switch
double
else
//Total 15

It's almost perfect, but the last step has to be bold. At this time, arrays and pointers also need to be removed. Also, the C1 language still has a lot of redundancy. For example, there are multiple expressions for controlling loops and branches, which can actually be simplified to one kind. Specifically, there are while loops, do...while loops, and for loops. Only the while loop needs to be retained; there are four forms of branch statements: if...{}, if...{}...else, if...{}...else if..., switch. They can all be implemented through two or more if...{}, so only if,...{} needs to be retained. But then again, the so-called branches and loops are just conditional jump statements, and the function call statement is just a push stack and jump statement. Therefore, only goto (unrestricted goto) is needed. Therefore, boldly remove all structured keywords, and there are no functions either. The keywords of the obtained C0 language are as follows:

break void
goto
int
double
//Total 5

This is the extreme of simplicity.

With only 5 keywords, it can be quickly implemented in assembly language. Through reverse analysis, we have restored the writing process of the first C language compiler, and also felt the wisdom and diligence of the前辈 scientists! We are all just dust on the shoulders of giants! 0 gives birth to 1, 1 gives birth to C, and C gives birth to everything. It's really clever!
Floor 3 Posted 2016-02-02 23:14 ·  中国 海南 移动
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
Floor 4 Posted 2016-02-02 23:17 ·  中国 海南 移动
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
Hand-in-Hand Guide to Making a C Compiler (9): Summary

Congratulations on completing your own C language compiler. In this chapter, we will vent a little and talk about some issues worth noting when writing a compiler; some difficulties encountered when writing a compiler.

This series:
Hand-in-Hand Guide to Making a C Compiler (0): Preface
Hand-in-Hand Guide to Making a C Compiler (1): Design
Hand-in-Hand Guide to Making a C Compiler (2): Virtual Machine
Hand-in-Hand Guide to Making a C Compiler (3): Lexical Analyzer
Hand-in-Hand Guide to Making a C Compiler (4): Recursive Descent
Hand-in-Hand Guide to Making a C Compiler (5): Variable Definition
Hand-in-Hand Guide to Making a C Compiler (6): Function Definition
Hand-in-Hand Guide to Making a C Compiler (7): Statement
Hand-in-Hand Guide to Making a C Compiler (8): Expression

Virtual Machine and Target Code
At the very beginning of the entire series, we started to implement the virtual machine. I don't know if you have the same feeling, but this part is actually very important for the entire compilation. I think it accounts for at least 50% of the importance.

Here, I want to explain such a view. When learning compiler principles, people often focus on lexical analysis and syntax analysis, but ignore code generation which is also important. For learning or exams, maybe it's okay, but when actually compiling a project, the most important thing is to "run it", so we need to give high attention to code generation.

At the same time, we also saw that in the later stage of parsing statements and expressions, the difficulty is no longer in syntax analysis, but in how to generate corresponding assembly code for operators.

Lexical Analysis
We wrote our lexical analyzer in a very violent way, and I think it's not bad.

But you can still learn relevant knowledge, understand the principle of automatically generating a lexical analyzer, which involves knowledge such as "regular expressions", "state machines" and so on. I believe that this knowledge can greatly improve your programming level.

At the same time, if you still want to write a compiler in the future, you might as well try these automatic generation tools.

Syntax Analysis
For a long time, syntax analysis has been a mystery to me until I really implemented one using the recursive descent method.

We spent a special chapter explaining the relationship between "recursive descent" and BNF grammar. I hope it can reduce your aversion to theory. At least, it's not too difficult to implement.

If you are interested, you can learn about these grammars, because there are many automatic generation tools that support them. You can take a look at tools like yacc, and a more advanced version is bison. There are also many similar supports in other languages.

Side Note: Recently, I learned about a representation method called "PEG grammar". Whether it's reading or implementing, it's easier than BNF. You can also learn about it.

About Coding
This is also my own feeling. No matter how good the tutorial is, the best way to fully understand it is probably to implement it yourself.

However, in the process of writing code, we will encounter many setbacks, such as having to consider many details or having great difficulty in debugging. But only by really calming down to overcome it can we grow.

For example, when writing the parsing of expressions, a large number of repetitive codes are particularly frustrating. Also, debugging a compiler is simply extremely painful.

P.S. If you write code according to this series, remember to write some functions for outputting assembly code in advance, which is very helpful.

Also, when writing the articles for this series, after the initial impulse passed, I was very upset when writing each article. I hope the articles themselves are not affected by my such emotions.

Conclusion
Programming is interesting and boring, only we who are in it can understand it.
Floor 5 Posted 2016-02-02 23:33 ·  中国 海南 移动
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
Learning Low-Level Programming: Writing a C Compiler by Hand

2013/07/19 · C/C++, Development · 1 Comment · C Language, Compiler

Share to:

This article is translated by ProgrammerBole Online - Newbie Floats Out of Water. Reproduction without permission is prohibited!

English original: Wilfred Hughes. Welcome to join the translation group.

Writing a compiler by hand to learn a more low-level programming approach is a very effective way to learn how computers actually work.

Compilers are usually regarded as extremely complex projects. In fact, writing a production-level compiler is indeed a huge task. But writing a small and usable compiler is not that difficult.

The secret is to first find the smallest usable project and then add the features you want. This method is also the approach mentioned in Abdulaziz Ghuloum's famous paper "A Shortcut to Constructing a Compiler". But this method does work. You just need to follow the first step in this paper to get a truly usable compiler! Of course, it can only compile a very small subset of the programming language, but it is indeed a real and usable compiler. You can freely expand this compiler and learn more profound knowledge from it.

Inspired by this article, I wrote a C compiler. In a sense, this is more difficult than writing a Scheme compiler (because you have to parse C's complex grammar), but it is convenient in some ways (you don't need to deal with runtime types). To write such a compiler, you just need to start from your available smallest compiler.

For the compiler I wrote, I call it babyc, and I chose this code as the first program I need to run:

int main() {
return 2;
}
There are no variables, no function calls, no additional dependencies, not even if statements, loop statements, everything looks so simple.

First, we need to parse this code. We will use Flex and Bison to do this. Here are examples of how to use them. Fortunately, our grammar is so simple. The following is the lexer:

"{" { return '{'; }
"}" { return '}'; }
"(" { return '('; }
")" { return ')'; }
";" { return ';'; }
[0-9]+ { return NUMBER; }
"return" { return RETURN; }
"int" { return TYPE; }
"main" { return IDENTIFIER; }
Here is the parser:

function:
TYPE IDENTIFIER '(' ')' '{' expression '}'
;

expression:
RETURN NUMBER ';'
;
Finally, we need to generate some assembly code. We will use 32-bit X86 assembly because it is very common and can run easily on your machine. Here are relevant websites for X86 assembly.

The following is the assembly code we need to generate:

.text
.global _start # Tell the loader we want to start at _start.

_start:
movl $2,%ebx # The argument to our system call.
movl $1,%eax # The system call number of sys_exit is 1.
int $0x80 # Send an interrupt
Then add the above lexer and parser code and write this assembly code into a file. Congratulations! You are already a compiler writer!

Babyc is born like this, and you can see what it looked like at the very beginning here.

Of course, it is in vain if the assembly code cannot run. Let's use the compiler to generate the real assembly code we want.

# Here's the file we want to compile.
$ cat return_two.c
#include <stdio.h>

int main() {
return 2;
}

# Run the compiler with this file.
$ ./babyc return_two.c
Written out.s.

# Check the output looks sensible.
$ cat out.s
.text
.global _start

_start:
movl $2, %ebx
movl $1, %eax
int $0x80
Great! Then let's really run the compiled code to make sure it gets the result we want.

# Assemble the file. We explicitly assemble as 32-bit
# to avoid confusion on x86_64 machines.
$ as out.s -o out.o --32

# Link the file, again specifying 32-bit.
$ ld -m elf_i386 -s -o out out.o

# Run it!
$ ./out

# What was the return code?
$ echo $?
2 # Woohoo!
We have taken the first step, and what to do next is up to you. You can follow all the instructions in that article and make a more complex compiler. You need to write a more delicate syntax tree to generate assembly code. The next steps are: (1) Allow returning any value (for example, return 3; some executable code); (2) Add support for "not" (for example, return ~1; some executable code). Each additional feature can teach you more about C language, how the compiler executes, and how other people writing compilers in the world think.

This is the way to build babyc. Babyc now has if statements, loops, variables, and the most basic data structures. You are welcome to check out its code, but I hope you can write one by yourself after reading my article.

Don't be afraid of some low-level things. This is a very wonderful world.
Floor 6 Posted 2016-02-03 01:07 ·  中国 海南 移动
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
Homemade Compiler: Syntax Analyzer (Part 1)
2013-04-29 22:45 3502 views Comments (0) Favorites Report
Classification: Homemade Compiler (7)
Copyright Notice: This article is an original article by the blogger. Reproduction is not permitted without the blogger's permission.

I feel that the syntax analyzer is a relatively large part in the front end of the compiler, so I plan to describe it in two blog posts. The first post focuses on describing the ideas, and the second post specifically discusses the implementation.

1. What the syntax analyzer does
When writing anything, you must first understand what this thing does, what input it accepts, and what output it produces.
A syntax analyzer accepts the lexemes generated by the lexer as input, generates an abstract syntax tree for the intermediate code generator, and then the intermediate code generator generates intermediate code and hands it over to the back end of the compiler. Of course, in some understandings, the abstract syntax tree can be regarded as a representation form of intermediate code and directly handed over to the back end. Anyway, in short, the syntax analyzer is something that generates an abstract syntax tree.
It is worth noting that the syntax analyzer not only generates an abstract syntax tree, but also finds various syntax errors and generates and maintains the symbol table during this generation process.

2. Symbol table
What is a symbol table? What is the use of a symbol table?
The so-called symbol table is a table that records various identifiers (that is, terminal symbols id, lexeme id) and their attributes. For example, record that the type of an int variable x is int, its scope, record a function name, record its function type, parameter list, etc.
The symbol table has a scope. For example, a simple piece of code:
view plain copy
public void function()
{
int i=0;
while(true)
{
int i=1;
}
}
Therefore, the construction of a symbol table must be a tree structure. We use the following structure to describe a symbol table in the compiler:
view plain copy
package ravaComplier.symTable;
import java.util.*;
public class SymTable {
private SymTable fatherSymTable;
private ArrayList<SymTable> blockSymTables;
private HashMap<String,Symbol> table;
private String blockid;
public SymTable(SymTable st,String str)
{
fatherSymTable=st;
blockid=str;
blockSymTables=new ArrayList<SymTable>();
table=new HashMap<String,Symbol>();
}
public void addSym(Symbol sym)
{
table.put(sym.id, sym);
}
public Symbol getSym(String id)
{
Symbol result=table.get(id);
if(result==null && fatherSymTable!=null)
{
return fatherSymTable.getSym(id);
}
return result;
}
public void addSymTable(SymTable st)
{
blockSymTables.add(st);
}
}
The code is so simple that there is no need to write comments.
The fatherSymTable is used to record the parent table of this symbol table, which is used for continuous upward backtracking to find symbols (getSym) for use.
blockid is used to give this table an id, which is used when printing debugging information.
addSym adds a symbol to this table. In addition, there is also an addSymTables to add child tables.
In addition, this class also overrides the toString() method for debug information. Due to space limitations, this method is not posted in the blog post, but the complete source file can be obtained in the resources I uploaded.
Maybe in the subsequent analysis description and writing code process, I will find that I need to add new functions to this class, and then supplement this class at that time.
Next, let's take a look at the simple Symbol class, which is the class representing a symbol:
view plain copy
package ravaComplier.symTable;

public class Symbol {
public String id;
public int type;
public Object value;
public Symbol(String i,int t,Object v)
{
id=i;
type=t;
value=v;
}
public static int TYPE_CLASSNAME=0;
public static int TYPE_MEMBERVAR=1;
public static int TYPE_LOCALVAR=2;
public static int TYPE_FUNCNAME=3;
public static int TYPE_CONSFUNC=4;
}
There are 3 fields: id, that is, the identifier; type, the enumerated values are listed; value, which defines different values according to different enumerated values. The code will be posted later if used.
There are a total of 5 types of symbols: class name, member variable, local variable, function name, and constructor name.
Of course, if new symbols are needed later, they can be flexibly added.

3. Representation of the syntax tree
A syntax tree cannot use a normal tree structure because the behavior and values of each different node are too many and different. The nodes in the syntax tree are non-terminal symbols or terminal symbols. For the id in it, we just let it point to the symbol in the symbol table. For non-terminal symbols, we create a new class for each non-terminal symbol to describe its behavior. For terminal symbols other than id, their information is either not recorded (such as meaningless commas, parentheses, etc.) or simply record their type (such as various operators).
Therefore, the establishment of each node is relatively flexible in this case. The following are two examples:
For the production rule: ops --> bitop | logiop | artmop | cprop
We create the following class to describe ops:
view plain copy
package ravaComplier.syntax.nodes;

public class ops {
private int type;
private Object value; //must be bitop,logiop,artmop,cprop
public ops()
{
//not implements
}
public static int TYPE_BITOP=0;
public static int TYPE_LOGIOP=1;
public static int TYPE_ARTMOP=2;
public static int TYPE_CPROP=3;
}
The int describes the operator type, and then the value is the specific operator class according to the corresponding type. Then give the class of cprop:
view plain copy
package ravaComplier.syntax.nodes;

public class cprop {
public cprop()
{
//not implemets
}
private int type;
public static int TYPE_GREATER = 0;//>
public static int TYPE_GREATEREQUAL=1;//>=;
public static int TYPE_LESS=2;//<;
public static int TYPE_LESSEQUEAL=3;//<=;
public static int TYPE_EQUAL=4;//==
}
This is a terminal symbol, so there is only one field to record the type of the operator.
This article ends here, and the next article will focus on analyzing the expansion process of the syntax tree.

Homemade Compiler: Syntax Analyzer (Part 2)
2013-05-16 23:13 5183 views Comments (1) Favorites Report
Classification: Homemade Compiler (7)
Copyright Notice: This article is an original article by the blogger. Reproduction is not permitted without the blogger's permission.
This blog post took a long time to write. The first reason is that the syntax analyzer itself is really more difficult than I expected, so it was refactored many times before it was completed. The second reason is that I got an Xbox to play with, which took up part of the spare time = =.
This blog post will be divided into the following parts to describe the specific implementation of the syntax analyzer, theory, some typical nodes and results.

1. Syntax-directed translation, LL and LL(X) grammar, left recursion and other things
Why write a syntax analyzer? The role of the syntax analyzer is not only to check whether the sequence in the lexeme list is a statement of our language, but more importantly, only the abstract syntax tree obtained with the help of the syntax analyzer can generate intermediate code or specific target code. This process is called syntax-directed translation (syntax-directed translation). On the cover of the Purple Dragon Book (Second Edition of Compiler Principles), a knight with a shield is dueling with a spitting dragon. The dragon has "Complexity of Compiler Design" written on it, and the knight's shield has "Syntax Directed Translation" written on it. Therefore, it is not an exaggeration to regard syntax-directed translation as the core of the front end of the compiler.

The process of expanding the syntax tree is essentially the process of continuously corresponding lexemes to the recursive formulas defined by our language. In other words, it is actually the process of continuously expanding the recursive formulas of the language to conform to the existing lexemes. This expansion process can be divided into two methods from the method: LL and LR. The first letter represents reading the lexeme sequence from left to right, and the second letter L represents trying to expand the leftmost non-terminal symbol first, and R represents trying to reduce lexemes from the right to non-terminal symbols. In other words, LL is a top-down expansion method, and LR is a bottom-up reduction method. The technology used in this article is LL, so the following also focuses on discussing LL.
In order to make the compiler efficient and fast, a good grammar design must be an LL(1) grammar. What is an LL(1) grammar? For example, when we face the following derivation:
ids-> id| ----------1
id.ids| -----------2
ids | -----------3
this
At this time, we read a lexeme id, which one to expand into 1, 2, or 3? Of course, we cannot judge at present, so we need to read the next lexeme to make a judgment. If


















view plain copy
package ravaComplier.syntax.nodes;

import ravaComplier.lexer.Lexeme;
import ravaComplier.symTable.SymTable;
import ravaComplier.symTable.Symbol;
import ravaComplier.syntax.GlobalVars;
import ravaComplier.syntax.SyntaxTreeGenerator;

/*
* This class tries to read lexemes and generate id nodes
*/
public class id {
public SymTable curST;//The symbol table of this node
public id() throws Exception
{

Lexeme lex=SyntaxTreeGenerator.readLexeme();//Read a lexeme
curST=SyntaxTreeGenerator.getCurTable();//Get the current symbol table
if(lex.type==Lexeme.ID)
{
//The type is correct
symEntry=SyntaxTreeGenerator.getCurTable().getSym(lex.value.toString());//Judge whether this id already exists in the symbol table
if(symEntry==null)
{
firstappear=true;

}
else
{
firstappear=false;
}
value=lex.value.toString();

symEntry=new Symbol(value,2,null);//Generate an entry
}
else
{
//Type error throws an exception
throw new Exception("ID required!\r\n");

}
GlobalVars.idlist.add(this);//Add all ids to the idlist.
}
public String value;
public boolean firstappear;
public type tp;//Type, assigned by the caller
public Symbol symEntry;//Point to the entry of the symbol table
}
This class represents an id node. First, it tries to read a lexeme. If it is not an id, a syntax error occurs. Secondly, it needs to judge whether this id is appearing for the first time. In some cases, this information is very important (such as when a variable is defined). Finally, the initialized id is added to the list in GlobalVars. It is worth noting that there are many lists in GlobalVars, which are mainly used for some checks after the syntax tree is generated.
(2) vardeclare
Let's take a more complicated one, the definition of a local variable
view plain copy
package ravaComplier.syntax.nodes;

import java.util.ArrayList;

import ravaComplier.lexer.Lexeme;
import ravaComplier.symTable.SymTable;
import ravaComplier.symTable.Symbol;
import ravaComplier.syntax.SyntaxTreeGenerator;

public class vardeclare {
/*
* var-declare --> type args|type args
*
*/
public SymTable curST;
public vardeclare() throws Exception
{
curST=SyntaxTreeGenerator.getCurTable();
tp=new type();
int pos=SyntaxTreeGenerator.savePos();//Get the current analysis position
Lexeme lex=SyntaxTreeGenerator.readLexeme();//Read the next lexeme
arrayDeclare=false;
if(!lex.value.equals("






"))
{
SyntaxTreeGenerator.loadPos(pos);
throw new Exception("] expected!");//Syntax error, the brackets are not closed when defining an array.
}
else
{
arrayDeclare=true;

}
}
ags=new args();
ArrayList<ids> al=ags.getidsList();//Get the parameter list. If args is a1,a2,a3, the returned list contains a1,a2,a3
SymTable st=SyntaxTreeGenerator.getCurTable();
for(int i=0;i<=al.size()-1;i++)
{
id ID=al.get(i).getLastID();//If id1.id2.id3, this function returns id3.
if(ID.firstappear==false)
{
throw new Exception("id declared duplicated!");//The defined variable name has appeared before, report an error
}
st.addSym(ID.symEntry);//Add id to the symbol table
ID.symEntry.value=ID;
ID.symEntry.type=Symbol.TYPE_LOCALVAR;
ID.tp=tp;//Assign a type to this id
if(arrayDeclare)
{
ID.tp.isArray=true;
}
}
}
public type tp;
public args ags;
public boolean arrayDeclare;
}
It can be seen that many attributes in the id node are determined by its caller, which is particularly evident in the writing of node logic.

(3) memberfundeclare
Take a more complicated one, the definition of a member function:
view plain copy
package ravaComplier.syntax.nodes;

import ravaComplier.lexer.Lexeme;
import ravaComplier.symTable.SymTable;
import ravaComplier.symTable.Symbol;
import ravaComplier.syntax.SyntaxTreeGenerator;

public class memberfuncdeclare {
public SymTable curST;
public memberfuncdeclare() throws Exception
{
/*member-func-declare --> private|public
NUL|static
type func-name( NUL|def-args ) { func-body }*/
curST=SyntaxTreeGenerator.getCurTable();
af=new accessflag();//Get an accessflag, that is, public or private
//Try to read static, if not, backtrack.
int pos=SyntaxTreeGenerator.savePos();
Lexeme lex=SyntaxTreeGenerator.readLexeme();
if(lex.type!=Lexeme.STATIC)
{
SyntaxTreeGenerator.loadPos(pos);
}
else
{

isstatic=true;
}
tp=new type();//Get type
fc=new funcname();//Get the function name.
fc.id.symEntry.value=this;
if(fc.id.firstappear==false)
{
//Judge whether the function name is duplicated, if duplicated, report an error.
throw new Exception("function name must be unique!");
}
SymTable st=SyntaxTreeGenerator.getCurTable();
st.addSym(fc.id.symEntry);//Add this function to the symbol table
fc.id.symEntry.type=Symbol.TYPE_FUNCNAME;
SymTable st1=new SymTable(st,fc.id.value+" symtable");//Establish a child table, each function has its own symbol table because the scope of variables inside is different from that outside
SyntaxTreeGenerator.setCurTable(st1);//Set the child table as the current symbol table, and all subsequent analysis in the function body uses this table
lex=SyntaxTreeGenerator.readLexeme();
if(!lex.value.toString().equals("("))
{
throw new Exception("( expected!");//Syntax check
}
try
{
pos=SyntaxTreeGenerator.savePos();
da=new defargs();//Try to search for the subsequent calling parameters, if there are no parameters, roll back according to the position stored in the previous line

}
catch(Exception e)
{
SyntaxTreeGenerator.loadPos(pos);
da=null;
}
lex=SyntaxTreeGenerator.readLexeme();
if(!lex.value.toString().equals(")"))
{
throw new Exception(") expected!");//Syntax check
}
lex=SyntaxTreeGenerator.readLexeme();
if(!lex.value.toString().equals("{"))
{
throw new Exception("{ expected!");//Syntax check
}
fb=new funcbody();//Construct the function body
lex=SyntaxTreeGenerator.readLexeme();
if(!lex.value.toString().equals("}"))
{
throw new Exception("} expected!");//Syntax check
}
SyntaxTreeGenerator.setCurTable(st);//Function ends, reset the symbol table
}
public accessflag af;
public type tp;
public boolean isstatic;
public funcname fc;
public defargs da;
public funcbody fb;
}


Through the above analysis, we can summarize the construction rules of each node:
1. Try to expand this node in a certain order
2. Each part of it is used as a member variable of this node
3. Interact with the symbol table appropriately during expansion

According to a similar idea, when we complete all nodes, the front end of the compiler is almost done. The following is the syntax tree obtained from the sample program in the log in the previous blog post. It can be seen that even a simple sample program has a quite complex syntax tree.

[ Last edited by zzz19760225 on 2016-2-3 at 01:15 ]
Floor 7 Posted 2016-02-03 01:22 ·  中国 海南 移动
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
Homemade Compiler: Lexical Analyzer
2013-04-21 23:03 1573 views Comment(1) Collect Report
Classification: Homemade Compiler (7)
Copyright Notice: This article is an original article by the blogger. Reproduction is not permitted without the blogger's permission.

The lexical analyzer code has been uploaded to personal resources.
When our program source file enters the compiler, the first thing encountered is the lexical analyzer.
The role of the lexical analyzer is to parse the source file, analyze the lexemes in it, and input the sequence set of these lexemes to the syntax analyzer.
Continuing from the previous article, list the so-called lexemes, which are terminal symbols:
if else while ( ) { } cpreop bitop logiop armtcop number literal id NUL new [ ] basetype class private public static return break continue . this
Among them, cprop includes > < >= <= == !=, that is, comparison operators
bitop is a bit operator, including << >> & | ^
logiop is a logical operator, including && ||
armtcop is an arithmetic operator, including + - * /
number is a numeric constant, such as 12345 integer or 1.2345 decimal
id is an identifier, following Java rules
literal is a string constant, such as "ROgerwong"
NUL is an empty string
basetype is a basic type, including int char double three types
Of course, for simplicity, the theory of non-deterministic finite automata and deterministic finite automata and their conversion algorithms are not intended to be discussed here. Just use the most simple method, continuously read characters into the buffer, then compare with these lexemes, and then add this lexeme to an ArrayList.
Define several data structures according to this method:
Define the lexeme data structure, which contains two fields, one representing the type and one representing the specific value. The values of the type are also marked.
[java] view plain copy
<p>package ravaComplier.lexer;</p><p>public class Lexeme {
public int type;
public Object value;
public Lexeme(int t,Object v)
{
type=t;
value=v;
}
@Override
public String toString()
{
return new String("<"+type+":"+value.toString()+">");
}

public static int IF=0;//if
public static int ELSE=1;//else
public static int WHILE=2;//while
public static int BRACKET=3;//various parentheses
public static int CPREOP=4;//comparison symbols
public static int BITOP=5;//bit operators
public static int LOGIOP=6;//logical operators
public static int ARMTOP=7;//arithmetic operators
public static int NUMBER=8;//immediate number
public static int LITERAL=9;//string
public static int ID=10;//id
public static int NUL=11;//empty
public static int NEW=12;//new operator
public static int BASETYPE=13;//basic data type
public static int CLASS=14;//keyword class
public static int ACCESSFLAG=15;//public or private
public static int STATIC=16;//keyword static
public static int RETURN=17;//keyword return
public static int BREAK=18;//break
public static int CONTINUE=19;//continue
public static int DOT=20;//.
public static int THIS=21;//keyword this
public static int SEMI=22;//semicolon
public static int EQUAL=23;//equal sign
}
</p>
Secondly, because it is a simple stupid method, we need to construct rules:
Define separators: spaces, tabs, newlines, +, -, *, /, ., ;, various parentheses operators, etc.
If a separator is encountered, the buffer in front of the separator is a lexeme, and the separator is a lexeme (except spaces, tabs, newlines).
But note the special case. If > and >=, & and &&, etc. are encountered, you need to look one character forward to determine the lexeme.
Then instantiate the separated lexemes into Lexeme type and add them to the return result.
The code is simple, but it is cumbersome to write:
[java] view plain copy

[java] view plain copy
package ravaComplier.lexer;

import java.io.*;
import java.util.*;

public class Lexer {
private static ArrayList<Lexeme> result;//return result
private static BufferedReader br;
private static StringBuffer buffer;//buffer

public static ArrayList<Lexeme> getLexerOutput(InputStream is)
{
result=new ArrayList<Lexeme>();
br=new BufferedReader(new InputStreamReader(is));
buffer=new StringBuffer();
while(Read())
{
addLexeme();
}
return result;
}
//Try to decompose the buffer into lexemes and add them to the lexeme collection
private static void addLexeme()
{
String str=buffer.toString();
String endstr=str.substring(str.length()-1,str.length());
//Judge single-character delimiters
if(endstr.equals(" ") || endstr.equals("\t") || endstr.equals(";") || endstr.equals("{") || endstr.equals("}") || endstr.equals("(") || endstr.equals(")") || endstr.equals("[") || endstr.equals("]") || endstr.equals("+") || endstr.equals("-") || endstr.equals("*") || endstr.equals("/") )
{
Lexeme lex=getLexeme(str.substring(0,str.length()-1));
if(lex!=null)
{
result.add(lex);
}
lex=getLexeme(endstr);
if(lex!=null)
{
result.add(lex);
}

buffer=new StringBuffer();
}
//Judge double-character delimiters
if(str.length()>=2)
{
endstr=str.substring(str.length()-2,str.length());
if(endstr.equals(">=") ||endstr.equals("<=") ||endstr.equals("==") || endstr.equals("||") ||endstr.equals("&&") || endstr.equals("!=") ||endstr.equals("\r\n"))
{
Lexeme lex=getLexeme(str.substring(0,str.length()-2));
if(lex!=null)
{
result.add(lex);
}
lex=getLexeme(endstr);
if(lex!=null)
{
result.add(lex);
}

buffer=new StringBuffer();
}
else if(endstr.charAt(0)=='=' || endstr.charAt(0)=='>' || endstr.charAt(0)=='<' || endstr.charAt(0)=='&' || endstr.charAt(0)=='|' )
{
Lexeme lex=getLexeme(str.substring(0,str.length()-2));
if(lex!=null)
{
result.add(lex);
}
lex=getLexeme(endstr.substring(0,1));
if(lex!=null)
{
result.add(lex);
}

buffer=new StringBuffer();
buffer.append(endstr.charAt(1));
}
}
}
//Get lexeme according to a string
private static Lexeme getLexeme(String lex)
{
Lexeme result=null;
if(lex.equals(" ") || lex.equals("\t") || lex.equals("\r\n") || lex==null|| lex.length()==0)
{
return null;
}
if(lex.equals("if"))
{
result=new Lexeme(Lexeme.IF,lex);
}
else if(lex.equals("else"))
{
result=new Lexeme(Lexeme.ELSE,lex);
}
else if(lex.equals("while"))
{
result=new Lexeme(Lexeme.WHILE,lex);
}
else if(lex.equals("{") || lex.equals("}")|| lex.equals("[") || lex.equals("]") || lex.equals("(") || lex.equals(")"))
{
result=new Lexeme(Lexeme.BRACKET,lex);
}
else if(lex.equals(">") || lex.equals("<") || lex.equals("==") || lex.equals(">=") || lex.equals("<=") || lex.equals("!="))
{
result=new Lexeme(Lexeme.CPREOP,lex);
}
else if(lex.equals("&") || lex.equals("|") || lex.equals("^"))
{
result=new Lexeme(Lexeme.BITOP,lex);
}
else if(lex.equals("&&") || lex.equals("||"))
{
result=new Lexeme(Lexeme.LOGIOP,lex);
}
else if(lex.equals("+") || lex.equals("-") || lex.equals("*") || lex.equals("/"))
{
result=new Lexeme(Lexeme.ARMTOP,lex);
}
else if(isNumber(lex))
{
result=new Lexeme(Lexeme.NUMBER,lex);
}
else if(isStr(lex))
{
result=new Lexeme(Lexeme.LITERAL,lex);
}
else if(lex.equals("new"))
{
result=new Lexeme(Lexeme.NEW,lex);
}
else if(lex.equals("int") || lex.equals("char") || lex.equals("double"))
{
result=new Lexeme(Lexeme.BASETYPE,lex);
}
else if(lex.equals("class"))
{
result=new Lexeme(Lexeme.CLASS,lex);
}
else if(lex.equals("private") || lex.equals("public"))
{
result=new Lexeme(Lexeme.ACCESSFLAG,lex);
}
else if(lex.equals("static"))
{
result=new Lexeme(Lexeme.STATIC,lex);
}
else if(lex.equals("return"))
{
result=new Lexeme(Lexeme.RETURN,lex);
}
else if(lex.equals("break"))
{
result=new Lexeme(Lexeme.BREAK,lex);
}
else if(lex.equals("continue"))
{
result=new Lexeme(Lexeme.CONTINUE,lex);
}
else if(lex.equals("."))
{
result=new Lexeme(Lexeme.DOT,lex);
}
else if(lex.equals("this"))
{
result=new Lexeme(Lexeme.THIS,lex);
}
else if(lex.equals(";"))
{
result=new Lexeme(Lexeme.SEMI,lex);
}
else if(lex.equals("="))
{
result=new Lexeme(Lexeme.EQUAL,lex);
}
else
{
result=new Lexeme(Lexeme.ID,lex);
}
return result;
}
private static boolean isStr(String lex)
{
if(lex.charAt(0)!='\"' || lex.charAt(lex.length()-1)!='\"')
return false;
for(int i=1;i<=lex.length()-2;i++)
{
if(lex.charAt(i)=='\"')
{
return false;
}
}
return true;
}
private static boolean isNumber(String str)
{
try
{
int i=Integer.valueOf(str);
return true;
}
catch(Exception e)
{}
try
{
double j=Double.valueOf(str);
return true;
}
catch(Exception e)
{}
return false;
}
//Read a character from the stream
private static boolean Read()
{
int d;
try {
d = br.read();
if(d==-1)
{
return false;
}
buffer.append((char)d);
} catch (IOException e) {
e.printStackTrace();
return false;
}


return true;
}
}

Then write a program by yourself to test whether it can be parsed correctly:
[java] view plain copy
class testclass{
private static int j=0;
public int i=1;
public testclass()
{
double c=1;
char[] d="123456";

}
private static double func1()
{
if(j==0)
{
return 1.5
}
else
{
while(i<=10)
{
i=i+1;
}
return i;
}
}
}

Then take a look at the output result:
[java] view plain copy
<14:class>
<10:testclass>
<3:{>
<15:private>
<16:static>
<13:int>
<10:j>
<23:=>
<8:0>
<22:;>
<15:public>
<13:int>
<10:i>
<23:=>
<8:1>
<22:;>
<15:public>
<10:testclass>
<3:(>
<3:)>
<3:{>
<13:double>
<10:c>
<23:=>
<8:1>
<22:;>
<13:char>
<3:[>
<3:]>
<10:d>
<23:=>
<9:"123456">
<22:;>
<3:}>
<15:private>
<16:static>
<13:double>
<10:func1>
<3:(>
<3:)>
<3:{>
<0:if>
<3:(>
<10:j>
<4:==>
<8:0>
<3:)>
<3:{>
<17:return>
<8:1.5>
<3:}>
<1:else>
<3:{>
<2:while>
<3:(>
<10:i>
<4:<=>
<8:10>
<3:)>
<3:{>
<10:i>
<23:=>
<10:i>
<7:+>
<8:1>
<22:;>
<3:}>
<17:return>
<10:i>
<22:;>
<3:}>
<3:}>
<3:}>

Seems relatively correct
Floor 8 Posted 2016-02-03 01:37 ·  中国 海南 移动
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
### cucu: a compiler you can understand (part 1)
Tags: compiler Compiler
2013-01-13 20:33 1317 views 0 comments Favorite Report
Classification: Compiler and Principles (2)
Table of contents (?)
Translator's Preface:
Recently, I was learning some basic knowledge about compilers, so I found this English blog. I searched on CSDN and it seemed that no one had translated it, so I simply translated it. Anyway, it's all for learning.
Original address: http://zserge.com/blog/cucu-part1.html
cucu: A compiler that you can understand (part 1)
Let's talk about compilers. Have you ever thought about writing a compiler by yourself?
I will show you how simple it is! But the first part of this blog is a bit theoretical, so I hope you can be patient.
Our goal
CUCU is a "toy" compiler for compiling a "toy" language. I hope this toy language can be as much like standard C language as possible, so a correct CUCU program can also be compiled with a C compiler. Of course, the entire C language standard is very complex, and the syntax used in our CUCU here is only a small part of the C language.
For example, here is a valid CUCU program fragment:
int cucu_strlen(char *s) {
int i = 0;
while (s) {
i = i + 1;
}
return i;
}
Grammar
Next, we need to define the grammar of our programming language. This is an important step because the design of our compiler will rely on this grammar.
Let's design the grammar from top to bottom. Our source file contains a program. What is a program? According to experience, we can know that a program is a list including variable declarations, function declarations, function definitions, etc. For example:
int func(char *s, int len); /* function declaration */
int i; /* variable declaration */

int func(char *s, int len) { /* function definition */
...
}
Let's try to write it in EBNF form (it doesn't matter if you don't know what EBNF is, it looks intuitive):
(Translator: For detailed information about EBNF, please refer to http://zh.wikipedia.org/wiki/%E6%89%A9%E5%B1%95%E5%B7%B4%E7%A7%91%E6%96%AF%E8%8C%83%E5%BC%8F)
<program> ::= { <var-decl> | <func-decl> | <func-def> } ;
This notation indicates that a function is a repeated sequence, and each item in this sequence is a variable declaration, function declaration, or function definition. Then, what are these so-called declarations and definitions? Let's continue.
<var-decl> ::= <type> <ident> ";"
<func-decl> ::= <type> <ident> "(" <func-args> ")" ";"
<func-def> ::= <type> <ident> "(" <func-args> ")" <func-body>
<func-args> ::= { <type> <ident> "," }
<type> ::= "int" | "char *"
Therefore, variable declaration is very simple: a type name plus an identifier, and then followed by a semicolon, just like we often use in C language.
int i;
char *s;
Function declaration is a bit more complicated. First, it is "type + identifier", and then <func-args> can be optionally added in parentheses.
The parameter list of a function is a sequence of "type + identifier" separated by commas, for example:
char *s, int from, int to
In fact, the trailing comma in the parameter list is allowed in the CUCU language, but it is not necessary. The reason for doing this is to make our code analysis simpler.
The types supported by the language are only int and char*, and the identifier is a sequence of letters, numbers, or underscores.
The only thing not explained is <func-body>. But first, we need to discuss statements and expressions.
A statement is the smallest independent element in our language. The following are some valid statements:
/* These are some simple statements */
i = 2 + 3; /* Assignment statement */
my_func(i); /* Function call statement */
return i; /* Return statement */

/* These are some compound statements */
if (x > 0) { .. } else { .. }
while (x > 0) { .. }
An expression is part of a statement and is smaller than a statement. Different from a statement, an expression always returns a value. Usually, an expression will be an arithmetic operation. For example, in the statement func(x, i + j), the expressions are x and i + j.
Therefore, based on the above analysis, we have:
<func-body> ::= <statement>
<statement> ::= "{" { <statement> } "}" /* Statement block */
| <ident> ";" /* Assignment */
| "return" <expr> ";"
| "if" "(" <expr> ")" <statement>
| "while" "(" <expr> ")" <statement>
| <expr> ";"
The following are some feasible expressions in the CUCU language:
<expr> ::= <bitwise-expr>
| <bitwise-expr> = <expr>
<bitwise-expr> ::= <eq-expr>
| <bitwise-expr> & <eq-expr>
| <bitwise-expr> | <eq-expr>
<eq-expr> ::= <rel-expr>
| <eq-expr> == <rel-expr>
| <eq-expr> != <rel-expr>
<rel-expr> ::= <shift-expr>
| <rel-expr> < <shift-expr>
<shift-expr> ::= <add-expr>
| <shift-expr> << <add-expr>
| <shift-expr> >> <add-expr>
<add-expr> ::= <postfix-expr>
| <add-expr> + <postfix-expr>
| <add-expr> - <postfix-expr>
<postfix-expr> ::= <prim-expr>
| <postfix-expr>
| <postfix-expr> ( <expr> { "," <expr> } )
<prim-expr> := <number> | <ident> | <string> | "(" <expr> ")"
Notice the recursively defined expressions? In addition, these expressions also explain the precedence of operators, with the precedence decreasing from bottom to top: parentheses and brackets have higher precedence, and assignment has lower precedence.
For example, according to the grammar definition, the operation order of the expression 8 >> 1 + 1 will be 8 >> (1 + 1)), not (like in (8 >> 1) + 1), because the precedence of >> is lower than +.
Lexical analyzer
When we have solved the grammar problem, we can almost start. The first thing is to make a lexical analyzer. Our compiler uses a byte stream as input, and the role of the lexical analyzer is to split this byte stream into smaller tokens for subsequent processing. The lexical analyzer provides a certain degree of abstraction to simplify the subsequent parser.
For example, a byte sequence "int i = 2+31;" will be divided into the following tokens:
int
i
=
2
+
31
;
In a normal lexical analyzer, a lexeme is a binary tuple consisting of type and value. Therefore, relative to the above list, we more expect to get a binary tuple <TYPE:int>, <ID:i>, <ASSIGN:=>, <NUM:2>, <PLUS:+>, <NUM:31>, <SEMI:;>. For simplicity, we are currently inferring the type from the value, which is very informal.
The main problem of the lexical analyzer is that once a byte is read from the stream, it can never be put back into the stream. Therefore, if we read a byte and this byte cannot be added to the current token, what should we do? Where should we store this byte and wait to process this byte after the current token is processed?
In fact, almost any lexical parser has a pre-read mechanism. Our grammar is simple, so we only need a byte - nextc as a buffer. It stores a byte read from the stream but not yet added to the current token.
In addition, I must remind here - I used many global variables in the lexical analyzer of the CUCU code. I know this is a bad habit, but if I put all variables as function parameters, the code of this compiler will not look so concise.
The entire lexical parser is just a function readtok(). And its algorithm is also very simple:
Skip all leading spaces
Try to read an identifier (a sequence of letters, numbers, and underscores)
If it is found not to be an identifier, try to read some operators, such as &, |, <, >, =, !.
If it is not an operator, try to read a string literal, such as "...." or '....'
If it still fails, maybe it is a comment, such as /* ... */
If it continues to fail, try to read a byte, maybe a parenthesis-like character, such as "(" or "







;
static int tokpos;
static int nextc;

void readchr() {
if (tokpos == MAXTOKSZ - 1) {
tok = '\0';
fprintf(stderr, "token too long: %s\n", tok);
exit(EXIT_FAILURE);
}
tok = nextc;
nextc = fgetc(f);
}

void readtok() {
for (;;) {
while (isspace(nextc)) {
nextc = fgetc(f);
}
tokpos = 0;
while(isalnum(nextc) || nextc == '_') {
readchr();
}
if (tokpos == 0) {
while (nextc == '<' || nextc == '=' || nextc == '>'
|| nextc == '!' || nextc == '&' || nextc == '|') {
readchr();
}
}
if (tokpos == 0) {
if (nextc == '\'' || nextc == '"') {
char c = nextc;
readchr();
while (nextc != c) {
readchr();
}
readchr();
} else if (nextc == '/') {
readchr();
if (nextc == '*') {
nextc = fgetc(f);
while (nextc != '/') {
while (nextc != '*') {
nextc = fgetc(f);
}
nextc = fgetc(f);
}
nextc = fgetc(f);
}
} else if (nextc != EOF) {
readchr();
}
}
break;
}
tok = '\0';
}

int main() {
f = stdin;
nextc = fgetc(f);

for (;;) {
readtok();
printf("TOKEN: %s\n", tok);
if (tok == '\0') break;
}
return 0;
}
If we take a C language source file as the input of this lexical analyzer, it will output a list of tokens, each token on a separate line.
Okay, let's take a short break and then move on to part 2.

### cucu: a compiler u can understand (part 2)
2013-01-14 22:08 748 views 0 comments Favorite Report
Classification: Compiler and Principles (2)
Table of contents (?)
Original address: http://zserge.com/blog/cucu-part2.html
So far, we have defined the grammar of our language and written a lexical analyzer. In this article, we will write a parser for our language. But before we start, we need some helper functions first:
int peek(char *s) {
return (strcmp(tok, s) == 0);
}

int accept(char *s) {
if (peek(s)) {
readtok();
return 1;
}
return 0;
}

int expect(char *s) {
if (accept(s) == 0) {
error("Error: expected '%s'\n", s);
}
}
The peek() function returns a non-zero value if the next token is equal to the passed string. The accept() function reads the next token. If it is the same as the passed parameter, it returns 1; otherwise, it returns 0. The expect() helps us check the grammar of the language.
The more difficult part
From the grammar of the language, we can know that statements and expressions are intermingled. Therefore, this means that once we start writing the parser, we must always keep these recursive generation rules in mind. Let's analyze from top to bottom. The following is the top-level function compiler():
static int typename();
static void statement();

static void compile() {
while (tok != 0) { /* until EOF */
if (typename() == 0) {
error("Error: type name expected\n");
}
DEBUG("identifier: %s\n", tok);
readtok();
if (accept(";")) {
DEBUG("variable definition\n");
continue;
}
expect("(");
int argc = 0;
for (;;) {
argc++;
typename();
DEBUG("function argument: %s\n", tok);
readtok();
if (peek(")")) {
break;
}
expect(",");
}
expect(")");
if (accept(";") == 0) {
DEBUG("function body\n");
statement();
}
}
}
This function first tries to read the type name, followed by the identifier. If a semicolon follows, it means it is a variable declaration. If followed by parentheses, it means it is a function. If it is a function, it then searches for parameters one by one. After that, if there is no semicolon, it means it is a function definition (with a function body); otherwise, it is just a function declaration (only the function name and type).
Here, typename() is a function used to skip the type name. We only accept the int type, char type, and its pointer (char*):
static int typename() {
if (peek("int") || peek("char")) {
readtok();
while (accept("*"));
return 1;
}
return 0;
}
The most interesting part is probably the statement() function. It can analyze a single statement, and this statement can be a block, a local variable definition/declaration, a return statement, etc.
Now let's take a look at what it looks like:
static void statement() {
if (accept("{")) {
while (accept("}") == 0) {
statement();
}
} else if (typename()) {
DEBUG("local variable: %s\n", tok);
readtok();
if (accept("=")) {
expr();
DEBUG(" :=\n");
}
expect(";");
} else if (accept("if")) {
/* TODO */
} else if (accept("while")) {
/* TODO */
} else if (accept("return")) {
if (peek(";") == 0) {
expr();
}
expect(";");
DEBUG("RET\n");
} else {
expr();
expect(";");
}
}
If what is encountered is a "block", that is, the part of {...}, it continues to try to parse statements in the block until the block ends. If it starts with a variable name, it means it is a local variable definition. The conditional statement (if/then/else) and loop statement are not listed here, leaving it to the reader to think about how to implement these parts according to our grammar.
Of course, most statements contain expressions, so we need to write a function to analyze expressions. The expression parser is a bottom-up recursive parser, so many expression parsing functions will call each other until the main expression is found. The so-called main expression, according to our grammar, refers to a number (constant) or an identifier (variable or function).
static void prim_expr() {
if (isdigit(tok)) {
DEBUG(" const-%s ", tok);
} else if (isalpha(tok)) {
DEBUG(" var-%s ", tok);
} else if (accept("(")) {
expr();
expect(")");
} else {
error("Unexpected primary expression: %s\n", tok);
}
readtok();
}

static void postfix_expr() {
prim_expr();
if (accept("

");
DEBUG(" ");
} else if (accept("(")) {
if (accept(")") == 0) {
expr();
DEBUG(" FUNC-ARG\n");
while (accept(",")) {
expr();
DEBUG(" FUNC-ARG\n");
}
expect(")");
}
DEBUG(" FUNC-CALL\n");
}
}

static void add_expr() {
postfix_expr();
while (peek("+") || peek("-")) {
if (accept("+")) {
postfix_expr();
DEBUG(" + ");
} else if (accept("-")) {
postfix_expr();
DEBUG(" - ");
}
}
}

static void shift_expr() {
add_expr();
while (peek("<<") || peek(">>")) {
if (accept("<<")) {
add_expr();
DEBUG(" << ");
} else if (accept(">>")) {
add_expr();
DEBUG(" >> ");
}
}
}

static void rel_expr() {
shift_expr();
while (peek("<")) {
if (accept("<")) {
shift_expr();
DEBUG(" < ");
}
}
}

static void eq_expr() {
rel_expr();
while (peek("==") || peek("!=")) {
if (accept("==")) {
rel_expr();
DEBUG(" == ");
} else if (accept("!=")) {
rel_expr();
DEBUG("!=");
}
}
}

static void bitwise_expr() {
eq_expr();
while (peek("|") || peek("&")) {
if (accept("|")) {
eq_expr();
DEBUG(" OR ");
} else if (accept("&")) {
eq_expr();
DEBUG(" AND ");
}
}
}

static void expr() {
bitwise_expr();
if (accept("=")) {
expr();
DEBUG(" := ");
}
}
The above is a large section of code, but there is no need to feel overwhelmed because they are all very simple. Each function that analyzes an expression first tries to call a higher-priority expression analysis function. Then, if the expected symbol is found, it continues to call the higher-priority function. Then, after analyzing the two parts of a binary expression (such as x + y, x & y, x == y), it returns the value. Some expressions can be chained (such as a + b + c + d), so they need to be analyzed cyclically.
We will output some debugging information when analyzing each expression, and these information will bring us some interesting results. For example, if we analyze the following code fragment:
int main(int argc, char **argv) {
int i = 2 + 3;
char *s;
func(i+2, i == 2 + 2, s);
return i & 34 + 2;
}
We will get the following output:
identifier: main
function argument: argc
function argument: argv
function body
local variable: i
const-2 const-3 + :=
local variable: s
var-func var-i const-2 + FUNC-ARG
var-i const-2 const-2 + == FUNC-ARG
var-s var-i const-2 + FUNC-ARG
FUNC-CALL
var-i const-34 const-2 + AND RET
All expressions will be written in postfix notation (such as 2 + 3 becomes 2 3 +). And this is a more convenient and reasonable form for stack-based computers. When the operands are on the top of the stack, the function can perform the pop operation to obtain the operands, and then push the result onto the stack.
Although for today's register-based CPUs, this may not be the optimal method, this method is simple and can meet the needs of our compiler.

symbols
Now, we have done a lot of work. We have written a lexical analyzer and parser with less than 300 lines of code. Next, we need to add the following functions so that these tokens (such as variable names, function names) can work correctly. A compiler should have a symbol table so that the address of these tokens can be found quickly. So when you write "i = 0" in the code, in fact, you are putting the value 0 into the memory location 0x1234 (assuming the variable i is at memory location 0x1234). Similarly, when we call the function "func()", in fact, it is just jumping to continue executing at 0x5678 (assuming the value of the func symbol is 0x5678).
We need a data structure to store symbols:
struct sym {
char type;
int addr;
char name;
};
Here, type has different meanings. We use a single letter to mark different types:
L - local variable. addr stores the address of the variable in the stack.
A - function parameter. addr stores the address of the parameter in the stack.
U - undefined global variable. addr stores its absolute address in memory.
D - defined global variable. The rest are the same as above.

So far, I've added two functions: sym_find(char *s) to find symbol by its name, and sym_declare() to add a new symbol.
Up to now, we also need to add two functions: sym_find(char *s) to find the symbol by its name, and sym_declare() to add a new symbol.
Now we can start to design the back-end architecture, which will be detailed in the next article.
If you forget the previous information, you can refer to part 1.


### cucu: a compiler u can understand (part 3)
201-01-28 15:04 824 views 0 comments Favorite Report
Classification: Compiler and Principles (2)
Table of contents (?)
Now let's talk about the back-end architecture of the compiler. C language should be a portable language, but in the process of porting, we don't need to rewrite the entire C compiler for a new CPU architecture. The back-end of the compiler is used to generate low-level bytecode, and the front-end of the compiler will call the functions of the back-end of the compiler. A good back-end design will make the compiler highly portable.
I hope CUCU will be a portable compiler (that is, so-called cross-compilation). Therefore, I plan to write the back-end code into an independent module.
But before we specifically consider a back-end code, we still have a lot of work to do.
Simplified CPU architecture
The CPU architecture we skipped has two registers (we denote them as A and B) and a stack. Register A is an accumulator. Like many RISC CPUs, we will use a fixed-length instruction set. To make it more interesting, we don't turn the instructions into hexadecimal code, but present them in a more natural way.
I use a simple way to design instructions. Each instruction is 8 bytes long (indeed, this is a bit long, but it doesn't matter, after all, this is a fake architecture). The first 7 bytes are ASCII symbols, and the last one is 0x10 ('\n').
This allows us to design more readable instructions, such as A:=A+B, A:=1ef8, or push A. These instructions are basically self-explanatory ("add the content of register B to register A", "put 0x1ef8 into register A", and "push the value of register A onto the stack").
A:=NNNN - Put 0xNNNN into register A.
A:=m - Put the content (as a byte) at the address in register A into register A.
A:=M - Put the content (as an int variable) at the address in register A into register A.
m:=A - Put the value in register A into the address pointed to by register B (as a byte).
M:=A - Put the value in register A into the address pointed to by register B (as an int variable).
push A - Push the value of register A onto the stack.
pop B - Pop the top element of the stack and put it into register B.
A:=B+A - Add the values of A and B and put the result into A.
A:=B-A - B minus A and put the result into A.
A:=B&A - Bitwise AND.
A:=B|A - Bitwise OR.
A:=B!=A - If B != A, then A is 1; otherwise, A is 0.
A:=B==A - If B == A, then A is 1; otherwise, A is 0.
A:=B<<A - Shift the value in B left by A bits and put the result into A.
A:=B>>A - Shift the value in B right by A bits and put the result into A.
A:=B<A - If B < A, then A is 1; otherwise, A is 0.
popNNNN - Pop NNNN elements from the stack.
sp@NNNN - Put the value of the element at address NNNN in the stack into register A.
jmpNNNN - The program jumps to continue executing at address NNNN.
jpzNNNN - If the value of A is 0, jump to NNNN to execute.
call A - Call the function whose address is in A.
ret - Return from the function.
CUCU back-end architecture design
When we include the "gen.c" file, it actually includes a specific implementation of the back-end architecture. Let's start with the two most basic functions: gen_start() and gen_finish(). These two functions are used to generate the program header (such as PE header and ELF header) and some preprocessed bytecode.
The compiler uses a function emit() to emit bytecode into the code array. Each element of this array represents a compiled program that can be used.
Therefore, the compiler only calls the interface provided by the back-end architecture, and the back-end architecture calls emit() to generate specific bytecode. This is the process by which the compiler compiles into machine language.
Therefore, now we need to define some of the most commonly used instructions, and then let the back-end architecture implement them. Let's start with the simplest program.
int main() {
return 0;
}
Let's analyze the process of function call. This process is also the process of how function parameters are passed to the function body and how the return value is processed. As we said earlier, parameters are passed on the top of the stack (the first parameter is pushed first). Let's make another agreement that register A carries the return value of the function.
In fact, we use register A to store all values, and register B is only used to store temporary variables.
For the above program, the expected bytecode should be in the following form:
A:=0000
ret
Therefore, we need a function to put an immediate number into register A, and a function to handle the return. We define these two functions as gen_const(int) and gen_ret().
When the compiler finds that a main expression is an immediate number, gen_const will be called. When a return statement is found, gent_ret will be called. Although some functions are of type void, so they do not have an explicit Return. But for safety and simplicity, we will call gen_ret() once at the end of each function, even if there is an explicit return in front of it.
Our compiler does not pursue optimization, efficiency, and safety, so this double return method is feasible for us.
Mathematical operations
Now let's compile mathematical expressions. These mathematical expressions are all similar, so we use an example to illustrate how the compiler handles them. Remember how the lexical analyzer works? It analyzes (more strictly speaking, compiles) the left value of the expression, the right value of the expression, and then the operator.
This is a typical process of compiling a mathematical expression (remember the joke about putting an elephant in the refrigerator):
..Calculate the left value
push A
..Calculate the right value
pop B
A:=A+B
When we calculate the left value, we need to temporarily store the result. Using the stack is a good choice. Therefore, a mathematical expression 1 + 2 + 3 we will compile into the following form:
A:=0001 -+ -+
push A | |
A:=0002 | 1+2 |
pop B | |
A:=A+B -+ | +3
push A |
A:=0003 |
pop B |
A:=A+B ----+
Some other things
Handling symbols is also very simple.
To call a function, we first need to put its address into register A, and then generate code call A using gen_call().
To access a local variable, use gen_stack_addr and return the address of this variable in the stack.
To access a global variable, use gen_sym_addr(). In addition, each time a new symbol is established, the compiler needs to generate some code (such as assembly code), and gen_sym is used to handle these situations.
gen_pop pops N elements from the top of the stack and increases the stack top pointer.
gen_unref is used to generate some pointer-related operations. Depending on the type (byte or int), code such as A:=m or A:=M will be generated.
gen_array pushes the address of an array onto the top of the stack.
Finally, when encountering if/while statements, gen_patch is used to append code for generating address jumps. Why is it called appending? Because when we encounter a statement that needs to jump, the address to jump to is unknown, and this address depends on the size of the compiled statement block. Therefore, the code for appending the address jump needs to be done after the statement block is compiled.
Almost successful, let's try the following program:
int main() {
int k;
k = 3;
if (k == 0) {
return 1;
}
return 0;
}

jmp0008 # Generated by gen_start() to jump to main, address is 0x08
push A # Apply space for local variable K
sp@0000 # Get the address of the space just applied for
push A # Push this address onto the stack
A:=0003 # Put 3 into A
pop B # Get the previously stored address of K
M:=A # Put the value in A as an int into K
sp@0000 # Get the address of K
A:=M # Get the value in it as an int and put it into A
push A # Store this value
A:=0000 # Set A to 0
pop B # Get the previously stored value of K
A:=B==A # Compare the values of A and B (that is, "k" and 0)
jmz0090 # If it is false (A != B, k != 0) - jump to to 0x90
A:=0001 # Put 1 into A as the return value
pop0001 # Release the space in the stack that stores the value of k
ret # return
jmp0090 # Else branch content is here, the next statement address is 0x90
A:=0000 # Put 0 into A as the return value
pop0001 # Release the space in the stack that stores the value of k
ret # return
ret # The second return for safety consideration earlier
Although our code is messy and bloated, it does work. More importantly, you can now understand the working principle of the compiler and can make your own compiler.
But, I must warn you...
Warning
Please never do it according to the above steps! If you want to write your own compiler, it is recommended to use the following mature tools:
flex/lex/jlex/...
yacc/bison/cup...
ANTLR
Ragel
and many others
In addition, you want some professional literature, such as the Dragon Book ("Compilation Principles", Translator's note). And the courses on coursera.org may be helpful to you.
If you need to make your system adapt to the existing language, you can learn about the back-end of LLVM and the back-end of GCC.
If you need more information about toy compilers, you can learn about SmallC.
If you want to write a compiler for a simple language, you can learn about PL/0 or Basic or C.
But please never write a compiler from scratch and use it in actual work.
Postscript
The code of the entire project can be found here. It is licensed under MIT, and anyone can use or modify it for free.
Anyway, the compiler is a very interesting thing. I hope you will like it.
Floor 9 Posted 2016-02-03 12:08 ·  中国 海南 移动
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
kickout

Reprint of Li Jiafang's "Detailed Explanation of Hard Disk Partition Table"
http://www.cn-dos.net/forum/viewthread.php?tid=115&fpage=174

Hard disk master boot sector = hard disk master boot record (MBR) + hard disk partition table (DPT)
--------------------------------------------------------------
Physical location: cylinder 0, side 0, sector 1
Size: 512 bytes
Among them: MBR 446 bytes (0000--01BD), DPT 64 bytes (01BE--01FD), end mark 2 bytes (55 AA)
Function: MBR guides the system to jump to DBR by checking DPT partition information;
Reading: Use NORTON DISKEDIT, select DRIVE——>PHYSICAL DISK-—HARD DISK in the OBJECT menu, then select DISK PARTITION TABLE in the OBJECT menu to read, and use the WRITE OBJECT TO option in the TOOLS menu to save to the specified file for backup;
Writing: Use NORTON DISKEDIT, select DRIVE——>FLOOPY DISK in the OBJECT menu, select the backed-up DPT file, then use the WRITE OBJECT TO——>PHYSICAL SECTOR option in the TOOLS menu to write to 001 (cylinder 0, side 0, sector 1);

Detailed explanation:
000H--08AH MBR boot program (find boot partition)
08BH--0D9H MBR boot string
0DAH--1BCH Reserved ("0")
1BEH--1FDH Hard disk partition table
1FEH--1FFH End mark (55AA)


Active partition boot sector (DBR)
--------------------------
Physical location: cylinder 0, side 1, sector 1
Size: FAT16 1 sector 512 bytes FAT32 3 sectors 1536 bytes
Function: Contains machine CMOS and other information (0000--0059), checks this information and boots the specified system file, such as NTLDR, etc.;
Reading: Use NORTON DISKEDIT, select DRIVE——>LOGICAL DISK-—DISK C in the OBJECT menu,
then select BOOT RECORD in the OBJECT menu to read, and use the WRITE OBJECT TO option in the TOOLS menu to save to the specified file for backup;
Writing: Use NORTON DISKEDIT, select DRIVE——>FLOOPY DISK in the OBJECT menu, select the backed-up DBR file, then use the WRITE OBJECT TO——>PHYSICAL SECTOR option in the TOOLS menu to write to 011 (cylinder 0, side 1, sector 1);


Detailed explanation:

000H--002H 3-byte jump instruction (go to boot program, jump to 03EH)
003H--03DH BIOS parameter area
03EH--19DH DOS boot program
19EH--1E5H Boot string
1E6H--1FDH File name (IO.SYS, MSDOS.SYS)
1FEH--1FFH End mark (55AA)


Hard disk partition table (DPT)
---------------------
Offset address Number of bytes Meaning analysis

01BE 1 Partition type: 00 means inactive partition: 80 means active partition; others are invalid partitions.

01BF~01C1 3 *Start address of the partition (side/sector/cylinder), usually the start address of the first partition starts at side 1, cylinder 0, sector 1, so these three bytes should be 010100

01C2 1 #Type of the operating system of the partition.

01C3~01C5 3 *End address of the partition (side/sector/cylinder)

01C6~01C9 4 Start logical sector of the partition

01CA~01CD 4 Total number of sectors occupied by the partition

Note: * Note the byte allocation in the start address (side/sector/cylinder) and end address (side/sector/cylinder) of the partition:

00000000 01000001 00010101
^^^^^^^^ ==~~~~~~ ========

^ Side (head) 8 bits
~ Sector 6 bits
= Cylinder 10 bits

# Operating system type of the partition (file format flag code)

4---DOS FAT16 32M
7---NTFS(OS/2)
83---LINUX>64M


DPT is a total of 64 bytes (01BE--01FD), as shown above, each partition occupies 16 bytes, so it can represent four partitions, which is why the sum of the primary partition and extended partition of a disk can only be four in total.


Logical drive
-----------
The information of the extended partition is located in the hard disk partition table (DPT) shown above, and the information of the logical drive is located in the starting sector of the extended partition, that is, the sector corresponding to the start address (side/sector/cylinder) of the partition. The information in this sector is different from the hard disk master boot sector in that it does not contain MBR, and the 16-byte partition information represents the start and end addresses of the logical drive, etc.


So, in the case where the disk only contains one primary partition and one extended partition (containing multiple logical drives), even if the data of the hard disk master boot sector (including DPT) is lost due to a virus or other reasons, the entire hard disk can be restored through the data of the logical drive.

For example: The following is the partition situation of a hard disk.

Cylinder Side Sector Cylinder Side Sector Start sector (logical) End sector Total sectors
MBR 0 0 1 - - - - - -
C 0 1 1 276 239 63 63 4,188,239 4,188,177
Extended 277 0 1 554 239 63 4,188,240 8,391,599 4,203,360
D 277 1 1 554 239 63 4,188,303 8,391,599 4,203,297


If the primary partition table is damaged, the data of the logical drive contained in the extended partition table can be manually found. In this example, it is the data corresponding to drive D. Then subtract 63 from its start sector (logical) to get the start sector (logical) of the corresponding extended partition, and change its start address (side/sector/cylinder) to side 0 to get the start address of the extended partition. Then, through the extended partition, the information of the primary partition C can be obtained, and then the entire hard disk can be restored by using the DISK/MBR command and manually filling in the partition table.

It is relatively cumbersome to use this method actually. If the size of each partition is known, the disk can be repartitioned to the original size by using PQ MAGIC 5 (note: never apply it, we just use it to obtain data), and view INFO to obtain the above data, record it, then cancel the partition operation, and then use NORTON DISK 2000 to manually modify the DPT table to restore the entire hard disk.

The partition table data corresponding to this example:
80 01
01 00 06 EF 7F 14 3F 00 00 00 11 E8 3F 00 00 00
41 15 05 EF BF 2A 50 E8 3F 00 60 23 40 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA

Extended partition table data:
00 01
41 15 07 EF BF 2A 8F E8 3F 00 21 23 40 00

Note: The logical start sector and total number of partitions are low bytes first. For example, the start address of the extended partition in this example is 50 E8 3F 00. When converting to decimal, it should be changed to 00 3F E8 50 first. The total number of occupied partitions 60 23 40 00 should be changed to 00 40 23 60 first. Similarly, when filling in this value manually, the high-low byte conversion should also be performed.



================================= kickout
All done, give a Kiss!
Floor 10 Posted 2016-02-03 19:06 ·  中国 海南 三亚 电信
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
### Chapter 1 The Unbelievable Physics - A Conversation with Michio Kaku

Dr. Michio Kaku is a professor at the City University of New York. He is an outstanding physicist and an excellent teacher, known as today's "Einstein." He is one of the creators of the superstring theory. He has written many books and has also explained the complex theories of "new" physics into understandable concepts in some documentaries.

□ Dr. Kaku, I have all your books in front of me... Please talk about the value of knowledge! Thank you for all you've done for human awakening.
Before the conversation begins, I want to tell you how I found you. A year ago, I was watching the Discovery Channel. I was attracted by a program, but unfortunately, the program had been on for a while, reenacting the story of a woman walking in a mall who was extremely pained. People passed straight through her body, and she was completely unaware of it; she just had a strong feeling that she somehow "became invisible." The camera turned to you, standing in the center of Times Square, explaining the existence of other dimensions, saying that the people in the documentary were actually in another dimension.
Can you explain in detail the world of dimension fusion?

Sure. First, I grew up in San Francisco. When I was a child, I often stood in the Japanese tea garden for hours watching carp swim. I often fantasized that I was a fish, a small fish swimming in this shallow pond. I pondered how they lived in a two-dimensional world: they could only move forward, backward, left, or right. So, any fish that dared to talk about the "upper" world, the three-dimensional world, or multi-dimensional space would be regarded as a "madman" or "idiot." I imagined there was a scientist fish that would say, "Pfft, liar! There is no 'upper' world. Here is only the world you see; what you see in the pond is what exists. That's it."
So I wanted to dive down, catch that scientist fish, and bring it to the "upper" world - the three-dimensional world, multi-dimensional space. What would it see? It would see a wonderful world where creatures have no fish fins... a new law of physics where these creatures can breathe without water... a whole new law of biology. Believe it or not, today, we physicists believe and are trying to prove that we are that fish. We have spent our whole lives in this three-dimensional space (forward, backward, left, right, up, down). Someone who dares to propose the existence of an invisible world, a world we can't see or touch, is regarded as a madman.

□ By the way, I am one of them. In my life, my profound experiences in the unknown world have always been scorned and ridiculed.

No one will laugh at you anymore. The current trend has changed greatly. Nowadays, physicists believe that in fact, we may consider the existence of higher dimensions, dimensions we can't see, and these dimensions are all around us, just like the "upper" world for the fish.

□ What do you mean by "multi-dimensional space"? You mentioned ten dimensions in your book, meaning "layers" of reality. I'm curious, why do you limit yourself to ten dimensions and how did you arrive at these conclusions?

Einstein had a dream that he spent 30 years pursuing - creating a "Theory of Everything," an equation an inch long that would summarize all the forces in the universe and let him read God's mind. However, whenever we combine the gravity equation, the light equation, and the ratio equation, we find that there isn't enough space in the three-dimensional world. That is, there isn't enough space to fit all the forces together. But if you assume the existence of a higher dimension, in fact, you can assume up to ten dimensions, then all the forces will fall neatly and elegantly into a simple related theory. This will shock the entire physics community. But Einstein didn't go far enough. He stopped at four dimensions, but if you go to six dimensions, seven dimensions, eight dimensions... all the way to ten dimensions, you can describe these higher-dimensional worlds simply and perfectly.
Now, let's assume the universe is like a soap bubble. We live on the surface of the soap bubble and can't leave. We are trapped here, like a fly stuck on a flypaper, and the soap bubble is expanding - this can be proven by a lot of data we get from satellites. But we still believe there are other soap bubbles. What's floating on those soap bubbles? What will those soap bubbles expand into? If the universe expands, what will happen?
We think it will become a higher-dimensional space. This makes me very happy. When I grew up, I went to Sunday school. There I studied the "Genesis" and the origin of the universe. But my parents were Buddhists, and they believed in Nirvana, which is neither the origin nor the end. Now, we physicists believe that we can integrate Buddhism with the "Genesis" in Judaism and Christianity.
We do believe that the universe originated from an explosion, but these big bangs are happening all the time. They happen in a broader sea of Nirvana, and we even named it "ten-dimensional hyperspace" currently. We think we have a beautiful picture depicting the origin, growth, expansion of the universe, and other already-formed universes, those parallel worlds, and a broader eternal realm: Nirvana - Nirvana in Buddhism; Nirvana in physics; ten-dimensional hyperspace.

□ So, you mean we might accept these concepts: that's all, that's always been all, and that will always be all. The big bang will be proof of all that has always existed?

Exactly. It's hard to reconcile the big bang with some theological theories, but this big picture, this picture of the multiverse, presents a beautiful and perfect harmony of many past patterns. Believe it or not, this picture matches the satellite data.
We currently have the WMAP satellite orbiting the Earth. The satellite shows that the universe is 13.7 billion years old and shows all the data that matches the picture: this picture we have in our hands - bubbles, like from a bubble bath; imagine a bubble bath or a popping, expanding bubble, some small bubbles, some colliding with other bubbles. This is the new picture created from reality. Throughout history, we can see this picture in religion.

□ How does this picture connect with religion?

If you look at the first paragraph of the first chapter of "Genesis," you'll see the moment when God created the universe. This is consistent with the big bang theory. In fact, there was a Catholic archbishop who said that the big bang theory is comparable to "Genesis." But later, we have some models from Hinduism and Buddhism that claim the universe has no origin and no end. The universe is eternal, so we have Nirvana. Thus, we not only see the integration of the two views - the big bang happens in Nirvana and the big bang is always happening.
Even at the moment we're talking, many universes are being born, and our satellite data matches this picture - the eternal big bang, the eternal expansion. In fact, one of my friends, Alan Guth, might win the Nobel Prize in Physics because this picture matches the satellite data. I think we have a very good integrated science that comes from satellite data, Einstein's unified field theory, and the religious theoretical framework.

□ We sincerely thank you for your excellent work that makes it possible for ordinary people to understand the complex concepts in the field of physics, as these concepts are really hard to grasp. Can you briefly tell us about the superstring theory (you are considered one of the creators of this theory) and how this theory is related to quantum physics?

More than 2,000 years ago, the Greek philosophers of the Pythagorean school analyzed the Greek lyre, hoping to find the mathematical ratios of music: the mathematics of harmony. Why do we have half steps and whole steps, third notes and fifth notes, and major and minor chords? Using the Greeks' mathematical understanding of them, the conclusion was that music follows mathematical laws. When they realized this, they were shocked, and thus they thought: perhaps the universe can also be explained in musical terms. However, they failed. At that time, they didn't know about atoms, and chemistry and physics hadn't developed yet, and these concepts were unknown.
Now, we have successfully completed the task. We have too many ions, electrons, and neutrinos; we have gamma rays and Higgs boson particles. To get a Ph.D. from the University of California, I had to remember hundreds of subatomic names and their foreign names.
Today we believe that all particles are just notes - notes vibrating on a rubber band. If I use a microscope to observe an electron, I see that it's not a point at all (that's an old photo), but just a vibrating rubber band. If I change the vibration frequency, it will become a neutrino; if I change it again, it will become a harmony. I change the frequency again, and it will become one of the hundreds of subatomic particles I memorized to get my physics Ph.D.
I hope that in the future, when applying for a physics Ph.D., you can just say "superstring theory" to get the degree. Nowadays, we physicists believe that physics is just the music of vibrating strings. Chemistry is the melody you play on these vibrating strings. The universe is the symphony of these vibrating strings. Then, what is God's mind?
I just said that Einstein spent 30 years pursuing a theory that would let him read God's mind. Believe it or not, we now have another candidate, a candidate that has excited the entire physics community. Time magazine, music magazines, and all the mainstream networks have written a lot about this - God's mind. We physicists believe it's the music of the universe resonating through hyperspace, and that's what we think God's mind is.

□ For this reason, I'm deeply attracted by your work. Because of my small contribution in the spiritual realm, everything I write is about the music of the universe. When I read your work, from a scientific perspective, I'll think: "Oh my god, science and spirituality are truly integrated!"

That's true. By the way, next month we will launch for the first time in Geneva, Switzerland, the largest machine that the current scientific community can build - the "Large Hadron Collider." It's 17 miles long. We hope to truly create a higher musical particle that we can't see, because we are the lowest note in the octave. Everything you see around us is vibration at the lowest frequency on a small rubber band. We plan to generate higher frequencies on the Large Hadron Collider.
Of course, the media has messed up the whole plan at present. The media says that this might create a black hole that could swallow the Earth, which is very stupid nonsense. We can't create a black hole in Geneva, Switzerland. We hope to get the music of a higher octave. That's all there is to this machine.

□ How does this connect with sphere music?

Musicians centuries ago tried to explain the movement of the stars and the movement of the objects around us using sphere music, and they wanted to explain movement with music.
This idea didn't come true in science because Isaac Newton came. He brought us the laws of motion, which work well for our satellites and stars. Then Einstein came, and he said Newton didn't go far enough; the movement of stars can't only be explained by gravity, but also by time and space (that is, the curvature of space and time). Now, we realize that Einstein also didn't go far enough. We don't just go into time and space, but also into multi-dimensional space, through the vibrating strings that create music - the music of multi-dimensional space vibration, bringing us back to sphere music. In a way, we've completed a philosophical full circle.

□ Let me try to follow your thoughts. According to your rubber band metaphor and the concept that vibration changes reality, does it mean that if there are enough people on this planet to raise the frequency of their thoughts, then we can change the material essence on the planet?

In this case, I'm not completely sure how to change the frequency of these particles - of course, there is a way, through the Large Hadron Collider.

□ Of course, this is music, right?

Yes, another possibility is to use our Earth-orbiting satellites to do it. By the way, I forgot to say just now: we now have a picture of the early universe... a picture of the early big bang. People often complain about our physicists; we don't have picture evidence to prove that the explosion happened 13.7 billion years ago. But now, we have pictures: pictures of the explosion itself. These pictures were taken in the microwave environment. If you go to the official website of NASA www.nasa.gov and type in WMAP, you can see the pictures of the big bang. This is indeed a satisfying fundamental picture. There's "Genesis," the origin, and the sudden explosion of the universe. We've now captured this scene. Then the next question is: "What happened before the big bang?"
We think that's where the superstring theory comes in.
The big bang happened 13.7 billion years ago. In fact, the superstring theory takes you before the explosion, because before the explosion, time hadn't started. The superstring theory predicts that there might be other universes there - so if the multiverse is like a bubble bath, then our soap bubble will separate from another soap bubble. It's like when you're taking a bubble bath, a soap bubble splits in two or two bubbles collide violently and merge into one bubble.
That's called the superstring theory.
Some physicists, especially some friends of mine at Princeton, strongly advocate the Big Splat Theory; however, one of my friends at MIT advocates the growth theory, also known as the "inflation theory." This theory holds that our universe developed from another universe. We're not sure which theory is correct. But the key point is, I emphasize, that the universe and time didn't start with the big bang. We think we can go back before the big bang, and we'll measure this time around 2014. At that time, we'll launch a new generation of satellites to help us gather pictures, not just pictures of the big bang, but also pictures of the moment when our universe was emerging from its "womb."
Maybe we'll even discover the umbilical cord - the umbilical cord connecting the baby universe and the mother universe. We haven't implemented this plan yet. However, we have arrangements. We don't have evidence now, but by 2014, when we launch a new generation of satellites to look for the umbilical cord connecting our baby universe and the mother universe, everything will be exciting.

□ This is indeed exciting, especially because it connects with some more profound concepts. These concepts come from the metaphysical world. Several of my books also discuss the interstellar umbilical cord of the sun. Please talk about the umbilical cord of the universe. It's very fascinating.

The idea of parallel universes is another theme from the religious and spiritual realm: other planes of existence. For example, the Catholic Church has always believed that heaven and hell represent two different spiritual planes and coexist with our universe. We physicists secretly laughed at this view, but we don't laugh at anyone because all evidence proves that there are indeed other universes, other parallel universes.
For example, think about the radio in your living room. Your radio receives a certain frequency, but you need to know that there are hundreds of other frequencies in your living room - there's Radio Moscow, Radio Havana, BBC... many frequencies you can't hear. Now ask the question, why can't you hear them? It's because your broadcast isn't in sync (we say no longer matching) with other bands. That's a scientific term: it's isolated from the BBC. You're listening to an American rock radio station and can't hear the BBC from London; you're isolated from the BBC radio station.
Currently, we think that everything around us is vibrating at a specific frequency; there are short waves vibrating in the room, and there are bands from other frequencies. Each frequency represents a different universe; for example, there might be a wave function of dinosaurs in your living room, and a wave function band of a world where Elvis Presley is still alive.
These are frequencies we've never contacted and are separated from; this theory is called "Many Worlds," which was proposed by a physicist named Hugh Everett at Princeton in the 1950s.
When this theory was first proposed, it was laughed at, but now, we're starting to believe that Everett was right and that many worlds really exist. We're just separated from them; we're no longer in contact with them... but we coexist with them.

□ Dr. Kaku, do you think someone will find these frequencies from other dimensions?

I've heard some similar stories. I don't know if they're credible because I need to test whether these people have a good sense of pitch, but if you look at physics books, you'll find that our physicists no longer laugh at Hugh Everett's view. The "Many Worlds" theory is also one of the top themes in theoretical physics today. You can go to any physics library in the world to look for the "Many Worlds" theory, and there you'll find 500 papers on quantum mechanics, which is the quantum theoretical basis of the "Many Worlds" theory.
Einstein didn't like this theory, and Einstein was wrong on this issue. Einstein didn't believe in quantum theory, but now, we think that quantum theory does describe this world. That's why we've developed laser beams, transistors. Without quantum theory, there would be no radio, no television, no laser beams, and no satellites. All of these depend on quantum theory, and the extension of quantum theory is "Many Worlds."

□ I've read one of your books, in which you discuss "life ships crossing dimensions." Can you explain it in detail? I find it very fascinating.

Many people have said to me: "Professor, this is all very good. You talk about higher dimensions, ten dimensions, but how can I visit other worlds? How can I get to these places?"
That's difficult.
First, people in religion and the church have talked about meditation and crossing the universe. And we physicists are practicing. We'll build a machine that, with a button press, will take us there, instead of through taking some hallucinogenic drugs or through meditation. In my book "Parallel Worlds," I even depicted what this machine would look like.
You have to understand what Einstein said: "Space and time are like a piece of cloth, and time is like a river." As this river meanders through the universe, it can speed up or slow down.
We've been using the satellite GPS system to get these measurements, such as in your rental car. In outer space, this system relies on the accelerated or decelerated time. But there's a new puzzle: the river of stars might have new eddies, splitting into two rivers. In this case, we can't rule out the possibility of life ships crossing the universe. We can't rule out the possibility of a time machine taking us through the river of time.
Of course, these are now considered "impractical," but Einstein himself realized that time travel is possible in his equations. In 1949, the first time travel scheme was found in Einstein's equations. Since then, our physicists have discovered hundreds of time travel schemes that can let us travel through multiple universes. Stephen Hawking even calls them "baby universes" - small universes that let us go back in time or connect different universes.
To be honest, a machine that can travel through several dimensions must be huge. The machine we're talking about is very large, so it can only be placed in outer space, but I think that in fact, the things we're considering are very amazing. Usually, science fiction writers and fantasy writers mention vortices in the river of time: entrances to other universes. This is the junction area. We're taking it very seriously now and have even depicted the blueprint of such a machine.

□ When do you hope science can achieve this great achievement?

This depends not only on us. In a way, we're talking about huge energy... but one day, if someone knocks on your door and claims to be your great-great-great-great-great-great-granddaughter, don't slam the door shut! It's true that when the time machine appears, your great-great-great-great-great-great-granddaughter lives in another time and space and decides to visit her ancestor. If someone claims to be your as-yet-unborn descendant, don't slam the door shut.

□ Without a doubt, Steven Spielberg must like this scene.

In fact, one of my friends, a physicist at Caltech, is advising Spielberg on his next movie, which is about time travel.
Spielberg takes this issue seriously. He works with physicists to create his next movie, just as Carl Sagan consulted a physicist when making his movie "Contact." He really worked with Jodie Foster to create this movie using the most advanced physics knowledge, which is about making contact with an extraterrestrial civilization.

□ Dr. Kaku, when will you go to make a movie?

Not recently. I'm surprised myself that my book made it to the New York Times bestseller list! I'm really shocked!

□ That's really great. I want to list some of your important books: "Hyperspace" - a book that must be read; "Parallel Worlds" - another wonderful book; and now we also plan to read this new book - "Physics of the Impossible." But before we read this book, I want to go to Switzerland to see what's going to happen there. Can you tell us the specific day?

Next month, in Geneva, Switzerland, scientists will start a 17-mile-long machine. It's not destructive, placed underground, so no radiation will leak out, and it can't create a black hole that could swallow the Earth. I think the media's reports are biased in this regard.
In May 2008, we'll start that machine. We get a certain electromagnetic wave, and this electromagnetic wave will only be generated when it reaches a certain speed. This electromagnetic wave will recreate a universe, a very small universe like Winnie-the-Pooh - Mother Nature will create energy, and there will be no danger.
The Earth has taken a bath in these cosmic rays, which are more powerful than anything our weak human beings have created on Earth, but we've created this machine, which can open a dynamic window to "Genesis" for us. I'm excited because it might help us prove the superstring theory.
Some people say that the superstring theory can't be proven because a miniature universe must be created in the laboratory. Of course, we're not gods and can't create a miniature universe in the laboratory. But the best thing you can do is create a higher form of music. We hope to create "super-symmetric particles." "Super-symmetric particles" are super particles that are emitted from small rubber bands vibrating in five-dimensional space, and we're at the bottom of the octave on this rubber band. But there are higher notes - we haven't seen them yet, and that's why we want to use the machine at CERN in Geneva, Switzerland, to create higher vibrations.
If it succeeds, it will shock the entire physics community. It will hit those who laugh at it. The latter think that because we can't create a miniature universe in the laboratory, the superstring theory can't be demonstrated. We can do most things well, such as creating super-symmetric particles, and we think this will be the next development stage of vibration on the vibrating string.

□ It would be exciting and interesting if this significant effort is put into practice before the famous 2012 transition. Many people show their fear of 2012. I want to ask you: where will we go, what are your feelings or opinions?

Let me say something related to this. I've read some translated works of the Maya people. In the Maya calendar, they discuss cycles, and of course, these cycles are thousands of years long. We're heading towards the end of a cycle. There are two ways to look at it. The first is, of course, that we're at the end of a cycle, the end of the world. But I think the Maya people's way of looking at it is the second, that it's a beginning. It's a cycle, a rebirth, so it doesn't mean the end of everything, just represents rebirth and recirculation. We've gone through several cycles in the past, and nothing happened, and we'll go through more cycles in the future. I think this is a revival, not destruction.

□ I also feel the same. I see it as an incredible prototype of our individual regeneration process.

Yes, if that's the case, we don't need to look at it with the dark eyes of pessimists anymore. I think we should see the universe as brilliant. The existence of the universe is absolutely amazing. The universe is a glorious and wonderful place, and we live here. It's hard to believe that a cycle will end everything in an instant, and most cycles represent rebirth. That's why I'm optimistic about the future of the world. Of course, the world is facing many challenges at present, but I believe in the indomitable spirit of human beings, and I believe we can overcome all difficulties. We've done it, and I believe we'll do it in the future.

□ I'm very happy to hear you say that. No matter how much darkness and difficulty there are on our planet now, we need positive thoughts and voices of celebration. I completely agree that the human spirit is invincible, and when we face the most difficult challenges, we also have a great chance to overcome them.

One of the benefits of looking at the world from a cyclical perspective is that you'll see past cycles, and you'll start to think: "Where have we been? What difficulties have we overcome in the past?" And that's a guide for the future. We don't look at the dark and death, but at those places where we can revisit where we've been. Unless we understand where we've always been, we can't understand where we're going. It's a fact that we look at our history through cycles, and this will force us to revisit history. I think this will bring us hope that we have the ability to shape the future.

□ What do you think will happen to us? How do you predict the imminent transition? You know, in front of us, we face challenges from dark things, no need to list them one by one, but relatively, we also have many positive and revolutionary changes. What do you think will happen to this planet in the next few years?

Let me tell you what will happen in the next century, because I firmly believe in birth and rebirth. Physicists are diligently searching for intelligent life in the universe, and we must also seriously analyze this question: "Once we make contact with these extraterrestrial beings, how will we define our civilization?" For us, this isn't an academic question: we have telescopes, satellites. One day, we'll communicate with them.
Some physicists have proposed a theory that there are three forms of civilization: the first form, the second form, and the third form.
The first is a true planetary civilization: they have a planetary culture; they should be able to control the weather, such as hurricanes and earthquakes; they build cities on the ocean. A planetary culture corresponding to this description will appear before us in 100 years.
The second form of civilization has now developed on several planets, and they control the planets... some neighboring planets. They colonize some neighboring solar system planets and control the energy output of the entire planet.
Then there's the third: this form of civilization is galactic; they truly control energy, not the energy of one planet, not 100, but the energy of billions of planets in the planetary system. That's the third form. If an extraterrestrial civilization visits us, it's probably the second form of civilization or the third form.
Now, looking back at ourselves, where are we? We're at the zero form of civilization.

□ Okay, then how do you define the zero form of civilization? This form of civilization sounds like a civilization that hasn't evolved.

Okay. Let's talk about the zero form of civilization. This form of civilization gets the energy we need from dead plants. There are savages in the jungle, primitive religious states, ignorance, poverty, and disease still exist. They can only dream of creating a planetary civilization society, but if you do some calculations with a calculator, the result is: it will take us 100 years to build a planetary civilization society. Whenever I read the newspaper, I always see the pain of the birth of the first form of civilization.
For example, what is the Internet? The Internet is the beginning of the first form of telephone system. Historically, we've seen for the first time a rough sketch of a star-level electronic communication system. The Internet is the baby, sprout, and seed of the first form of telephone system. What language will they speak? They'll most likely speak English. English has already been an elite language. I give speeches in English at every meeting, and elite businessmen, politicians, and scientists also speak English. Within 100 years from now, English will also become the language of the middle class. The middle class will also speak English, just like their own language.
What is the European Union? The European Union is the embryonic form of the first form of economy - why do we have the European Union? To counter the North American Free Trade Agreement, that is, to counter the United States. Thus, we see the embryonic form of the first form of economy. Everywhere I go, including when I listen to the radio and watch TV, I can see the beginning of the first form of culture. Young people like rock music. They like RAP (rap while singing) and dance to the most popular beats. The elite like more advanced fashion, and I think this is always the case worldwide. We all like movies, and that also gives us a form of the first form of culture.
Everywhere we go, we can see the beginning of the first form of culture, but there's also a backwash, and this is what I'm worried about. Some forces don't want to have the first form of planetary civilization because this civilization is multicultural, enlightened, and progressive; it believes in equality and future progress. These forces are terrorists.
Terrorists trust the negative first form of civilization - a civilization controlled by dogma, strict religious creeds, and a very strict hierarchy, where men are on top and women are stepped on.
That's terrorists; they don't believe in the progressive first form of planetary civilization. It's not known yet whether we can transform from the zero form to the first form. Some people think that maybe in 2012, we'll transform to the first form, but I think 2012 is just a revival, a new beginning. Where are we heading? Where is the Age of Aquarius that people have been talking about? For me, that's the first form of civilization - planetary, inclusive, progressive, scientific, democratic - I think that's where we're heading, unless we blow ourselves up first.

□ I want to add some thoughts to your planetary-level classification. It's not only the so-called "terrorists" who don't want our species to evolve. There are other forces behind the terrorists, do you think?

Oh, yes, I agree.

□ I understand, so we don't have to play anyone's role.

Yes, of course, we have nuclear weapons, population growth, biological and chemical weapons, the greenhouse effect, and global warming - a series of problems. There are forces that don't want us to evolve to a planetary civilization, so I think there's a race resisting time. That's a pattern I see.
On the one hand, we have the forces of enlightenment and education, which are growing into an excellent, multicultural, planetary civilization; on the other hand, we have the forces of ignorance, sectarianism, fundamentalism, and racism. We have a dark side within ourselves, which comes from the primeval forest. We still have our ancient brains, having to fight everyone in the swamp and forest, and we still have these bad things within us. We don't know who will win, but I think that overall, we'll survive, we'll get through 2012 safely, and eventually our civilization will move towards the first form. I'm optimistic.

□ Me too! By the way, please tell us the overall meaning and purpose of your book "Physics of the Impossible."

Arthur C. Clarke, who recently passed away, made a very famous statement, and I'll quote it: "Any sufficiently advanced technology is indistinguishable from magic."
Now, think about magic.
Magic can disappear, become invisible, and reappear in another place. Magic is the ability to turn one object into another, make them disappear, and make them appear somewhere. In the future, we'll have this ability, such as invisibility. Two years ago, at Duke University and Imperial College London, we made a major breakthrough in the research of invisibility. We can make an object disappear, at least when you use microwave rays to see it. Just a few months ago, at Caltech, they demonstrated the same with visible light. I think that in the next few decades, we'll be able to make objects invisible - so, Harry Potter, be careful!

□ Isn't that the essence of the famous "Philadelphia Experiment"?

In a way, yes. We now have the ability to demonstrate visible light that can hide an object and can also recreate the object at the other end. We all thought these were impossible before. I teach optics to students in college, and I often tell them that invisibility is possible because light is not like water; light can wrap around a large round stone and then recreate the object at the other end, just like a river.

□ How do you do it?

There's a new substance called "metamaterial." It has many impurities that we never thought about before, but these impurities can recoil with light, so it can hide an object.
Bring Harry Potter over and put him in a cylinder. The light touching the cylinder surrounds the cylinder and then reforms at the other end. This principle was proven two years ago. This shocked the entire physics community. In this regard, every physics textbook is wrong.

□ But this is so amazing! This is "unbelievable physics," right?

Yes, this is unbelievable physics. Now, another "unbelievable" thing, disappearing like a magician and then reappearing in another place. We physicists call it "telepathy." At the atomic level, we've been able to do this in the laboratory. We've been able to make a photon disappear and then make it reappear 100 miles away, such as from the Canary Islands to another Canary Island.
Next, we want to do something with the space shuttle. We want to take a photon on Earth, make it disappear, and then make it appear on the space shuttle. After 2020, we plan to do this experiment on the moon.
There's no end to such experiments. We can indeed make an object reappear on the moon. Now, for atoms, not just photons, we've had successful experiments with cesium atoms and beryllium atoms, and of course, these are all successful in the laboratory. I estimate that within the next 10 years, we should be able to make a molecule, such as DNA, disappear and then make it appear elsewhere, and maybe even a virus or a cell.
It's not impossible to do invisibility experiments on humans. To be honest, a person has 50 trillion cells. We can't transport all the cells, but our technology has reached this level, and I think we should consider the philosophical limitations. When you're transported, you're actually dead, you disappear. You decompose in the process, and then you reappear elsewhere. This scene often appears in movies, such as "Jumper" and "Star Trek." You just saw Captain Kirk die in a room, and then you saw a person pretending to be him - Captain Kirk appear and say, "I'm the real Captain Kirk. I have his memories, genes, nervous system, and his quirks."
This makes you wonder about the soul. If you see him decompose and then he reappears with all his complete memories, what happens to his soul?

□ That's also a concern of me and other metaphysicists.

We physicists also find this problem very tricky. To you, what does it mean that all the atoms in your body decompose? You're not here anymore, no matter who you used to be... have you gone? Who is the other person standing there, looking so much like you, having the same thought patterns, the same memories, and claiming that he is you? In the next few decades, this will be an academic question, and how will we handle it? Is there a soul? We'll face such a problem in the laboratory.
In my book "Physics of the Impossible," I describe many technologies you see in movies (such as "Harry Potter," "The Terminator," "E.T.," "Star Wars," and many Hollywood blockbusters), which are actually based on technologies that our physicists think will be realized in the future. I can't give you a time frame for when we'll have these technologies, decades, centuries, years, or a thousand years.

□ Will the realization of this technology be faster than imagined? Let's look back at "Star Trek" and then at those handheld communication devices. One of my colleagues said that Nokia is produced according to the model in "Star Trek."

Yes, we have the same feeling.

□ We already have these products, and they look exactly the same and have the same functions.

Yes, and then look at telepathy. You know, telepathy was once considered a boring trick of fortune-tellers and Las Vegas magicians, but our physicists have actually been able to replicate limited patterns of telepathy. For example, at Brown University, they put a chip about 1/4 the size of a penny in the brains of completely paralyzed stroke patients and then connected the chip to a laptop. We trained them to read emails, type, solve word puzzles, play video games, and surf the Internet in 3 hours.

□ That sounds a bit creepy, Dr. Kaku... But you see, think about all the vegetative people, who are in a whole-body paralysis and can't do anything... and now they can write emails.

Yes, but let me interject, then what about healthy people connecting to a computer!
One day in the future, people will have a choice.
They'll have a choice. Some people might really want to insert a tiny implant, and they can just think to surf the Internet. These are all possible. Will they have a choice? We hope they'll think more about the meaning of inserting the chip, what inserting the chip means.
Now, if you want to know something creepy, there are really some.
We can use MRI machines and brain scans to see the brain patterns of a liar. When you lie, more energy is needed. To lie, you have to know the truth and make an excuse. These are a lot of energy, and they're easy to see in a brain scan X-ray. In the future, we might be able to see the outlines of thoughts and feelings just by reading the results of brain scan X-rays. Of course, this might go to court.
There's an insurance company that refused to pay an insurance claim because the company thought the person burned down his own house. The insurance company said: "You burned down your own house. Therefore, we won't pay you a cent." The person was very angry and replied: "I'll sue you and present my brain scan X-ray to the court, and I'll prove that I didn't burn down the house."
In the future, these means might all go to court. We might be able to determine whether a suspect is guilty by reading the brain scan X-rays of the suspect.

□ Do you think this will bring us to the first form of planetary civilization?

Once we use a laser to scan the people in this room, what will happen to their souls? Ethical questions follow. If you read a person's brain, then what about privacy? Do we have privacy? Do we want to dissect our thoughts like in science fiction? We have to face these questions. Whether we like it or not, some things are always coming. This book "Physics of the Impossible" tells you what's coming on this road... whether you like it or not.
I also discuss the possibility of interstellar spaceships appearing in the next few decades.
NASA has already started to consider what to carry when sending spaceships to those planets. In fact, I'm a consultant for NASA. I have to examine some proposals for building interstellar spaceships put forward to NASA. It will certainly take decades, not immediately, but we're considering establishing contact with extraterrestrial civilizations in outer space.
This is no longer something in movies; our scientists are seriously researching it. Paul Allen, the Microsoft billionaire, has invested $26 million to build a new telescope (an electronic telescope) to eavesdrop on the conversations between extraterrestrial civilizations, so this is a very big deal. Billionaires have started to invest money in this field, and some people claim that within 25 years, we might have our first contact with another intelligent civilization.

□ I think it's not far off, maybe at some point in the future, when you still remember this conversation, but I think it might be earlier than 25 years.

Maybe... very likely soon.

□ I really want to ask you, what kind of "unbelievable" invention do you want to see created? What do you think humanity needs most?

Ah! What do we need most now? I think we need a way to resolve these ancient conflicts that have existed since human beings appeared, and we also need some fair ways.
Of course, most people would rather want something smaller: more money for medical insurance; less money spent on... fraud in the political field. I hope science and technology can give us a bigger pie. We're not fighting for a small pie and getting into civil wars and aggression. I hope science and technology can give us a bigger pie, so that we don't have to often fight for pie division in sectarian struggles.
But if I have to think about a machine that can really liberate us now, then maybe it's building a time machine to observe the future. We might be able to see how things happen, learn lessons from it, and maybe one day change our future.

□ Will finding these bring us more happiness?

Yes, but also bring distress and pain. It's a trade-off.

□ Well, I can't help but think here: in your unbelievable life and mind, you're looking for ways to create the impossible. Maybe a machine that will appear before us in the future is derived from your invention in Switzerland, where you established a higher realm. I also believe that everything is music, and I hope that one day we can see a higher octave on this planet through outer space and the outer universe.

Yes, I also hope so; I hope I can realize Einstein's dream and read God's mind. I think we're all moving towards some cosmic questions now. Can we solve these questions in our backyard? How wonderful it would be if we could live to see these questions answered by technological breakthroughs.
The superstring theory is promising to lead us down this road.
1<词>,2,3/段\,4{节},5(章)。
Floor 11 Posted 2016-02-03 19:26 ·  中国 海南 三亚 电信
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
10 LOLI Hell 11th Floor

A few months ago, I saw an article about alchemy on Brother yinyfly's place. Then I told him that *Nicholas' Will* is very interesting. Unexpectedly, he mistakenly thought it was a novel. Since you're asking sincerely, I'll be merciful and tell you. To prevent the world from being destroyed, to safeguard world peace... Drifting away...

*Nicholas' Will* mainly tells the legendary life of the great alchemist Nicolas Flamel (French: Nicolas Flamel, English translation: Nicholas Flammel or Nicholas Flamel, and there are more Chinese translations), as well as the mysterious alchemy related to him. So this book is not a made-up novel, but a historical document hovering between historical facts and legends. Because there are a large number of exquisite illustrations in it, there should be no e-books!

←This is Mr. Nicolas Flamel, who was born in 1330, was originally a scribe on the street of notaries in France, a great philanthropist, a great alchemist, and the originator of the Philosopher's Stone! His sudden wealth, the mysterious patterns left in the Cemetery of the Innocents, the empty coffin, and the autobiography two hundred years later all tightly connect this seemingly weak old man with alchemy!
←The sixth of the seven pictures of inherited alchemy recorded in *The Book of Abraham the Jew* that directly led Mr. Nicholas to embark on the path of alchemy. Although when we look at these pictures that contain the truth of the world, we are as confused as reading *Relativity*, it is not difficult to see that there is an essential difference between alchemy and that in *Fullmetal Alchemist*.
←Familiar? Surprised? Your jaw dropped on the keyboard, right! Yes, this pattern looks very much like the logo of the World Health Organization, right! Thank you all for sending me a house. Although you only sent one brick each... Well, I know you thought of the logo designed by Hiromu Arakawa. This is also one of the seven pictures of inherited alchemy recorded in *The Book of Abraham the Jew*.
←One of the earliest alchemical documents introduced to Europe, *The Emerald Tablet*: the short 13 sentences unveiled the prelude to alchemy stepping onto the historical stage. The one on the left was translated by some Latin researchers in the early 20th century according to a 12th-century Latin manuscript, and the one on the left was translated by a cow in 1680. What? Which cow? It's the cow 123's cow. Still don't know? It's Isaac Newton! But Newton's那份 seems to be in Old English, which is hard to understand!
The mysterious pattern left by Mr. Nicholas on the wall of the Fourth Mortuary in the Cemetery of the Innocents on the Saint-Denis Street in Paris as mentioned before. *Interpretation of the Incomprehensible Symbols Carved by Nicolas Flamel on the Fourth Vault of the Cemetery of the Innocents in Paris* was first published in Paris in 1612, and the author signed as Nicolas Flamel. If there is no doubt here, please scroll back to the first picture and take a look again. The long name is obviously added by the editor, and the author uses the first person to tell the arduous process of his search for the Philosopher's Stone and exploration of truth, and at the same time interprets the mysterious mural and the mural revealing the manufacturing process of the Philosopher's Stone.
←This is the manuscript of *Interpretation of the Incomprehensible Symbols Carved by Nicolas Flamel on the Fourth Vault of the Cemetery of the Innocents in Paris* translated by the cow in 1693 now collected in the Massachusetts Institute of Technology. What? Ask again what the cow is? Go back and read it yourself! Newton's enthusiasm for alchemy is not only limited to his "inactive" later period. Some people think that Newton had already obtained inspiration from *Interpretation of the Incomprehensible Symbols Carved by Nicolas Flamel on the Fourth Vault of the Cemetery of the Innocents in Paris* and then derived the theory of gravity and light! So Newton is called "the cow"! No, it's "the first scientist, the last magician"!
The zodiac has independent meanings in alchemy. There are also many such pictures in *Nicholas' Will*, for example, the anthropomorphic seven metals from *Alchemy of Nicolas Flamel* by Danny Morier in 1772 in Brother yinyfly's article.
←Mr. Nicholas' former residence. Left is in 1990, right is in 2004. Now it's "Nicolas Flamel Restaurant". Remember that the Philosopher's Stone in *Fullmetal Alchemist* was also recorded in the recipe!
←Great faces. Alchemist's genealogy. In addition to that very powerful cow, there are many people who also have very high attainments in other fields, such as the chemist Boyle who is also included in it! What? You ask me who is in the lower right corner of the right picture? Since you're asking sincerely, I'll be merciful and tell you. To prevent the world from being destroyed, to safeguard world peace... It's a back照应嘛... OTZ
That's all I want to say!
1<词>,2,3/段\,4{节},5(章)。
Floor 12 Posted 2016-02-03 19:43 ·  中国 海南 三亚 电信
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
1<词>,2,3/段\,4{节},5(章)。
Floor 13 Posted 2016-02-03 20:27 ·  中国 海南 三亚 电信
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
1<词>,2,3/段\,4{节},5(章)。
Floor 14 Posted 2016-02-06 07:25 ·  中国 海南 移动
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
freebasic
Edit Entry
This entry lacks an abstract, basic information section, and entry classification. Please supplement relevant content to improve the entry! Edit Now >>
Table of Contents
1 Attributes
2 Features
3 Disadvantages
1 Attributes Edit
【Language】FreeBASIC, the dark horse in the BASIC language world

Users who have learned QuickBASIC can get started with the BASIC language,

Completely free and open source, can generate high-quality machine code, cross-platform,

FreeBASIC is like its name, free and based on the established BASIC syntax,

2 Features Edit
Easy to learn and use is its advantage, but it is not only simple, but also very powerful in function.

- Almost supports all original QB instructions, and has many additional functions

- Generates fast and high-quality machine code without relying on virtual machines such as VM

- Completely free, including source code, and the compiled program has no licensing issues

- Supports multiple platforms such as MS-DOS/Win32/Linux, and can also compile GUI programs

- Has support from many third-party function libraries (Allegro/SDL.. and DirectX/Win32API)

- Supports Unicode, making it very easy to use Chinese

- It is very easy to compile EXE/OBJ/LIB/DLL to be used with other languages

3 Disadvantages Edit
- Code optimization has not been fully completed

Official homepage http://www.freebasic.net/

FBEdit http://fbedit.freebasic.net/

FBIde http://fbide.freebasic.net

FreeBASIC is a free and open-source 32-bit BASIC compiler. It can run on Windows (32-bit), protected-mode DOS, and Linux (x86) systems. It was originally developed as a code-compatible, free and open-source replacement for Microsoft QuickBASIC, but now it has rapidly grown into a powerful development tool. The default installation already includes the following function libraries: Allegro, SDL, OpenGL, Gtk, Windows API, etc.

In addition to being highly compatible with Microsoft QuickBASIC in terms of syntax, FreeBASIC has added some new features, such as pointers, unsigned data types, inline assembly, preprocessors, etc.

FreeBASIC is a self-hosting compiler (its compiler is written in FreeBASIC language), developed by V1ctor.
------------------------------------------------------------------------
BASIC
Edit Entry
This entry lacks a basic information section and entry classification. Please supplement relevant content to improve the entry! Edit Now >>

BASIC (Beginners' All-purpose Symbolic Instruction Code, also translated as Begic), meaning "Beginner's General Symbolic Instruction Code", is a programming language designed for beginners. In terms of microcomputers, because the BASIC language can give full play to the operating functions of microcomputers, BASIC has long become one of the main languages of microcomputers.
Table of Contents
1 Introduction
2 History
Early Stage
Microcomputer
Structured
Visual
3 Name
4 About
5 Basic Commands
6 Language Features
1 Introduction Edit
Beginner's All-purpose Symbolic Instruction Code (Beginner's General Symbolic Instruction Code), initially written as BASIC by the author, later widely called Basic by Microsoft. The BASIC language was created by professors John G. Kemeny and Thomas E. Kurtz of Dartmouth College in the mid-1960s. Due to its excellent intention and the simple and easy-to-learn basic characteristics of the BASIC language, it quickly became popular. Almost all small, micro and home computers, and even some large computers, provide users with the ability to write programs in this language. In terms of microcomputers, because the BASIC language can give full play to the operating functions of microcomputers, BASIC has long become one of the main languages of microcomputers.

With the rapid development of computer science and technology, especially the widespread use of microcomputers, computer manufacturers have continuously expanded the functions on the original BASIC, and various BASIC versions have emerged, such as TRS-80 BASIC, Apple BASIC, GWBASIC, IBM BASIC (that is, BASICA), True BASIC. At this time, BASIC has developed from an early small and simple learning language to a powerful use language. Many of its functions can already be compared with other excellent computer high-level languages, and some functions (such as drawing) even exceed other languages.

Generally, natural human languages have standard languages and dialects, and computer languages are also like this. Many computers have the BASIC language, but their syntax, rules, and functions are not exactly the same. The same computer may also have different versions of the BASIC language or different brands of BASIC languages produced by different software development companies. However, everyone has consistently inherited the basic form and spirit designed by the creators of BASIC, and respectively given unique design methods and added some functions.

2 History Edit
Early Stage
The language function was very weak, with very few statements, only 14 statements, and later developed to 17 statements, which is the so-called "basic BASIC". This period of BASIC language was mainly used on minicomputers and executed in compiled form.

Microcomputer
In the 1970s, BASIC developed into a widely used general language. It was also in this decade that the microcomputer was born. The first microcomputer equipped with the BASIC language was Bill Gates, the president of Microsoft Corporation. At that time, he was only 19 years old. Driven by Bill Gates' first microcomputer BASIC, various computers were successively equipped with the BASIC language. Due to different models, their extensions to the basic BASIC language were also different, leading to the situation that BASIC language programs could not be compatible with each other. The BASIC language in this period began to use the interpretation execution method, which facilitated users' maintenance of the program.

Structured
The idea of structured programming emerged in the 1970s. Its main idea is to try to make the program execute in the traditional writing order as much as possible, reduce the jump between statements, adopt modular design, and each module completes a relatively simple function. Structured programs can increase the readability of the program.

In the mid-1980s, the American National Standards Institute (ANSI) proposed a new BASIC standard draft based on the idea of structured programming. After that, some structured BASIC languages appeared, mainly Quick BASIC, True BASIC, etc. They not only fully meet the requirements of structured and modular programming, but also retain the advantages of easy learning, easy use, and easy maintenance of the BASIC language. At the same time, they provide both interpretation execution and compilation execution methods.

Visual
In the mid-1980s, Microsoft Corporation launched the Windows operating system, which provides a graphical user interface. Through the mouse, window, menu, etc., to control the computer, making the operation more intuitive and simple, and making it easier and more convenient to use the computer.

The BASIC language based on the Windows operating system is Visual BASIC (meaning "visual BASIC"), developed by Microsoft Corporation of the United States. It was launched by Microsoft Corporation in 1991 and is a powerful software development tool. With it, applications with a good user interface can be designed. Visual BASIC was highly valued as soon as it appeared, with great development potential. Bill Gates claimed: "Visual BASIC is the best example to meet the challenges of computer programming."

In 1975, Bill Gates founded Microsoft and successfully ported the compiler of the Basic language to the ALR computer using the Intel processor. When IBM selected Microsoft to create the PC operating system in 1982, it also selected Microsoft's Basic as its computer's ROM-Basic. Microsoft also included GW-BASIC, QBasic, and other then-best Basic interpreters in its released DOS operating system for free. Quick BASIC was launched by Microsoft Corporation in 1987.

In 1991, with the release of MS-DOS 5.0, Microsoft Corporation also launched a simplified version of Quick BASIC, QBASIC, and provided it to users for free as part of the operating system. Since the appearance of the Windows operating system, the graphical user interface (GUI) BASIC language (that is, Visual Basic) has been widely used.

Visual Basic.NET was launched in 2001

Visual Basic.NET 2003 was launched in 2003

Visual Basic 2005 was launched in Visual Studio 2005 on November 7, 2005.

The BASIC language was originally created in the form of an interpreter, and many different named versions have evolved, such as: BASICA, GW-BASIC, MBASIC, TBASIC, etc. Microsoft Corporation also launched QuickBASIC in the MS-DOS era and gradually improved it to have both interpretation and compilation translation methods. When Windows became popular in 1988, Microsoft Corporation launched Visual Basic for Windows, which became a unique easy-to-learn and use programming language in the Windows operating environment. Microsoft Corporation also specially developed Visual Basic for MSDOS for MS-DOS users. Microsoft's early rise was largely due to the BASIC language.

Programs compiled by Visual Basic 6.0 can run smoothly under Windows 7 and can also run smoothly under the latest operating system Windows 8 launched by Microsoft.

3 Name Edit
Beginner's All-purpose Symbolic Instruction Code (Beginner's General Symbolic Instruction Code), initially written as BASIC by the author, later widely called Basic by Microsoft.

The BASIC language was originally a high-level language created for college students on campus, with the purpose of making it easy for college students to use computers. Although the initial BASIC only had 16 statements, because BASIC was relatively easy to learn at that time, it quickly moved from the campus to the society and became the first choice language for beginners to learn computer programming.

In 1975, Bill Gates founded Microsoft and successfully ported the Basic language's compiler to the ALR computer using the Intel processor. When IBM selected Microsoft to create the PC operating system in 1982, it also selected Microsoft's Basic as its computer's ROM-Basic. Microsoft also included GW-BASIC, QBasic, and other then-best Basic interpreters in its released DOS operating system for free.

Visual Basic.NET was launched in 2001

Visual Basic.NET 2003 was launched in 2003

Visual Basic.NET 2005 was launched in Visual Studio.NET 2005 on November 7, 2005.

There is also Visual Basic.NET 2008 in Visual Studio.NET 2008

4 About Edit
BASIC was developed by American scientist Thomas Kurtz in May 1965. More than 10 years later, Bill Gates (the former president of Microsoft Corporation) ported it to the PC. For more than 30 years, the BASIC language has been the most widely used high-level language for beginners to learn computer languages. It can perform numerical calculations, drawing, playing music, and has very powerful functions, and it is very easy to learn.

5 Basic Commands Edit
PRINT: Display content or result

INPUT: Type in

LET: Assign value

GOTO: Unconditional transfer

FOR TO...NEXT: Loop

IF THEN ELSE: Condition

DO WHILE...LOOP: Conditional loop

END: End

RUN: Run

CLS: Clear screen

6 Language Features Edit
1. Simple and easy to learn: Most of the words used in the BASIC language are the original meaning or abbreviation of English words, and the writing of operation symbols and expressions is also similar to that in mathematics. There are only 17 standard BASIC statements.

2. Interactive: People can "communicate" with the computer through the keyboard and display screen. When running the program, the computer will display the syntax errors and the attributes of the errors in the program, allowing the user to modify them.

3. Wide application: It can not only perform scientific calculations but also data processing, etc.

4. Two execution methods: Interpretation method and compilation method. Among them, the interpretation method can run the program while entering it, which is very suitable for beginners.

BASIC is the abbreviation of Beginner's All-purpose Symbolic Instruction Code, meaning the Beginner's General Symbolic Instruction Code language. It was a language system designed by professors Thomas and John G. Kemeny of the United States on the basis of the Fortran language in 1964. This simple and easy-to-learn programming language only had 17 statements, 12 functions, and 3 commands at that time, which is generally called basic BASIC now.
--------------------------------------------------------------------------
quick basic
Edit Entry
This entry lacks an abstract and entry classification. Please supplement relevant content to improve the entry! Edit Now >>
Chinese Name Quick Basic
Nature BASIC variant
English Name Beginner's All-purpose Symbolic Instruction Code
Unit Microsoft Corporation of the United States
Table of Contents
1 Overview
2 Improvements
3 Common Syntax
Conditional Statements
Loop Structure Statements
Basic Statements
Variables
Simple Double Loop in the Application of Bubble Sort Program
Several Common Operation Statements and Functions
Exercises for Beginners
4 Shortcuts
5 Examples
1 Overview Edit
QBASIC is a variant of the BASIC (Beginner's All-purpose Symbolic Instruction Code) language, developed by Microsoft Corporation of the United States and launched with MS-DOS 5.0 in 1991. It cannot be compiled into an independent executable file. The source code is first compiled into intermediate code in the integrated development environment (IDE), and then the intermediate code is interpreted and executed in the IDE. It is designed to replace GW-BASIC and is integrated in MS-DOS 5.0 and its higher versions (including Windows 95). QBASIC is based on QuickBASIC 4.5 previously launched by Microsoft, but the compilation and connection parts of the latter are removed.

Microsoft no longer integrates QBASIC in newer versions of Windows. However, users of Windows 98 can find it in the \\TOOLS\\OLDMSDOS directory on the CD, and in Windows 95, it is stored in the \\OTHER\\OLDMSDOS directory. Microsoft's technical support for it is only valid for MS-DOS licensed users.

QBASIC has a commendable integrated development environment and a powerful integrated debugger, which all made people耳目一新 at that time. Until today, QBASIC is still the theme of many programming books for beginners.

2 Improvements Edit
Similar to Quick BASIC but different from earlier implementation versions of Microsoft's other BASIC, QBASIC is a structured programming language. Compared with GW-BASIC, the main improvements of QBASIC are:

Expanded variable and constant types

Variable name length: 40 characters

Added long integer and fixed-length character type variables

Can define numerical constants and string constants

Subroutines and functions as separate modules

No need for line numbers

Note: Quick BASIC is abbreviated as QB, which is a compiled language; qbasic is an interpreted language, and has the same syntax as Quick BASIC. At the same time, for the sake of time, the PRINT statement in QBASIC can be directly replaced with?.

3 Common Syntax Edit
Conditional Statements
Line if statement: IF condition THEN statement group ELSE statement group 2

Block if statement

IF condition THEN

statement group

ELSE

statement group 2

END IF

The IF statement can also be used in this way

IF condition THEN

statement group

ELSEIF condition THEN

statement group 2

END IF

Multi-branch statement SELECT usage:

SELECT CASE variable or string

CASE situation 1

statement group

CASE situation 2

statement group 2

END SELECT

Loop Structure Statements
1. Counting loop

for control variable = initial value to final value 'step statement can be present or not. If there is no step statement, the step size is +1

statement body

next control variable

2. While loop

There are two formats:

(1)

WHILE condition

loop body

WEND

(2)

DO WHILE condition

loop body

LOOP

3. Until loop

DO

loop body

LOOP UNTIL condition

Basic Statements
CLS: That is, Clean the screen, clear the screen

Input statement: input "display content", variable name list

Or change "," to ";", after modification, there will be an extra "?" when entering

Output statement: print "display content", variable name list 1, "display content", variable name list 2...

When changing, the space between the two contents is 1 space, otherwise it is about 14 spaces, and the last one without a semicolon is a line break.

Assignment statement: assigned variable = expression 'let can be present or not

Definition of one-dimensional array: dim variable name (subscript)

Definition of two-dimensional array: dim variable name (subscript 1, subscript 2)

Code example (assignment, summation operation, and display result)

A=10

B=20

C=A+B

Print C

END

Variables
Variable length is less than or equal to 40, no keywords are allowed, such as Let

Numbers: such as 15%, -32768, 215654#, 2.0158e+15, 8.545646d+20, -18.75, etc. Variable names are such as a variable name

Strings: such as "15%", "abc", etc. Variable names are such as a$ variable name

Supplement: In QB, geometric drawing can also be done. The specific usage is as follows (see the internal help of QB for details)

SCREEN 12'639*479 16-color graphic mode

CIRCLE(100,150),10,4'Draw a circle with radius 10 at x coordinate 100 and y coordinate 150 with color 4 (red).

SYSTEM

Simple Double Loop in the Application of Bubble Sort Program

CLS

DIM n AS INTEGER

INPUT n

DIM a(n)

FOR i = 1 TO n

INPUT a(i)

NEXT i

FOR i = 1 TO n

FOR j=1 TO i-1

IF a(j) > a(j+1) THEN SWAP a(j), a(j+1)

NEXT j

NEXT i

FOR i = 1 TO n

PRINT a(i)

NEXT i

END

Several Common Operation Statements and Functions
Addition operation:

Sum of AB = A+B

Subtraction operation:

Difference of AB = A-B

Multiplication operation:

Product of AB = A*B

Division operation

Quotient of AB = A/B

Exponentiation operation

A to the power of B = A^B

Square root operation

Square root of A = SQR(A)

Swap variables

SWAP value A, value B

Exercises for Beginners
1. Input 20 numbers, find their maximum, minimum and average.

2. Among 1-500, find all integers that can simultaneously satisfy being divided by 3 with a remainder of 2, divided by 5 with a remainder of 3, and divided by 7 with a remainder of 2;

3. If a number is the same when read from the left and the right, it is called a palindrome number. For example, 686 is a palindrome number. Find all palindrome numbers within 1000.

4. Given the sequence 1, 5, 12, 22, 35,... Find the 20th number.

5. Input an integer greater than 1, and print out its prime factorization. For example, if 75 is input, print: "75=3*5*5".

6. Input 10 positive integers, calculate their sum and sum of squares;

7. Input 20 integers, count the number of positive, negative and zero;

8. Output numbers between 1-999 that can be divided by 3 and have at least one digit as 5;

9. There is a six-digit number, its units digit is 7. Now move the units digit to the first digit (hundred thousands place), and the other digits remain unchanged to get a new six-digit number. If the new number is 4 times the old number, find the original six-digit number.

10. There is such a six-digit number labcde. Multiply it by 3 to become abcdel. Program to find this number.

11. Try to find 6 prime numbers that are less than 160 and form an arithmetic sequence.

1-1/3+1/5-1/7+……until the absolute value of a certain term is less than 10^-6

Additional:

We use a positive integer sequence to represent the height of a place. When the height of a place is a sequence that rises one by one,

we call it a staircase. For example, 4, 5, 6, 7, 8 is a staircase with a length of 5. Now given a positive integer sequence,

please find its first longest staircase and output it. If there is no staircase, output "No".

Example of running result:

Please enter the length of the sequence: 8

Please enter the sequence: 2 3 2 3 4 4 5 6

The result is 2 3 4

4 Shortcuts Edit
Ctrl+C+Break: Interrupt the running program;

F5: Run the program;

Shift+F5: Re-run the program from the first statement;

F4: When the program is interrupted and running, view the running result screen, and press F4 again to switch back to the code screen;

F1: Get help.

F8: Step by step run

F9: Breakpoint (same as QB stop statement, press F5 to continue running)

5 Examples Edit
【1】A kilogram of mushrooms in the vegetable market is 7.5 yuan. Write a program to input the weight from the keyboard, and the computer automatically calculates the total price.

INPUT X

zj=7.5*X

PRINT zj

END

【2】High-precision multiplication program

CLS

INPUT a$

INPUT b$

la = LEN(a$)

lb = LEN(b$)

lc = la + lb

DIM a(la), b(lb), c(lc)

FOR i = 1 TO la

a(i) = VAL(MID$(a$, la + 1 - i, 1))

NEXT i

FOR i = 1 TO lb

b(i) = VAL(MID$(b$, lb + 1 - i, 1))

NEXT i

FOR i = 1 TO la

FOR j = 1 TO lb

x = a(i) * b(j): w = i + j - 1

c(w) = c(w) + x MOD 10

c(w + 1) = c(w + 1) + c(w) \ 10 + x \ 10

c(w) = c(w) MOD 10

NEXT j

NEXT i

DO WHILE c(lc) = 0

lc = lc - 1

LOOP

FOR i = lc TO 1 STEP -1

PRINT USING "#"; c(i);

NEXT i

END
-----------------------------------------------------
gw-basic
Edit Entry
This entry lacks an abstract image and entry classification. Please supplement relevant content to improve the entry! Edit Now >>
GW-BASIC is a dialect version of the advanced programming language BASIC. Regarding the meaning of GW, there are currently three statements. One is that it is named after Greg Whitten, an early programmer of Microsoft. One is that it is named after Gates, William, the founder of Microsoft. There is also a statement that it is a joke name given by the developers, gee-whiz (two English onomatopoeias, similar to the sound of yelling livestock in Chinese). One thing is certain, this version of BASIC was first developed by Microsoft for Compaq in 1984. In November of this year, Microsoft first provided OEM versions of DOS for computer manufacturers other than IBM. GW-BASIC was introduced as a component of MS-DOS. First provided OEM versions of DOS for computer manufacturers other than IBM. GW-BASIC was introduced as a component of MS-DOS.
Chinese Name gw-basic
Time 1984
Nature Programming language
Belongs to Microsoft
Table of Contents
1 Historical Status
2 Features
3 Syntax
1 Historical Status Edit
The development of Basic has gone through three stages:

Unstructured Basic language, such as: gw-Basic, MS Basic

Structured Basic language, such as: True Basic, Turbo Basic, Quick Basic

Object-oriented programming language, that is, Visual Basic

gw-basic belongs to unstructured basic, that is, the earliest basic language, without loop and while loop statements, and has great limitations.

2 Features Edit
GW-BASIC is completely compatible with BASICA provided by Microsoft for IBM PC. The difference is that the latter depends on the BASIC interpreter in ROM, while the former does not need it. Therefore, GW-BASIC can run on many IBM PC compatible machines, which expands its scope of use with the promotion of PC compatible machines. The first widely circulated GW-BASIC version number is 2.0, and the last released GW-BASIC version number is 3.23, probably in 1988. From then on, unless otherwise specified, the GW-BASIC we generally refer to refers to this version.

GW-BASIC runs slowly, which is largely because it is an interactive development tool - this development model was first proposed by Dartmouth University, the birthplace of BASIC. Like many early BASIC dialects, GW-BASIC lacks many syntactic components required for structured programming, but it is flexible enough. In addition, it has many drawing statements and some simple sounding statements, which are enough for a programmer to use it to develop a simple game software, commercial software or such things. It can run on most PCs, which provides a cheap way for those who want to become programmers to learn how to program.

3 Syntax Edit
GW-BASIC has a simple integrated development environment (IDE). All program lines must have a line number, and statements without line numbers are considered commands that need to be executed immediately. In the user interface, except for the function shortcut key description at the bottom of the screen and the copyright statement at the top, the other parts are used to display and write statements. The standard save format of the source file is a unique binary compression format of GW-BASIC, but it also provides an option that allows developers to save the source file in ASCII text file format. The GW-BASIC IDE provides the following common commands: RUN (execute the current source code), LOAD (load the source code from the disk), SAVE (save the source code to the disk), LIST (display the content of the opened source file), SYSTEM (return to the operating system). They can all appear on the source program code line, but except for SYSTEM, the above usage is still quite rare.

As mentioned earlier, GW-BASIC has very poor support for structured programming methods, so it is a great improvement for GW-BASIC programmers to be able to write programs with good structure with it. In GW-BASIC, the IF/THEN/ELSE conditional statement must be written in one line, although the WHILE/WEND has allowed multiple lines of code to be included; custom functions can only be written in the form of a one-line statement like DEF FNf(x) = <mathematical function about x> (for example, DEF FNLOG(base,number)=LOG(number)/LOG(base)); variables are usually determined by a type symbol at the end of the variable name: A$ means it is a string, A% means it is an integer, etc.; by using keywords such as DEFINT, DEFSTR, etc., the default type can be defined for a group of variables using the same first letter; the type of other variables is defaulted to single-precision floating point number.

Many GW-BASIC programmers are untrained, and they often cannot see the benefits of writing simple-structured programs, so there is a phenomenon of abuse of the GOTO statement. They are often unwilling to use structured statements that can perform the same function. See spaghetti code.

GW-BASIC supports game joystick and light pen input devices, but not mice. It can read and write disk files, LPT ports and COM ports, and can also handle port event traps, but cannot handle tape devices. It can also drive the standard built-in speaker of IBM PC and its compatible machines to make sounds by using the PLAY statement or SOUND statement.
1<词>,2,3/段\,4{节},5(章)。
Floor 15 Posted 2016-02-06 21:33 ·  中国 海南 移动
超级版主
★★★★
Credits 3,673
Posts 2,020
Joined 2016-02-01 00:00
10-year member
UID 181465
Gender Male
Status Offline
Level triggering means that a specified action is only performed when there is a high level (or low level). Edge triggering means that when there is a transition from high level to low level or vice versa, this transition process triggers an action. The rising edge, as the name implies, is the moment (process) when the low level transitions to the high level. For example, in the figure _______
____/, the / part is the rising edge.

______
\_____ ,in this figure, the \ part is the falling edge
---------------------------------------------------------------

Level refers to the logical 0 and 1 triggers. Edge refers to the pulse transition trigger, which logically is 0-1 or 1-0, which is what the person upstairs meant.

To put it in simple terms, level is voltage. High level is high voltage, low level is low voltage. High level triggering means triggering when the voltage is high. Edge triggering means triggering when the voltage changes from high to low or from low to high. Rising edge triggering means triggering when the voltage changes from low to high. Falling edge triggering means triggering when the voltage changes from high to low.

luts3702 2006-05-13 08:32
Computers process digital signals, that is, binary numbers 0 and 1. This is generally achieved by using the two states of turning on and off of electronic devices. When the electronic device is cut off, the output potential is raised, which represents the digital 1, so it is also called high level. When the device is turned on, it represents the digital 0, so it is also called low level.

Shared by Ren Jilan, an expert in science education classification

The current location you are in: Home > Basic Knowledge > Analog > Common Questions in Analog Technology
What are TTL level and CMOS level? Differences?
Source: 21ic Author:
Keywords: CMOS TTL level
What is TTL level

TTL level signals are most commonly used because the binary system is generally used for data representation. +5V is equivalent to logic "1", and 0V is equivalent to logic "0". This is called the TTL (transistor-transistor logic level) signal system, which is the standard technology for communication between various parts inside devices controlled by computer processors.

TTL level signals are ideal for data transmission inside devices controlled by computer processors. First, the data transmission inside devices controlled by computer processors has low requirements for power supply and low heat loss. In addition, TTL level signals are directly connected to integrated circuits without the need for expensive line driver and receiver circuits. Moreover, the data transmission inside devices controlled by computer processors is carried out at high speed, and the operation of the TTL interface can just meet this requirement. In most cases, TTL-type communication uses parallel data transmission methods, but parallel data transmission is not suitable for distances exceeding 10 feet. This is due to reasons related to reliability and cost. Because there are problems of phase deviation and asymmetry in the parallel interface, these problems all have an impact on reliability; in addition, for parallel data transmission, the cost of cables and connectors is also higher than that of serial communication methods.

The level of TTL circuits is called TTL level, and the level of CMOS circuits is called CMOS level.

The full name of TTL integrated circuits is transistor-transistor logic integrated circuits (Transistor-Transistor Logic). There are mainly five series: 54/74 series standard TTL, high-speed TTL (H-TTL), low-power TTL (L-TTL), Schottky TTL (S-TTL), and low-power Schottky TTL (LS-TTL). The standard TTL input high level is at least 2V, the output high level is at least 2.4V, the typical value is 3.4V, the input low level is at most 0.8V, the output low level is at most 0.4V, and the typical value is 0.2V. For S-TTL, the input high level is at least 2V, the output high level is at least 2.5V for category I, 2.7V for categories II and III, the typical value is 3.4V, the input low level is at most 0.8V, and the output low level is at most 0.5V. For LS-TTL, the input high level is at least 2V, the output high level is at least 2.5V for category I, 2.7V for categories II and III, the typical value is 3.4V, the input low level is at most 0.7V for category I, 0.8V for categories II and III, the output low level is at most 0.4V for category I, 0.5V for categories II and III, and the typical value is 0.25V. The power supply VDD for TTL circuits is only allowed to be within the range of +5V ± 10%, and the fan-out number is less than 10 TTL gate circuits.

CMOS integrated circuits are the English abbreviation of complementary symmetry metal oxide semiconductor integrated circuits. Many basic logic units of the circuit are connected in a complementary symmetry form using enhanced PMOS transistors and enhanced NMOS transistors, and the static power consumption is very small. The power supply voltage VDD range of COMS circuits is relatively wide, and it can work normally at +5--+15V. The voltage fluctuation is allowed to be ±10. When the output voltage is higher than VDD-0.5V, it is logic 1. When the output voltage is lower than VSS+0.5V (VSS is the digital ground), it is logic 0. The fan-out number is 10--20 COMS gate circuits.

TTL level signals are most commonly used because the binary system is generally used for data representation. +5V is equivalent to logic "1", and 0V is equivalent to logic "0". This is called the TTL (transistor-transistor logic level) signal system, which is the standard technology for communication between various parts inside devices controlled by computer processors. TTL level signals are ideal for data transmission inside devices controlled by computer processors. First, the data transmission inside devices controlled by computer processors has low requirements for power supply and low heat loss. In addition, TTL level signals are directly connected to integrated circuits without the need for expensive line driver and receiver circuits. Moreover, the data transmission inside devices controlled by computer processors is carried out at high speed, and the operation of the TTL interface can just meet this requirement. In most cases, TTL-type communication uses parallel data transmission methods, but parallel data transmission is not suitable for distances exceeding 10 feet. This is due to reasons related to reliability and cost. Because there are problems of phase deviation and asymmetry in the parallel interface, these problems all have an impact on reliability; in addition, for parallel data transmission, the cost of cables and connectors is also higher than that of serial communication methods. The CMOS level and TTL level: The CMOS level voltage range is 3-15V. For example, when the 4000 series is powered by 5V, the output is above 4.6 as high level, and the output is below 0.05V as low level. The input is above 3.5V as high level, and the input is below 1.5V as low level. For TTL chips, the power supply range is 0-5V, and 5V is common. For example, the 74 series is powered by 5V. The output is above 2.7V as high level, and the output is below 0.5V as low level. The input is above 2V as high level, and below 0.8V as low level. Therefore, there is a problem of level conversion between CMOS circuits and TTL circuits to match the level thresholds of both.

Differences between TTL level and CMOS level:

(1) TTL high level is 3.6-5V, low level is 0V-2.4V.

CMOS level Vcc can reach 12V.

The output high level of CMOS circuits is about 0.9Vcc, and the output low level is about 0.1Vcc.

The input ends of CMOS circuits that are not used cannot be left floating, which will cause logic confusion.

The input ends of TTL circuits that are not used are floating as high level.

In addition, the power supply voltage of CMOS integrated circuits can vary within a large range, so the requirements for the power supply are not as strict as those for TTL integrated circuits.

They can be compatible with TTL levels.

(2) TTL level is 5V, and CMOS level is generally 12V.

Because the power supply voltage of TTL circuits is 5V, and the power supply voltage of CMOS circuits is generally 12V.

The 5V level cannot trigger CMOS circuits, and the 12V level will damage TTL circuits, so they cannot be compatible and matched with each other.

(3) TTL level standard:

Output L: <0.8V; H:>2.4V.

Input L: <1.2V; H:>2.0V.

The output low level of TTL devices should be less than 0.8V, and the high level should be greater than 2.4V. For input, if it is lower than 1.2V, it is considered 0, and if it is higher than 2.0, it is considered 1.

CMOS level:

Output L: <0.1*Vcc; H:>0.9*Vcc.

Input L: <0.3*Vcc; H:>0.7*Vcc.

Generally, whether the pins of single-chip microcomputers, DSPs, FPGAs and others can be directly connected. In general, those with the same voltage can be connected, but it is better to carefully check the values of VIL, VIH, VOL, VOH in the technical manual to see if they can be matched (VOL should be less than VIL, and VOH should be greater than VIH, which refers to within a connection). Some are okay in general applications, but the parameters are a bit mismatched, and they may not be stable in some cases, or devices of different batches may not operate.

For example: The output of 74LS devices is connected to 74HC devices. In general, they can all operate well, but in terms of parameters, they are mismatched, and they cannot operate in some cases.

74LS and 54 series are TTL circuits, and 74HC is a CMOS circuit. If their numbers are the same, the logic functions are the same, but the electrical performance and dynamic performance are slightly different. For example, the TTL logic high level is > 2.7V, and the CMOS is > 3.6V. If the previous stage of the CMOS circuit is TTL, there is a hidden unreliable hidden danger, but the opposite is okay.

1. TTL level:

Output high level >2.4V, output low level <0.4V. At room temperature, generally the output high level is 3.5V, and the output low level is 0.2V. The minimum input high level and low level: input high level >=2.0V, input low level <=0.8V, and the noise margin is 0.4V.

2. CMOS level:

The 1 logic level voltage is close to the power supply voltage, and the 0 logic level is close to 0V. And it has a wide noise margin.

3. Level conversion circuit:

Because the values of the high and low levels of TTL and COMS are different (ttl 5v<==>cmos 3.3v), level conversion is required when connecting to each other: that is, using two resistors to divide the voltage, there is nothing complicated. Haha.

4. OC gate, that is, open collector gate circuit, OD gate, that is, open drain gate circuit, must be externally connected with pull-up resistors and power supply to use the switching level as high and low levels. Otherwise, it generally only acts as a switching high voltage and large current load, so it is also called a driving gate circuit.

5. Comparison between TTL and COMS circuits:

1) TTL circuits are current-controlled devices, while coms circuits are voltage-controlled devices.

2) TTL circuits are fast, with short transmission delay time (5-10ns), but high power consumption. COMS circuits are slow, with long transmission delay time (25-50ns), but low power consumption. The power consumption of COMS circuits itself is related to the pulse frequency of the input signal. The higher the frequency, the hotter the chip set, which is a normal phenomenon.

3) Locking effect of COMS circuits:

Due to the input of too much current in COMS circuits, the internal current increases sharply, and the current keeps increasing unless the power supply is cut off. This effect is the locking effect. When the locking effect occurs, the internal current of COMS can reach more than 40mA, and it is easy to burn out the chip.

Defense measures:

1) Add clamping circuits at the input and output ends to prevent the input and output from exceeding the specified voltage.

2) Add decoupling circuits at the power supply input end of the chip to prevent the VDD end from having a momentary high voltage.

3) Add current limiting resistors between VDD and the external power supply, even if there is a large current, do not let it enter.

4) When the system is powered by several power supplies respectively, the switch should be in the following order: when starting, first turn on the power supply of the COMS circuit, then turn on the power supply of the input signal and load; when turning off, first turn off the power supply of the input signal and load, then turn off the power supply of the COMS circuit.

6. Precautions for using COMS circuits:

1) COMS circuits are voltage-controlled devices, and their input total resistance is very large, and they have strong ability to capture interference signals. Therefore, the unused pins should not be left floating, but should be connected with pull-up resistors or pull-down resistors to give them a constant level.

2) When connecting to a low-internal-resistance signal source at the input end, a current-limiting resistor should be connected in series between the input end and the signal source to limit the input current to within 1mA.

3) When connecting long signal transmission lines, a matching resistor should be connected at the COMS circuit end.

4) When a large capacitor is connected at the input end, a protection resistor should be connected between the input end and the capacitor. The resistance value is R=V0/1mA. V0 is the voltage on the external capacitor.

5) If the input current of COMS exceeds 1mA, the COMS may be burned out.

7. Input load characteristics of TTL gate circuits (processing of special cases with resistors at the input end):

1) Floating is equivalent to the input end being connected to a high level. Because at this time, it can be regarded as the input end being connected to an infinite resistance.

2) After connecting a 10K resistor in series at the input end of the gate circuit and then inputting a low level, the input end presents a high level instead of a low level. Because according to the input load characteristics of TTL gate circuits, only when the series resistor connected to the input end is less than 910 ohms, the low level signal input can be recognized by the gate circuit. If the series resistor is larger, the input end will always present a high level. This must be noted. There is no need to consider these for COMS gate circuits.

8. TTL circuits have open collector OC gates, and MOS tubes also have open drain OD gates corresponding to the collector. The output of it is called open drain output. There is leakage current output when the OC gate is cut off, that is, leakage current. Why is there leakage current? That is because when the triode is cut off, its base current is approximately 0, but it is not really 0, and the current passing through the collector of the triode is not really 0, but approximately 0. And this is the leakage current. Open drain output: the output of the OC gate is open drain output; the output of the OD gate is also open drain output. It can absorb a large current, but cannot output current outward. Therefore, in order to be able to input and output current, it needs to be used with the power supply and pull-up resistors together when used. OD gates are generally used as output buffers/drivers, level converters, and to meet the needs of absorbing large load currents.

9. What is a totem pole, and what is the difference between it and an open drain circuit?

In TTL integrated circuits, the output with an output connected with a pull-up transistor is called totem pole output, and the one without is called an OC gate. Because TTL is a triode, the totem pole is two triodes push-pull connected. So push-pull is totem.

Extended reading: Differences and connections between inductors and beads

TTL level

Edited

TTL level signals are most commonly used because the binary system is generally used for data representation. +5V is equivalent to logic "1", and 0V is equivalent to logic "0". This is called the TTL (transistor-transistor logic level) signal system, which is the standard technology for communication between various parts inside devices controlled by computer processors.

Chinese name TTL level English name transistor transistor logic Regulation Binary Also known as transistor-transistor logic level Application Computer processor

Contents

1 Advantages

2 Standards

3 Involved disciplines

▪ With CMOS tubes

▪ Attachment

Advantages

Edited

TTL level signals are ideal for data transmission inside devices controlled by computer processors. First, the data transmission inside devices controlled by computer processors has low requirements for power supply and low heat loss. In addition, TTL level signals are directly connected to integrated circuits without the need for expensive line driver and receiver circuits. Moreover, the data transmission inside devices controlled by computer processors is carried out at high speed, and the operation of the TTL interface can just meet this requirement. In most cases, TTL-type communication uses parallel data transmission methods, but parallel data transmission is not suitable for distances exceeding 10 feet. This is due to reasons related to reliability and cost. Because there are problems of phase deviation and asymmetry in the parallel interface, these problems all have an impact on reliability.

In digital circuits, the level used in circuits composed of TTL electronic components. Level is a voltage range. It is specified that the output high level >2.4V, and the output low level <0.4V. At room temperature, generally the output high level is 3.5V, and the output low level is 0.2V. The minimum input high level and low level: input high level >=2.0V, input low level <=0.8V, and the noise margin is 0.4V.

Standards

Edited

"The full name of TTL integrated circuits is transistor-transistor logic integrated circuits (Transistor-Transistor Logic). There are mainly five series: 54/74 series standard TTL, high-speed TTL (H-TTL), low-power TTL (L-TTL), Schottky TTL (S-TTL), and low-power Schottky TTL (LS-TTL). The standard TTL input high level is at least 2V, the output high level is at least 2.4V, the typical value is 3.4V, the input low level is at most 0.8V, the output low level is at most 0.4V, and the typical value is 0.2V. For S-TTL, the input high level is at least 2V, the output high level is at least 2.5V for category I, 2.7V for categories II and III, the typical value is 3.4V, the input low level is at most 0.8V, and the output low level is at most 0.5V. For LS-TTL, the input high level is at least 2V, the output high level is at least 2.5V for category I, 2.7V for categories II and III, the typical value is 3.4V, the input low level is at most 0.7V for category I, 0.8V for categories II and III, the output low level is at most 0.4V for category I, 0.5V for categories II and III, and the typical value is 0.25V."

Involved disciplines

Edited

"TTL level" is most commonly used in relevant electrical majors, such as: circuits, digital circuits, principles and interface technology of microcomputers, single-chip microcomputers, etc. In digital circuits, there are only two levels (high and low). The high level is +5V, and the low level is 0V. Similarly, CMOS level, 232 level, 485 level, etc. are also widely used.

With CMOS tubes

1. CMOS is composed of field effect tubes, and TTL is composed of bipolar transistors.

2. The logic level range of CMOS is relatively large (3-15V), and TTL can only work at 5V.

3. The difference between the high and low levels of CMOS is relatively large, and the anti-interference ability is strong, while the difference of TTL is small, and the anti-interference ability is poor.

4. CMOS has very low power consumption, and TTL has relatively large power consumption (1-5mA/gate).

5. The working frequency of CMOS is slightly lower than that of TTL, but high-speed CMOS has a speed similar to that of TTL.

Simple understanding:

TTL level, the power supply working voltage of TTL is 5V, so the TTL level is determined according to the power supply voltage of 5V. CMOS level, the power supply working voltage of CMOS is 3V-18V. The power supply working voltage range of CMOS is wide. If the power supply working voltage of your CMOS is 12V, then the input and output level voltages of this CMOS should be suitable for the input and output requirements of 12V. That is, the CMOS level depends on the power supply working voltage you use. It is within the power supply working voltage range of CMOS from 3v to 18V. For specific values, it depends on the power supply working voltage added to the CMOS chip.

Attachment

Detailed explanation of the differences between TTL and cmos

TTL——Transistor-Transistor Logic

HTTL——High-speed TTL

LTTL——Low-power TTL

STTL——Schottky TTL

LSTTL——Low-power Schottky TTL

ASTTL——Advanced Schottky TTL

ALSTTL——Advanced Low-power Schottky TTL

FAST(F)——Fairchild Advanced schottky TTL

CMOS——Complementary metal-oxide-semiconductor

HC/HCT——High-speed CMOS Logic (HCT is compatible with TTL level)

AC/ACT——Advanced CMOS Logic (ACT is compatible with TTL level) (also called ACL)

AHC/AHCT——Advanced High-speed CMOS Logic (AHCT is compatible with TTL level)

FCT——FACT expansion series, compatible with TTL level

FACT——Fairchild Advanced CMOS Technology

1. TTL level:

Output high level >2.4V, output low level <0.4V. At room temperature, generally the output high level is 3.5V, and the output low level is 0.2V. The minimum input high level and low level: input high level >=2.0V, input low level <=0.8V, and the noise margin is 0.4V.

2. CMOS level:

The 1 logic level voltage is close to the power supply voltage, and the 0 logic level is close to 0V. And it has a wide noise margin.

3. Level conversion circuit:

Because the values of the high and low levels of TTL and COMS are different (ttl 5v<==>cmos 3.3v), level conversion is required when connecting to each other: that is, using two resistors to divide the voltage, there is nothing complicated.

4. OC gate, that is, open collector gate circuit, OD gate, that is, open drain gate circuit, must be externally connected with pull-up resistors and power supply to use the switching level as high and low levels. Otherwise, it generally only acts as a switching high voltage and large current load, so it is also called a driving gate circuit.

5. Comparison between TTL and CMOS circuits:

1) TTL circuits are current-controlled devices, while CMOS circuits are voltage-controlled devices.

2) TTL circuits are fast, with short transmission delay time (5-10ns), but high power consumption.

CMOS circuits are slow, with long transmission delay time (25-50ns), but low power consumption.

The power consumption of CMOS circuits itself is related to the pulse frequency of the input signal. The higher the frequency, the hotter the chip set, which is a normal phenomenon.

3) Locking effect of CMOS circuits:

Due to the input of too much current in CMOS circuits, the internal current increases sharply, and the current keeps increasing unless the power supply is cut off. This effect is the locking effect. When the locking effect occurs, the internal current of CMOS can reach more than 40mA, and it is easy to burn out the chip.

Defense measures:

1) Add clamping circuits at the input and output ends to prevent the input and output from exceeding the specified voltage.

2) Add decoupling circuits at the power supply input end of the chip to prevent the VDD end from having a momentary high voltage.

3) Add current limiting resistors between VDD and the external power supply, even if there is a large current, do not let it enter.

4) When the system is powered by several power supplies respectively, the switch should be in the following order: when starting, first turn on the power supply of the CMOS circuit, then turn on the power supply of the input signal and load; when turning off, first turn off the power supply of the input signal and load, then turn off the power supply of the CMOS circuit.

6. Precautions for using CMOS circuits:

1) CMOS circuits are voltage-controlled devices, and their input total resistance is very large, and they have strong ability to capture interference signals. Therefore, the unused pins should not be left floating, but should be connected with pull-up resistors or pull-down resistors to give them a constant level.

2) When connecting to a low-internal-resistance signal source at the input end, a current-limiting resistor should be connected in series between the input end and the signal source to limit the input current to within 1mA.

3) When connecting long signal transmission lines, a matching resistor should be connected at the CMOS circuit end.

4) When a large capacitor is connected at the input end, a protection resistor should be connected between the input end and the capacitor. The resistance value is R=V0/1mA. V0 is the voltage on the external capacitor.

5) If the input current of CMOS exceeds 1mA, the CMOS may be burned out.

7. Input load characteristics of TTL gate circuits (processing of special cases with resistors at the input end):

1) Floating is equivalent to the input end being connected to a high level. Because at this time, it can be regarded as the input end being connected to an infinite resistance.

2) After connecting a 10K resistor in series at the input end of the gate circuit and then inputting a low level, the input end presents a high level instead of a low level. Because according to the input load characteristics of TTL gate circuits, only when the series resistor connected to the input end is less than 910 ohms, the low level signal input can be recognized by the gate circuit. If the series resistor is larger, the input end will always present a high level. This must be noted. There is no need to consider these for COMS gate circuits.

8. TTL circuits have open collector OC gates, and MOS tubes also have open drain OD gates corresponding to the collector. The output of it is called open drain output.

There is leakage current output when the OC gate is cut off, that is, leakage current. Why is there leakage current? That is because when the triode is cut off, its base current is approximately 0, but it is not really 0, and the current passing through the collector of the triode is not really 0, but approximately 0. And this is the leakage current. Open drain output: the output of the OC gate is open drain output; the output of the OD gate is also open drain output. It can absorb a large current, but cannot output current outward. Therefore, in order to be able to input and output current, it needs to be used with the power supply and pull-up resistors together when used. OD gates are generally used as output buffers/drivers, level converters, and to meet the needs of absorbing large load currents.

9. What is a totem pole, and what is the difference between it and an open drain circuit?

In TTL integrated circuits, the output with an output connected with a pull-up transistor is called totem pole output, and the one without is called an OC gate. Because TTL is a triode, the totem pole is two triodes push-pull connected. So push-pull is totem. Generally, the totem pole output has a high level of 400UA and a low level of 8MA.

Entry tags: scientific and technological products, science, books

[ Last edited by zzz19760225 on 2016-2-6 at 22:05 ]
1<词>,2,3/段\,4{节},5(章)。
1 2 3 6 Next ›
Forum Jump: