The conditional instructions. ------------------------------- The conditional instructions, through which the branching, such is carried out as in C. C-- has two instructions of branching - if and IF. If makes close branch on condition, and IF makes short (8-digit) branch on condition. IF is executed faster and can save up to 3 bytes in the size of the code, but can carry out transitions only within the limits of 127 bytes code. The conditional instructions, as well as in C, can be accompanied as single by the command, and block from several commands, made in curly brackets { }. The conditional instructions have the same limitations, as conditional Expressions. If the instruction IF is followed more than 127 bytes of the code, by the compiler will produce the following message on an error: IF jump distance too far, use if. It can simple be corrected, by replacing in this place the instruction IF on if. The else and ELSE commands are used precisely the same as in the C language. Difference them in that ELSE has limitation of the jump address of 127 bytes, same both IF. Esle generates the code on 1 byte longer, than ESLE. The IF and else commands, and also if and ELSE can freely mix up as in the following example: if (x == 2) WRITESTR ("Two"); ELSE {WRITESTR (" not two. "); Printmorestuff (); } If the instruction ELSE is followed more than 127 bytes of the code, by the compiler will produce the following message on an error: ELSE jump distance too far, use else. It can simple be corrected, by replacing in this place the instruction ELSE on esle. Cycles do {} while. -------------------- In such cycle the block of the code making an iteration body, will repeat, while the conditional has value "true". The truth of the conditional is checked after execution of an iteration body, Therefore block of the code will be executed at least once. The example do {}, while of a cycle, in which body will be executed five times: count = 0; do { count ++; WRITEWORD (count); WRITELN (); } while (count < 5); The conditional in do {} while of the instruction should correspond to those to rules, as in the instructions IF and if. Cycles loop, LOOPNZ, loopnz. ------------------------------ The cycles loop repeat the block of the code, while defined variable or the register executing a role of the counter of a cycle, contain value, distinct from zero. At the end of execution of the block of the code making an iteration body, indicated, the variable or register - decreases on 1, and then is checked on equality to zero. If the variable (or register) is not equal to zero, the iteration body will be is executed again, and the process will be repeated. Example of use of a cycle loop in which as the counter of a cycle the variable is used: Count = 5; Loop (count) {WRITEWORD (count); WRITELN (); } The greatest effect gives use of the register CX for cycles with small the body, as in this case by compiler generates a cycle with application the machine LOOP command. If before start the counter of cycles contains zero value, command the iteration body will be executed a maximum number of times for a range of variable (256 times for the 8-bit counter (variable of a type byte or char), 65536 for the 16-bit counter (variable of a type word or int), and 4294967296 for the 32-bit counter (variable of a type dword or long)). In the following example the cycle will be executed 256 times: BH = 0; Loop (BH) { } If in the command is not indicated of any counter of a cycle, the cycle will be to proceed indefinitely. The following example continuously will output character of an asterisk (*) on the screen: loop () WRITE ('*'); The programmer, if wants, can use or to change value by variable of the counter of a cycle inside a cycle. For example, the following cycle will be executed only 3 times: CX = 1000; loop (CX) { IF (CX > 3) CX = 3; } The cycle can also be interrupted by an operator of break BREAK or break. Here that an example with use BREAK: CX = 1000; loop (CX) { IF (CX > 3) BREAK; } The cycles LOOPNZ/loopnz differ from a cycle loop, that before an input in the cycle is checked equality to zero of argument of a cycle. If the argument is equal to zero, that iteration body will be never executed (in a cycle loop in this case iteration body the maximum number of times) will be executed. ! Short a Cycle ^ Bwhile of ^ B, ^ BWHILE ^ B. Cycle while, WHILE. Syntax: While () The cycle is executed so long as the value of will not become False. in the beginning is calculated. If initially is false, That body of an operator while is not executed at all and the handle at once is transferred On following(тnext) an operator of the program. The cycle WHILE is similar to a cycle while, but thus the code on 3 is generated Than byte is shorter. The size of the generated code in a cycle WHILE should be less 127 bytes. Examples: While (i < 20) { W(i)) I ++; } WHILE (i < 20) W(i)) // the cycle or will be infinite or not // Will be executed never ! Short a Cycle ^ Bfor of ^ B, ^ BFOR ^ B. Cycle for, FOR. Syntax: For ([< initial expression >]; []; [< increment >]) The cycle for is executed so long as the value of will not become False. If initially is false, a body of an operator for at all The handle at once is executed operator of the program also is transferred on following(тnext). < Initial expression > and are usually used for initialization And modification of parameters of a cycle. First step at execution for is the calculation < initial Expressions >, if it is present. Then the condition > is calculated < and is made It an estimation as follows: 1) If is true, the body of an operator is executed. Then is calculated (if it is), and the process repeats. 2) If is lowered(omitted), it the value is accepted for true. In This case the cycle for represents an infinite cycle, which can To be completed only at execution in it a body of operators break, goto, Return. 3) If is false, the looping for comes to an end and The handle is transferred to the following operator. The cycle FOR is similar to a cycle for, but thus the code on 3 bytes is generated Shorter. The size of the generated code in a cycle FOR should be less than 127 Byte. Examples: For (i = 0; i < 5; i ++) { WRITESTR ("STRING"("LINE")); W(i)) WRITELN (); } The number < of initial expressions > and number of is not limited. Everyone The operator in < initial expressions > and should be divided By comma. An example: For (a = 1, b = 2; a < 5; a ++, b + = a) {... Also there is a possibility of logical association of . To unite It is possible up to 32 conditions. Each joined condition should be made in Brackets. An example: For (a = 0; (a> = 0) . . (a < 10); a ++) {... ! Short an Operator the switch ^ Bswitch of ^ B. Operator the switch switch. Syntax: Switch () { Case : ... Case : ... ... Default: } The operator the switch switch is intended for a choice of one of Several alternate paths of execution of the program. The execution begins From calculation of value of . After that the handle is transferred to one From of a body of the switch. In a body of the switch contain Constructions: case :, which syntactically represent Labels of operators. The operator receiving handle, is that operator, Value of which constant coincide with value of The switch. The value of should be unique. The execution of a body of an operator - switch switch begins with, selected, Thus of operator up to the end of a body or so long as also proceeds Any operator will not transfer handle for bounds of a body. The operator the following behind the default key word, is executed, if any From constants is not equal to value of . If default is lowered(omitted), any The operator in a body of the switch is not executed, and the handle is transferred on Operator the following for switch. For an output(exit) from a body of the switch the operator of break is usually used Break (BREAK). Example: S(i)) Case 'A': W(i)) I ++; BREAK; Case 32: WRITE ('_'); I ++; BREAK; Default: WRITE ('i'); } The operator 'switch' now in the compiler can be realized two By ways: tabulared and method of sequential checks. The tabulared method is fastest, and at the large number of operators 'Case' and at an insignificant difference between maximum and minimum By values 'case' it(he) still can be and more compact. But at him(it) is and Disadvantages: in a 16-bit mode the compiler always uses the register BX, and in 32-bit mode, if an operand 'switch' is the register, it value Will be desroyed. The method of sequential checks was used by the compiler earlier, but in This version it(he) has varied slightly: the block of matchings is transferred in a beginning of a body Operator 'switch', it has allowed to be saved from 1-2 superfluous 'jmp'. But Now compiler can not define(determine), what type of transition to use at To check of values 'case'. Now it will be by your care. If the size The code from a beginning of a body of an operator 'switch' up to the location of an operator 'Case' is less than 128 bytes, it is possible to use short transition. In it Case you can specify an operator 'CASE', that will result in generation more The compact code. The compiler in warnings will to you prompt about Possibilities of use of operators 'CASE'. Use of an operator 'CASE' in cases, when the size of the block of the code more than 128 bytes will result in output By the compiler of the message about an error. By optimization of the code on the size, the compiler previously calculates The size of the code, which can be obtained by both methods and realizes Compact. By optimization on speed the advantage is given back tabulared To method, if the size of the table turns out not too large. For an operator 'switch' the form - 'SWITCH' is entered as well short it. It can be applied in case the size of the block of the code between the beginning of a body Operator and operator 'default' (if it(he) is absent, by the end of a body Operator 'switch') there are less than 128 bytes. About possibility of use of short The forms the compiler will communicate in warnings. For an operator case/CASE, which can be used only in a body The block of an operator switch/SWITCH, it is possible to specify a range of values. At first It is necessary to specify smaller value, then after dots greater. An example: Switch (AX) { Case 1... 5: WRITESTR (" Range AX from 1 to 5 "); BREAK; }; Earlier you had to write a more bulky construction: Switch (AX) { Case 1: Case 2: Case 3: Case 4: Case 5: WRITESTR (" Range AX from 1 to 5 "); BREAK; }; Besides that the new format of record is more compact and more читабелен, But thus the compiler creates the more compact and fast code. ! Short an Operator of transition ^ Bgoto of ^ B, ^ BGOTO ^ B. Operator of transition goto, GOTO. Syntax: Goto