Info Guide #01 |
|
Assembler - A short lesson in assembler for lamers.
A brief lesson in assembler for lamers Alone Coder Preface. Until recently I believed that light is no longer suschestuvuet people who do not just can not write a computer game, and - terrible to say! - Can not distinguish the case from the port. My naive assumption rubbished recent visit to the Ryazan radio market in Gorky Park. - Do you have any program for Sinclair? - Said the young man came to the table in the center of the site. I have already alerted the word: maybe someone else does not know, but the computer on which you are viewing this text, called the ZX Spectrum, from the Latin word meaning, it seems <Rainbow>. I went closer. - Sorry, I'm a local encoder. My name is Dima Bystrov ...- I began awkwardly. Into conversation, we exchanged addresses and goodbye, promising to visit each other. A couple of weeks, we met again; each of my program expressly surprised my new acquaintance, who probably have not seen fresh software already for two years. I threw him major utilities and wished soon write something good. It took another two weeks ... He returned my drives and brought the old glossy magazine cover. The magazine was called <Home Computer>; It was, in truth, pisyushny Journal ( - adjective from , which we term, coders, using instead IBM PC). Category <HELP!> Most interested me. This topic questions readers and their responses, the data certain Dr. HELP. When I came across These questions, I could not help laughing: . The next question, I specifically note. Now you'll understand what I'm generally led this conversation. Get ready ... . Here is a description of how the laptop should be vacuumed and wiped with a cloth. But the most important authors of the journal is not able to understand: the reader BELIEVE the hardware can be spoiled, Did not press the wrong button! Dispel these myths and help readers eccentric computer magazines to feel themselves masters of the computer and this should be Page. Chapter 1 how to overcome fear and to write elementary program in assembler Firstly, I will give some basic concepts that we use and application of which you can not avoid in the future. CODER - the man who wrote (do) program in machine code. Machine code - instructions the processor Z80 in the computer, which reduce to simple computing operations. Program in machine code is always faster kakihlibo written in other languages (BASIC, Pascal, C ,...). ASSEMBLER (AFM) - a method of recording, MK on paper (screen, disk, ..) in the form of letters and numbers. And A. - a program that translates ACM in MK Lamer - not a coder. Also, LA - who likes to spoil someone else's program. Hacker - a good coder, which alters someone else's program for the better (eg, inserts eternal life, beautiful screen saver packs the game, rarely - bugfix in the program or add to it new opportunities). Memory - computer memory. In a narrower sense - random access memory (RAM, it can still be recorded). Byte - character / number from 0 to 255 / number -128 to 127, and corresponds to -128 128, -127 corresponds to 129, etc. Of these, is memory. BIT - one-eighth bayta.Soderzhit minimum of information: either 0 or 1. (Despite the endless repetition of meaningless phrases in the literature, I hasten to inform you that at least information - it is a zero bit, and if you say that does not think so, then give more example of a smaller data: 0, with probability 1 / 3 or 1 with probability 2 / 3.) Numbered in a byte from right to left with zero, and the sum bits, multiplied by two in their degree of numbers give the value of bytes. Assembler is the best ALASM. It has 2 modes - command and editing. The first set after loading. Log in second - to collect EDIT (Button E) name. For example: EDIT AAA. Immediately you enter the newly AAA.H text file, and you can change it. Now you're sitting in front of a screen, and the head Your reason was bad think. In such cases, I write first and last lines of future programmy.Vot they usually look like: ORG 25000 RET The first indicates the location in memory where the program will work (from 24576 to 65535) and not a command processor (ie does not take place). Last - exit program (or subroutine, which is essentially the same thing) to where it had dared start (cause). Teams such as the directives ORG Assembler. Another example of a directive: ENT She needs to know with any place to start your teeming ORG'ami program, since some Assemblers (MASM) prefer the latter ORG, others (ALASM) first, and some - generally refuse to broadcast (assemble) the program without ENT or ENT $. Lines for inserting commands in the middle of the text apart buttons SS + W (<>), and the shift buttons SS + Q (<=). First this may seem inconvenient, but then you find it very good way. Useful guidelines DEFB "text", # 3F, 5, "a" She puts in the place of memory where would have placed the following command line program, any set of bytes (characters). Similar team DEFW, but it sticks Bytes two and can not stomach tekstov.Esche enigmatic team DEFS 40,1,2,3 - which 40 times in a row shoved into the memory bytes 1,2,3. Lattice # indicates a hexadecimal number # 3F = 3 * 16 + F, where A, B, C, D, E, F, respectively are 10,11,12,13,14,15. But I digress. That our program has done anything, although we still do not able, blatantly using some strange program from the ROM (there are many interesting things): ORG 25000 CALL 3582 RET Now we call the program shift the whole screen up on familiarity (SCROLL UP CHR $) in the same way as our program caused the system (Basic, ALASM, TR-DOS ,...). To start this plagiarism (hazing?) and see how it works, exit edit mode to command mode. For Click EXTEND; will be waiting command line editor. You can enter different commands: B - beginning of the text, E - end S - search for something, X - replacement, and much more I do not remember what, but you have to press Q - Quit. Now the program is written: SAVE. After this assembliruem: ASSEMBLE and run: RUN. Screen is moved up, cheers. You can also call it from BASIC: QUIT (Yield in BASIC), RANDOMIZE USR 25000. Return to ALASM - RANDOMIZE USR 23600. From built-in debugger StS: DEBUG (Debugging), JUMP 25000. RANDOMIZE USR 23600. Go to Edit - EDIT. For a complete mastery of the system you should read HELP'y to ALASM and StS.Dalneyshy text for compactness implies what you already know how to handle them. Chapter 2 everything little by little The Register is a small variable which is not stored in memory and the processor. In the case of 1 byte. The names of registers: A B C D E H L I have listed only the general purpose registers, just the same registers 24.6 last collected in pairs, called simply <Register pairs> (reg pair, rp) and can used as a double-byte number / address (160K of memory simultaneously addressed 64 K, so the byte address is transmitted 2 bytes) Register can be set to team LD E, 100 or LD BC, 40000 or LD A, H but not LD HL, BC A label (label) is similar to the row number in BASIC for islyucheniem so that it does not need placed in each stroke.Metku can use expressions instead of the relevant chisla.Metka contains 7 letters long and starts with the letter. Example: ORG 25000 LD DE, TEXT LD BC, TEND-TEXT CALL # 203C; again the program from the ROM RET TEXT DEFB "TEXT - a label, 13 TEND Printed text at TEXT = 25010 length TEND-TEXT = 17 bytes. All of these calculations produces ALASM during translation. If you do not want the program to run only in a row, you can use go to label: ORG 25000 LD BC, # 50F0; B = # 50 = 80, C = # F0 = 240 JP 8933 Appears on the screen point. JP 8933 passes control to the ROM, which of course, already contains a RET, so that we can do not worry about the return. Go no more than 127 bytes forward / backward is called a short or a relative address, and writes JR. When you need to do something more times (to perform the cycle), in particular, the command DJNZ address: ORG 25000 LD B, 80 CYCLE LD (BUF), BC; write; 8933 Port B LD C, B CALL 8933 LD BC, (BUF); in BC old value DJNZ CYCLE RET BUF DEFW 0 Draw diagonal.DJNZ subtracts from B unit, and if it does not zero, becomes at that address. It is easy to calculate that so the loop is executed exactly as many times how much was in the B at the entrance to his (t.e.80). Parentheses indicate that we have in mind not a number, a memory cell with this number. LD (BUF), BC BC writes at BUF. Use cells with labels for the storage registers is inconvenient, so contrived Team: PUSH rp (shove) and POP rp (push). The first stores rp in the memory area called the stack, the second takes out the current value of rp. Many meanings, put on the stack are removed from there in reverse order. There's also stored the address to which passes the command RET. Therefore the number of PUSH and POP should be the same. Example: ORG 25000 LD B, 80 CYCLE PUSH BC LD A, 80 SUB B LD C, A CALL 8933 POP BC DJNZ CYCLE RET Draw a diagonal inverse. Team SUB r belongs to the category arifmeticheskologicheskih and subtracts from the A register r. Other well-l command: ADD A, r; add r to A ADD HL, rp; rp added to HL ADC A, r; A = A + r + CY ADC HL, rp; HL = HL + rp + CY SBC A, r; A = A-r-CY SBC HL, rp; HL = HL-rp-CY DEC r / rp; reduce rp 1 INC r / rp; increase rp 1 AND r; bitmap A and r is written in A. Obnulyayutsya all the bits of A, which correspond zero bits r. OR r; bitwise OR A r is written in A. In unit address all of the bits A, corresponding to which bits r were equal to 1. XOR r; the same, but the bitwise XOR. B each bit of A is written to 1 if bits this number in A and r were different, 0 otherwise. CP r; A comparison with the r CPL; A = 255 A.Vse bits are inverted (01) NEG; A =- A. DAA; BCD correction; produce it immediately after the addition / subtraction, if s / v'emye numbers are BCD. It is unlikely you will come in handy t.k.d e-numbers - a method of recording two digits in a byte, and Dr Dr # 34 is assumed to be 34. A few words about what is CY.On is called a flag (sign bit) transfer and is set to 1 if the result of addition exceeds 255 (65535 in case of 2-byte addition) or by subtracting the larger number of smaller, or in other special specified circumstances. Another flag is used Z, which equals 1 if the result of a-l operation is zero, and zero otherwise. For He is called a flag of zero. All Flags are in the flags register F, which, together with register A is yet another (fourth) rpAF, which, unfortunately, can not used in double-byte calculations. Comparison - the same deduction, but the result was not memorable, but only appropriately changing the flags: Z = 1 if A = r; Z = 0 if A <> r; CY = 1 if A <r; CY = 0 if AЄr. Why flags? Yes, and that at it then get the condition branching program, calling the latter or out of it. Team conditional short transition looks like: JR cc, <label>, where cc - condition: Z - Z = 1 Because there are flags, that is NZ - Z = 0 and more rarely ispolzuC - CY = 1 emye conditions used in the koNC - CY = 0 manda a long transition JP. That is, if the condition is satisfied, it should make the transition, otherwise - no. Chapter 3 Several important practical tips The program becomes your only when you were working on making it. By the way, we should not also be happy after writing any program, if the latter is up to you was written at least one person. There are two ways of writing programs at ACME: on paper, pen / pencil and then set on the computer and sitting directly behind / in front of ekranom.Pervy method is good that you can write programs away from your computer, and bad low productivity and increasing the number of errors; the second bypasses the shortcomings of the first and loses dostoinstva.Zhelatelno his own two. Despite the fact that the Pentagon is 128-st, remember that not every computer - the Pentagon. Consequently, it is not necessary to use two screens, when quite alone, as well as eat all the 71680 processor cycles, or to put the first team to break EI. NEVER operate the on / off the drive to verify that the disk drive, or play through the music! In 5313 the domestic drive at the same time disrupted the mechanism of lowering the head on the drive and the drive can not write INFORMATION! Keep copies of your important programs on their own and / or foreign disks. The latter sometimes even safer. Do not have copies of stuff. Keep track system. Save your time, e.g. Do not look twice the same TV shows, do not go at discos and drinking. Remember: the life of the a man and a short, but remember about you only if you have time to do a lot for it. Learn to speak, print, walk and think quickly. Have patience; errors in programs cases and in the masters of CodeBusters, but they know how to fix them => and you nauchites.Sperva complex structure of the program you will be able to keep in mind, but after a short time regular work on a computer the size of program, typed at a time, will be unrestricted. Do not make a program of pieces! Tags recommended that programs should come up with this: the name of the subroutine + letter / figure, for example: MOVTXT0 in semiconductors MOVTEXT; READKYQ in READKEY; can SYMN13 in SYM. If someone tells you that a certain method is always the best, do not believe him. For example, consider moving pamyati.Medlenny LDIR indispensable in a text editor; LDI is very useful for vertical shear screen and the lack of memory for sdvigalku; POP HL: LD (...), HL is used in shifts, multicolor and mapping; POP rp ... PUSH rp well to accelerate mapping (sm.statyu in the next issue); LD rp, ... PUSH rp hitherto only used for drawing reticula, although in one fell swoop increases the width Multicolor 24 CHR $ + allows us to derive screen in one interrupt (sm.statyu). Despite the fact that the switching of two images gives a variety of graphic modes, remember that this leads to flickering screen, which may fit for trial, but not for the program that the user active use. You can not even remember: Quod licet Iovi, non licet bovi, which means that <What could Jupiter, it is not allowed to bull>. Switching three screens do not use ever, because it entails not flicker, but the blinking screen. Do not limit the complexity of the compiled Your programs, justifying the fact that someone is a very smart or he has more free time than you have. No gods pots fired! I do not have free time, but I wrote this journal! To be continued ..
Similar articles:
В этот день... 21 November