/******************************************************* * File: sumlist.m * Dir: cis235/Examples * Date: Sept 15, 2002 * Author: DSS * Computer: KUNET suns * Assembler: as under the gcc compiler * Compile: sa sumlist * Execute: sumlist * Purpose: sum a list of integers * - list ends when 0 is input * - example of loop with mid-loop test *******************************************************/ .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 !Get the value !scanf(informat, &value1,&value2); set informat, %o0 set value, %o1 !value to be stored is pointed at by value call scanf 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 this slot? !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) !always back to top of loop ba loop nop !can we fill this slot? print: ! printf(outputformat,sum); set outputformat,%o0 !put format string in %o0 !load the sum set sum,%o1 ld [%o1],%o1 call printf,0 !print it nop !delay slot done: call exit, 0 !exit routine mov 0, %o0 !move return code into position during delay