<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>
Date   : Sun, 18 Oct 1998 18:29:43 +0100
From   : jgh@... (Jonathan Graham Harston)
Subject: Multi-statement Assembler

Mark Usher wrote:
MU> JGH> So instead of, for example:
MU> JGH> PHA
MU> JGH> LSR A
MU> JGH> LSR A
MU> JGH> LSR A
MU> JGH> LSR A
MU> JGH> JSR PRNYB
MU> JGH> PLA
MU> JGH> etc...
MU> JGH> Which is alot more readable
MU> JGH> you could do:
MU> JGH> PHA:LSR A:LSR A:LSR A:LSR A
MU> JGH> JSR PRNYB:PLA
MU> Which looks quick and nasty. Also it takes just as long to type a
MU> colon as it does to hit a return. I know if one of my progammers did
MU> the latter he'd be making the coffee for a month.
 
I find writing, for instance, CLC:ADC #n much quicker than pressing a
Return in the middle. But, yes, the above was a quick run-off, just to
demonstate the concept.
 
However, I'd much prefer to read through (very contrived, admittedly):
 
----8<----
\******************************************************************
\* RoutineEntryName - Nonsense routine to demonstate tidy writing *
\* On entry- InputValue  = Entry value to use                     *
\* On exit - OutputValue = Returned value, out=(in+(in DIV 16))   *
\*           all registers preserved, including flags             *
\*           zero page pointer zp/zp+1 also preserved             *
\******************************************************************
\
.RoutineEntryName
PHP:PHA:TXA:PHA:TYA:PHA       :\ Save all registers
LDA zp:PHA:LDA zp+1:PHA       :\ Save zp pointer
LDA InputValue                :\ Value passed to routine
LSR A:LSR A:LSR A:LSR A       :\ Divide by 16
AND #15                       :\ Loose top nybble
CLC:ADC InputValue            :\ Add to original value
STA OutputValue               :\ ...and store in output
PLA:STA zp+1:PLA:STA zp       :\ Restore zp pointer
PLA:TYA:PLA:TXA:PLA:PLP       :\ Restore all registers
RTS                           :\ Return
\
----8<----
 
...than the same thing with one statement per line. The code section above
takes up 10 lines, whereas one-per-line would make it 30 lines - a
threefold increase.
 
The above routine fits neatly onto one screen, whereas 1pl would require
scrolling back and forwards to follow.  One of the first documentation
principles I got told ages ago was that if a routine was too long to fit
on a single screen, it was probably too long.
 
Each line contains a single atomic concept, even though that concept may
break down to more than one atomic machine code instruction.
 
For instance, having 'restore all registers' as an atomic concept can help
prevent you accidently putting a destination label in the middle of them
and ending up with an unbalanced stack.
 
Of course, you can go too far with multi-statement lines as well, cramming
multiple concepts on a line, or spreading them over lines.  I wouldn't
want to go anywhere near something that looked like:
 
PHP:PHA:TXA:PHA:TYA:PHA:LDAzp:PHA:LDAzp+1:PHA:LDAi:LSRA:LSRA:LSRA:LSRA
AND#15:CLC:ADCi:STAo:PLA:STAzp+1:STAzp:PLA:TAY:PLA:TAX:PLA:PLA:RTS
 
Especially if that's how the author /wrote/ it, rather than that being the
output of some compression program.
 
(Hmmm... Why would you want to scrunch a /source/ program, I wonder?)
 
Agreed, the style of writing programs, including the arrangement of
comments and the co-positioning or related instructions is a very
contentious topic, and can generate almost as many churned-out books and
recommendation documents as how to use Windows or DOS.  People can have
hard-held views almost as strongly held as the favourite editor. :)
 
-- 
J.G.Harston (JGH BBC PD Library) 70 Camm Street, Walkley, SHEFFIELD S6 3TR
jgh@...                --- BBC+Master / Z80+6502 / CoPro+Tubes / Econet+SJ
Devolution / Transport / Planning / Highways ---- Focus Editor for Walkley
Walkley Ward Website:  http://homepages.nildram.co.uk/~amilton/walkley.htm
-- 
    ___  ___  ___  ___  ___  ___   |  Free Internet E-mail and Usenet News  | 
|  /   \/   \/   \/   \/   \/   \  | +44 181 654 2212 also +44 181 655 4412 |
|    A    R    C    A    D    E    |   Croydon  UK  -  Fidonet#2:254/27.0   |
|     The Definitive Acorn BBS     | http://arcade.demon.co.uk  at weekends |
<< Previous Message Main Index Next Message >>
<< Previous Message in Thread This Month Next Message in Thread >>