/* sparc assembly program to read in a string and print it backwards */ .global main .section ".data" /* data section */ .data .align 4 str0: .asciz "Enter a string: " .align 4 str1: .asciz "%c" /* format string used by scanf */ .align 4 str2: .asciz"\n" .section ".bss" /* uninitialized data section */ .bss .align 4 strbuffer: .skip 64 ! 64 byte buffer .section ".text" /* code section */ .text .align 4 main: save %sp,-120,%sp /* print prompt string */ sethi %hi(str0),%o0 or %o0,%lo(str0),%o0 ! mov address of str0 into o0 call printf nop ! pipeline delay slot /* use %l1 as pointer to position in buffer, %l0 as counter */ set strbuffer,%l1 ! set is a macro for sethi+or lo mov 0,%l0 /* scanf loop */ scanlp: set str1,%o0 mov %l1,%o1 call scanf nop /* set str0,%o0 ! print "\n" call printf nop */ add %l0,1,%l0 ldub [%l1],%o2 ! load (unsigned) byte from buffer to %o2 cmp %o2,10 ! check for carriage return bne scanlp inc %l1 ! macro for add %o1,1,%o1, fills delay slot /* at this point l0 holds number of chars+1, o1 points to char past last */ dec %l0 dec %l1 /* print loop: */ prlp: set str1,%o0 ! o0 erased after each printf! ldub [%l1],%o1 call printf nop dec %l1 dec %l0 cmp %l0,0 bge prlp nop /* print \n to end */ set str2,%o0 call printf nop restore mov 1,%g1 ta 0 ! trap to OS