Using SPIM, write a program to count the number of odd numbers of adata array. A
ID: 3613386 • Letter: U
Question
Using SPIM, write a program to count the number of odd numbers of adata array. Asshown in the assembly code below, a 20-word data array is allocatedin the data segment
in memory; it contains 20 integers.
The inputs to the program would be 2 numbers which indicate thestarting index and the
ending index of the part of the array to be parsed. Please notethat the starting_index
should be less than ending_index. Eg. If the array has 23, 34, 37,45, 56, 67 and the
inputs are 2 and 5, the considered numbers for parsing would be 34,37, 45, 56. The
output needs to be 2 as there are 2 odd numbers in the selectednumbers.
The outline of the code is provided that will help you take in theinput and print the
output. The choices entered as input are available in $t0 (startingindex) and $t1
(ending_index).
Add your code after begin: and before done:
The result should be stored in register $t2 and then printed out bythe already available
code.
(Hint: you can use instruction la $s0, item to load the address ofthe first word of
the array into register $s0.)
Complete the assembly program shown on the next page and submit,via ANGEL, the
completed and tested program. You may assume that only legal inputswill be provided.
Both you and your programming partner should submit the programmaking sure to
include in your comments the name of the person whom you workedwith. One point of
extra credit will be given for the programs with the fewest numberof source (i.e.,
assembler) instructions (not counting the input and output codeprovided). Please report
in your comments the number of source instructions and the numberof instructions
executed.
Note: Remember that it is a good idea to have a working version ofthe basic solution
before attempting the extra credit versions.
.data
.align 2
item: .word 5
.word 9
.word 23
.word 29
.word 38
.word 45
.word 46
.word 48
.word 56
.word 59
.word 60
.word 68
.word 75
.word 79
.word 81
.word 83
.word 88
.word 90
.word 91
last: .word 98
.align 0
str: .asciiz "The answer is "
str2: .asciiz "Enter the your choice: "
str3: .asciiz "Starting index: "
str4: .asciiz "End index: "
cr: .asciiz " "
.text
.align 2
.globl main
main: li $v0, 4
la $a0, str2
syscall
li $v0, 4
la $a0, cr
syscall
li $v0, 4
la $a0, str3
syscall
li $v0, 5
syscall
add $t0, $v0, $zero
li $v0, 4
la $a0, str4
syscall
li $v0, 5
syscall
add $t1, $v0, $zero
begin: ##### THIS SECTION OF THE CODE IS TO BE COMPLETED
##### BY YOU AND YOUR PROGRAMMING PARTNER
##### INCLUDE A COMMENT THAT TELLS THE NUMBER OF SOURCE
##### INSTRUCTIONS YOU INSERTED HERE
##### INCLUDE A COMMENT THAT TELLS THE NUMBER OF
##### INSTRUCTIONS EXECUTED BETWEEN begin AND done
done: li $v0, 4
la $a0, str
syscall
li $v0,1
add $a0, $t2, $0
syscall
li $v0, 4
la $a0, cr
syscall
li $v0, 10
syscall
Explanation / Answer
please rate - thanks .data .align 2 item: .word 5 .word 9 .word 23 .word 29 .word 38 .word 45 .word 46 .word 48 .word 56 .word 59 .word 60 .word 68 .word 75 .word 79 .word 81 .word 83 .word 88 .word 90 .word 91 last: .word 98 .align 0 str: .asciiz "The answer is " str2: .asciiz "Enter the your choice: " str3: .asciiz "Starting index: " str4: .asciiz "End index: " cr: .asciiz " " .text .align 2 .globl main main: li $v0, 4 la $a0, str2 syscall li $v0, 4 la $a0, cr syscall li $v0, 4 la $a0, str3 syscall li $v0, 5 syscall add $t0, $v0, $zero li $v0, 4 la $a0, str4 syscall li $v0, 5 syscall add $t1, $v0, $zero begin: ##### THIS SECTION OF THE CODE IS TO BE COMPLETED ##### BY YOU AND YOUR PROGRAMMING PARTNER ##### INCLUDE A COMMENT THAT TELLS THE NUMBER OF SOURCE ##### INSTRUCTIONS YOU INSERTED HERE ##### INCLUDE A COMMENT THAT TELLS THE NUMBER OF ##### INSTRUCTIONS EXECUTED BETWEEN begin AND done #####16 instructions added li $t2,0 addi $t0,-1 addi $t1,-1 la $t4,item sll $t0,$t0,2 sll $t1,$t1,2 add $t3,$t4,$t0 add $t1,$t4,$t1 loop: lw $t4,0($t3) andi $t4,$t4,1 beqz $t4,even addi $t2,1 even: addi $t3,4 ble $t3,$t1, loop done: li $v0, 4 la $a0, str syscall li $v0,1 add $a0, $t2, $0 syscall li $v0, 4 la $a0, cr syscall li $v0, 10 syscall
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.