!-------------------------------------- ! sumopt2.S ! Optimization by Eliminating nop Instructions !-------------------------------------- .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 between the two 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: "); sethi %hi(pcPrompt1), %o0 call printf or %o0, %lo(pcPrompt1), %o0 ! scanf("%d", &iFirst); set pcScanfFormat, %o0 sethi %hi(piFirst), %o1 call scanf or %o1, %lo(piFirst), %o1 set piFirst, %l1 ld [%l1], %l1 ! printf("Enter another integer that is greater than the first: "); sethi %hi(pcPrompt2), %o0 call printf or %o0, %lo(pcPrompt2), %o0 ! scanf("%d", &iSecond); set pcScanfFormat, %o0 sethi %hi(piSecond), %o1 call scanf or %o1, %lo(piSecond), %o1 set piSecond, %l2 ld [%l2], %l2 ! iSum = 0; mov 0, %l3 ! for (i = iFirst; i <= iSecond; ++i) iSum += i; ba looptest1 mov %l1, %l0 loop1: inc %l0 looptest1: cmp %l0, %l2 ble,a loop1 add %l3, %l0, %l3 ! printf("The sum of all integers between the two is %d.\n", iSum); set pcResult, %o0 call printf mov %l3, %o1 ! return 0; mov 0, %i0 ret restore