flat assembler
Message board for the users of flat assembler.

Index > Main > A bit of programming styles

Goto page 1, 2, 3  Next
Author
Thread Post new topic Reply to topic
neville



Joined: 13 Jul 2008
Posts: 507
Location: New Zealand
neville
[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.
Post 23 Jul 2008, 21:32
View user's profile Send private message Visit poster's website Reply with quote
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf
thank you guys for your help,
@neville:would you please write a new 'putc' subroutine,and show me
how to improve it.

Thanx.
Post 24 Jul 2008, 04:47
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4237
Location: 2018
edfed
your putc can use direct memory access.

instead of putc, you can use a txt function.

then, mov esi,string
call txt

mov al,[esi]
or al,al
je .end

something like that is my method.

for a tab, simply mov esi,tab

tab db ' ',0

or
cmp al,9
je .tab
...
.tab:
mov cl,8
@@:
mov byte[edi],' '
inc edi
dec cl
jne @b

but it will be certainly bigger than your original putc.
Post 24 Jul 2008, 16:06
View user's profile Send private message Visit poster's website Reply with quote
neville



Joined: 13 Jul 2008
Posts: 507
Location: New Zealand
neville
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 Wink

_________________
FAMOS - the first memory operating system
Post 24 Jul 2008, 22:16
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
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.
Post 25 Jul 2008, 00:26
View user's profile Send private message Reply with quote
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf
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.
Post 25 Jul 2008, 01:25
View user's profile Send private message Reply with quote
abuashraf



Joined: 11 Nov 2006
Posts: 88
abuashraf
could some one explain to me what's wrong wiith a subroutine that call
itself back.
Post 27 Jul 2008, 00:20
View user's profile Send private message Reply with quote
neville



Joined: 13 Jul 2008
Posts: 507
Location: New Zealand
neville
abuashraf wrote:
could some one explain to me what's wrong wiith a subroutine that call
itself back.

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! Wink

Yes, there are programming styles, but some things are very fundamental and should always be avoided.

_________________
FAMOS - the first memory operating system
Post 27 Jul 2008, 01:27
View user's profile Send private message Visit poster's website Reply with quote
rugxulo



Joined: 09 Aug 2005
Posts: 2341
Location: Usono (aka, USA)
rugxulo
Isn't this the old "recursion vs. iterative" argument we've seen before?
Post 27 Jul 2008, 02:57
View user's profile Send private message Visit poster's website Reply with quote
sinsi



Joined: 10 Aug 2007
Posts: 692
Location: Adelaide
sinsi
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.
Post 27 Jul 2008, 03:54
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2911
Location: [RSP+8*5]
bitRAKE
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    
Above, is a snippet from my BlobDrop game. EBP follows a path on a 2D grid of positions (replacing EBX values with EAX value). The grid data itself is the limiting condition which forces the recursion to end - which makes for some fast code (verses the non-recursive version).

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)/¯ unlicense.org
Post 27 Jul 2008, 14:52
View user's profile Send private message Visit poster's website Reply with quote
neville



Joined: 13 Jul 2008
Posts: 507
Location: New Zealand
neville
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... Wink

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 Wink

_________________
FAMOS - the first memory operating system
Post 28 Jul 2008, 01:56
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2911
Location: [RSP+8*5]
bitRAKE
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 Wink
Bah, I don't have time for you right now, and too many beers past to be very nice about your ignorance. It is clear we have a difference of philosophy when it comes to coding. You are a cookie cutter coder, eh? Multiple exit conditions cause you a headache, huh? Why do you even bother with ASM when you bind yourself in such chains?

Rewrite the code I posted in non-recursive form then I will reply.

_________________
¯\(°_o)/¯ unlicense.org
Post 28 Jul 2008, 06:34
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7718
Location: Kraków, Poland
Tomasz Grysztar
Do you even have a slightest idea how much of recursion exists in the fasm's core code? Wink
Think about it next time you use INCLUDE or nested macros. Very Happy
Post 28 Jul 2008, 08:46
View user's profile Send private message Visit poster's website Reply with quote
neville



Joined: 13 Jul 2008
Posts: 507
Location: New Zealand
neville
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!! Very Happy

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 ... Wink

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
Post 28 Jul 2008, 10:01
View user's profile Send private message Visit poster's website Reply with quote
neville



Joined: 13 Jul 2008
Posts: 507
Location: New Zealand
neville
Tomasz Grysztar wrote:
Do you even have a slightest idea how much of recursion exists in the fasm's core code? Wink
Think about it next time you use INCLUDE or nested macros. Very Happy

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
Post 28 Jul 2008, 10:05
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar
Assembly Artist


Joined: 16 Jun 2003
Posts: 7718
Location: Kraków, Poland
Tomasz Grysztar
neville wrote:
C'mon Tomasz, what has INCLUDE got to do with recursion??

Look into the sources - you can see it for yourself. Wink
Post 28 Jul 2008, 10:49
View user's profile Send private message Visit poster's website Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
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. Razz
Post 28 Jul 2008, 12:26
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2911
Location: [RSP+8*5]
bitRAKE
neville wrote:
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!! Very Happy
Don't trust anyone who has been drinking - especially if you're poking them with a short stick.
Quote:
I wish you well though, and hope you don't spend too much of your life debugging your recursive code.
I'm an old goat at heart and work like an ox. I never debugged it - funny how it just worked - I must be lucky, right?
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.
How is this article anything but a warning against call overhead being greater than the function being executed in the call? He provides a single sentence of enlightenment for the reader: "If you required to use recursion at least try to optimize it with dynamic programming approaches (such as memoization)". Where is the scientific method? He doesn't present any good use of recursion. Nor does he explain why it might be required.
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.
LOL - OMG, I'm tearing up on this one - I laughed so hard. Your experience has touched me.
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 ... Wink
...and then you go off into left field.
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.
I'm not selling my perspective or any technique. I'm saying put another tool in the toolbox and learn when and how to use it. What is the point for spreading ignorance as a way to success? Show me a pitfall because I want to dive in and understand it better!

_________________
¯\(°_o)/¯ unlicense.org
Post 28 Jul 2008, 15:20
View user's profile Send private message Visit poster's website Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
bitRAKE wrote:
...and then you go off into left field.
Ever code any algorithms using graph data structures?
Recursion makes it much easier.
Personally I used graph data structures (is that how you call those with "nodes" but not restricted to right/left as in binary trees?), and I did not use recursion.

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 Confused
Post 28 Jul 2008, 15:34
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page 1, 2, 3  Next

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.