Write an MIPS program that computes the sum of the integers from 1 to 100 by add
ID: 3888089 • Letter: W
Question
Write an MIPS program that computes the sum of the integers from 1 to 100 by addition:
1 + 2 + 3 + 4 + 5 + ... + 98 + 99 + 100
Do this by using the blt macro (branch if less than). Before the loop, initialize a register to zero to contain the sum, initialize another register to one (to be the counter), and another register to 101 to mark the end of the sequence. Inside the loop, add the counter to the sum, increment the counter and blt back to the top of the loop if the counter is less than 101.
Single-step the program until you get a good idea of how it is working. Then let the program run. To stop the program, use syscall with the code 10 in $v0.
Explanation / Answer
.file 1 "" .section .mdebug.abi32 .previous .nan legacy .module fp=32 .module nooddspreg .abicalls .section .rodata.str1.4,"aMS",@progbits,1 .align 2 $LC0: .ascii "Sum is %d " .section .text.startup,"ax",@progbits .align 2 .globl main .set nomips16 .set nomicromips .ent main .type main, @function main: .frame $sp,32,$31 # vars= 0, regs= 1/0, args= 16, gp= 8 .mask 0x80000000,-4 .fmask 0x00000000,0 .set noreorder .cpload $25 .set nomacro lw $5,%got($LC0)($28) addiu $sp,$sp,-32 lw $25,%call16(__printf_chk)($28) li $6,5050 # 0x13ba sw $31,28($sp) movz $31,$31,$0 .cprestore 16 addiu $5,$5,%lo($LC0) .reloc 1f,R_MIPS_JALR,__printf_chk 1: jalr $25 li $4,1 # 0x1 lw $31,28($sp) move $2,$0 j $31 addiu $sp,$sp,32 .set macro .set reorder .end main .size main, .-main .ident "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.1) 5.4.0 20160609"
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.