Professional Documents
Culture Documents
7/15/10
3.0 COMMENTS
3.1 Content
Comment all coding to convey the global role of an instruction, and not simply a literal translation of the
instruction into English. In general, this will consist of a comment per line of code, except when the
meaning is obvious without the comment. If a particularly difficult, obscure, or elegant instruction
sequence is used, a paragraph of comments should immediately precede that section of code.
3.2 Decision Points
Comments at decision points should indicate clearly what decision is being made and what the outcomes
are. For example:
cmpa
max
;is it maximum?
beq
exit
;if yes, time to exit
; else,...
3.3 Subroutine Calls
Comments at the point of call to a subroutine should state clearly why the subroutine is being called.
3.4 Data
It is perhaps even more important to comment data than instructions. Describe the function for which the
data is used. Tables should be thoroughly commented.
4.0 NAMING STANDARDS
4.1 Registers
CPU Registers - Only the standard names are permitted as register names; do not equate a register to
another name even if the assembler allows it.
Device Registers - These are symbolically named identically to the hardware notation used in the device
documentation. For example, if the port A data register is documented as PORTA, only this symbolic
name may be used to refer to this register.
4.2 Instruction Mnemonics
Executable instruction mnemonics should be written in all lower case, e.g., ldaa.
4.3 Assembler Directives
Assembler directives should be written in all upper case, e.g., ORG.
4.4 Equated Symbols
Equated symbols should be all (or mostly) upper case. The name should express the meaning of what the
symbol is defining, e.g., RWDATA for the read/write data area. Symbols should be arranged alphabetically
within functional groups unless there is another obvious useful order.
Program-Local Symbols - Self-relative address arithmetic (*+n) is absolutely forbidden in branch
instructions (except the special case: bra *); its use in other contexts must be rare.
Other Symbols - Frequently-used bit patterns such as CR and LF can be made conventional symbols on
an as-needed basis. In general, hard-coded constant values should be avoided; use symbols instead. For
example, in referring to the length of a buffer, the best method is to refer to a symbol whose value is
automatically the correct length of the buffer.
7/15/10
;
; User Equates
;
LARRAY: EQU
100
;
; Local Data Read/Write
;
array: DC.B
LARRAY
;length of array
;work array
This method has several advantages. First, if a single constant that is referred to in numerous places in
the code must be altered, you need perform only one edit (to the symbol definition) to effect the change.
Second, if all the symbols are gathered in one place in alphabetical order, reading the code is much
simplified. Third, you can find all references to a symbol in a cross-reference listing. The cross-reference
capability allows you to examine all the references to a symbol and confidently assess the effects of
altering the symbol definition. Fourth, using a symbol name reflecting the true meaning behind a value
makes the code clearer than using the number itself. These advantages are lost if you use constants.
4.5 Data Labels
Labels of read-only and read/write data should be all lower case. The name should express the meaning
of the data the symbol is defining, e.g., counter.
4.6 Subroutine Names
The name of a subroutine should be all lower case and should be on a line by itself preceding the target
instruction. The name should convey the function of the subroutine, e.g., print_header.
4.7 Branch Targets
The label of a branch or call target should be all lower case and should be on a line by itself preceding the
target instruction. In a short program the name can express the meaning of the target block of
instructions. However, in larger programs it is best to use an alphabetically ordered sequence of labels
related to the name of the program module. For example, in the subroutine named print_header, the
branch targets could be: ph10, ph20, ph30, etc.
4.8 Multiword Names
Multiword names can use an underscore _ to separate the words, e.g., print_header. Alternatively,
uppercase can be used for the second and succeeding words, e.g., printHeader.
4.9 Table Entry Offsets
Equated symbols that represent offsets to individual entries in a table or structure should be of the form
table.ITEM. For example, if the a parameter table parms holds two parameters to be passed to a
subroutine, the name of an array and the length of the array, the equated symbol offsets to those items
could be parms.ADDR and parms.LEN, whose values would be 0 and 2, respectively.
4.10 Constant Data
Constants should be expressed in the base and format that conveys the true meaning and context. Let the
assembler do the work of converting to machine format. For example:
year: DC.B 365
ldaa #'N'
ORG $0800
;days in year
NOT
NOT
NOT
7/15/10
7/15/10
or continuous loop
;******************************************
; Constant data in ROM
Const: SECTION
Constant definitions
;******************************************
; Variable data in RAM
Data:
SECTION
Variable Data
7/15/10
7/15/10
5.3 Subroutines
As stated above, modular programming is accomplished by implementing major functions of the flow
chart in separate subroutine modules. Of primary importance is that the interface to the calling module be
clear and understandable. This clarity requires adherence to a set of calling and return conventions and
careful documentation of the interface in the subroutine.
5.3.1 Calling Conventions - The following calling conventions must be observed.
Argument Passing The standard methods for passing arguments to and from a subroutine used in this
course are: all arguments in the registers, all arguments on the stack, or a combination of these two.
Register Conventions - A common convention is that on entry a subroutine saves all registers it intends to
alter except result registers, and then restores them on exit (state preservation). The only exception to this
is a short convenience subroutine that is not designed to be reused in other programs. Saving and
restoring occurs on the stack.
5.3.2 Exiting - All subroutine exits occur through a single rts instruction.
5.3.3 Argument Checking - Subroutines are responsible for verifying the validity of arguments passed to
them. The design of a subroutine's calling sequence should aim at minimizing the validity checks by
minimizing invalid combinations.
5.3.4 Local Data If at all possible, the subroutine should store all local variables on the stack, rather
than using dedicated space in RAM. Use local symbol offsets to access the variables in the stack frame.
Local constants should be stored immediately preceding the subroutine code.
5.3.5 Subroutine Format
The following documentation sections must be included in comments within each subroutine.
1. Subroutine Name. Name (entry point) used to call the subroutine.
2. Subroutine Function. This section can be as general or specific as necessary to clearly describe the
function of the subroutine. At a minimum, it should include which registers are destroyed.
3. Inputs. A list of the inputs expected by the subroutine. This includes the calling sequence, condition
flag settings, and global data settings.
4. Outputs. A list of the outputs produced as a result of entering this subroutine. These include delivered
results, condition flag settings, but not side effects (all these outputs are visible to the caller).
5. Effects. A list of all effects, including side effects, produced as a result of entering this subroutine.
Effects include alterations in the state of the system not explicitly expected in the calling sequence, or
those not visible to the caller.
5.3.5 Naming in Subroutines
All symbols defined in the subroutine should be prefixed with the name of the subroutine (or a shortened
version thereof) in order to avoid naming conflicts. After the prefix, follow the standard rules for naming.
For example, in the subroutine named clear:
1. Branch target: clear1, clear2, clear_loop.
2. Local constant data: clear_max.
3. Local symbol equate: clear_LO4.
4. Local offset equate: clear.ADDR
7/15/10
7/15/10
7/15/10
.
rts
.
bra
exit
.
bra
exit
.
bra
exit