Optron #28
08 июня 1999 |
|
Likbez - Assembler view from afar: Commands unit increments, the direct and relative transition, the organization of the cycle.
Assembler: a view from afar. Continued. Beginning at | | 20, 21, 24, 25 {} Infarh, 1999 Command of the unit increment The very first article of our "Likbekza" as an example of assembly instructions mentioned command increments INC. It is time come to grips with it, as well as her "co-worker" - a team of DEC (from the English. "decrement" - multiple decrease in the value of the variable on a given constant value). Does not diminish their importance! After all, if you ever need to increase or reduce the contents of a register or the register pair on the unit, then that do: to work with arithmetic actions? God forbid! And here we come to the aid of these very team of positive and negative unit increments. Consider the options use them, not forgetting about the flag: Mnemonic Register F Bits (Flags) Operator pa-Operand C Z P / V S N H tion DEC r. x V x 1 x DEC (HL) DEC (ii + n) DEC rr. . . . . . DEC ii INC r. x V x 0 x INC (HL) INC (ii + n) INC rr. . . . . . INC ii Legend - similar to previous Articles "literacy classes." Though the team and is very simple, look at an example. Before you - a piece of software: LD A, 5 LD HL, # C000 LD (HL), A INC HL DEC A LD (HL), A The first three lines are clear: in the "A" zapisyvetsya number 5 in the register a couple of "HL" - number # C000. Then, the transfer the contents of "A" at (HL), then the contents of bytes at # C000 is five. The next line contains the command "INC HL ", after which the" HL "will is the number # C001. Accordingly, after "DEC A" in the battery will no longer five, but four, which is the next team to be sent to the address (HL) - # C001. That's how you can easily set the increment registers, register pairs and byte addressable through the "HL", "IX" or "IY". Extend memories From our previous lessons you could create a false impression that the Z80 processor executes its commands sequentially, one after another without any other options. But it is not so! Of course, follow their chain of command. But what controls this process? Remember the last session - register PC, he is - the program counter. It saves the address of the team, which should performed. Processor, processing a command in the memory location addressable by PC, automatically increases the his unit. Thus, the contents PC is constantly modified during the program. And if we want to change the sequence of commands in the program, it suffices to change the contents of PC. And it can be done by manipulation bits and check the status of bits and bytes, which is the last time with us and discussed. It's time to apply this knowledge in practice. So ... Direct transition There is a command processor, such as JP (From the English. Jump - jump, jump). For her should be two-byte parameter: the address to we want to make the transition. When command processor will meet JP, he will take following her address, place it in the PC and ... move to the next team - the one whose address he had just received. Address transition does not necessarily need to be directly in the memory trace for JP. With no less success, they may be the contents of register pair HL, IX, IY. The command is written as follows: JP nn JP cc, nn JP (HL) JP (ii) - And what is "cc" mean? - Can you ask. And this is - nothing, as a condition. Yes, transition team can not only be unconditional! Remember the table from the past Our lesson: there was a column, as "Mnemonic conditions." That's it we are here apply. For example: Team Address # 6000 JP C, # C003 # 6003 JP (HL) Imagine that in the PC recorded the number of # 6000 and the processor begins execution team. Along the way, PC will increase by three (Exactly as many bytes is the command JP C, # C003). In the process, will be checked whether the condition "C". And it satisfied if CY = 1. After performing Team Z80 # C003 puts the word in PC and begins to execute the following command, which is now located at # C003. And if the condition is not satisfied (CY = 0)? Then, the CPU is no new data to put the PC will not, and following commands, which will serve as Z80, is located on at # 6003. And this team is are all the same transition, but now no conditions will not be checked, the PC will overwrite the contents of "HL", there is will move. Referring to the condition "C", for instance, and all obo others. Here are the mnemonics conditions You can use the command "JP cc, nn": C, NC, Z, NZ, M, P, PE, PO. Complex is nothing in them do not. Consider their all in order: Z - Zero (Z flag is set) NZ - not zero (the Z flag is cleared) C - carry (the flag C is set) NC - no carry (C flag is cleared) M - a negative result (flag S mouth stalled) P - positive result (flag S Throw shen) PE - Parity or pereponenie (set Flag P / V) PO - no parity / overflow (flag P / V reset) Now it remains to draw attention to the that the conditional can only be a transition directly to the specified address, and If the address is in register pair, then transition can only be unconditional. The relative shift This transition differs from the direct order that the team is given no direct address, and shift, which the processor does not write in the PC, and adds to it. And in this case, the displacement can be represented both positive and negative number (remember the lesson in "Optocouplers" | 21), so that the transition can be made in any direction from the team. Recorded a team like this: JR e JR cc, e JR mnemonic comes from the English "Jump relative", which means "on the transition." Note the following command: "E". It means the amount of displacement with respect to the contents of PC. As you can see, and this command can be conditional. The rules are deystvut similar to "JP", but here is a set of possible conditions somewhat less. Checked only the C, NC, Z, NZ. Consider the mechanism of action commands JR in a conditional form: Team Address # 6000 JR NC, 2 # 6002 LD A, 5 # 6004 ....... So, Z80 starts to fulfill the team at # 6000. Since it is double-byte, PC will be increased by 2 and after command will be equal to # 6002. In process the command processor checks the condition of NC on the truth. Now, if CY = 0, then to the program counter will be added and still command option JR (in this case 2), the operation "LD A, 5" will be skipped and the next executed command will be the one that is located at # 6004. And what is a meaning in such a team? How it differs from JP? It's very simple. After all, the team operates with a JP and is a direct address to the memory of three bytes. But sometimes the transition should be made to short distance (as in our example). Then it makes no sense to spend extra bytes for a full address, if enough range 128 ...- 127. In addition, conditional jump command can be useful when writing a so-called relotsiruemyh procedures for which the address location in memory do not matter (to these procedures, we'll get - all the time). By these things add a little question: what will be, if in our example, instead of "2" put a "-2"? And finally, as usual, on the impact of instruction "JR" and "JP" on the flags. So - all These commands abslyutno no effect on Flags do not have! Looping Consider the case where we need to organize the cyclic execution of some portion of the program. Suppose that in the case "B" stores the number of repetitions. So ... LD B, 5 LOOP ........ body cycle ........ DEC B JR NZ, LOOP The above fragment may initially cause some confusion: "And what is LOOP? "The answer is simple. The fact is that when You write the program in any assembly language, it provides for the convenience of such concept as a "mark". All references to Ares at her, and not a real address, which is very facilitates writing and debugging programs. A word to remember is much simpler than, for example, "# 7A3E". Yes, and if necessary add a line or two in a ready-made program for you will not have to recalculate all addresses - this will happen automatically. I should note that this fragment Programs like this, you will almost never encounter. Combination of commands DEC B: JR NZ, LOOP almost always replaced with a single command DJNZ, reads like a "decrease in and go, if not zero. "This team - two-byte, and that its use gives some saving memory. Again, because of her case in the commonly used for organizing counters. It has a disadvantage here: assume only to 256, but it can be avoided use nesting. Now we rewrite our program to Given all vysheizozhennogo: LD B, 5 LOOP ........ body cycle ........ DJNZ LOOP To be continued ...
Other articles:
ZX-waggons - Overview of electronic media: ZX-News in 1947, Don News 7, Buzz 17. |
Likbez - Assembler view from afar: Commands unit increments, the direct and relative transition, the organization of the cycle. |
Lit. Page - Grief "Y-II" (Diary - continued). |
Feedback - A few words about Optocouplers 27. On the applications of the Spectrum. |
Advertising - Advertisements and announcements ... |
Category X - On Lviv market. |
four kilobytes - the 200 th anniversary of Alexander Pushkin. |
Similar articles:
В этот день... 23 November