SPARC Assembly/SPARC Instructions
From Wikibooks, the open-content textbooks collection
Contents |
[edit] Instruction Format
Here is a quick-rundown of the arithmetic instruction format, when writing your code:
- mnemonic %rsrcA, op2, %rdest
The mnemonic (also called the opcode) is the specifies the type of operation to be preformed. rsrcA is the first operand, while op2 is the second. rsrcA must be a register, and op2 can usually be an immediate (13 bit) constant, or a second register. rdest is the destination register, and not all instructions have one (for instance: cmp for compare). For instance, the following instructions are completely valid:
add %r3, %r4, %r5
add %r3, 16, %r5
- The first instruction adds register 3 and register 4, and places the result in register 5.
- The second instruction adds register 3, and the number 16, and places the result in register 5.
As you can see, SPARC assembly places a % sign before each register, while literals are written normally.
Warning: The syntax is backwards from Intel syntax (used in PPC chips, and some compilers).
It should also be noted that many SPARC instructions such as the logic and arithmetic instructions have a version of each instruction with "cc" appended to the end of the opcode. These instructions preform the same function as their non-appended counterpart, with the additional capability to set condition codes (more on these later) that regulate branching within a program. For example, the "add" instruction mentioned above has a counterpart "addcc", which when preforming an addition sets specific condition code registers that later branch instructions check when deciding whether or not to branch to another part of the program.
[edit] Comments
Comments will not be read by the compiler, and are indicated with an exclamation point. Anything following the exclamation point on that line will be ignored. Comments help you understand what you are doing, and why you are doing it. The following is a bad example of commenting:
sub %l0, %l3, %g1 ! Subtract local register 3 from l0, and put the result in global 1. ! add %g1, %l4, %g1! We should add in local 4, but I'm not doing that yet
We can easily tell what we are doing; the code says that. The comments have told us nothing.
However this is better:
sub %l0, %l3, %g1 ! Subtract our monthly expenses from our monthly income.
! This will later be used in function _foo.
! add %g1, %l4, %g1! Local 4 will hold "other" sources of income, but we're not there yet.
Now we know what we're doing (we're figuring spare change for the month), and why we're doing it (function foo will apparently use this). Also, note that the second line in both examples will not be executed as their instructions have been commented our (apparently to be implemented later).
Do note that many people comment almost every single line of code in assembly, due to the fact that it can sometimes be difficult to tell what's going on without useful things like variable names, complex structures, etc.
[edit] Labels
Labels are indicated by placing a "label_name:" at some point in your code. As labels are just areas that give the compiler addresses, they can be used for anything that's stored directly in the program: most popularly, these are used for function jumps (via branch or call statements), or ways to reference constant variables. If it is a function, and not a jump, ".global" must be placed before it. There is one special label/global, just like every language, and that is the "main" label, which is where the program starts.
.global main
main: save %sp, -64, %sp
mov 5, %l0
cmp %l0, 5
be,a end
ta 1 ! Should NEVER happen
end: mov 1, %g1
ta 0
In this program are a bunch of things that might not be understandable, but this is a full program. It starts at the main function (defined by ".global main, followed by the label). It then copies the value "5" into a local register, and then compares that register to the value of "5". If they are equal, it branches to the label end, where the program then exits.