Suggestions on formatting of code

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
PeterJ
Site Admin
Posts: 2466
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Suggestions on formatting of code

Post by PeterJ »

Hi,

Can I ask about formatting code?

I appreciate the 'proper' way of doing it is like the example below (This example of for InkSpector):

Code: Select all

	TARGET ZXSPECTRUM48
	SAVETAP "sc.tap"
	ORG	$6000

main:	ld a,2
	call 5633
loop:	ld de,string
	ld bc,eostr-string
	call 8252
	jp loop

string defb 22,10,10,17,5,72,69,76,76,79,32,87,79,82,76,68,33
eostr  equ $

	END main
However, Pasmo is far more relaxed about formatting, and this runs just fine:

Code: Select all

org $6000
main
ld a,2
call 5633
loop
ld de,string
ld bc,eostr-string
call 8252
jp loop

string defb 22,10,10,17,5,72,69,76,76,79,32,87,79,82,76,68,33
eostr  equ $

END $6000
I understand that the first line is much better in terms of readability, but having to tab after every line is a bit of a pain.

Any thoughts from others on this?
0 x

User avatar
chilledgamer
Dizzy
Posts: 50
Joined: Wed Jun 24, 2020 1:22 am
Location: Torquay
Contact:

Re: Suggestions on formatting of code

Post by chilledgamer »

Of course I am new and cannot comment on proper formatting, but just thought I would let you know something I found out recently.

I am now using VSCode. and I can highlight as many lines as I want with the mouse and press tab, and it just works :D. I'm sure you already new this , but just in case :d
0 x

User avatar
Einar Saukas
Dynamite Dan
Posts: 1303
Joined: Wed Nov 15, 2017 2:48 pm

Re: Suggestions on formatting of code

Post by Einar Saukas »

Properly organizing and documenting source code is done for your own convenience, not for the assembler/compiler.

You don't HAVE to do it. But good luck trying to do something useful without doing it :)
0 x

User avatar
PeterJ
Site Admin
Posts: 2466
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Suggestions on formatting of code

Post by PeterJ »

I'm all for documenting @Einar Saukas!

Thanks @chilledgamer, I use that with Python on Visual but just tend to use notepad for my z80.

It's just interesting how some assemblers actually force good formatting on you.
0 x

Dr beep
Berk
Posts: 32
Joined: Mon Oct 01, 2018 8:53 pm

Re: Suggestions on formatting of code

Post by Dr beep »

I would not only go for the outlined version for readability, but also add all kinds of comment so after years you still can figure out what it exactly does.

To give you an example, I have a project, started 2011, bug found after 2 years and took me untill 2016 to find what went wrong.
Code itself is perfect but the stack of records grew over the program. i had to replace piece of code. Why code is placed at #6000 and not At #8000 is now explained in commentlines.
0 x

User avatar
Seven.FFF
Manic Miner
Posts: 490
Joined: Sat Nov 25, 2017 10:50 pm
Location: USA

Re: Suggestions on formatting of code

Post by Seven.FFF »

Sometimes it is for the assembler. Some assemblers have rules that anything starting in column 0 is a label, and anything in columns 1+ is an opcode. Some assemblers allow reserved words as labels without trailing colons provided they are in column 0.

Basically the simpler the parser, the more likely you are to break its assumptions and produce uncompilable or wrong code if you don't follow its formatting rules. There are some real doozies out there with simplistic parsers.
0 x
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel NXTP ESP Update ESP Reset CSpect Plugins

User avatar
PeterJ
Site Admin
Posts: 2466
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Suggestions on formatting of code

Post by PeterJ »

Just to add, I do normally include comments. I didn't in my sample as I didn't want to loose focus from the formatting angle. Cheers.

Thanks @Seven.FFF. Pasmo must be pretty clever to workout my dummy coding!
0 x

User avatar
Morkin
Manic Miner
Posts: 998
Joined: Mon Nov 13, 2017 8:50 am
Location: Bristol, UK

Re: Suggestions on formatting of code

Post by Morkin »

One suggestion that I found useful when I was using ZX Spin is to always put a colon (:) at the end of labels, so I could use CTRL+F to find them (including the colon).

Without the colon, once the code got quite big I was encountering loads of matches, e.g. in LD statements, before I got to the label itself when I did CTRL+F.

Other assemblers may have ways to find labels in another way, but after a while it's easy to end up with hundreds of labels, so even an alphabetical list takes a while to scroll through especially if a lot of labels are similar.
1 x

User avatar
PeterJ
Site Admin
Posts: 2466
Joined: Thu Nov 09, 2017 7:19 pm
Location: Surrey, UK

Re: Suggestions on formatting of code

Post by PeterJ »

Thanks @Morkin,

I know a lot of people quite rightly love Spin, but because it's so long in the tooth now I have problems with it in W10 (I know it's fine for some users).

At the sort of length code I do, notepad and Pasmo is fine for me, but that functionality with labels is useful. I will start using colons though so I can find labels in notepad.

Maybe one day we will get Spin V2 @ZXDunny? Here's hoping.
1 x

User avatar
ketmar
Microbot
Posts: 167
Joined: Tue Jun 16, 2020 5:25 pm

Re: Suggestions on formatting of code

Post by ketmar »

mine is usually like this. labels are always on a separate line, and instruction arguments are always at column 8. in-line comments are either at column 16 (if it fits), but almost always aligned.
some code snippetsShow

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; build fully unrolled screen blitter
;; generates 4329 bytes of code
;; blitter timing is 35897, so we can avoid DI/EI
;; in:
;;   nothing
;; out:
;;  all registers are dead
;;  all alternate registers are dead
;;
VScrBuildBlit:
  ld    de,CodeEnd

  exx

  ; start of vscr
  ld    hl,V_ADDR+1

  ; start of scr$+width/2
  ld    bc,scraddr8x8(4,1)+VSCR_WIDTH/2-1

  ; start of attr+width/2
  ld    de,scrattr8x8(4,1)+VSCR_WIDTH/2-1

  exx

  ; put prologue
  ld    hl,blitjit_prologue
  ld    bc,blitjit_prologue_len
  ldir

  ld    a,VSCR_HEIGHT

.jitnext9:
  ex    af,af'

  ; put scr$ copy parts (8 lines)
  ld    b,8

.jitnextvline:
  push  bc

Code: Select all

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; draw sprite with 16x8 tiles, with full-tile clipping
;; in:
;;  B: y (chars)
;;  C: x (chars)
;;  HL: sprite data
;; out:
;;  all main registers are dead
;;  AF': dead
;; visible vscr starts at (4,1)
;; size: (24,12)
;;
DrawSpriteSimple16xn:
  ; horizontal clip
  ld    a,c
  cp    VSCR_WIDTH
  ret   nc
  ; vertical clip
  ld    a,b
  cp    VSCR_HEIGHT
  jp    nc,.checkneg
  ld    a,(hl)  ; block counter
  add   a,b     ; end y
  sub   a,VSCR_HEIGHT+1
  ld    a,(hl)  ; most of the time it will be ok
  ; if the result is positive (or zero), we're ok
  jp    m,.vertok
  ; fix counter
  ld    a,VSCR_HEIGHT
  sub   b
.vertok:
  ex    af,af'
0 x

Post Reply