Write a MIPS assembly language program that a) Prompt the user for an integer in
ID: 3890718 • Letter: W
Question
Write a MIPS assembly language program that
a) Prompt the user for an integer in the range of 0 to 50. If the user inputs 0 the program stops.
b) Otherwise, the program stores the numbers from 0 up to the input value into an array of words in memory, i.e. initializes the array with values from 0 up to N where N is the value that user has inputted.
c) The program then adds the value of all items of the array together (up to N) by loading them from the main memory then add them up, then prints out the sum with the message "The sum of integers from 0 to N is:". For example, if the user gave 5 as the input the program prints out "The sum of integers from 0 to 5 is 15".
Explanation / Answer
Here is the code: .file 1 "" .section .mdebug.abi32 .previous .nan legacy .module fp=32 .module nooddspreg .abicalls .rdata .align 2 $LC0: .ascii "Enter a integer between 0 and 50: " .align 2 $LC1: .ascii "%d " .align 2 $LC2: .ascii "The sum of integers from 0 to %d is:Sum = %d " .text .align 2 .globl main .set nomips16 .set nomicromips .ent main .type main, @function main: .frame $fp,88,$31 # vars= 24, regs= 10/0, args= 16, gp= 8 .mask 0xc0ff0000,-4 .fmask 0x00000000,0 .set noreorder .cpload $25 .set nomacro addiu $sp,$sp,-88 sw $31,84($sp) sw $fp,80($sp) sw $23,76($sp) sw $22,72($sp) movz $31,$31,$0 sw $21,68($sp) sw $20,64($sp) sw $19,60($sp) sw $18,56($sp) sw $17,52($sp) sw $16,48($sp) move $fp,$sp .cprestore 16 sw $0,28($fp) lw $2,%got($LC0)($28) nop addiu $4,$2,%lo($LC0) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) addiu $2,$fp,40 move $5,$2 lw $2,%got($LC1)($28) nop addiu $4,$2,%lo($LC1) lw $2,%call16(__isoc99_scanf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,__isoc99_scanf 1: jalr $25 nop lw $28,16($fp) lw $2,40($fp) nop bne $2,$0,$L2 nop move $4,$0 lw $2,%call16(exit)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,exit 1: jalr $25 nop $L2: move $2,$sp move $4,$2 lw $2,40($fp) nop addiu $3,$2,-1 sw $3,32($fp) move $3,$2 move $23,$3 move $22,$0 srl $3,$23,27 sll $18,$22,5 or $18,$3,$18 sll $19,$23,5 move $3,$2 move $21,$3 move $20,$0 srl $3,$21,27 sll $16,$20,5 or $16,$3,$16 sll $17,$21,5 sll $2,$2,2 addiu $2,$2,3 addiu $2,$2,7 srl $2,$2,3 sll $2,$2,3 subu $sp,$sp,$2 addiu $2,$sp,16 addiu $2,$2,3 srl $2,$2,2 sll $2,$2,2 sw $2,36($fp) sw $0,24($fp) b $L3 nop $L4: lw $3,36($fp) lw $2,24($fp) nop sll $2,$2,2 addu $2,$3,$2 lw $3,24($fp) nop sw $3,0($2) lw $2,24($fp) nop addiu $2,$2,1 sw $2,24($fp) $L3: lw $2,40($fp) lw $3,24($fp) nop slt $2,$3,$2 bne $2,$0,$L4 nop sw $0,24($fp) b $L5 nop $L6: lw $3,36($fp) lw $2,24($fp) nop sll $2,$2,2 addu $2,$3,$2 lw $2,0($2) lw $3,28($fp) nop addu $2,$3,$2 sw $2,28($fp) lw $2,24($fp) nop addiu $2,$2,1 sw $2,24($fp) $L5: lw $2,40($fp) lw $3,24($fp) nop slt $2,$3,$2 bne $2,$0,$L6 nop move $sp,$4 lw $2,40($fp) lw $6,28($fp) move $5,$2 lw $2,%got($LC2)($28) nop addiu $4,$2,%lo($LC2) lw $2,%call16(printf)($28) nop move $25,$2 .reloc 1f,R_MIPS_JALR,printf 1: jalr $25 nop lw $28,16($fp) move $2,$0 move $sp,$fp lw $31,84($sp) lw $fp,80($sp) lw $23,76($sp) lw $22,72($sp) lw $21,68($sp) lw $20,64($sp) lw $19,60($sp) lw $18,56($sp) lw $17,52($sp) lw $16,48($sp) addiu $sp,$sp,88 j $31 nop .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.