X86 Assembly/Comments

From Wikibooks, open books for an open world
< X86 Assembly
Jump to navigation Jump to search

When writing code, it is very helpful to use some comments explaining what is going on, and particularly why. Sometimes why means repeating drawn conclusions, that, for instance, at one point it has been established as knowledge that data meet certain criteria.

A comment is a piece of regular text that the assembler just discards when turning assembly code into machine code. In assembly, comments are usually denoted by a semicolon ;, although GAS uses # for single line comments and /* … */ for block comments possibly spanning multiple lines.

Here is an example:

	xor rax, rax                          ; rax ≔ false
	
	; divisibility by four
	test rcx, 3                           ; are the two right-most bits set?
	jnz done                              ; yes ⇒ not divisible by 4
	
	setz al                               ; al ≔ ZF  [i.e. `true`, since `jnz` above]
	

Everything after the semicolon, on the same line, is ignored.

Sometimes, during debugging, regular comments can be used to track down bugs, that means errors in programs that cause unexpected and undesired behavior. For that, actual source code is commented out:

Label1:
	mov ax, bx
	;mov cx, ax   ; possibly _overwriting_ some needed value?
	

Here, the assembler never sees the second instruction mov cx, ax, because it ignores everything after the semicolon.

HLA Comments[edit]

The HLA assembler also has the ability to write comments in C or C++ style, but we cannot use the semicolons. This is because in HLA, the semicolons are used at the end of every instruction:

mov(ax, bx); // This is a C++ comment.
/*mov(cx, ax);  everything between the slash-stars is commented out. 
                This is a C comment*/

C++ comments go all the way to the end of the line, but C comments go on for many lines from the "/*" all the way until the "*/". For a better understanding of C and C++ comments in HLA, see Programming:C or the C++ Wikibooks.