!-------------------------------------- ! sumstack.S ! The Stack !-------------------------------------- #define FIRST_OFFSET 4 #define SECOND_OFFSET 8 #define SUM_OFFSET 12 #define I_OFFSET 16 #define MAIN_LOCAL_SIZE 16 !-------------------------------------- .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" !-------------------------------------- .section ".text" .align 4 .global main main: save %sp, (-92 - MAIN_LOCAL_SIZE) & -8, %sp ! printf("Enter an integer: "); set pcPrompt1, %o0 call printf nop ! scanf("%d", &iFirst); set pcScanfFormat, %o0 sub %fp, FIRST_OFFSET, %o1 call scanf nop ! printf("Enter another integer that is greater than the first: "); set pcPrompt2, %o0 call printf nop ! scanf("%d", &iSecond); set pcScanfFormat, %o0 sub %fp, SECOND_OFFSET, %o1 call scanf nop ! iSum = 0; clr [%fp - SUM_OFFSET] ! i = iFirst; ld [%fp - FIRST_OFFSET], %l0 st %l0, [%fp - I_OFFSET] loop1: ! if (i > iSecond) goto loopend1; ld [%fp - I_OFFSET], %l0 ld [%fp - SECOND_OFFSET], %l1 cmp %l0, %l1 bg loopend1 nop ! iSum += i; ld [%fp - I_OFFSET], %l0 ld [%fp - SUM_OFFSET], %l1 add %l1, %l0, %l1 st %l1, [%fp - SUM_OFFSET] ! ++i; ld [%fp - I_OFFSET], %l0 inc %l0 st %l0, [%fp - I_OFFSET] ! goto loop1; ba loop1 nop loopend1: ! printf("The sum of all integers between the two is %d.\n", iSum); set pcResult, %o0 ld [%fp - SUM_OFFSET], %o1 call printf nop ! return 0; mov 0, %i0 ret restore