/******************************************************* * File: sumlist_delay_a.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 * - eliminate sum variable, use %01 instead of %l2 and * - demo annulled branch *******************************************************/ define(_sum,%l5) .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 value: .word 0 .global main main: save %sp,-96,%sp mov 0,_sum ! 0 into %o1 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 not 0, do it again cmp %l1,0 bne,a loop !nop !can we fill slot? Not with cmp, which sets cond codes !but we can use an annulled branch, and put the add in !the delay slot. It will execute only if branch is taken !sum+=value add %l1,_sum,_sum print: ! printf(outputformat,sum); set outputformat,%o0 !put format string in %o0 call printf,0 !print it mov _sum,%o1 done: call exit, 0 !exit routine mov 0, %o0 !move return code into position during delay