Valhalla Legends Forums Archive | Assembly Language (any cpu) | SPARC

AuthorMessageTime
iago
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.)

[code]! 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"

[/code]
March 5, 2003, 3:22 PM
Etheran
<-- dumb ;)
March 5, 2003, 5:38 PM
Yoni
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?
March 5, 2003, 6:30 PM
iago
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 ;-)
March 5, 2003, 8:25 PM
iago
Also note the backwards mov/add/sub/etc.  

[code]mov src,dest ! Doesn't set flags
add num1,num2,dest ! Doesn't set flags
addcc num1,num2,dest ! Sets flags[/code]


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:
[code]add %g0,src,dest[/code]

%g0 is ALWAYS 0.  A cmp is actually this:
[code]sub num1,num2,%g0[/code]

%g0 will never change from 0, so the result is discarded.

clr %L2 will end up like this:
[code]add %g0,0,%L2[/code]

And so forth.
March 5, 2003, 8:59 PM
Hostile
A nop is needed after every function call because of SPARCs tendency to pipeline instructions.
March 11, 2003, 5:48 PM
Yoni
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)
[code]mov ebx, ecx
mov eax, ebx[/code]
March 11, 2003, 7:41 PM
iago
If it sets the flags, we've been told it has unpredictable results and should not be done.
March 12, 2003, 11:47 AM
Yoni
Oh well. I'll keep my x86 to myself.
March 12, 2003, 4:16 PM

Search