For an assignment, I had to write a very simple SPARC program.. Read in an integer, n, and then n integers. Display the min, max, sum, and average.
Just for the heck of it, I thought I'd show you what it looks like :-)
(ps. it wont run unless you link it with SPARCio.c, which is just a library of i/o stuff, decin, decout, strin, strout, etc.)
! Standard prolog
.section ".text"
.global main
.align 4
main: save %sp,-96,%sp ! Allocate 96 bytes from the stack
!NAME iago
!STUDENT NUMBER 666
!COURSE 74.222
!INSTRUCTOR Sara
!ASSIGNMENT 3
!QUESTION 1
!
! This program will read in an integer value of n and then n integers.
! It will calculate and print the minimum, maxiumu, sum, and average
! of all the numbers
! Register usage:
! %L7 - n
! %L6 - The current number
! %L5 - Total (sum)
! %L4 - Max
! %L3 - Min
! %L2 - Backup of n
! Print identification banner
set banner,%o0
call strout
nop
! Get the value for n
set nprompt,%o0
call strout
clr %L5 ! Clear Total in delay slot
call decin
nop
mov %o0,%L7
mov %o0,%L2
! Prompt for the first value
set iprompt,%o0
call strout
nop
! Read in the first value
call decin
nop
! The first value in is the min AND the max AND the sum!
mov %o0,%L3
mov %o0,%L4
mov %o0,%L5
! Branch to the end of the loop (if the value was 0 which we can assume it's not, it will end up as
! -1 and screw stuff up.)
ba endloop
nop
! The beginning of the loop
begin:
! Read in the next value
call decin
nop
mov %o0,%L6
add %L6,%L5,%L5
! Check if it's the new min
cmp %L6,%L3
bg notmin
nop
mov %L6,%L3
notmin:
! Check if it's the new max
cmp %L5,%L4
bl endloop
nop
mov %L6,%L4
endloop:
deccc %L7
bnz begin
nop
! All the n's have been read.
! Display the min, max, and sum
set min,%o0
call strout
nop
mov %L3,%o0
call decout
nop
call newline
nop
set max,%o0
call strout
nop
mov %L4,%o0
call decout
nop
call newline
nop
set sum,%o0
call strout
nop
mov %L5,%o0
call decout
nop
call newline
nop
! Now determine the average, and put it in %L6
mov %g0,%y
sdiv %L5,%L2,%L6
! display the average
set avg,%o0
call strout
nop
mov %L6,%o0
call decout
nop
call newline
nop
end:
set EOP,%o0
call strout
nop
ret ! Return
restore ! Delay slot
! The data section
.section ".data"
banner: .ascii "NAME iago\n"
.ascii "STUDENT NUMBER 666\n"
.ascii "COURSE 74.222\n"
.ascii "INSTRUCTOR Sara\n"
.ascii "ASSIGNMENT 3\n"
.asciz "QUESTION 1\n"
nprompt: .asciz "Enter n:"
iprompt: .asciz "Enter n integer(s):"
min: .asciz "Minimum = "
max: .asciz "Maximum = "
sum: .asciz "Sum = "
avg: .asciz "Average = "
EOP: .asciz "End of Processing\n\n"
<-- dumb ;)
I have no experience with SPARC so excuse me if this seems ignorant (it is) but why do you have to use so many nops?
I forgot to mention that. SPARC does something weird, it reads one line below what it's executing, so in this situation:[code
set nprompt,%o0 call strout
clr %L5[/code]
or, in fact, in any situation where there's a jump, it reads the call while executing the set, then it reads the clr and executes the call, then it executes what's below it.
SPARC has what's called a delay slot; the line directly after a jump is executed before the jump. It's rather silly, but you can either nop it (like we're actually supposed to) or put an instruction there that has to be executed either way.
When asked what happens if it's another jump, the prof responded, "Nobody knows." It's actually system-dependant and has unpredictable results. Sounds like it could be fun ;-)
Also note the backwards mov/add/sub/etc.
mov src,dest ! Doesn't set flags
add num1,num2,dest ! Doesn't set flags
addcc num1,num2,dest ! Sets flags
And technically, every command is 4 bytes: a one byte command and 3 one byte operands. mov is actually a synthetic command, which is replaced by:
add %g0,src,dest
%g0 is ALWAYS 0. A cmp is actually this:
sub num1,num2,%g0
%g0 will never change from 0, so the result is discarded.
clr %L2 will end up like this:
add %g0,0,%L2
And so forth.
A nop is needed after every function call because of SPARCs tendency to pipeline instructions.
What if you do something that isn't a call/jump where the second instruction depends on the first? Such as (pretend it's SPARC and not x86)
mov ebx, ecx
mov eax, ebx
If it sets the flags, we've been told it has unpredictable results and should not be done.
Oh well. I'll keep my x86 to myself.