flat assembler
Message board for the users of flat assembler.
Index
> Main > A bit of programming styles Goto page 1, 2, 3 Next |
Author |
|
neville 23 Jul 2008, 21:32
[edit by Loco] This discussion started here: fat12 root directory [/edit]
abuashraf, just to clarify more: some people like to write "self-commenting" code so writing "1 shl 3" instead of "8" reminds them that it is bit 3 being tested in this case. By the way, your "putc" subroutine is not good programming practice because: 1. it has 2 exits 2. it calls itself. |
|||
23 Jul 2008, 21:32 |
|
abuashraf 24 Jul 2008, 04:47
thank you guys for your help,
@neville:would you please write a new 'putc' subroutine,and show me how to improve it. Thanx. |
|||
24 Jul 2008, 04:47 |
|
neville 24 Jul 2008, 22:16
Programming style is a very individual thing, but the 2 issues I mentioned are generally recognised as undesirable for ease of future program maintenance or debugging.
Of course you can stick with the BIOS teletype function int 10h/0eh, but: 1. It is better to jmp back to the first "popa", than to repeat the "popa" and "ret" instructions in the tab code. So all subroutines have 1 entry and 1 exit, both with defined conditions. 2. If you don't want to repeat the teletype function in your tab code, then create another subroutine for it. (I have it as one of my standard library functions called ttype which I can call anytime) Code: ;TELETYPE: print ascii [al] at current cursor using BIOS teletype mode ;enter with char in al, cursor set TTYPE: PUSH AX MOV AH,0EH INT 10H POP AX RET ;exit with cursor advanced unless bell (al=7) 3. I would definitely replace the 7 lines of repeated code with a loop such as edfed suggests, but that is a style choice too. I code in upper case, my style! It is better for you to actually write your own code though; you'll get more experience that way _________________ FAMOS - the first memory operating system |
|||
24 Jul 2008, 22:16 |
|
LocoDelAssembly 25 Jul 2008, 00:26
Another way is taking advantage of the interpreter layer:
Code: SPACES_FOR_TAB = 7 . . ; Rest of code here . . repeat SPACES_FOR_TAB ; Or just repeat 7 if you are not interested in auto-documented numbers nor in program parametrization call putc end repeat With that the assembler will assemble for you the seven calls and will be easy to change the number of calls in future without having to type or Copy&Paste more calls. PS: abuashraf, if you prefer I can split from this thread the parts that are not really related to FAT12, just ask if you want me to do the split. |
|||
25 Jul 2008, 00:26 |
|
abuashraf 25 Jul 2008, 01:25
Quote: if you prefer I can split from this thread the parts that are not really related to FAT12, just ask if you want me to do the split. That would be great. sorry guys,but till now I don't know what's the problem with a subroutine that calls itself. |
|||
25 Jul 2008, 01:25 |
|
abuashraf 27 Jul 2008, 00:20
could some one explain to me what's wrong wiith a subroutine that call
itself back. |
|||
27 Jul 2008, 00:20 |
|
neville 27 Jul 2008, 01:27
abuashraf wrote: could some one explain to me what's wrong wiith a subroutine that call OK, seeing it was my comment to start with: 1. it creates unstructured convoluted code which makes debugging much more difficult 2. it can easily lead to unexpected side effects such as CRASHes due to stack overflow or corruption, unintentional endless loops etc. For example, what would this do? Code: main: call sub ret sub: call sub ret To be honest, I was a bit horrified when I saw your 'putc' code, and if you can't see what would happen in the above example, I'm afraid I would never employ you as a programmer! Yes, there are programming styles, but some things are very fundamental and should always be avoided. _________________ FAMOS - the first memory operating system |
|||
27 Jul 2008, 01:27 |
|
rugxulo 27 Jul 2008, 02:57
Isn't this the old "recursion vs. iterative" argument we've seen before?
|
|||
27 Jul 2008, 02:57 |
|
sinsi 27 Jul 2008, 03:54
The only time I use recursion is for enumerating files/directories since you can keep a local copy of WIN32_FIND_DATA, but you need to be careful about stack overflows. I have seen it used for calculating factorials and the like but a recursive function is fairly rare in my experience.
Just a note about using tab, it isn't 8 spaces, it aligns itself to cursor x=0/8/16/24 etc. If the cursor is at x=0, then 8 spaces are printed, making it cursor x=8, but if cursor x=6 it should only print 2 spaces, still making it cursor x=8. |
|||
27 Jul 2008, 03:54 |
|
bitRAKE 27 Jul 2008, 14:52
When using recursion we must insure exit condition is well defined - which is often easier said than done. Despite this I use recursion whenever possible. Sometimes, it is easier to think of the problem in terms of recursion:
Code: .recurse.0: push ebp .recurse: cmp [ebp-4*(GAME.COLUMNS+1)],ebx je .Up .rUp: cmp [ebp+4],ebx je .Right .rRight:cmp [ebp+4*(GAME.COLUMNS+1)],ebx je .Down .rDown: cmp [ebp-4],ebx je .Left .rLeft: pop ebp retn .Up: mov [ebp-4*(GAME.COLUMNS+1)],eax push .rUp push ebp sub ebp,4*(GAME.COLUMNS+1) jmp .recurse .Right: mov [ebp+4],eax push .rRight push ebp add ebp,4 jmp .recurse .Down: mov [ebp+4*(GAME.COLUMNS+1)],eax push .rDown push ebp add ebp,4*(GAME.COLUMNS+1) jmp .recurse .Left: mov [ebp-4],eax push .rLeft push ebp sub ebp,4 jmp .recurse When a value matching EBX is found adjacent to (square) EBP then EBP is pushed on the stack along with code pointer for further processing. Then EBP is updated to adjacent square and recursion on that square takes place. Exit is insured by placing a boarder around 2D grid with values outside the range allowed by EBX (like a fence on a yard). I know many avenues for optimization exist with the code. The goal was to be brief and repetitive enough to make the code easy to digest on future inspection. _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
27 Jul 2008, 14:52 |
|
neville 28 Jul 2008, 01:56
I'm quite surprised that some people are defending recursion for any reason. If my life depended on a piece of software not failing, I would not allow it to have any recursive code.
Why would you use recursion for calculating factorials, for example? No stack is infinite in size, so you can guarantee that stack overflow will occur at some point. Iteration is definitely the way to go. I don't believe it is a matter of programming style but just common sense. The only "advantage" of recursive loops is that the code looks "shorter", but when you analyse it from the CPU's point of view it is actually much longer and most often much slower. Maybe high-level language programmers might think they get away with recursion but their compilers might not actually produce recursive code, and they would never even know. Let's face it, HLL programming isn't real programming anyway - only ASM is real programming... And in ASM, I can't think of any application where recursion could really be justified, except maybe in some fancy game graphics, eh bitRAKE _________________ FAMOS - the first memory operating system |
|||
28 Jul 2008, 01:56 |
|
bitRAKE 28 Jul 2008, 06:34
neville wrote: And in ASM, I can't think of any application where recursion could really be justified, except maybe in some fancy game graphics, eh bitRAKE Rewrite the code I posted in non-recursive form then I will reply. _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
28 Jul 2008, 06:34 |
|
Tomasz Grysztar 28 Jul 2008, 08:46
Do you even have a slightest idea how much of recursion exists in the fasm's core code?
Think about it next time you use INCLUDE or nested macros. |
|||
28 Jul 2008, 08:46 |
|
neville 28 Jul 2008, 10:01
bitRAKE wrote: Rewrite the code I posted in non-recursive form then I will reply. OK, OK, I certainly can't be bothered rewriting your code so if you're as good as your word I won't hear from you again!! I wish you well though, and hope you don't spend too much of your life debugging your recursive code. If you get time, take a look at http://www.codeproject.com/KB/recipes/Iterative_vs_Recursive.aspx just one of several references I googled just now. Hey, when you've written as much code as I have, you want to do whatever you can to make sure it works as expected. Debugging is such a waste of time. But sure, system programming is different from coding games. I just hope Microsoft et al don't use too much recursive code in their OS's. Come to think of it, maybe thats what the problem is ... Abuashraf, if you're new to programming of course feel free to develop your own programming style, be be aware of the pitfalls. If you google "programming iteration recursion" (for example) you'll find heaps of references like the one above. Yes, ASM gives you tremendous freedom to do what you like. BitRAKE is right, there are no "rules" as such, just well-meaning advice from individuals with different perspectives, which you also have the freedom to take, or not. _________________ FAMOS - the first memory operating system |
|||
28 Jul 2008, 10:01 |
|
neville 28 Jul 2008, 10:05
Tomasz Grysztar wrote: Do you even have a slightest idea how much of recursion exists in the fasm's core code? C'mon Tomasz, what has INCLUDE got to do with recursion?? Nested macros maybe, but hey, not for me... _________________ FAMOS - the first memory operating system |
|||
28 Jul 2008, 10:05 |
|
Tomasz Grysztar 28 Jul 2008, 10:49
neville wrote: C'mon Tomasz, what has INCLUDE got to do with recursion?? Look into the sources - you can see it for yourself. |
|||
28 Jul 2008, 10:49 |
|
Borsuc 28 Jul 2008, 12:26
As a matter of fact, I don't like recursion either. You can always use iterative methods without pushing (uselessly) the return address of the function on the stack each time, since you know it's going to be the same function.
|
|||
28 Jul 2008, 12:26 |
|
bitRAKE 28 Jul 2008, 15:20
neville wrote:
Quote: I wish you well though, and hope you don't spend too much of your life debugging your recursive code. Quote: If you get time, take a look at http://www.codeproject.com/KB/recipes/Iterative_vs_Recursive.aspx just one of several references I googled just now. Quote: Hey, when you've written as much code as I have, you want to do whatever you can to make sure it works as expected. Debugging is such a waste of time. Quote: But sure, system programming is different from coding games. I just hope Microsoft et al don't use too much recursive code in their OS's. Come to think of it, maybe thats what the problem is ... Ever code any algorithms using graph data structures? Recursion makes it much easier. Quote: Abuashraf, if you're new to programming of course feel free to develop your own programming style, be be aware of the pitfalls. If you google "programming iteration recursion" (for example) you'll find heaps of references like the one above. Yes, ASM gives you tremendous freedom to do what you like. BitRAKE is right, there are no "rules" as such, just well-meaning advice from individuals with different perspectives, which you also have the freedom to take, or not. _________________ ¯\(°_o)/¯ “languages are not safe - uses can be” Bjarne Stroustrup |
|||
28 Jul 2008, 15:20 |
|
Borsuc 28 Jul 2008, 15:34
bitRAKE wrote: ...and then you go off into left field. Simply, it may be easy, but it's ineffective if you do it the traditional way. You push the return address of the function each time, like making 1000 copies of the same variable. What's the point if you know what the function is? EDIT: not to mention duplicate variables |
|||
28 Jul 2008, 15:34 |
|
Goto page 1, 2, 3 Next < Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.