Chapter 12th APO Programming language
The APO programming language is based on assembly language and object-oriented programming. There are only 7 basic instructions: assignment instruction, copy instruction, BTX (bit x Test 1, or 0 transfer) instruction, check table Jump command switch (RN) {...}, shift and loop instruction s, three operand operation instruction, call and return instruction. All instruction sizes, except 32-bit immediate number assignments are 2 words, others are 32-bit, one word. Instruction execution time, except that the 32-bit immediate number assignment is 2ns, the copy instruction depends on the length of the copy, and the other instructions are 1ns. An application can use only 21 registers of R0-R4,R8-R15 as a high-speed register local variable, where R0-R4 is used as the method parameter register, and R0 is usually the return result register.
In general, the form of a subroutine is expressed as a function in C, and a function or method is expressed in a so-called object-oriented language. APO uses assembly language, usually using pseudo-directives (abbreviated form macros for a set of instructions) to describe call command calls, such as: Method name (parameter 1, parameter 2, parameter 3, parameter 4, parameter 5); The compiler translates up to 6 assembly instructions that are relevant. In fact, the parameter I is required to correspond to the Write register ri (ri = R0----R4); This requires a number of instructions; the abbreviation compiler will automatically expand into a corresponding set of instructions. Parameters can have one or more, or none. No Parameter form: Method name (); After compiling, it is a command call. Results returned: The value can be written to register R0, or it can be written to register r8-r31 or static variable. Call is pressed into the H2 r0-r4 and PSR, PRR, PPC, and RET is a popup r1-r4 and PSR, PRR, PPC.R0is not covered! Therefore, R1-R4 can be seen as an automatic variable. A pointer or data entry parameter can also be written to R8-r31 first, and then the method is called.Note: Register R24-r31is a local variable used by the system method; When the user application is used, it should be noted that when the system method is called, they may be overwritten.
Methods (subroutines) are written in the following form:
Method Name:
Directive 1;
......
instruction N;
RET;
Or: Method name {...}
The collection of some methods forms a library of methods, which are placed in a read-only protected area and managed by the kernel. Some of the public method libraries are resident in protected areas. The method Library of the user process is dynamically present in the protected area, and when the user process exits, if the user method library has a reference count of 0, the user method library is kicked out of the protected area. The method library is a file form. The so-called object-oriented language expression of the interface concept is actually refers to the object of a method library.
The call instruction is used to invoke a method, at which point the next command address PPC will be pressed into the stack, the register H0:R0-R4 and PSR, PRR, PPC; In case of return, the RET instruction is used to return from a method The address of the next command saved by the call will pop up from the stack to the H0 line register, RET is the r1-r4 of H0 and PSR, PRR, PPC. The program goes to the next command before call executes. Call method Format:
Method name (parameter 1, parameter 2, parameter 3, parameter 4, parameter 5);
The difference from the C function:
1), no need to specify the function return value type; The value returned depends on the method or programmer's description.
2), no complex formal parameters, arguments, pointers, Void, array references, etc., all as 32-bit register parameters. It is up to you to decide what kind of argument you think it is.
3), no header file, no function prototype declaration.
4), except for register variables, only the static variables you declare.
The immediate number can be 8-bit or 16-bit in a group with "." separated by the number. Variables that have already been declared can be directly placed in the register-related position; In fact, the address of the variable is the register-related position. such as: Method 1 (3, 5.5,, 6.0.1.5,AAA); Entry Parameters 4, write register R0, R1, R3, R4; R2 regardless; where R4 is the address of the variable AAA. R0-R4 is an automatic variable within a method that is overwritten before it is called when the method returns. Typically, in a method, R0-R4 is only used as a read-only entry parameter, and the output is available in registers: R8-r31. In APO, the use of stacks is no more useful than Call,ret. APO has only register-type automatic variables, static variables (dynamic variables in the code that request memory allocations, essentially static variables). Can be directly: for example, R17 = 89; user-defined; not necessarily the compiler allocates variable space. After all, register space is limited, and their distribution is clearer. The advantage of using register variables is that there is no need to write variable declarations, etc., and if more space is needed, you can only request dynamic variables for memory allocations; buxh new (YYY); Can get the memory space of X line, the initialization method of the space has to be written by itself. YYY release You don't have to think about it. Once the yyy is not used, the system is immediately recycled. It is best to allocate static variables and initialize them at the outset.
I. Variables and spaces
Declaring a variable is actually allocating a storage space of a certain size, and the compiler translates the variable name into the relative logical address of the storage space. The address of the register variable is a fixed actual address, and the size is a bit container of 32 bits, so there is no need to declare it; Storage space is a unit, such as bu1[32] A; The space for declaring variable A is a 1-bit array of 32, with BU32 A; The space declaring variable A is a 32 bit container, with bu1w A; The space declaring variable A is a bit container of 32-bit words, with bu2z A; The space that declares the variable A is a 2-bit 16 character bit container; The address of the variable space is the modifier that should be used. Z. W. e description; For example BU256 A; So a.0, is the bit address of the No. 0 bit of variable a? Or the No. 0 character address of variable a? Or the No. 0 Word address of variable a? Or the No. 0 line address of variable a? The compiler generally considers the No. 0 bit. If you say a.0.z, it is clear, is the No. 0 character of the variable A, A.3.W, is the space of variable A in words, is the 3rd word address. The register variable is a 32 bit container, r3.0.z shorthand is r3h high half word, r3.1.z shorthand is r3l low half word. Similarly, the contents of an address in a variable space are assigned a value that should be used as a modifier. Z. W. e description; For example bu64k A; Then A.3.W = 1; Is the content of the word address assigned to the 3rd Word address in variable a? or the character address content? Or a bit of address content? If you say a.3.w.1, it is clear that it is the 1th digit of the 3rd Word address of variable A; a.3.w.w = 1; Is the content of the word address assigned to the 3rd Word address in variable a. A.3.w.h= 1; Is the high-half-word address content assigned to the 3rd Word address in variable a. A.3.E.E = 0; is to assign a value of 0 to the line address content in the 3rd line address in variable a. A.256.e.e= 1; Compiler error, assignment beyond the scope of the variable declaration, because the variable a is only 0-255 rows.
The user program simply can manipulate its own declared variable space, and register variable r0-r4, r8-r31; So, there is no pointer variable. Pointer manipulation, such as (RX), (variable), is not allowed in the user application, the compiler will error, and the pointer operation will pose a risk. Registers are 32-bit, such as R2.230, R3.3.W, (R16) and so on, the compiler is an error. But can also be divided into high and low 2-word operation, such as R1H, r1l, and row register mode h1:r8-r15, H2:r16-r23, H3:r24-r31. Note that the H3 line register is overwritten when the system method is called, so do not use r24-r31 as much as possible. The variable is of a spatial size, and the assignment cannot exceed its range, otherwise the compiler will give an error. Ii. Introduction of the directive:
1. Assignment instruction:
1), Bit assignment value: if R3.11 = 0; a.b.c.9.w.3 = 1; A.rh. w.6 = 0; B.r1. w.0 = 1; Wait a minute.
2), 16-bit assignment: if r4l = 11; A.b.c.5.w.h = 3; Wait a minute.
3), 32-bit assignment: if R31 = 11; A.B.C.9.E.W = 7; Wait a minute.
4), line assignment: H2 = 0; A.b.c.e= 0; A.B.E = 1; And so on, the line is only 0, or all 1 assignments. In addition to bit arrays, the minimum number of member variables in a table is a word, so a 16-bit assignment indicates a high h, low l half word.
5), variable, register mutual assignment:
R9 = A.B.C.9.W; R11 = A.B.3.W; C.9.W = R13; B.a.10.z = H3; Wait a minute.
You should pay attention to the size of the container in place, otherwise there will be truncation. Typically, the register variable size is the same.
6), Operation assignment: Only register variables, such as: R3 = ALU, #5; The actual is R3 = R3 ALU, #5;
Can only be a 16-bit immediate number.
The ALU has: ADD addition +, ADC with carry addition, SUB subtraction-, SBC with carry subtraction, CMP comparison,
Dadd decimal adjustment, BIC (! N and RD) immediately count back and then "and" &! N
BIS (N or RD) or |, and and &,
BIT (N and RD "with" but not saved), XOR "Xor ^",
ORN (! N or RD) immediate number to reverse and then "or", RSB (n–rd) minus,
MLU multiply *, DIV divide/, Ssat saturation operation
Other wording: RD = ALU N; Rd+, rd-; Wait, you can.
2. COPY command:
Copies part of a variable to another variable, with an immediate number of length, or a register. The length cannot exceed 64K. There are 3 ways of copying Z, W, E. The copy content size cannot exceed the space size of the target declaration, otherwise it is truncated. There are 3 parameters: The target variable, the source variable, the length, and when the instruction executes, these 3 parameters are copied to the dedicated register B5, B6, b7l;b7h for the compiler to give the range monitoring parameters automatically. The user code cannot write to the private register b0-b7; it can only be an implicit operation. You can also use the "+,-" 2 modes, do not indicate the usual + mode, (B5) = (B6); b5+, b6+, until the length of 0,-mode is b5-, b6-, until the length of 0. In the code, you may have set the B5, B6 when you first use copy, and the second time, you want to take advantage of the original set of B5 or B6; then you can pass a parameter less. However, the compiler verifies that it is out of bounds. Time consuming: 3ns + N (length)/2 ns.
such as: COPY.W (A, B.3.W, #N); The 3rd member of variable b begins, copying n characters to variable A; if it is copy.e, copy n rows to variable a. One to many copies, the source variable must be fixed with the # number; COPY.E (A, #H2, #N); The immediate number can also be a register variable, such as COPY.W (A, B.3.W, r1l), and so on. 3, BTX (bit test for x transfer) directive:such as: BT0 R3.7, #A; if the R3 7th bit is 0 hops to A execution. BT1 a.b.23, #A; If the 23rd bit of A.B is 1 hops to A execution. Test Status register the post-position general instructions of the PSR, about 2 kinds of writing can be.
JEQ/JZ marking; The 0-bit is shifted to the label statement equal to when the program jumps BT1 PSR. Z , marking;JNE/JNZ Marking, 0-bit reset when transferred to the label statement unequal program jump BT0PSR. Z , marking;The JC/JHS is placed when the carry bit is shifted to the label statement greater than or equal to the program jump BT1 PSR. C , marking;Jnc/jlo marking, carry bit reset when transferred to the label statement less than when the program jump BT0PSR. C , marking;JN marking, negative bit is shifted to the label statement is negative when the program jumps BT1PSR. N , marking;JNN label; when the negative bit is cleared, the transfer to the label statement is greater than or equal to the program jump BT0PSR. N , marking;JV marking; The overflow bit is placed when transferred to the designator statement BT1PSR. V , marking; JNV label; transfer to designator statement BT0 PSR without overflow. V , marking;JCZ marking; C-position, Z-Zero transfer to the label statement is greater than when the program jumps BT1 PSR. CZ , marking;JZC marking; C zeroing, z-position when transferred to the label statement less than or equal to when the program jumps BT0PSR. CZ , marking;JL marking; N. Xor. V = 1 o'clock transfer to the label band symbol is less than when the program jumps BT1 PSR. NV , marking;Jge marking; N. Xor. V = 0 o'clock transfer to a label with symbol greater than or equal to when the program jumps BT0 PSR. NV , marking;JGT marking; Z Clear 0 and N or v set, or n clear 0, v clear 0 with symbol greater than when jump BT1 PSR. GT , marking;Jle marking; Z, or n set and V 0, or N 0 and V 1 with symbols less than or equal to jump BT0PSR. GT , marking;JMP designator, unconditionally transferred to the label statement BT1PSR. B1 , marking;
program Status Register PSR = R5:From $ 8 7 0 (+)-----N Z C V CZ ZC LT GE GT LE B1 YJ in out hold execution sign x numberN: Minus sign 1.is negativenumber
nn:n = 0.Non-Negative,greater than or equal to
Z: The result is a 0-position 1 flag.Phasewait(EQ)
NZ: The result is non-0,z = 0.Resultsnot 0or unequal NE
C: Into position 1 sign.greater than or equal to(or HS)
NC: no carry, C = 0.SmallinLO
V: Overflow position 1 flag.
NV: no overflow, V = 0.
CZ:C 1, result not 0 (Z = 0).Greater thanHI
ZC:C = 0, or z = 1.less than or equal toLS
Lt:n XOR v = 1;n! = V.with symbolsSmallinLT
Ge:z XOR v = 0;n = v.with symbolsgreater than or equal toGE
Gt:z = 0,n or V 1, or n = 0, v = 0.with minus signGreater thanGT
Le:z, or N-set and v= 0, or n = 0, V = 1.with symbolsless than or equal toLE
B1: for 1.always 1, AL
4. Check table Jump command: Switch (RN) {...};
Sometimes, a method has n branches, n values in the Register RN, so that using the table Jump command, it will be according to the value of the RN register to enter the correct branch. RN is: one of the r0-r4,r8-r31.
RN is a 16-bit or 32-bit register-shaping variable that can be obtained from an expression. This is similar to the case of C-language switch ().
5. Shift and Cyclic instruction s: only register variables
S RD, RN, #N; S is one of the following, N is the number of bits moved up to 32.
Logical left Shift << (variable carry) LSL (C); Logical Right SHIFT >> (variable carry) LSR (C);
Arithmetic right shift s>> (variable rounding) ASR (C); Loop right Shift (variable carry) ROR (C);Loop right-shift RRX with the carry-round;
such as: Lslc R1, R3, #6; R1 = R3 c<< 6, i.e. R1 = R3 shift left 6 bits, high into C.
Shift and cyclic instructions can also be written as: RD = RN S N; 6, three operand operation instruction: only register variable
RD = RN1 ALU RN2 S #N; RN2 shifts or loops n-bits are stored with the results of the RN1 operation to Rd.
7. Call and return instructions:
Calling call is a direct write method name, and the method returns a Ret.
If you want to do floating-point arithmetic, or more other functions will be used in the system library method, APO language on 7 basic instructions. The specialized instructions for the hardware module are used, and the kernel programming also contains instructions for pointers operation. Third, object-oriented programming
Object-oriented programming has been introduced in the previous chapter, and here are just a few examples.
Example 1:
bu1w [+] A; The members of the array variable A are assigned a value of their subscript.
Array member Assignment method () {//instruction number 6W, memory space row 1E, run time: 402ns.
R1 = 0;
L1:
A.R1 = R1; If a is already declared a is a word group, a.r1. W.W can be shortened to A.R1
r1+;
Cmp. W R1, #100;
JNZ L1;
Ret
} Assembly language is clear and simple. Some people might say that it would be 6 lines of code, but in fact, some high-level languages compiled into a sink code, will be far more than here. Assembly language, the space of instruction, time can be clearly mastered, the bottom is clear; it's not something else to replace! In fact, simple things require complex implementations; complex things are just simple code. This is our real-world law!
Example 2: Sorting
1. Bit schema ordering without ordinal number
This paper introduces the comparison instruction of multi-functional hardware module CMP and bit query instruction Bix. Hardware module 256E, can be divided into 2 parts: input 128E, Output 128E, if the input 128E in accordance with the conditions of the results are given in the output, Then the flag psr.in set, request re-input 128E, if the output 128E results full, then the flag psr.out set, request to read the results. This eliminates the need to start SS1 multiple times. For unordered numbers within 64K, we can construct a bitmap variable of 64K bits, and let the sequence values map the corresponding bits in the graph variable, and then use the bit query instruction of the multi-function hardware module Bix, it is easy to get the ordered sequence number by small to large, and the speed is very fast. For an unordered sequence of more than 64K and size x, we can construct a bitmap variable with n 64K bits; Let the sequence map the corresponding bit in the graph variable, and then use the same method.
BU16 [X] M, N; Declares a 16-bit unordered array variable m, and the sorted result variable N. bu64k wtkj = 0; Declares a bitmap variable with a memory space of 256H. Compiler initialized to 0
Sequence of unordered numbers within range 64K (M, N, X, wtkj) {//occupied: 25W, 4 e; time-consuming: 325us
r16l = 0;
PTXU1:
r16h = m.r16l; Read into an unordered sequence m[x], abbreviated M.R16L.Z.Z to m.r16l
Wtkj. r16h = 1; Map bitmap variable wtkj one of the spaces.
r16l+; x; X-, non-0 continues the loop. Time: 5Xns
JNZ PTXU1;
COPY. H (YJMK, WTKJ, 128); Copy the bitmap variable wtkj 128E to the hardware module, 67ns
BIX; SS1; Bit query, sort.
BT1 PSR. Out, PTXU2; Output full, jump.
PT1:
COPY. H (YJMK, WTKJ.128.E, 128); Copy bitmap variable wtkj the remaining 128E to the hardware module
SS1;
BT1 PSR. Out, PTXU3; Output full, jump.
PT2:
COPY. H (R1, YJMK.128.E, 128); Output results.
PT3:
Ret
PTXU2:
PTX ();
JMP PT1;
PTXU3:
PTX ();
JMP PT2; Jump, output last result
PTX:
COPY. H (R1, YJMK.128.E, 128); Output results.
PT4:
R1 = B5; Save the result variable address ordinal.
SS1; BT0 PSR. Out, PT3; Output dissatisfaction, jump back.
COPY. H (, YJMK.128.E, 128); No, continue to output the results.
JMP PT4; Cycle, continue to SS1, until the complete single-digit map.
}
Take the time to consider the unordered array of m[64k]: bit mapping, 320000ns, bit ordering, 4680ns. So, time-consuming or in-place mapping 320us, bit ordering is just 4.68us. The method of bitmap sorting, for cases where there are duplicates; only one of the numbers is preserved; therefore, the members of the array are unique when sorted by bitmaps. Bitmap ordering requires the use of a common resource-hardware module, either the System Method KEY_YJMK (), the YJMK, and the lock process scheduling, or the bitmap sort upgrade to a system method. There is a bitmap sorting method in the APO system, but the maximum number can only be 24 bits, when the bitmap variable of 256 64K bits is used; a block of data. 32-digit bitmap ordering, the application can only do 256 kinds of array classification, and then call the system bitmap sorting method wtpx (unordered array variable, maximum number, array members). After sorting, the result is placed in the input unordered array variable, as for the size of the bitmap, the establishment that is the method according to the maximum value of things.
2, with the number of ordinal order
This can no longer be used in the order of the bit schema, similar to the database table, according to the value of a field in large, or small order to sort out the corresponding record group. This can only be the base sorting method, the cardinality is half word, then the 32-bit unordered array of the order will come 2 times, the base is byte, will come 4 times, but easy to implement hardware; I'm still in the study. in the preceding chapters, the line symbol is H; When writing to this chapter, it is found that line h conflicts with the high half word H; therefore, the symbol for the line is changed to E. There is no way to write a new chapter; a whole chapter may be changed. Faint!
Chapter 12th APO Programming language