Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write an MIPS program to compute three sums: 1 + 2 + 3 + 4 + ... + 99 + 100 # al

ID: 3888097 • Letter: W

Question

Write an MIPS program to compute three sums:

1 + 2 + 3 + 4 + ... + 99 + 100 # all values 1…100

1 + 3 + 5 + 7 + ... + 97 + 99 # all odd values 1…100

2 + 4 + 6 + 8 + ... + 98 + 100 # all even values 1…100

Do this by using the blt macro (branch if less than).

Use a register (say $t0) for the sum of evens, a register (say $t1) for the sum of odds, and another (say $t2) for the sum of all numbers.

Do this with only one counting loop. The loop body will contain the logic to add the count to the proper sums.

Don't use division to check odd or even, it is not necessary. A bitwise instruction is enough.

Stop the program with a syscall with 10 in $v0.

Explanation / Answer

[1]

.text
.globl main
main:
    li      $t0, 0               # $t0 = loop counter
    li      $t1, 0               # $t1 = sum of numbers
loop:
    addi    $t0, $t0, 1          # generating numbers in register $t0
    add     $t1, $t1, $t0        # compute the sum
    bne     $t0, 100, loop       # if t0 reached 100 then go to loop.

    li      $v0, 4
    la      $a0, result
    syscall                      # print out "Sum = "

    li      $v0, 1
    move    $a0, $t1
    syscall                      # print out actual sum


exit:
    li      $v0, 10              # terminate program run and
    syscall                      # Exit


                .data
result:         .asciiz "Sum = "

-------------------------------------------------------------------

For Even :

.text
.globl main
main:
    li      $t0, 0               # $t0 = loop counter
    li      $t1, 0               # $t1 = sum of even numbers
loop:
    addi    $t0, $t0, 2          # generating numbers in register $t0
    add     $t1, $t1, $t0        # compute the even sum
    bne     $t0, 100, loop       # if t0 reached 100 then go to loop.

    li      $v0, 4
    la      $a0, result
    syscall                      # print out "Sum = "

    li      $v0, 1
    move    $a0, $t1
    syscall                      # print out actual sum


exit:
    li      $v0, 10              # terminate program run and
    syscall                      # Exit


                .data
result:         .asciiz "Sum = "

------------------------------------------------------

For ODD :

.text
.globl main
main:
    li      $t0, 0               # $t0 = loop counter
    li      $t1, 1               # $t1 = sum of numbers
loop:
    addi    $t0, $t0, 2          # generating numbers in register $t0
    add     $t1, $t1, $t0        # compute the even sum
    bne     $t0, 100, loop       # if t0 reached 100 then go to loop.

    li      $v0, 4
    la      $a0, result
    syscall                      # print out "Sum = "

    li      $v0, 1
    move    $a0, $t1
    syscall                      # print out actual sum


exit:
    li      $v0, 10              # terminate program run and
    syscall                      # Exit


                .data
result:         .asciiz "Sum = "

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote