Sparc Assembly, what can be said about it....

Ok, so we have a RISC architecture, which is really supposed to have code generated for it by compilers, so what do we do? none other than learn how to code assembly on it *grins*.

I guess the best place to start would be none other than hello world:

hello.s <---Not actually part of the code, just to give an example of how to name the files...

.align 4
.global main
    mov 0,%o0
    set string %o1
    mov 14,%o2
    mov 4,%g1
    ta 0
    mov 0,%o0
    mov 1,%g1
    ta 0
.align 8
    .asciz "Hello, World!\n"
That should be compilable using gcc, but I make no guarantees that something didn't go wrong with transferring it across. Also, this code is intended for Solaris, but it'll probably still work on something like NetBSD as I think it's using the SunOS compatability syscalls

Now, for some more useful code (I guess...), something a but more useful, how to print out a fibonacci sequence (although I think there are still some errors in here):


.align 4
.global main
    save %sp,-112,%sp
    mov 0,%l0       ! int i=0
    mov 1,%l1       ! int j=1
    mov 20,%l2      ! int max=20
    mov 0,%l3       ! int count=0
    ! %l4 is our temp variable
    set message,%o0
    mov %l2,%o1
    call printf     ! display a message of our intentions
loop:                ! do{
    set nummess,%o0
    mov %l0,%o1
    call printf             ! printf("%d\n", i)
    inc %l3                 ! count++
    add %l0,%l1,%l4         ! temp=i+j
    mov %l1,%l0             ! j=i
    mov %l4,%l1             ! i=temp
    cmp %l3,%l2
bl loop ! } while(count < max)
nop mov 0,%o0 call exit ! exit(0) nop ret restore .section ".rodata" .align 8 message: .asciz "The first %d fibonacci numbers are:\n" nummess: .asciz "%d\n"
I hope that shows that some C can be converted into sparc assembly quite easily (or, atleast I thought so). Also, my writing this, taught me to not put out code done at 2am until it has been proofread after a good night sleep...

Log in or register to write something here or to contact authors.