From: bjj@ecl.psu.edu (Bryan J. Jensen)
Newsgroups: alt.folklore.computers,comp.unix.misc
Subject: Re: The need for an assembler (was: Favorite CPU)
Date: 12 Aug 92 23:36:12 EDT
Organization: Engineering Computer Lab, Pennsylvania State University

In article <1992Aug11.114808.18631W@lumina.edb.tih.no>, ketil@edb.tih.no (Ketil Albertsen,TIH) writes:

> Example 2: While the difference in speed between XOR AX, AX and MOV AX, 0
> was significant in the 8086, it is zero in the 386 - yet asm programmers
> use that old cryptic way of zeroing AX, even in 386 specific code.

There are good and bad assembly languages.  The best, as found on the
DECsystem-10, can not be compared to what we find in the 8086 family.
I'll be the first to agree with you on the undesirable quirks in the 8086.
While moving to a high level language covers many of these quirks,
it does nothing for the worst of all - the 64KB memory addressing limitation.

If you insist on using the 8086 as an example of assembly language
programing, I will continue to use C as THE example of a high level language.

I suspect that, to the majority of graduating seniors, assembly language
means 80x86 assembly language, and a high level language IS C.

This probably explains both the current unpopularity of assembly
language and all the non-working HLL programs being turned out.

Note, there is no reason why assemblers can't do local optimizations too.
For instance, MOV AX,0 could be changed to XOR AX,AX.  In addition,
additions can be made to the assembler to improve readability, such
as structured program control structures (loops, conditional blocks).
As you pointed out, assembler programmers frequently do this using
macros.  Assemblers have also been written to do this.  Such changes
do not typically reduce the exactness of assembly language programming.

Features are added to assemblers to aid in program develeopement
just as features are added to compilers.  Unfortunately, less work
has been done in this area for assemblers then compilers due to the
current unpopularity of assembly language.

I view assembly language as just another language.  It has its good and
its bad points, as does any language.  C is of course more portable then
assembly language.  C and assembly are comparable in safety - neither
provides any safety without additional checking built on top of the
compiler.  Assembly language is more exact, thus it's best suited
when the final result simply must work (as when life or company
reputation depends on it).  Neither language is suited for
numerical programming.

There are stylistic differences too.  C is best suited for starving
graduate students hacking out code while looking forward to graduation.
Assembly language is better suited to a more formal environment where
the same company which writes the software is stuck with maintaining it.
This is because, apparently, there is nothing that can be done to
motivate the average C programmer to use comments.  In contrast, assembly
language programmers have no shortage of space on the page for their
comments, so they usually have plenty, sometimes drifting off to subjects
unrelated to the adjacent code.

Examples follow.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This is 6809 code I wrote.  It was written in a hurry, as is all code.

; CHKANS - Check an answer for exact match (case insensitive)
;
; INPUTS:
;
;	X  Address of ASCIC string to compare (must be all uppercase)
;
; OUTPUTS:
;
;	Z condition flag, EQ if match, NE if no match
;
; IMPLICIT INPUTS:
;
;	KBUF,KFILL  Specify other string to compare (either case)

CHKANS:	TFR DP,A		; Keyboard buffer address in Y
	LDB #KBUF
	TFR D,Y

	LDA LO(KFILL)		; Check password length
	ANDA #$7F
	CMPA 0,X+		; Length must be correct to match
	IF$ (EQ)
	  TST A			; If lengths are 0, then we always match
	  IF$ (NE)		; Non-zero length, compare further
	    DO$
	      LDB 0,Y+		; Character from buffer
	      CMPB #@140	; Converted to uppercase
	      IF$ (UGE)
	        SUBB #@40
	      ENDIF$

	      CMPB 0,X+		; Compare with reference
	      BNE EXITDO	; Exit loop if no match

	      DEC A
	    UNTIL$ (EQ)
	  ENDIF$
	ENDIF$
	RTS

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Typical C code, this from GNUPLOT.

unary()
{ /* add code for unary operators */
	if (equals(c_token,"!")) {
		c_token++;
		unary();
		(void) add_action(LNOT);
	}
	else if (equals(c_token,"~")) {
		c_token++;
		unary();
		(void) add_action(BNOT);
	}
	else if (equals(c_token,"-")) {
		c_token++;
		unary();
		(void) add_action(UMINUS);
	}
	else
		factor();
}


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Note how the assembly language programmer pretended to be writing
a fully defined module while the C programmer didn't even bother.
This is not a direct result of the languages, however, there is
something about these two languages that encourages these divergent
styles.

The 6809 code was firmware - it was expected to work - and it did.
The C code was freeware, it was expected to have bugs, which it does.
Problem was solved with a mailing list.

The requirements from the 6809 code were well understood, and significant
algorithms defined, before programming began.  Major additions made later
fit the original structure (else they wouldn't have been made).

The C code probably started as a small plotting program and grew far
beyond the original dream using suggestions from many individuals.

Speed was a prime consideration with the 6809 code.  The originally
conceived algorithms took this into account using concepts which
could not be described in C.  There was NO optimiziation of
individual instructions.  It was obvious how to write optimal code.
Most of the code (including the above section) was not speed critical,
however, writing that portion in another language would have resulted
in a complex interface, and the final result would not have been
aesthetically pleasing.

What we have is different problems with different languages best
suited to the solution.

---

Speaking of aesthetics, how does the HLL programmer define tables?
The above 6809 source code contained a table, which I think is
typical of what assembly language programmers do:

     DefineService (IBM,0,9600,EVEN,0,<PSUECL2>)

	CHN (@000,4A7)
	CHN (@001,4A6)
	CHN (@025,4A1)
	CHN (@026,4A3)
	CHN (@027,4A2)

     DefineService (RASTER,1,9600,NONE,4,<Raster Technology Terminal>)

	CHN (@002,TTY)

     DefineService (2020,0,9600,NONE,3,<DECSYSTEM 2020>)

	CHN (@010,TTY32)
	CHN (@011,TTY33)
	CHN (@012,TTY34)
Newsgroups: alt.folklore.computers