Programming - using the stack to speed up programs.

Realtime #01
                                    
                                    
MAXWELL ═══════ Today we will tell you how you can greatly increase the speed of your completing your programs, namely, about such a programming technique as "pour over the stack." Benefits this method speaks for itself. But let’s not get ahead of events and dis- Let's talk about everything in order. Let's give this example: You probably know that for cleaning screen programmers usually use are called by the program using LDIR commands. I quote it below ( execution time is given in brackets commands or groups of commands when you select completing it in a cycle in process cycles sora ): LD NL,#4000(10) ; start of screen area LD DE,#4001 (10) ; start of screen +1 LD BC,#17FF (10) ; size of the area to be cleaned LD (NL),L (7) ; clear LDIR (21*B143) ; complete to completion RET Total 129040 processor cycles. But to clear the screen in programs with many moving objects (on- example in music demos with using two pages of screen memory) this procedure is not suitable due to its slow execution. But the 'transfer' method is very suitable walking on the stack'. This method will allow remove everything from the screen so quickly extra that you won't even notice this. The whole 'zest' lies in that we, with one command PUSН, we carry two bytes at once into the area where which we want to cleanse. Use a new method: LD (STEK),SP (20) ; save stack for return LD NL,#0000 (10) ; what will we enter LD SP,#S800 (10) ; install the stack LD BC,3072 (10) ; area size /2 LOOP PUSН НL (11*3072) ; enter DEC BC (B*3072) ; check for ending LD A,B (4*3072) ; OR C (4*3072) ; JR MZ,LOOP (12*3072+7); if not everything, then let's go to LOOP LD SP,(STEK) (10) ; restore stack RET STEC DEFW 0 Here you need to take care to save the location address stack, and at the end of the procedure it will need to be restored. Otherwise you deo effects and computer freezing You are guaranteed. In a couple of SP registers we enter the address of the beginning of the area at- rebuts, as the stack grows from above down. The BC 'sits' in the register pair number 6144 (screen area size) reduced by 2 times, since when microprocessor execution of commands PUSННL is placed on the stack immediately 2 bytes - H and L. Well, let's see what we have there counted in clock cycles: 113731! Replacement what's the difference? But that's not all, what old 'SPESSY' is capable of! Why execute unnecessary commands for checks 'the area has run out or else not yet' ? After all, you can do the same seven other way and put one- well, the same command several times in a row Here's what might happen: LD (STEK+1),SP (20) LD НL,#0000 (10) LD SP,#S800 (10) PUSН НL (11*3072) PUSН НL PUSН НL ....... u так 3072 раза ....... PUSН НL SТEК LD SP,#0000 (10) REТ Во первых мы засылаем значение сте- ка сразу в команду восстановления, а во вторых мы не используем лишние комманды для возврата в цикле , хотя такой способ и занимает больше места в памяти. Итакhere we have 33842 cycles!!! So how? Have we convinced you? By the way, what- would make it easier to 'clog' all these PUSNHL in the TASM 3.00i assembler is pre- DEFS function is provided, . In our case it will look like this: DEFS 3072, 229. All you have to do is paste this line to the place in the program where You must have commands PUSН NL. So now you can feel free to sit down at the keyboard and write write programs without thinking about what all this will work too poorly lazy. This technique can be used not only ko with PUSН commands, but also with any others. So, for example, if you want display several dots on the screen in predefined addresses (eg measures of a star in some cosmic what shooter), then this can be organ- lower it like this: LD B,1 ; this is the point (%00000001) LD (STEC+1),SP ; remember the stack LD SP,ADDR ; installed a stack on an array of addresses POP НL ;I took the screen address from the array LD A,(NL) ; what is on the screen goes to register A OR B ; placed a point LD (NL),A ; returned to the screen POP НL ; repeated LD A,(NL) ; OR B ; LD (NL),A ; ....... ; as many times as necessary; ....... ; STEC LD SP,#0000 ; restored stack RET ; ADDR DEFW #4000 ; here is an array of addresses DEFW #4020 ; screen area DEFW ..... ; u so on You can check it - it will work! Well, about the application of everything that is about I said something here, you can guess for yourself. Perhaps from the next number I I'll start giving specific examples various special effects and programs. If anyone is interested, call please write, send your work - we will support you. Желаю удачи !!!

Share your thoughts about the article