Write assembly under tc2.0

Source: Internet
Author: User
Reprinted from husubmit
According to the Help System of tc2.0, assembly can be used in tc2.0 by using the ASM Keyword: The format is:
ASM opcode <operands> <; newline>, which is optional in the format of <>. For example:

Main ()
{
Char * c = "Hello, world/n/r$ ";
ASM mov ah, 9; ASM mov dx, C; ASM int 33;
Printf ("You sucessed! /N ");

}
Or:

Main ()
{
Char * c = "Hello, world/n/r$ ";
ASM mov ah, 9
ASM mov dx, c
ASM int 33
Printf ("You sucessed! ");
}
The two formats are actually one. If you use the first style, remember:
Each assembly statement must start with ASM. If a row contains multiple sentences,
Do not forget the semicolon (semicolon) between two sentences ),
However, the semicolon after the last statement assembly (if no other statement is followed) can be dispensable, as in the first example
ASM int 33; do not use the semicolon next to it, because there is no other
But if it is like this:
ASM mov ah, 9; ASM mov dx, C; ASM int 33; printf ("You sucessed! ");
Then ASM int 33; the semicolon next to it will be good, so as to avoid compilation errors!
This is quite like the C language.

Another format is
ASM {assembly language statement}. This format should be widely used.
Their example is as follows (the statement arrangement format is the same as the preceding two types ):
ASM {
MoV ax, var1
Add ax, var2
......
}
However, this format is not supported by tc2.0!
Only later TC ++ 3.0 and later ide support!

Use of tools:

When running TCC. EXE, it should be under an independent dos shell (Don't be afraid, this is not a new thing, I mean
Yes, it is not running in DOS shell such as tc. I once lost in this case. When I found out, I wanted to beat my computer.

It's okay. Otherwise, there will be no such file .)


Before Running TCC, you need to use a command.com or another program to change the environment. Otherwise, the prompt information during compilation is not displayed, and only a few lines are blank.
After running command.com


Once your C source file contains these good things, you must use the command-line of TCC. EXE for compilation. The specific command parameter TCC. EXE is provided and will not be elaborated here.

The simplest is: TCC [path] C source file name (using this method, TCC will automatically call tasm. EXE and tlink. and enables tlink. find the desired. EXE file correctly. OBJ and. lib File

If you perform one-step compilation, you may encounter many problems, mainly tlink. EXE is not found by itself. OBJ and. lib file. You can create one by yourself. BAT file, if you want to specify. the/L parameter can be used for the Lib file directory. An example is provided later in the article ). but you should pay attention to it. Check whether there is tasm in your TC directory. EXE file, confirm tasm. the EXE version must be later than 2.0, and whether it can be backward compatible. however, in most cases, the TC directory does not contain tasm. EXE, or the version is not normal.

Tasm5.0: http://download.pchome.net/development/linetools/download-9027.html

And in turboc. cfg (You can use NotePad to create one.This file includes TCC. EXE runtime parameters. All parameters here will be used by the automatic TCC. EXE during the runtime, for example:

-Ih:/TC/include
-LH:/TC/lib

-NH:/TC/Output

Set some parameters in the file, the most typical is the above three, other parameters are as follows:


Another important thing is that tc2.0 supports most of the 8086 commands (of course there are some conventions in usage, but now I don't plan
For details, because it is complicated and may be written later-if needed ).
If the conventions I mentioned above are complicated, how simple is the following method!
Let's use Borland's built-in variables for tc2.0 for pseudo assembly.
Maybe you still don't know that there are some built-in pseudo registers in tc2.0 (which can be regarded as register variables, but they are much better than register variables)
_ Ax, _ Ah, _ Al,
_ BX, _ BH, _ BL,
_ CX, _ CH, _ Cl,
_ Dx, _ DH, _ DL,
_ Di, _ Si, _ sp,
_ CS, _ DS, _ es, _ SS
Note the size, _ ax, _ BX, _ CX, _ dx, _ CS, _ DS, _ es, _ SS, _ Si, _ Di, _ sp and so on are 16-bit registers equivalent to the C language's unsigned int type, and the rest are 8-bit registers (equivalent to unsigned char) (How can TC support 32-bit storage, so eax and so on are not available, FS, GS and IP registers are invalid), and do not forget to use forced type conversion when passing parameters.
The interrupt call command is __int _ (Interrupt _ #) (note that the front and back regions of int are both underscores)
For example:
# I nclude <dos. h>
Unsigned int _ stklen = 0x200;
Unsigned int _ heaplen = 0;

Main ()
{
_ Dx = (unsigned INT) "Hello, world./R/N $ ";
_ AX = 0x900;
_ Int _ (0x21 );

}
Dos. H is the header file that contains the built-in interrupt Call Statement of _ int _ (). Therefore
The missing. _ stklen and _ heaplen are two internal elements defining the runtime stack and heap size.
Reference variables (this is a term I think, which means that if these two variables are in the source file
Explicitly declared, the Compilation Program will reference it to construct information during the compilation period to generate
If the target file is not explicitly declared, the Compilation Program will automatically determine it ).
These two variables also have some conventions. If _ stklen is not explicitly declared, _ heaplen is assigned a value of zero.
Both the stack and stack Are defult.
Finally, there is an unspecified flag register flags in tc2.0, which is also a built-in
The pseudo register is _ flags, which is a 16-bit register. These built-in registers can be used.
Calculation, but pay attention to the types they represent (if necessary, type conversion );
It seems that this is not a good way (and to use this method, you only need to use a DOS. h header file,
TCC compilation is not required and can be compiled directly in TC20 IDE ).
Tc2.0 also provides some simple and easy-to-use functions to call dos functions, such:
Int86 (...), int86x (...) (but these methods still need to call functions, so it is better to use
The overhead of the system increases because the memory allocation of the Union regs structure is involved,
The use of pseudo registers is the most concise), port communication functions such as: inportb (...), inport (...),
Outportb (...), outport (...), pointer Conversion Function: fp_off, fp_seg, mk_fp.
It is available in the help system. You can refer to it when using it.

Example of tlinkbat. BAT:
Rem the Lib environment variable is the directory of the. OBJ and. Lib File
Set Lib = H:/TC/lib/
In the following sentence of REM, c0s (C 0 s) is a. OBJ file, which is the Startup File of a C program.
Tlink % lib % c0s % 1, % 1, % 1,/L % lib % EMU. Lib % lib % maths. Lib % lib % CS. Lib
Set Lib =
(You can delete sentences starting with REM in use)

___________________________________________________
Some conventions:
Let's first talk about the compiler compilation principles when writing an assembly under TC20 (inline assembly -- the name of your own, what do you want to call:
1. All statements in the Assembly Language outside the main () function are processed as data declaration statements, that is, they are placed in the Data Segment during compiler compilation, for example:
ASM string1 DB "hello", 'World! ', 0ah, 0xd, "$"
Main ()
{
ASM mov dx, offset string1
ASM mov ah, 9
ASM int 33
ASM mov dx, offset string2
ASM int 33
}
ASM string2 DB "the string can be declared after the main () function! $"
Data Definition statements in the Assembly Language outside main (), such,
As long as it is out of main (), including this sentence: ASM mov ax, 0x4c00), after compilation, It will be placed in the data segment, and the C language data declaration statement will still follow the C rule!
2. All assembly language statements in the Main () function are put in the code segment after compilation, including the sentence:
ASM string2 DB "the string can be declared after the main () function! $"
3. Do not use keywords in C language in statements starting with ASM, which may lead to errors in the compilation phase.

So what are the conclusions of these three articles? (Close your eyes and think about it. You may change it from here.
I really appreciate myself. Yes, you should believe that you are right !)
Let's take a look at this conclusion:
1. According to the Compilation Principle 1. We can't write Assembly command statements outside main () (Don't laugh, it is worth noting that it is the same as the C language !), Do not perform any segment definition or macro definition anywhere (this is determined by the compiled form, that is, all Assembly format statements in TC20 can only be, direct data definition and statement instructions )!
2. According to Compilation Principle 2. We can't define data using Assembly statements in main,
Most people write an assembly in TC20 for the first time)
3. Similar forced types cannot be used in Assembly statements starting with ASM.
All right, day is good, just breathe! In this case, a general framework is coming out! As long as you follow this principle, you can avoid many inexplicable errors!
In layman's terms:
The data definition of the Assembly statement is placed outside main (), and the command is placed inside main.
If you do not have a better document, remember me!

Some details:
An inline assembly statement starting with ASM does not support escape characters of C, but uses C language to declare an array of characters (containing escape characters ), when int 33 Ah = 9 is used to output this string, the escape character is valid (this is mainly because the internal representation is different after compilation, think about the answer ).
Inline assembly supports C, such as numerical representation and string declaration format,
For example, a hexadecimal data can be expressed in two ways: 0xa and 0ah. The string can be like this:
"Hello, world! $ "(Like C) can also be like 'hello, world! $ '(Compile your own method ).
Like C, you should pay attention to the value assignment type, and be stricter than C (assembly is never done by yourself
Such as type conversion), so everything needs to be done by yourself! And you should not try to use the C format
To do this, such as the format ASM mov dx, (unsigned) a (A is such a thing,
Char A [] = "Hello, world! ";), And such a sentence will also cause errors: ASM mov dx, word ptr a (logical error), but this is not an error during compilation, it is a runtime error (for specific reasons, think about the operation and the consequences of something like word label ), you can use a sentence like this to make "man-in-the-middle" such as int I = (unsigned) A; ASM mov dx, I (never use ASM mov dx, (unsigned) a. however, to tell everyone the good news, you can point to a string with a pointer, and then you will be surprised that you can actually do this:
Char * P = "Hello, world"; ASM mov dx, P, and then output this string using the int 33 Ah = 9 function without errors (this also shows the pointer feature, it is a two-byte (TC20) variable, including an address, which has nothing to do with the type of the variable to which it points ).
The inner Assembly statement is not supported-> This operator. There is a label problem. In the last example, you will see something special!

The above is just a few small things (which are also common), and there are still many details to talk about, but I cannot list them one by one due to my limited time, for example, the application of C structure in inline assembly can be considered based on its operating mechanism. In addition, this is just a learning thing, so you should learn it by yourself (find the relevant documents, of course, there is nothing more complete now), and the situation will be much better, I have learned a lot about inline assembly, such as compilation principles and how to make the code more efficient and take up the least space. finally, we recommend a method to generate the C source file assembly code using the-s switch of TCC.
(Maybe many people use it) is a good learning material! I wish you all a success!

Cstarter
02-11-17

/* Due to my limited time and ability, errors and details are inevitable. Please forgive me!
My email: wxe85@sina.comCstarter1985 @ hotmail. comqq: 170594633 */
Some examples:
In the following example
<IBM-PC assembly language programming> rewriting of Chapter 11th of Tsinghua Edition
You can directly type TCC filename on the command line. Of course, you must have tasm. exe
/*
ASM mus_frep DW 330,294,262,294, 3 DUP (330)
Asm dw 3 dups (294), 330,392,392
Asm dw 330,294,262,294, 4 DUP (330)
Asm dw 294,294,330,294,262,-1
ASM mus_time DW 6 DUP (25), 50
Asm dw 2 DUP (25, 25, 50)
Asm dw 12 dups (25), 100
*/
ASM mus_frep DWP 330,392,330,294,330,392,330,294,330
Asm dw 1, 330,392,330,294,262,294,330,392,294
Asm dw 1, 262,262,220,196,196,220,262,294,330,262
Asm dw-1
ASM mus_time DW 3 DUP (50), 25, 25, 50, 25, 25,100
Asm dw 2 DUP (50, 50, 25, 25), 100
Asm dw 3 DUP (50, 25, 25), 100

Main ()
{
Asm jmp start
/* Set the voice frequency.
<IBM-PC assembly language programming> Tsinghua edition chapter 11th Detailed Description */

Sound:
ASM mov Al, 0b6h
ASM out 43 H, Al
ASM mov dx, 12 h
ASM mov ax, 533 H * 896
ASM Div di
ASM out 42 h, Al
ASM mov Al, ah
/* This delay is used to prevent the last operation error of the two I/O operations,
Because the CPU speed is much faster than the bus speed, it is required to wait for the first operation to complete and then perform the second operation */

ASM mov CX and 1000
Delay:
ASM loop delay

ASM out 42 h, Al
ASM in Al, 61 H
ASM mov ah, Al
ASM or Al, 3
ASM out 61 H, Al

/* Use the 15 h interrupt function, 86h delay, CX: dx = microseconds */
ASM movax, 2710 H
ASM Mul BX
ASM mov CX, DX
ASM mov dx, ax
ASM mov ah, 86 H
ASM int 15 h/* available _ int _ (0x15); instead */

ASM mov Al, ah
ASM out 61 H, Al
Asm jmp add_count
/*------------------*/
Start:
ASM mov Si, offset mus_frep
ASM Lea bp, mus_time
FREP:
ASM mov Di, [Si]
Asm cmp Di,-1
ASM je end_mus
ASM mov BX, [BP]
Asm jmp sound
Add_count:/* The number cannot be written in assembly language */
ASM Add Si, 2
ASM add bp, 2
ASM JMP FREP
End_mus :;

}

For the above programs, it is much easier to use the pseudo Register Method to write one!

/* A voice program! (Source: <PC technology insider> power version-this version is not good, not as good as Tsinghua version )*/
# I nclude "Dos. H"
Main ()
{
Static Union regs ourregs;
Outportb (0x43, 0xb6 );
Outportb (0x42, 0xEE );
Outportb (0x42,0 );
Outportb (0x61, (inportb (0x61) | 0x03 ));
Ourregs. H. Ah = 0x86;
Ourregs. X. Cx = 0x001e;
Ourregs. X. dx = 0x8480;
Int86 (0x15, & ourregs, & ourregs );
Outportb (0x61, (inportb (0x61) & 0xfc ));
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.