/******************************************************* * File: sumlist_filldelay.m * Dir: cis235/Examples * Date: Sept 15, 2002 * Author: DSS * Computer: KUNET suns * Assembler: as under the gcc compiler * Compile: sa sumlist_filldelay * Execute: sumlist_filldelay * Purpose: sum a list of integers * - list ends when 0 is input * - example of loop with mid-loop test * - fill delay slots when possible *******************************************************/ .data .align 4 prompt: .asciz "Enter an integer, 0 to get sum >" informat: .asciz "%d" outputformat: .asciz "The sum of the list is %d\n" .align 4 sum: .word 0 value: .word 0 .global main main: save %sp,-96,%sp loop: !Prompt user set prompt,%o0 call printf nop !can't fill this one !Get the value !scanf(informat, &value1,&value2); set informat, %o0 set value, %o1 !value to be stored is pointed at by value call scanf !nor this one nop ! can't fill delay slot !add new value to sum !load value just read into register set value,%l1 ld [%l1],%l1 !if it's 0, time to finish cmp %l1,0 be print nop !can we fill slot? Not with cmp, which sets cond codes !otherwise, load the sum set sum,%l2 ld [%l2],%l2 !sum+=value add %l1,%l2,%l2 !store the sum set sum,%l1 !%l1 points to sum !st %l2,[%l1] !*(%l1)=%l2 (the new sum)...can fill delay slot! !always back to top of loop ba loop st %l2,[%l1] !*(%l1)=%l2 (the new sum) print: ! printf(outputformat,sum); set outputformat,%o0 !put format string in %o0 !load the sum - wait! it's still in %l2! !set sum,%o1 !not necessary !ld [%o1],%o1 !mov %l2,%o1 !not here! Do it below... call printf,0 !print it !move the sum into %o1, and do it in the delay slot! mov %l2,%o1 done: call exit, 0 !exit routine mov 0, %o0 !move return code into position during delay