Log inRegister an accountBrowse CSDbHelp & documentationFacts & StatisticsThe forumsAvailable RSS-feeds on CSDbSupport CSDb Commodore 64 Scene Database
You are not logged in - nap
CSDb User Forums


Forums > C64 Coding > Code indenting
2017-09-11 07:40
oziphantom

Registered: Oct 2014
Posts: 490
Code indenting

I've always been a strict
label code comment
kinda guy but of late I've been experimenting with code readability and speed boosts. Indenting does seem to improve things
updateMovement
	ldx Ents.movementPtr
-	lda YXDeltas,x
	cmp #$AA
	bne +
	ldx Ents.movementPtrBase
	stx Ents.movementPtr
	jmp -
+	clc
	adc Ents.y
	sta Ents.y
	inx	
	lda YXDeltas,x
	beq _endAdd
	bmi _sub
	clc
	adc Ents.x
	sta Ents.x
	bcc _endAdd
	inc Ents.xMSB
	jmp _endAdd
_sub
	clc
	adc Ents.x
	sta Ents.x
	lda Ents.xMSB
	adc #$ff
	sta Ents.xMSB
_endAdd
	inx
	stx Ents.movementPtr
	rts
vs
updateMovement
	ldx Ents.movementPtr
-	lda YXDeltas,x
	cmp #$AA
	bne +
	    ldx Ents.movementPtrBase
	    stx Ents.movementPtr
	jmp -
+	clc
	adc Ents.y
	sta Ents.y
	inx	
	lda YXDeltas,x
	beq _endAdd
	    bmi _sub
                clc
                adc Ents.x
                sta Ents.x
                bcc _endAdd
                    inc Ents.xMSB
                    jmp _endAdd
       _sub
            clc
            adc Ents.x
            sta Ents.x
            lda Ents.xMSB
            adc #$ff
            sta Ents.xMSB
_endAdd
	inx
	stx Ents.movementPtr
	rts
this kind of falls apart when you get the to the leap frog code. I.e something like
bmi C
beq B
A
A
A
bne C
A
A
jmp D
B
B
B
B
beq D
C
C
C
C
D
D
D
So I was wondering if anybody else had tumbled down this rabbit hole or had other things to do to help?
2017-09-11 07:52
Oswald

Registered: Apr 2002
Posts: 5094
dont know, but what is the deal with bmi sub ? with signed numbers you dont need to branch if add or sub ?

also instead of $aa branch on 0 or negative could spare some cycles.

also instead of YXdeltas 2 tables seems better Xdeltas and Y deltas so no inx is needed to reach X or Y.

my 2 cents of criticism.
2017-09-11 07:53
Frantic

Registered: Mar 2003
Posts: 1648
From my point of view you're answering your own question. That is, to use additional indenting in those cases where it actually helps readability. I understand that this conflicts with your idea of being "strict", but why not be strict about function (readability in this case) instead of being strict about form?
2017-09-11 08:25
oziphantom

Registered: Oct 2014
Posts: 490
the $AA resets the loop, so multiple delta runs are stored in one <256 list, to which the code indexes into say an ent might use the deltas from 34-41 when it hits $AA it goes from index 41 back to 34.
I could split the Y and X into two tables but then I would have to duplicate the $AA in both so the indexes lined up, which would eat more bytes than it would save, so its a RAM/Cycle pay off.

the bmi is to handle the MSB transition, (both do do an ADC ) just how it updates the 9th bit. If you have a nice way to handle 9bit maths inline, do tell.
2017-09-11 09:29
Trash

Registered: Jan 2002
Posts: 122
I would do this and skip indenting for the different cases:
bmi C ; Why case C
beq B ; Why case B
;------------
; Case A, <explanation>
;------------
A
A
A
bne C ; Why case C
A
A
jmp D ; Why case D
;------------
; Case B, <explanation>
;------------
B
B
B
B
beq D ; Why case D
;------------
; Case C, <explanation>
;------------
C
C
C
C
;------------
; Case D, <explanation>
;------------
D
D
D
2017-09-11 11:34
Cruzer

Registered: Dec 2001
Posts: 1048
Looks retarded to me. I would just add some newlines to split the code into logical blocks. The only time it makes sense to indent asm code is if it's in curly brackets, e.g. because of an if-statement or for-loop.
2017-09-11 12:06
Dano

Registered: Jul 2004
Posts: 234
What Cruzer said.
2017-09-11 12:18
chatGPZ

Registered: Dec 2001
Posts: 11386
not a single label named "shit". unreadable.
2017-09-11 12:46
Mr. SID

Registered: Jan 2003
Posts: 424
You lost me at the + and - labels... :)
2017-09-11 13:16
Oswald

Registered: Apr 2002
Posts: 5094
I still hate all that branching going on, why not just 16 bit math?

bmi case:

+3 cycle bmi -2 cycle at adc # = +1 cycle slower than adc abs,x

bpl case:

not taken branch:2
not taken bcc: 2
inc 5
jmp 4

13 cycles = +1 cycle slower than lda adc sta (Each 4)


only 1 case its faster.

instead of #$aa it could be a dec counter bmi.
2017-09-11 16:28
Perplex

Registered: Feb 2009
Posts: 255
Quoting Cruzer
The only time it makes sense to indent asm code is if it's in curly brackets, e.g. because of an if-statement or for-loop.


This.
2017-09-12 00:17
Skate

Registered: Jul 2003
Posts: 494
C64 has no tab key for a reason. :)
2017-09-12 00:51
chatGPZ

Registered: Dec 2001
Posts: 11386
and TASS already auto-idents the way god intended!
2017-09-12 07:32
oziphantom

Registered: Oct 2014
Posts: 490
Quoting Perplex
Quoting Cruzer
The only time it makes sense to indent asm code is if it's in curly brackets, e.g. because of an if-statement or for-loop.


This.


Python, Ruby et al ?
2017-09-12 07:44
oziphantom

Registered: Oct 2014
Posts: 490
Quoting Skate
C64 has no tab key for a reason. :)


And the C128 does for a reason ;) The Kernal even has Tab Stops build in.
2017-09-15 19:37
Skate

Registered: Jul 2003
Posts: 494
Quote: Quoting Skate
C64 has no tab key for a reason. :)


And the C128 does for a reason ;) The Kernal even has Tab Stops build in.


We are free to use indenting for our C128 projects then, good to know.

I have a C128 but it will take a few more decades until i need that much memory and speed. ;)
2017-09-15 20:00
Compyx

Registered: Jan 2005
Posts: 631
Quoting Cruzer
Looks retarded to me. I would just add some newlines to split the code into logical blocks. The only time it makes sense to indent asm code is if it's in curly brackets, e.g. because of an if-statement or for-loop.


I agree with the retarded bit. But even using brackets doesn't make sense, asm doesn't have block-level scope. And if there are assemblers that support that, those are retarded.
2017-09-16 05:19
Hypnosis

Registered: Mar 2015
Posts: 36
I also indent my code in loops, if-end and if-else-end cases. I indent as well as I can in other cases but fail when it gets too hairy. My assembler has some syntactic sugar to help this structuring by using curly braces. They limit the lifetime of local variables but also add automatic loop and continue labels, so you can write a loop like this.

	ldx #0
	{
		lda string,x
		beq @continue

		jsr CHROUT
		inx
		bne @loop
	}
2017-09-16 20:03
Digger

Registered: Mar 2005
Posts: 437
I am also doing indenting for loops and shit, like Cruzer said.

What's interesting however is logically grouping vars and pointers like:
Ents.movementPtrBase
Ents.movementPtr
Ents.xMSB
...

etc.

How does this work in principle?
2017-09-17 06:15
oziphantom

Registered: Oct 2014
Posts: 490
using tass and do
Ents .block
    movementPtrBase .byte ?
    movementPtr .byte ?
    xMSB .byte ?
.bend

or make a struct with it use dstruct to instance multiple copies where you want it.

Then you access it with Ents.movementPtr.
If this is a Hi/Lo word you can do
HLWord .union
 .word ?
 .struct
 	lo .byte ?
 	hi .byte ?
 .ends
.endu
then make one as
movementPtr .dunion HLWord
if you do it inside the Ents block/struct you would then have
Ents.MovementPtr and
Ents.MovementPtr.lo and
Ents.MovementPtr.hi
2017-09-19 15:39
White Flame

Registered: Sep 2002
Posts: 136
I tend to add just a single space at a time. I find it a lot more readable than the full tab-style examples above, since asm tends to be very column-aligned.

For loops, I indent the body including the first instruction, and de-indent the branch instruction:
  ldx #$7f
:  lda init,x
   sta dest,x
   dex
  bpl :-


For if/then/else/etc branching, I just try to keep conditional areas indented, but when it goes all spaghetti that's just a guideline.
2017-09-19 16:15
chatGPZ

Registered: Dec 2001
Posts: 11386
Quote:
when it goes all spaghetti

i recommend a bit of good oil, cheese, and red wine :)
2017-09-20 05:20
ChristopherJam

Registered: Aug 2004
Posts: 1409
I was going to stay out of this thread, as I tend not to indent asm blocks. Then I caught myself doing this last night:
for i in 1..30
{
    ldx so+i
    lda yv,x
    ldy so+i-1
    cmp yv,y
    bge next
        sty so+i
        ldy so+i-2
        cmp yv,y
        bge swap1
            sty so+i-1
            ldy so+i-3
            cmp yv,y
            bge swap2
                jmp abort
        swap2
            stx so+i-2
            jmp reload
    swap1
        stx so+i-1
reload
    ldx so+i
next
}
  ; 51 bytes per pair, *31 = 1550 bytes (less a bit for the first few entries)

(code may contain bugs, and is wrong for i in 1..2 and also I want to swap x & y every second iteration of the code generator so I can drop the "ldy so+i-1" on subsequent iters, but those are all by the by)
2017-09-22 00:10
Cruzer

Registered: Dec 2001
Posts: 1048
Quoting Compyx
asm doesn't have block-level scope. And if there are assemblers that support that, those are retarded.
I dunno. If it's used for keeping the code organized, I beg to differ. For instance I like to keep each subroutine in braces, so I can have local labels like "loop" and "done" that would otherwise have to be either global (I remember the days of turbo-ass and having to come up with meaningless labels like "loop43") or multi-labels referenced by label+ or label-, which have the disadvantage that if you forget to add the label, it usually ends up referring to a label in another subroutine. But even when using multi-labels you avoid this if the subroutines are in different blocks, since it will just give a compile time error.

Are for-loops and other scripting retarded? Certainly if it's used for generating speedcode or data that could be generated quicker at runtime than it would take to load. But for importing gfx or generating small chunks of data or code such as setting the y-position of 8 sprites it makes a lot of sense to me.
RefreshSubscribe to this thread:

You need to be logged in to post in the forum.

Search the forum:
Search   for   in  
All times are CET.
Search CSDb
Advanced
Users Online
Unlock/Padua/Albion
TheRyk/MYD!
Chesser/Blazon
B.A./QUANTUM
Visage/Lethargy
Alakran_64
LordCrass
E$G/HF ⭐ 7
algorithm
Abynx/Wavefx
Airwolf/F4CG
Wayne/Art Ravers
iceout/Avatar/HF
Guests online: 105
Top Demos
1 Next Level  (9.7)
2 13:37  (9.7)
3 Mojo  (9.7)
4 Coma Light 13  (9.6)
5 Edge of Disgrace  (9.6)
6 What Is The Matrix 2  (9.6)
7 The Demo Coder  (9.6)
8 Uncensored  (9.6)
9 Comaland 100%  (9.6)
10 Wonderland XIV  (9.6)
Top onefile Demos
1 Layers  (9.6)
2 No Listen  (9.6)
3 Cubic Dream  (9.6)
4 Party Elk 2  (9.6)
5 Copper Booze  (9.6)
6 Dawnfall V1.1  (9.5)
7 Rainbow Connection  (9.5)
8 Onscreen 5k  (9.5)
9 Morph  (9.5)
10 Libertongo  (9.5)
Top Groups
1 Performers  (9.3)
2 Booze Design  (9.3)
3 Oxyron  (9.3)
4 Triad  (9.3)
5 Censor Design  (9.3)
Top Coders
1 Axis  (9.8)
2 Graham  (9.8)
3 Lft  (9.8)
4 Crossbow  (9.8)
5 HCL  (9.8)

Home - Disclaimer
Copyright © No Name 2001-2024
Page generated in: 0.076 sec.