Thanks for the sake of my OCD, it must be aligned always!
Suggestions on formatting of code
Re: Suggestions on formatting of code
Re: Suggestions on formatting of code
and it is also easier to quickly scan the code too. syntax highlighting helps too, but with aligned parts it is even easier. this habit (aligning) actually came from GENS assembler (it did it automatically for you).
Re: Suggestions on formatting of code
Basically what everybody says:
1) comment, comment, comment ...
2) align by columns
3) meaningfull labels
1) comment, comment, comment ...
2) align by columns
3) meaningfull labels
Code: Select all
dutyModification
ld a, (ix + DUTY)
ld c, (ix + DUTYDELTA)
add a, c ;add duty change to duty
bit 7, c ;is delta negative ?
jr nz, negDutyDelta
ld b, (ix + DUTYMAX) ;compare with max duty
cp b
jr c, noDutyChange ;will not change duty delta sign
dutyChange
ld a, c ;negate duty delta
neg
ld c, a
ld a, b ;and set min/max limit as duty
ld (ix + DUTYDELTA), c ;store new delta
noDutyChange
ld (ix + DUTY), a ;store new duty
ret
negDutyDelta
ld b, (ix + DUTYMIN) ;compare with min duty
cp b
jr nc, noDutyChange ;will not change duty delta sign
jr dutyChange
noteModification
ld a, (ix + NOTELEN) ;decrease note length
dec a
ld (ix + NOTELEN), a
noteNoLastTick
ld a, (ix + ARPFLAG) ;is appergio enabled ?
or a
jr z, noteNoArp ;no
ld a, (tickCounter) ;if appergio enabled
and 1 ;every second tick play tone one octave higher
jr z, noteNoArp
ld a, 12 ;octave plus
noteNoArp
add a, (ix + BASENOTE) ;add nothing or octave to base note
call getNoteFreq ;get note frequency
ld a, (ix + VIBRATOFLAG)
or a
jr z, noteNoVibrato ;no vibrato
ld de, 47 ;1 + 11*2 = 23
add hl, de ;move note
ld a, (hl)
inc hl
ld h, (hl) ;get frequency for note+1
ld l, a
or a
sbc hl, bc ;difference between notes to HL
ld a, (ix + VIBRATODEPTH)
vibratoDepth
srl h
rr l
dec a
jr nz, vibratoDepth ; delta of frequency / ( 2^vibratoDepth)
ld a, (vibratoCounter) ; now multiply vibrato depth by vibracounter
or a
jr z, noteNoVibrato ;level 0
ld d, h
ld e, l
bit 1, a
jr nc, vibratoBit1
add hl, hl
vibratoBit1
rra
jr c, vibratoBit0
add hl, de
vibratoBit0
add hl, bc ; add vibrato to base note frequence
ld c, l
ld b, h
noteNoVibrato
ld a, (ix + PORTAMENTOFLAG)
or a
jr z, noteNoPortamento ;no portamento
ld e, (ix + PORTAMENTOLO)
ld d, (ix + PORTAMENTOHI)
ld l, e
ld h, d
add hl, bc ;add portamento to frequency
ld c, l ;store final frequency
ld b, h
ld l, (ix + PORTAMENTODELTALO)
ld h, (ix + PORTAMENTODELTAHI)
add hl, de ;update portamento
bit 7, (ix + PORTAMENTODELTAHI) ;is delta negative ?
jr z, notePortamentoChange
ccf
notePortamentoChange
jr nc, notePortamentoContinue
xor a ;switch portamento off
ld (ix + PORTAMENTOFLAG), a
notePortamentoContinue
ld (ix + PORTAMENTODELTALO), l ;store portamento delta
ld (ix + PORTAMENTODELTAHI), h
noteNoPortamento
;BC is final frequency
ld a, (ix + RELEASEFLAG)
or a
ret z ;if not release then exit
ld a, (ix + NOTELEN)
or a
ret nz ;is last tick ?
ld bc, 0 ;erase frequency for the very last tick
ret
Proud owner of Didaktik M
Re: Suggestions on formatting of code
I do both ways.
When coding for Spectrum I never use automatic formatting, I always do it my own, (sometimes inconsistent ) way.
When I have short labels I May do
But what if I have a label like CopyEnemyGraphicDataToBuffer ?
And yes, I use this kind of labels too, we may have this comfort, it's 2020.
Imagine typing these all spaces/tabs with each instruction:
So in case of long labels I prefer
When coding for Spectrum I never use automatic formatting, I always do it my own, (sometimes inconsistent ) way.
When I have short labels I May do
Code: Select all
DS1 LD A,(HL)
INC HL
DS2 LD B,(HL)
And yes, I use this kind of labels too, we may have this comfort, it's 2020.
Imagine typing these all spaces/tabs with each instruction:
Code: Select all
CopyEnemyGraphicDataToBuffer LD A,(HL)
INC HL
LD B,(HL)
Code: Select all
CopyEnemyGraphicDataToBuffer
LD A,(HL)
INC HL
CopyEnemyGraphicDataToBuffer 2
LD B,(HL)
Re: Suggestions on formatting of code
I would just strongly advise keeping komments aligned:
LAZY AND BAD:
GOOD:
LAZY AND BAD:
Code: Select all
LD C,D ;comment1
LD A,(PlayerStatus) ;comment2
GOOD:
Code: Select all
LD C,D ;comment1
LD A,(PlayerStatus) ;comment2
Re: Suggestions on formatting of code
I know it doesn't make a jot of difference to the assembler, but it's interesting how some of you are using uppercase, and others lowercase.... Just be consistent I suppose is the answer.
Re: Suggestions on formatting of code
Again it may make a difference, as some assemblers will treat a reserved word as an opcode if it’s entirely upper or lower case, but allow the same reserved word as a label if it is in mixed case.
Also some editors and IDEs detect the intent of the current line, and will auto-indent the next line by the same amount when you hit enter, so having a large indent optimised for long labels doesn’t cause more effort.
Also some editors and IDEs detect the intent of the current line, and will auto-indent the next line by the same amount when you hit enter, so having a large indent optimised for long labels doesn’t cause more effort.
Robin Verhagen-Guest
SevenFFF / Threetwosevensixseven / colonel32
NXtel • NXTP • ESP Update • ESP Reset • CSpect Plugins
SevenFFF / Threetwosevensixseven / colonel32
NXtel • NXTP • ESP Update • ESP Reset • CSpect Plugins
Re: Suggestions on formatting of code
It's called S T Y L EI know it doesn't make a jot of difference to the assembler, but it's interesting how some of you are using uppercase, and others lowercase.... Just be consistent I suppose is the answer.
Re: Suggestions on formatting of code
IN THE OLD DAYS UPPERCASE WAS USEFUL SINCE LOWERCASE LETTERS COULD BE HARDER TO READ, THESE DAYS, UPPERCASE IS JUST CONSIDERED SHOUTING!!!!
(yes I grew up from uppercase to lowercase).
(yes I grew up from uppercase to lowercase).
Re: Suggestions on formatting of code
https://api.zxinfo.dk/v3/ - ZXDB API for developers
zxinfo-file-browser - Cross platform app to manage your files
https://zxinfo.dk - another ZXDB frontend
zxinfo-file-browser - Cross platform app to manage your files
https://zxinfo.dk - another ZXDB frontend
Re: Suggestions on formatting of code
Excellent [mention]kolbeck[/mention]
I haven't laughed so much in months.
I haven't laughed so much in months.
-
- Microbot
- Posts: 147
- Joined: Fri Nov 24, 2017 5:09 pm
- Location: Syracuse, NY, USA
- Contact:
Re: Suggestions on formatting of code
My preference has been for camelCase, just easier on my eyes.
Last edited by andydansby on Thu Jul 02, 2020 6:04 pm, edited 1 time in total.
Re: Suggestions on formatting of code
One of my favorite episodes
https://api.zxinfo.dk/v3/ - ZXDB API for developers
zxinfo-file-browser - Cross platform app to manage your files
https://zxinfo.dk - another ZXDB frontend
zxinfo-file-browser - Cross platform app to manage your files
https://zxinfo.dk - another ZXDB frontend
Re: Suggestions on formatting of code
as for me, i just can't remember how to type those big letters. have to copy-paste them from a premade text file.
- Ast A. Moore
- Rick Dangerous
- Posts: 2641
- Joined: Mon Nov 13, 2017 3:16 pm
Re: Suggestions on formatting of code
Okay, the video was brilliant!
Now, to address some of Peter’s concerns. Many text editors that are tailored to programmers will do a few things for you to make your life easier. For one, they provide syntax highlighting (not the editors themselves, but their extensions). Take a look at the one I wrote for TextMate:
Second, TextMate (and many others) will automatically tabulate your code. Once you’ve started a line with a tab, when you press Return, the next line will be automatically tabulated to line up with the line before. I don’t know what text editor you use, but I know that the reason I switched to TextMate from TextEdit (the macOS version of Notepad) and wrote the Z80 syntax highlighter was precisely because of these two problems: code readability and tabulation.
Now, to address some of Peter’s concerns. Many text editors that are tailored to programmers will do a few things for you to make your life easier. For one, they provide syntax highlighting (not the editors themselves, but their extensions). Take a look at the one I wrote for TextMate:
Second, TextMate (and many others) will automatically tabulate your code. Once you’ve started a line with a tab, when you press Return, the next line will be automatically tabulated to line up with the line before. I don’t know what text editor you use, but I know that the reason I switched to TextMate from TextEdit (the macOS version of Notepad) and wrote the Z80 syntax highlighter was precisely because of these two problems: code readability and tabulation.
Every man should plant a tree, build a house, and write a ZX Spectrum game.
Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
Re: Suggestions on formatting of code
Thank you [mention]Ast A. Moore[/mention],
I currently just use notepad, but use VS Code for other stuff, so will see if that has some of the functionality you mention. VS Code is good because it runs on both Windows and Linux.
I currently just use notepad, but use VS Code for other stuff, so will see if that has some of the functionality you mention. VS Code is good because it runs on both Windows and Linux.
- flatduckrecords
- Manic Miner
- Posts: 785
- Joined: Thu May 07, 2020 11:47 am
- Location: Oban, Scotland
- Contact:
Re: Suggestions on formatting of code
I’ve found that re-indenting unfamiliar code is a useful aid to understanding it. [mention]Alessandro[/mention] shared a really useful text-scrolling routine a while ago, and walking through it a few lines or a block at a time helped me to understand it better. Nothing wrong with the original formatting, I should say! (I’m very new to assembly so still developing my own taste/style anyway) but I found the process of visually marking each line in way that made sense to me (i.e. to distinguish what’s a label, from an opcode, from a reference to a label, etc.) to be a useful exercise.
When writing my own code I use VS Code which auto-indents each new line, as [mention]chilledgamer[/mention] mentioned. Being able to block-edit chunks of code is really helpful too. Plugins are available to help with syntax-highlighting (which I find helpful) and auto-suggest (but that just seems to get in the way as often as it helps!)
When writing my own code I use VS Code which auto-indents each new line, as [mention]chilledgamer[/mention] mentioned. Being able to block-edit chunks of code is really helpful too. Plugins are available to help with syntax-highlighting (which I find helpful) and auto-suggest (but that just seems to get in the way as often as it helps!)
Re: Suggestions on formatting of code
same here! i'm using this method to understand other people's code for decades.flatduckrecords wrote: ↑Thu Jul 02, 2020 8:48 pm I’ve found that re-indenting unfamiliar code is a useful aid to understanding it.
- Turtle_Quality
- Manic Miner
- Posts: 505
- Joined: Fri Dec 07, 2018 10:19 pm
Re: Suggestions on formatting of code
So no-one indenting for loops, conditional code etc ???
Just throwing that in there as that was the norm with VBA code where I worked a few years ago, and kind of helped with visibility. Obviously it was not practical on the Spectrum but now everyone editing code on PCs not a 32 column Spectrum
Just throwing that in there as that was the norm with VBA code where I worked a few years ago, and kind of helped with visibility. Obviously it was not practical on the Spectrum but now everyone editing code on PCs not a 32 column Spectrum
Definition of loop : see loop
Re: Suggestions on formatting of code
it is not even always possible, considering that jumps can create a fine web. it is much easier (and cleaner, i believe) to split the code to "logical blocks" with empty lines.Turtle_Quality wrote: ↑Thu Jul 02, 2020 9:33 pm So no-one indenting for loops, conditional code etc ???
Re: Suggestions on formatting of code
Exactly, it's not always possible/useful to apply best formatting practises from higher level languages to assembly.ketmar wrote: ↑Thu Jul 02, 2020 9:37 pmit is not even always possible, considering that jumps can create a fine web. it is much easier (and cleaner, i believe) to split the code to "logical blocks" with empty lines.Turtle_Quality wrote: ↑Thu Jul 02, 2020 9:33 pm So no-one indenting for loops, conditional code etc ???
I have a habit of always putting an empty line after jp/jr/call/ret. I also agree with ketmar that blank lines are a good way of separating logical blocks of code. Multiple lines and/or decorated comments for larger units, aka
Code: Select all
; --------------------------------------
; A new section starts here
; --------------------------------------
Re: Suggestions on formatting of code
I use VS code with a z80 formatter/highlighter found on marketplace- works great on Mac as well.
https://api.zxinfo.dk/v3/ - ZXDB API for developers
zxinfo-file-browser - Cross platform app to manage your files
https://zxinfo.dk - another ZXDB frontend
zxinfo-file-browser - Cross platform app to manage your files
https://zxinfo.dk - another ZXDB frontend
Re: Suggestions on formatting of code
I always use uppercase for the Z80 instructions. Just seems to make them immediately stand out against the rest of the test. That and it's a habit from the past!
Cosmium
https://cosmium.itch.io/
https://cosmium.itch.io/
- Ast A. Moore
- Rick Dangerous
- Posts: 2641
- Joined: Mon Nov 13, 2017 3:16 pm
Re: Suggestions on formatting of code
To me, lowercasing the instructions has always looked neater. Not as SCREAMY AND SHOUTY as using all uppercase. Oh, and it’s too much extra work to hold down the Shift key.
Every man should plant a tree, build a house, and write a ZX Spectrum game.
Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
Author of A Yankee in Iraq, a 50 fps shoot-’em-up—the first game to utilize the floating bus on the +2A/+3,
and zasm Z80 Assembler syntax highlighter.
- Einar Saukas
- Bugaboo
- Posts: 3099
- Joined: Wed Nov 15, 2017 2:48 pm
Re: Suggestions on formatting of code
Next time try using CAPS LOCKAst A. Moore wrote: ↑Fri Jul 03, 2020 1:04 amTo me, lowercasing the instructions has always looked neater. Not as SCREAMY AND SHOUTY as using all uppercase. Oh, and it’s too much extra work to hold down the Shift key.
EDIT: Jokes aside, I personally think lowercase mnemonics are more legible too.