Style Guide for Z80 Assembly Code

Page 4/5
1 | 2 | 3 | | 5

By pgimeno

Master (228)

pgimeno's picture

11-09-2020, 17:06

I've been restraining myself but...

Those who favour tabs over spaces have never had to deal with code written by people with different tab settings and who mixed tabs with spaces. When indentation goes chaotic, you realize that the only way to make it work is to use spaces for everything.

That said, the only standard tab setting is every 8 columns, and assembler is a good fit for using that convention.

By Timmy

Expert (128)

Timmy's picture

11-09-2020, 17:37

theNestruo wrote:

So the original author of wyz_player is also on the bracket gang... Cool (that's probably caused because it is intended for AsMSX).

With VSCode (or any editor that supports search/replace with regex) you can replace [] indirections with () indirections. Search for \[([^\]]+)\] and replace with ($1) .

Or just click here :) (not sure if it is the same version that intended to convert...)

Thank you so much for looking into it! It's probably not needed now because someone else got it translated for me already.

The problem usually is not the square brackets, but it's likely there are other assembler specific commands there too.

I don't know if I end up using the wyz_player in a game but I managed to make a ROM with the wyz_player. :)

By Thom

Hero (636)

Thom's picture

12-09-2020, 19:49

pgimeno wrote:

I've been restraining myself but...

Those who favour tabs over spaces have never had to deal with code written by people with different tab settings and who mixed tabs with spaces. When indentation goes chaotic, you realize that the only way to make it work is to use spaces for everything.

Good point! That's probably why they chose spaces over tabs in PEP8.

By theNestruo

Master (177)

theNestruo's picture

12-09-2020, 20:41

pgimeno wrote:

I've been restraining myself but...

Thom wrote:

Good point!

WHAAAT!? Space-gang members!? I though we were among civilized people here!!! :-P (just kidding)

I prefer tabs for indentation (just for indentation; not to vertically align code). Particularly at work (non-ASM code).
With tabs for indentation, everyone can view the same source code according his/her visualization preferences: 4 spaces by tab, 2, 8... Even 4 by default but 2 when viewing code side-by-side, etc.

P.S.: google "Fibonacci indentation"

By santiontanon

Paragon (1069)

santiontanon's picture

12-09-2020, 22:52

haha, I really do not have a strong preference between spaces or tabs and in my assembler code I probably use a messy mix. But at work our linter is setup for allowing only spaces for indentation (4 spaces per indent) and code cannot be pushed if it's indented with tabs. So, that's what I'm used to seeing. Although I don't really care, I just rely on the linter to tell me if it's right or wrong Wink

By thegeps

Hero (541)

thegeps's picture

13-09-2020, 20:00

my style:

main:					;main loop
	ld	a,(nomorelives)
	or	a
	jp	nz,gameover
	ld	hl,(rowcount)		;load in HL register the remaining map rows
	dec	hl			;decrease it
	ld	a,h			;and check if
	or	l			;it has reached the 0 val
	jp	z,midlevel		;if so jump to midlevel routine
	call	cngchr			;else call the tilescrolling routine
	ld	a,(event)		;load event value in a register
	ld	hl,(evlist)		;point HL to eventlist address
	cp	(hl)			;event and value in eventlist address are the same?
	call	z,spmove		;if so call spmove to set a new sprite values in our SAT in RAM
	call	shot_or_not		;call a routine that decide if a shot will be fired this frame and set the bullet flag
	ld	a,(bullet_flag)		;check the bullet flag
	dec	a			;if it is set to 1 (so it is wiil be 0 after dec instruction) then a bullet will be fired
	call	z,set_bullet		;so go and set parameters for this bullet
	ld	a,(dead_flag)		;check if we were killed and we are in respawn time
	dec	a			
	jp	z,skip_control		;if so we can't drive the starship until end of respawn
	call	control			;call the joy/keyboard check
skip_control:
	call	cshot_collision		;check central shot/enemies collision 
	call	lshot_collision		;do the same for left shot
	call	rshot_collision		;do the same for right shot
	ld	a,(power)		;check power level
	cp	8
	jp	c,no_pod		;if it is less than 8 then we haven't a orbital pod, avoid its collision check
	call	pod_collision		;check pod collision with enemy bullets
no_pod:
	call	play_sfx		;call a routine that set sound fx to be played

	ld	a,(cheat)		;cheats are enabled?
	or	a
	jp	nz,main			;if so then restart this loop (avoiding collisions)
main_collision:
	call	ship_collision		;check starship/enemies collision
	ld	a,(respawn_time)	;check if we are in respawn time
	or	a
	jp	nz,main			;if so (timer not decreased to zero) then restart this loop (avoiding bullets collisions)
	call	bullet_collision	;check starship/bullets collision
	jp	main			;restart this loop

By pgimeno

Master (228)

pgimeno's picture

14-09-2020, 19:54

theNestruo wrote:
pgimeno wrote:

I've been restraining myself but...

Thom wrote:

Good point!

WHAAAT!? Space-gang members!? I though we were among civilized people here!!! :-P (just kidding)

I prefer tabs for indentation (just for indentation; not to vertically align code). Particularly at work (non-ASM code).
With tabs for indentation, everyone can view the same source code according his/her visualization preferences: 4 spaces by tab, 2, 8... Even 4 by default but 2 when viewing code side-by-side, etc.

Maybe good for your work environment, but for free software projects published on the web, note that browsers use tab stops every 8 columns (like terminals), and that typically makes long lines to get lost or wrapped.

Furthermore, it's not immune to this. I wrote this with 4 spaces per tab, using spaces for vertical alignment:

	if (it_is_true)                  // When true, do something
		for (i = 0; i < 10; i++)     // that loops and performs
			printf("%d\n", i);       // some operations.

Basically, it's impossible to align anything that is to the right of the text, no matter whether you use spaces or tabs for the alignment, unless the tab size matches the one used to write the code. This does not happen if the indentation is purely spaces-based.

When applied to comments in assembler, the result is even worse. And what do you place between a label and an instruction?

	if      COMPARE                  ; Needs a comparison
		ld      b,5                  ; 5 characters to compare
loop:       cp      (hl)             ; Check the character
;     ^^^^ what to use here? spaces or tabs? I used spaces. If using tabs, how many?
			inc     hl               ; Prepare next character
			scf                      ; Set error flag
			ret     nz               ; Return error if not equal
		djnz    loop                 ; loop back
	endif
	and     a                        ; Clear error flag
	ret                              ; Return with no error

Here's how it looked like when I wrote it with 4-space tabs:

And if I set tabs to 2, it's even worse:

That's why I'm not sold on the "tabs for indent, spaces to align" method either. Especially for assembler.

Edit: Forgot to add, using tabs for indent and spaces to align usually makes writing the code slower vs. using the tab key to insert spaces, because you can't hit tab until you're close enough; you have to enter every space manually.

theNestruo wrote:

P.S.: google "Fibonacci indentation"

Er.... why?! Why would anyone do that to themselves? :D

By theNestruo

Master (177)

theNestruo's picture

14-09-2020, 20:54

pgimeno wrote:

Maybe good for your work environment, but for free software projects published on the web, note that browsers use tab stops every 8 columns (like terminals), and that typically makes long lines to get lost or wrapped.

I agree; I was thinking in a project-wide scope.

pgimeno wrote:

Furthermore, it's not immune to this. I wrote this with 4 spaces per tab, using spaces for vertical alignment:

	if (it_is_true)                  // When true, do something
		for (i = 0; i < 10; i++)     // that loops and performs
			printf("%d\n", i);       // some operations.

Basically, it's impossible to align anything that is to the right of the text (...)

I scarcely comment to the right; that may influence my preference for tabs:

	// Peform some operations when true
	if (it_is_true)
		for (i = 0; i < 10; i++)
			printf("%d\n", i);
pgimeno wrote:

When applied to comments in assembler, the result is even worse. And what do you place between a label and an instruction?
(...snippet...)

Again, I think my commenting style suffers less for tab size changes:

; Comparison needed?
	if      COMPARE
; Compares 5 characters
		ld	b,5
loop:
		cp	(hl)
		inc	hl ; (for the next character)
; Returns carry flag (=error) if not equal
		scf
		ret	nz
		djnz	loop
	endif ; if COMPARE
; Returns no-carry if equals
	and	a ; (clears carry flag)
	ret

Actually, my commenting style is similar in other languages: Description of what the code does before the corresponding block of code, and they are never to the right. Technical comments (i.e.: not required to understand what the code does but how), go between parenthesis and can go to the right. This way, I can "scan" the code by reading just the non-technical comments.

pgimeno wrote:
theNestruo wrote:

P.S.: google "Fibonacci indentation"

Er.... why?! Why would anyone do that to themselves? Big smile

I guess it started for the lolz... but it helps realizing that "yes, you are nesting too deep here".

By ren

Paragon (1495)

ren's picture

14-09-2020, 21:27

No asm experience but I'm a tabs + spaces for vert. alignment guy as well Smile
Did run into a (JavaScript) linter issue long ago (might have been fixed in the meantime), some feature didn't understand you could actually have tab indent followed by some spaces for vert. alignment..

@pgimeno: your 3 indent level alignment example will also fail with space indent. You can't vert. align stuff that's on different indentation levels (if you want to allow for preference size).

Surely, when using the tab+v.spaces method you must make sure you're precise/consistent, or otherwise settle on a fixed tab size I suppose..

Quote:

note that browsers use tab stops every 8 columns (like terminals), and that typically makes long lines to get lost or wrapped.

That shouldn't hold one back, there's the CSS tab-size property.

-edit @TheNestruo: so, e.g. in you snake.asm where you place comments horizontally, like @ the first .LOOP, or already e.g. at the Symbolic constants 'table': you should have used spaces there.. right? ;)

By theNestruo

Master (177)

theNestruo's picture

14-09-2020, 21:39

ren wrote:

-edit @TheNestruo: so, e.g. in you snake.asm where you place comments horizontally, like @ the first .LOOP, or already e.g. at the Symbolic constants 'table': you should have used spaces there.. right? ;)

LOL @ myself xDDD
Well, if you look at the rest of the msxlib sources, they'll follow my own conventions... more or less; I'm not afraid of breaking the rules when it makes sense to break them. snake.asm in particular is profusely commented because of its example nature. Geez, it even has that kind of redundant comments I hate such as "call INIT_CHARSET ; Initializes the charset" and "call UPDATE_HI_SCORE ; Updates the high score" xDDD

Page 4/5
1 | 2 | 3 | | 5