Problems with learning assembly language.

ZXNet echo conference «code.zx»

From 812/29.00 To Butch 12 November 1997

Big hello to you, Butch!!! Butch to All wrote the other day: [drenched in beer] Q> 1) I didn’t understand flags (how do they work? B> melt in the program) I. Zero flag (Z) This flag will be set: 1. if the contents of register "A" are equal to zero: LD A,1 - enter in "A" 1 DEC A - decrease "A" by one. Result: A=0 flag Z=1 2. When comparing numbers (with battery only): LD A,(23560) - enter into “A” the contents of cell “23560” CP 32 - compare with "32" Meaning: The contents of cell “23560” are entered in “A” Compare "A" with the number 32. If "A" is equal 32, then the Z flag will be set. In accordance with this, we can set the conditions Via: JP Z,ADDR - go to ADDR address if "Z" is set. CALL Z,LABEL - call a subroutine if "Z" is set JP NZ,ADDR1 - go to address ADDR1 if "Z" is cleared. CALL NZ,LABEL1 - call the subroutine if "Z" is reset. example: .... LD A,(40000) enter the contents of cell 40000 into “A” CP 40 check if content is equal to 40 JR Z,LABEL1 if equal, go to label LABEL1 .... However, the Z flag will not be set when commanded LD A,0 II. carry flag ( CY ) The carry flag will be set if when adding/subtracting two registers (orregister and number or register pairs) the result will be a number greater/less than 255. LD A,255 - enter 255 in “A” LD B,1 - enter 1 in “B” ADD A,B - Add and place the result in "A" Meaning: In “A” we enter 255, in “B” 1. when added the numbers 256 are obtained. However register "A" holds a maximum number of 255. Therefore, 1 is written in “A” and set- The “C” sign comes into play. There are commands that take into account the flag Renosa: ADC - Addition taking into account the sign "C" SBC - Subtraction taking into account the sign "C" LD HL,16384 - enter in HL 16384 LD BC,16383 - bring from aircraft 16383 SBC HL,BC - subtract taking into account "C" If the "C" sign was not installed, then when subtraction in HL will be 1. If the sign has been is set, then when subtracting in HL it will be 0. And of course the conditions: CALL C,LABEL1 CALL NC,LABEL2 JP C,LABEL3 JP NC,LABEL4 example: LD A,10 - In “A” we enter 10 LD B,D - in “B” we enter the contents of the register “D” ADD A,B - add A with B, the result is “A” CALL C,LABEL2 - if the amount is more than 255, then go to the label LABEL2 III. Negative result (S) The flag will be set if, during arithm- logical or logical operation will receive - negative result. LD A,0 enter in "A" 0 DEC A decrease "A" by one. Result: in A=255, carry flag CY=1, negation flag S=1. In general, I don’tI remember certain teams that would quickly this flag was set or reset - get out. Terms: CALL M,LABEL1 - call sub. if the flag is set. CALL P,LABEL2 - call. cond. if the flag is reset JP M,LABEL3 - transition to LABEL3 if the flag is set JP P,LABEL4 - transition if gflag is cleared IV. Overflow parity flag (P/V) But regarding this flag, I would like to will be enlightened.:(( B> 2) And, based on this, I did not understand the work In> commands:RLCA,RLA,RRCA,RRA,RLC,RL,RRC,RR, В> SLA,SRA,SRL. В> Whenever I look at it, it’s a nightmare. There are so many of them, everything is different B> are similar, but differ only in different work in In> them, the CY flag. [drenched in beer] Well... this is not a nightmare... 1. RLCA - cyclically shift left. LD A,11 RLCA Meaning: enter 11 into “A” (binary - 00001011) then we shift to the left: A CY<-- 00001011 <- | | |___________| As a result: A=00010110 CY=0 All bits are shifted left, and the most significant bit becomes mvsto junior and duplicates - in the carry flag. 2.RLA Cyclic left shift via CY flag LD A,11 R.L.A. --CY<---00001011<-- | | |_________________| As a result: A=0001011? CY=0The bits are shifted to the left, the most significant bit is is included in the CY flag, and the value of the flag CY is set to the low byte position: if the flag was set, then the low byte will be equal to 1, if reset, then 0. 3. RRCA Similar to RRCA, except all bits shift to the right: LD A,11 RRCA A ->00001011--->CY | | |__________| As a result: A=10000101 CY=1 4. RRA Similar to RLA, but all bits are shifted turn to the right: -->00001011-->CY- | | |_______________| As a result: A=?0000101 CY=1 If the flag was set, then the most significant bit will be equal to 1, if reset, then 0. 5. RLC S, RL S, RRC S, RR S. Similar to the above, but instead of S maybe not the battery, but: B,C,D,E,H,L, (HL), (IX+D), (IY+D) Otherwise, they fully correspond to them. 6. SLA S: shift to the left. CY<--00001011-- 0 Result: A=00010110 CY=0 The most significant bit of the register is written into the flag transfer, and in place of the least significant bit is written is set to 0. 7. SRA S: shift right without changing Old our bit: -->00001011-->CY |__| Result: A=00000101 CY=1 or: -->10001011-->CY |__| result: A=11000101 CY=1 When shifting, the least significant bit is written to the flag Cy, and the most significant bit remains unchanged. (this way you can divide by 2 taking into account volume of the sign). 8. SRL S: shift right. 0-->10010110-->CY result:A=01001011 Cy=1 When shifting to the place of the most significant bit of the record, 0 is written and the least significant bit is written to carry flag. That seems to be all. I hope it will be understood - but, well, if not... then don’t blame me, o’- explained it as best he could. В> Bye. Best regards, Sergej. P.S. don't forget that each team shifts bits for only one position. -+- IS-DOS 4.5

From John Stunner To Butch 14 November 1997

Glad I found you alive, Butch!!! However, on 08 Nov 97 you wrote something addressed to All: B> 1) I didn’t understand the flags (how they work B> melt in the program) Flags are one of the basics. And you need to understand them very well. I'll try it for you I can explain everything as well as I know it myself. To begin with, I will list all the main flags of the Z80, and, in principle, processors in general: Z - zero flag C - carry flag V - overflow flag S - sign flag (negation) Let's say you need to subtract one number from another. A, more specifically, subtract register C from the battery (register A). This is done with a simple command SUB C Let's take the situation when A=C=5. That is, C-A=5-5=0. The result was zero. In this case, the zero flag one is recorded. (let me remind you, a flag is just a bit of the flag register F). In order for you to understand all the benefits of flags, I will give an example where this can be done use. If you carefully studied the commands, then you came across a number of commands, such as: JP NZ,ADDR JP Z,ADDR What does the JP Z,ADDR team do? It first checks the zero flag, and if it is installed, then it makes the transition to ADDR. Well, I hope you understand the whole ideology. Now I will describe the purpose of all the flags: Z - already explained. C - is set when the operand is transferred. Let's say we subtract the number 35 from 30. We get the number 251. And what could beIt is easy to recognize that such an inevitability has happened; the transfer flag is set. S - is set in case of receiving a negative result. The point is that in an 8-bit register you can store numbers not only in the range from 0 to 255, and also in the range from -128 to 127. How is this achieved? Very simple, the most significant bit of the register is treated as a sign. Look what it looks like in binary representation of the number 127 bit number 76543210 value 01111111 If we add one to the number 127, we get the most significant bit set. And when working with signed arithmetic, this will not be considered as a number 128, and as a number -128. The sign flag serves to signal whether the value or positive. V - Installed just in the case of the situation described above, when it occurs change of sign to the opposite. Simply put, if the result of an operation is 7 bits If the operand is inverted, the overflow flag is set. B> 2) And, based on this, I did not understand the work B> commands:RLCA,RLA,RRCA,RRA,RLC,RL,RRC,RR, B> SLA,SRA,SRL. B> As soon as I look at it, it’s a nightmare. There are so many of them, everything is different B> are similar, but differ only in different work in B> them, flag CY. B> For example: B> RLCA CY<----7<--A<--0<-- B> ^ | B> | | B> -------------- Very simple. Let's say the number in the accumulator was 119. When it comes to shifts, it is easier to switch to the binary number system. Accordingly 127 equals in binary system 01110111. We execute the RLCA command. All bits move to the left. 76543210 was 01110111 move 01110111 the bit that “crept” outside the register “creeps” into ml. discharge and at the same time, copied to flag C. we get 11101110 flag C = 0. B> SRL 0-->7-->S-->0-->CY About the same thing, only the shift occurs to the right, the evicted bit is copied in C, a zero is entered into the freed low-order bit. B> What is this? How is this? Have a beer ;) With best wishes, John. ===> #/FUCK/# *mUlTiMeDiA*, #/MAKE/# more $dEmOs$ ===>