Polesse #04
16 апреля 1999 |
|
Assembler - Flags, comparison operations and transfers.
ASSEMBLER FOR BEGINNERS (C) THINKER Hello dear readers! We continue this section for those who want to learn how to program in assembly language. Today we talk about flags, comparison operations and transfers. But now you need to understand such a thing as a label. Tags - Is the same address, but recorded in a mnemonic form. If you know Pascal, you will understand what I mean. For those who have not yet understood: Imagine a program in BASIC, but instead line numbers, words: BEGIN PRINT "*" CONT GOTO BEGIN BEGIN and CONT - a label. It remains to add that the labels are created for convenience only and are the same addresses. Now you can continue. To start us be clear for myself: the flag is a bit that can be "1" or "0". In BASIC, for example, there is a systemic variables that contain exactly the flags. In the assembly language for this purpose is served by a flag register "F" (register pair "AF"). It can not write any value with the entity "LD" (Remember what that is?). The value of "F" can only be changed indirectly. So we gradually moved on to comparison operations - CP. This entity is only compares the register "A" (accumulator) register with another or with a different value. CP B; compare register "A" to register "B" CP 10; compare register "A" with the number "10" One of the flags in the register "F" - is a flag called "Z". It is installed if the "A" is equal to the value, or register with the which we compare. However, we need to use the value "F", otherwise "CP" will not make sense to apply. Now we need to talk about unconditional jump. During the transition team responsible processor "JP", which can be compared with operator BASIC GOTO (GO TO). JP 15616, and the transition to the address 15616 JP LABEL; the transition to a label LABEL From the unconditional transition is very easy to make a conditional. We can go to the address on the condition: the flag or no. For example: LD A, 5; record in the battery number "5" CP 5, compare the "A" with the number "5". Setting , Is the flag "Z" unnecessarily "A" is equal to 5 JP Z, 15,616, 15,616 go to the address, if the flag "Z" ; Set RET; return There are several conditions that can be used for conditional branching: A = S ---> Z A <> S ---> NZ A <-S ---> C A> = S ---> NC For example, we need to go to the address ADR, if the "A" is not equal "10." CP 10; compare register "A" with the number "10". In dependence depending on the result of comparison sets Xia or reset flag "Z". If A <> 10 (A <> S), then the flag "Z" discharged. JR NZ, ADR; go to ADR, if the flag "Z" is cleared. RET; return. Now, let's write a program from a little more complicated. Quest: Wait pressing any key when you press "1", then print the character "1" if you press any other key, then output "0". I will say that the code of the last pressed key is contained at 23,560. Try to write your own solution if you do not, here's the answer: ; PREPARING THE SCREEN TO THE -------------------------------- CALL 3435; clear screen LD A, 2; installation flow CALL 5633; Print LD A, 0; zero the system variable LD (23560), A; containing the code of the last key pressed ; WAITING Pressing ----------------------------------------- WAIT LD A, (23560) record in the "A" code of the last. pressed. keys CP 0; if 0, then JR Z WAIT; go to the label "WAIT" - the beginning of the block ; COMPARISON. IN CASE "A" - the key code ---------------------- CP "1" compare "A" code kalvishi "1" JR Z, PR_1; if "=", then go to the label PR_1, ; Otherwise - continue ; Prints "0" --------------------------------------------- ------- LD A, "0" puts the battery in the number "0" RST 16; call the routine battery RET; return ; WITHDRAWAL "1" --------------------------------------------- ------- PR_1 LD A, "1"; actions are similar to the above RST 16; RET; As of today, I think enough. If there are any Voros, write, call, generally find a way to connect, I explain. Until we meet again.
Other articles:
Similar articles:
В этот день... 21 November