!-------------------------------------- ! sumopt1.S ! Optimization by Minimizing Memory Access !-------------------------------------- .section ".rodata" pcPrompt1: .asciz "Enter an integer: " pcPrompt2: .asciz "Enter another integer that is greater than the first: " pcScanfFormat: .asciz "%d" pcResult: .asciz "The sum of all integers is %d.\n" !-------------------------------------- .section ".data" !-------------------------------------- .section ".bss" .align 4 piFirst: .skip 4 .align 4 piSecond: .skip 4 !-------------------------------------- .section ".text" .align 4 .global main main: ! Register map: ! %l0 int i; ! %l1 int iFirst; ! %l2 int iSecond; ! %l3 int iSum; save %sp, -96, %sp ! printf("Enter an integer: "); set pcPrompt1, %o0 call printf nop ! scanf("%d", &iFirst); set pcScanfFormat, %o0 set piFirst, %o1 call scanf nop set piFirst, %l1 ld [%l1], %l1 ! printf("Enter another integer that is greater than the first: "); set pcPrompt2, %o0 call printf nop ! scanf("%d", &iSecond); set pcScanfFormat, %o0 set piSecond, %o1 call scanf nop set piSecond, %l2 ld [%l2], %l2 ! iSum = 0; mov 0, %l3 ! i = iFirst; mov %l1, %l0 loop1: ! if (i > iSecond) goto loopend1; cmp %l0, %l2 bg loopend1 nop ! iSum += i; add %l3, %l0, %l3 ! ++i; inc %l0 ! goto loop1; ba loop1 nop loopend1: ! printf("The sum of all integers between the two is %d.\n", iSum); set pcResult, %o0 mov %l3, %o1 call printf nop ! return 0; mov 0, %i0 ret restore