!====================================================================== ! sum.S ! Integer Branch 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" !====================================================================== .align 4 piSum: .word 0 !====================================================================== .section ".bss" !====================================================================== .align 4 pi: .skip 4 .align 4 piFirst: .skip 4 .align 4 piSecond: .skip 4 !====================================================================== .section ".text" !====================================================================== !---------------------------------------------------------------------- ! 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, -96, %sp ! printf("Enter an integer: "); set pcPrompt1, %o0 call printf nop ! scanf("%d", &iFirst); set pcScanfFormat, %o0 set piFirst, %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 set piSecond, %o1 call scanf nop ! i = iFirst; set piFirst, %l0 ld [%l0], %l0 set pi, %l1 st %l0, [%l1] loop1: ! if (i > iSecond) goto loopend1; set pi, %l0 ld [%l0], %l0 set piSecond, %l1 ld [%l1], %l1 cmp %l0, %l1 bg loopend1 nop ! iSum += i; set pi, %l0 ld [%l0], %l0 set piSum, %l1 ld [%l1], %l2 add %l0, %l2, %l2 st %l2, [%l1] ! ++i; set pi, %l0 ld [%l0], %l1 inc %l1 st %l1, [%l0] ! goto loop1; ba loop1 nop loopend1: ! printf("The sum of all integers between the two is %d.\n", iSum); set pcResult, %o0 set piSum, %o1 ld [%o1], %o1 call printf nop ! return 0; mov 0, %i0 ret restore