Comments are even more important in assembly language than they are in high-level languages. They are not just for other people to read - you read your own code more often than others do. You need the comments when debugging, for example, to check that you have a correct algorithm and to check that the assembly language code implements it correctly.
There are generally four types of comments.
As in high level language programs, the most important comments in an assembly language program are subprogram comments. These comments should
Subprogram comments should be put in when you begin writing code. Most students spend more time debugging code than writing it. When you are debugging code, you will need to read through your code to try to find out what you are doing wrong. That is difficult and prone to error without comments that indicate how subprograms are called. The time you are saving by not putting in the comments gets paid back, probably many times over.
Most programs with subprograms will need to use a run-time stack. Subprogram invokations can set up a stack frame for storing data that cannot be held in registers. For example, in a recursive subprogram, each invokation of the subprogram needs its own stack frame if for no other reason than to hold the return address. The stack frame can also be used for local variables and for saving and restoring registers.
The design of the stack frame for a subprogram should be done before writing code. Every item on the stack should be given a good descriptive name. Then the displacement for each name should be recorded in subprogram comments.
It is not necessary to comment every line. When three or four lines of code work together to accomplish some higher level task, it is better to have a single higher level comment for the group of lines.
With this kind of commenting, you can engage in two kinds of thinking when you are debugging. By looking at your comments, you can check the correctness of the algorithm. Then if the algorithm is correct, you can look at code and compare it to the comments - does the code do what the comment says? This kind of debugging only requires checking assembly language for short segments of code. The rest of the time you are thinking in terms of a higher level algorithm.
Another kind of helpful commenting is comments describing how a register is used. If a register is used for a single purpose within a subprogram it is useful to describe it and to give it a meaningful name, and use that name in your comments in the body of the code. Otherwise the algorithm comments look like:
# Here are some useless comments. lb $t3, ($t0) # load $t3 sb $t3, ($t1) # store contents of $t3 sub $t2, $t2, 1 # decrement $t2 add $t0, $t0, 1 # increment $t0 add $t1, $t1, 1 # increment $t1
These comments do not help anyone with a minimal knowledge of assembly language. They certainly will not help you to read your own code.
The following comments are better; they get across a higher level view of what is happening:
# Register usage: # # $t0 (ptr1) - pointer into str1 # $t1 (ptr2) - pointer into str2 # $t2 (numLeft) - number of characters left to be copied # $t3 (temp) - temporary register for character move # lb $t3, ($t0) # copy a character from *ptr1 to *ptr2 sb $t3, ($t1) sub $t2, $t2, 1 # decrement numLeft add $t0, $t0, 1 # advance ptr1 and ptr2 add $t1, $t1, 1