.global main .section ".data" ! initialized data section .data .align 4 .str0: .asciz "Enter string (<= 24 chars): " .align 4 .str1: .asciz "\nThe number of chars you typed is %d" .align 4 .str2: .asciz "%c" ! formatting string; .section ".text" ! source code section .text .align 4 main: save %sp,96,%sp ! creates new register set sethi %hi(.str0),%o0 ! print greeting prompt or %o0,%lo(.str0),%o0 call printf nop sethi %hi(.strbuf),%o0 ! pass address of strbuf to o0 or %o0,%lo(.strbuf),%o0 call gets ! gets string, stores in .strbuf nop mov 0,%l0 ! l0 is counter; counts # of chars .beginloop: sethi %hi(.str2),%o0 ! prepare for printf("%c",char) or %o0,%lo(.str2),%o0 sethi %hi(.strbuf),%l1 ! pass address of strbuf to l1 or %l1,%lo(.strbuf),%l1 ! start address of buffer ldub [%l1+%l0], %o1 ! load (unsigned) byte from buffer cmp %o1,0 ! test for end of line; be .endofloop ! exit nop call printf ! prints char nop ba .beginloop ! unconditional branch to beginloop add %l0,1,%l0 ! seen one char - no delay slot wasted! .endofloop: ! prints # of chars and quits sethi %hi(.str1),%o0 or %o0,%lo(.str1),%o0 mov %l0,%o1 call printf nop mov 1,%g1 ! trap to os ta 0 .section ".bss" ! global buffers section .bss .align 8 .strbuf: .skip 24 ! 24 byte buffer ! 24%8==0, so no need to realign! temp: .skip 4 ! 4 byte buffer