!-------------------------------------- ! sumsub.S ! Subroutines !-------------------------------------- #define FIRST_OFFSET 4 #define SECOND_OFFSET 8 #define SUMBETWEEN_OFFSET 12 #define MAIN_LOCAL_SIZE 12 #define I_OFFSET 4 #define SUM_OFFSET 8 #define SUMBETWEEN_LOCAL_SIZE 8 !-------------------------------------- .section ".rodata" pcPrompt1: .asciz "Enter an integer: " pcPrompt2: .asciz "Enter another integer that is greater than the first: " pcResult: .asciz "The sum of all integers between the two is %d.\n" pcScanfFormat: .asciz "%d" !-------------------------------------- .section ".data" !-------------------------------------- .section ".bss" !-------------------------------------- .section ".text" .align 4 sumBetween: ! Register map: ! %i0 int iStart ! %i1 int iEnd save %sp, (-92 - SUMBETWEEN_LOCAL_SIZE) & -8, %sp ! iSum = 0; clr [%fp - SUM_OFFSET] ! i = iStart; st %i0, [%fp - I_OFFSET] loop1start: ! if (i > iEnd) goto loop1end; ld [%fp - I_OFFSET], %l0 cmp %l0, %i1 bg loop1end nop ! iSum += i; ld [%fp - SUM_OFFSET], %l0 ld [%fp - I_OFFSET], %l1 add %l0, %l1, %l0 st %l0, [%fp - SUM_OFFSET] ! ++i; ld [%fp - I_OFFSET], %l0 inc %l0 st %l0, [%fp - I_OFFSET] ! goto loop1start ba loop1start nop loop1end: ! return iSum; ld [%fp - SUM_OFFSET], %i0 ret restore !-------------------------------------- .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 ! iSumBetween = sumBetween(iFirst, iSecond); ld [%fp - FIRST_OFFSET], %o0 ld [%fp - SECOND_OFFSET], %o1 call sumBetween nop st %o0, [%fp - SUMBETWEEN_OFFSET] ! printf("The sum of all integers between the two is %d.\n", iSumBetween); set pcResult, %o0 ld [%fp - SUMBETWEEN_OFFSET], %o1 call printf nop ! return 0; mov 0, %i0 ret restore