!-------------------------------------- ! 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 ".data" !-------------------------------------- .section ".bss" !-------------------------------------- .section ".text" .align 4 sumBetween: ! Register map: ! %o0 int iStart ! %o1 int iEnd ! %o2 int i ! %o3 int iSum ! 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 !-------------------------------------- .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