This is just a small Sparc primer giving just the basics you will need for this assignment. A more complete primer can be found off the class web site.
Even though we will keep our variables in the activation record on the stack or on the heap (via malloc), the Sparc commands only work with registers. You will have to move your values from memory into registers before a command and then back into memory after the command.
%g1-7 : global registers; don't change value when a new function is called
%g0 holds the value 0, %g1 is a good temporary register
%l0-7 : local regs -> their values change when a new procedure is called
%o0-5 : output regs -> fill these with parameters before you call the procedure
%i0-5 : input regs -> in a procedure this is where the parameters are -
notice no more than 6 parameters to a function
%sp
%fp
- format of a statement
<label>: <command> arg [,arg[,arg]] ! comment
- basic commands
add, sub, or, and, xor, xnor, sll, srl, sra,
all of these have the form:
<command> %regs, roi, %regd ! %regd = %regs <command> roi
roi = register or immediate value (value is -4096 <= value < 4096)
no extended or carry
notice no divide or multiply commands - use C function .mul, .div
notice no mod commands - use C function .rem
- pseudo commands
nop, cmp, tst, set (mov), not, neg, inc, dec, clr
not real assembly commands; the assembler (cc) will take care of
converting these into appropriate assembly commands. CC never uses
psuedo ops but they make reading the assembly MUCH easier
clr %reg
is really
or %g0, %g0, %reg
- ld / st commands
only worry about word size data
example: x := 99
set 99, %l0
st %l0, [%fp-8]
example: x := x + 3 where x is being stored at %fp-8
ld [%fp-8], %l0
add %l0, 3, %l0
st %l0, [%fp-8]
- Branching commands
conditions codes in icc
N Z only - don't worry about Z & V
<branch command> label
ba, bn, bne, be, bg, bge, gl, ble, bpos, bneg
Delay slot:
commands take more than 1 cycle - remember pipelining (fetch, decode,
fetch operands, execute & store),
even with branch command you are gaurenteed the next command will
be executed so always do a nop after branch (same is true with
any procedure call)
- procedure commands
call <label>
nop
There are many different segments to an assembly program, but we only care about 2 - one for code and one for static data.
All assembly code must be defined in the text segment. To define code in the text segment, preceed a block of assembly code with the following line:
.text
Static data (in our case, we will only do this for string literals) must be defined in the data segmnent. Preceed a block of static data with the following line:
.data
.text .global <funcname> <funcname>: save %sp, <funcname>_STORAGE, %sp ret restore <funcname>_STORAGE = -96 ! we are using a 2 pass assembler so this worksYour function / data can go anywhere around main; before / after - it doesn't matter.
.data .align 4 HelloStr: .asciz "Hello" ScanfStr: .asciz "%d"