!====================================================================== ! sumsubleaf.S ! Leaf Subroutines !====================================================================== #define FIRST_OFFSET 4 #define SECOND_OFFSET 8 #define SUMBETWEEN_OFFSET 12 #define MAIN_LOCAL_SIZE 12 !====================================================================== .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 ".text" !====================================================================== !---------------------------------------------------------------------- ! int sumBetween(int iStart, int iEnd) ! ! Return the sum of all integers between iStart and iEnd. ! ! Register map: ! %o0 int iStart ! %o1 int iEnd ! %o2 int i ! %o3 int iSum !---------------------------------------------------------------------- .align 4 sumBetween: ! iSum = 0; clr %o3 ! i = iStart; mov %o0, %o2 loop1start: ! if (i > iEnd) goto loop1end; cmp %o2, %o1 bg loop1end nop ! iSum += i; add %o3, %o2, %o3 ! ++i; inc %o2 ! goto loop1start ba loop1start nop loop1end: ! return iSum; mov %o3, %o0 retl nop !---------------------------------------------------------------------- ! int main(int argc, char *argv[]) ! ! Read two integers from stdin, and write to stdout the sum of all ! integers between the two. ! ! Register map: ! %i0 int argc ! %i1 char *argv[] !---------------------------------------------------------------------- .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