Features of MIPS Assembly Language

Source: Internet
Author: User

Http://forum.eepw.com.cn/thread/119955/1

 

The assembly language is the read/write version of the cpu binary command. We will have a separate chapter later to describe the assembler.
Statement. Readers who have never been familiar with assembly languages may be confused when reading this book.

Most MIPS assembly languages are well-known and contain some register numbers. However, toolchain can
To make it easier to use the microprocessor language. The tool chain should at least allow programmers to reference some mnemonic characters
The Assembly Language of lattice requires strict numerical encoding. Most of us use familiar C preprocessors. C pre-processing
The handler removes the C-style annotation and obtains an available assembly code.

With the help of a C Preprocessor, MIPS assembler uses a mnemonic to represent registers. The mnemonic also represents
The usage of each register is described in section 2.2)

For readers who are familiar with assembly languages but are not familiar with MIPS, the following are some examples.

/* This is a comment */
# So is this

Entrypoint: # this's a label

Addu $1, $2, $3 # (registers) $1 = $2 + $3

Like most assembly languages, MIPS assembly languages are also behavioral units. The end of each row is
And ignore any content after "#". It is considered as a comment. There can be multiple commands in one line.
Commands must be separated by semicolons.

A label is a word followed by the colon. A symbol can be a combination of any string.
A symbol is used to define the entry of a piece of code and a storage location of a data segment.

As shown above, many commands have three operands/operators (operand ). The target register is on the left side (note that this
Point is opposite to inetel x86 ). Generally, the sequence of register results and operators is similar to that of C language or other operators.
The language is the same. For example:

Subc $1, $2, $3

Meaning:

$1 = $2-$3;

In this regard, we will talk about this much first.

2.2 registers

For a program, there can be 32 General registers: $0-$31. Two of them have only two
Different from others.

$0: No matter what value you store, the return value is always zero.

$31: always stores the return address of the normal function call command (JAL. Note that call-by-registe's jalr indicates
So that any register can be used to store its return address. Of course, if you don't need $31, the program will look a little old.
Strange.

In other aspects, all registers are the same. Can be used in any command (you can also use $0
Is the target register of an instruction. Of course, no matter what data you store, the data disappears .)

In the mips architecture, the program counter is not a register. In fact, you 'd better not think that way. In
In a CPU with a pipeline, the value of a program counter has multiple optional values at a given time point. This is a bit
Confused. The return address of the JAL command follows the second command.

...
Jal printf
Move $4, $6
Xxx # return here after call

The above explanation makes sense, because the command after the jal command is closely tracked is caused by the delay in the delay slot
Location) -- remember that the rule about the latency location is that this command will be in the transfer target (such as the printf above)
Before execution. The delay location command is often used to pass function call parameters.

There is no status code in MIPS. The CPU Status Register or internal does not contain the result status information calculated by any user program.
.

Hi and lo are two register sizes related to the multiplication handler used to store results. They are not
General registers cannot be used in addition to multiplication and division. However, MIPS defines
Some commands can store any value in hi and lo. If you think about it, we will find that it is very necessary
To restore an interrupted program.

Floating-point coprocessor (FPA), if any, has 32 floating-point registers. By Assembly
In simple terms, the language ranges from $ f0 to $31.

In fact, for mips I and MIPS II machines, there are only 16 even number registers for math.
. Of course, they can be used for both single-precision (32-bit) and double-precision (64-bit ). When you create a double-precision
During operation, the register $ f1 stores the remainder of $ f0. Registers with odd numbers are only used as
Data transmission.

The mips iii cpu has 32 FP registers. However, to maintain software compatibility with the past, it is best not to use the odd
Number register.

2.2.1 usage of mnemulator and General Register

We have already described some architecture content. Next we will introduce some software content.

Register number enable usage
0 zero always returns 0
1 at is a temporary variable used as the assembler
2-3 v0, v1 subfunction call return results
4-7 parameters of a0-a3 sub-function call
8-15 temporary variables for t0-t7, which do not need to be saved and restored when used by subfunctions
T8-t9 24-25
16-25 s0-s7 sub-function register variable. The sub-function must save and restore used variables in function return.
The call function knows that the values of these registers have not changed.
K0, k1 is usually interrupted or the exception handling program is used to save some System Parameters
28 gp global pointer. Some operating systems maintain this pointer to facilitate access to "static" and "extern"
Variable.
29 sp Stack pointer
30 s8/fp 9th register variables. Sub-functions can be used for callback pointer
31 return location of the ra sub-function □
'7d

Although the hardware does not have mandatory rules for specifying register usage, these registers are used in actual use.
Follow a series of conventions. These conventions have nothing to do with hardware, but if you want to use someone else's code, the compiler
And the operating system, you 'd better follow these conventions.

Register Conventions use a series of register conventions. When using registers, try to use this
Some agreed names or Enis, instead of directly referencing register numbers.

Around 1996, SGI began to use the new register conventions in the compiler it provided. This new convention can be used
To create a program that uses a 32-bit address or a 64-bit address, called "n32" and "n64" respectively ". We will not discuss this for the moment
Some will be discussed in detail in Chapter 10th.

Register name conventions and usage

* At: this register is used by some merging commands compiled. If you want to display
For example, saving and restoring registers in the exception handling program), there is an assembly direve ve that can be used to disable assembly.
The at register is used after Directive (but some macro commands in the Assembly cannot be used again ).

* V0, V1: used to store the results or return values of non-floating-point operations of a sub-Program (function. If the two
If the memory is insufficient to store the value to be returned, the compiler will use the memory. For details, see section 10.1.

* A0-a3: used to pass the first four non-floating-point parameters when the sub-function is called. In some cases, this is incorrect. Please
Parameter 10.1 details.

* T0-t9: As agreed, a subfunction can use these registers without saving them. In expression
During formula calculation, these registers are very good temporary variables. The compiler/programmer must note that when calling
When a subfunction is used, the values in these registers may be damaged by the subfunction.

* S0-s8: As agreed, sub-functions must ensure that the contents of these registers must be restored to the letter when the function returns
Number of previous values called, or do not use these registers in the subfunction, or save them on the stack and
Restore when exiting. This Convention makes these registers very suitable for using register variables or storing some
The original value must be saved during the call.

* K0, K1: used by the OS exception or interrupt processing program. The original value will not be restored after being used. Therefore
They are rarely used elsewhere.

* GP: if a global pointer exists, it will point to the static data area determined at runtime.
A location of the domain. This means that, using GP as the base pointer, the system only needs
A command is required. If there is no global pointer, two values are required to access a static data region.
Command: obtains the 32-bit address constant determined by the compiler and loader. The other is for Data
. To use GP, the compiler must know whether a data is within the 64 K range of GP at the time of compilation.
. Generally, this is impossible. You can only guess it. The general approach is to set small global data (small
Global data) is placed within the GP coverage range (for example, a variable is 8 bytes or smaller), and the linker alarm is triggered.
If the small global data is still too large, it exceeds the range that GP can access as a base pointer.

Not all compilation and running systems support the use of GP.

* SP: the upper and lower of the stack pointer must be displayed using commands. Therefore, MIPS generally only enters
The pointer of the stack is adjusted only when the stack exits. This is achieved through the called subfunction. SP is usually adjusted to this
The lowest stack required by the called sub-function, so that the compiler can use the offset relative to the SP
To access the stack variables on the stack. For details, see section 10.1 stack usage.

* FP: the other agreed name of FP is S8. If the sub-function needs to dynamically expand the stack size at runtime
The dummy pointer can be used by the quilt function to record the stack. Some programming languages support this. Sink
Programmers often use this FP usage. The library function alloca () in C language uses FP for dynamic adjustment.
Full stack.

If the bottom of the stack cannot be determined at the time of compilation, you cannot use SP to access the stack variables. Therefore, FP is
It is initialized as a constant position relative to the function stack. This method is invisible to other functions.
.

* Ra: When any sub-function is called, the return address is stored in the RA register. Therefore, a sub-process
The last command in sequence is JR Ra.

If you want to call other sub-functions, you must save the Ra value, usually through the stack.

There is also a standard convention for the usage of floating-point registers. We will be in section 7.5. Here, I
We have introduced the storage introduced by MIPS.

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.