Implement a MIPS assembly language program that defines main, readArray and remo
ID: 3876438 • Letter: I
Question
Implement a MIPS assembly language program that defines main, readArray and removeEvenOdd procedures.
The readArray takes an array of integers as its parameter, reads in integers from a user to fill the array and also print each value as long as it is with the number of elements specified by the parameter "howMany" and "length".
The removeEvenOdd procedure takes parameters of an array of integers, its length, and even value, an asks a user how many integers to read in and calls the readyArray procedure. Then it should go through the array to see if each number is even (divisible by 2) if the parameter even is 0. In that case, such even integer in the array is changed to 0. If the parameter even is 1, then it changes every odd integer in the array to 0. Then it prints out the updated content of the array.
The main procedure asks a user to specify whether to remove even or odd numbers by asking them to enter an integer. Then it calls the removeEvenOdd procedure. It repeats these two steps twice.
Please see the following C program to understand how it should work.
If your program causes an infinite loop, press Control and 'C' keys at the same time to stop it.
Name your source code file assignment6.s.
The following shows how it looks like in a C program:
The following is a sample output (user input is in bold):
Enter zero to remove even numbers or non-zero to remove odd numbers
0
Specify how many numbers should be stored in the array (at most 8):
8
Enter an integer:
1
Enter an integer:
-12
Enter an integer:
53
Enter an integer:
-4
Enter an integer:
5
Enter an integer:
32
Enter an integer:
1
Enter an integer:
7
The array content:
1
-12
53
-4
5
32
1
7
After removing even integers
1
1
53
1
5
1
1
7
Enter zero to remove even numbers or non-zero to remove odd numbers
1
Specify how many numbers should be stored in the array (at most 8):
5
Enter an integer:
1
Enter an integer:
-5
Enter an integer:
-2
Enter an integer:
3
Enter an integer:
2
The array content:
1
-5
-2
3
2
After removing odd integers
0
0
-2
0
2
--------------------------------------------------
What to turn in:
programs should be well commented.
Each procedure needs to have a header using the following format:
############################################################################
# Procedure readArray
# Description: -----
# parameters: $a0 = address of array
# return value: $v0 = length
# registers to be used: $s3 and $s4 will be used.
############################################################################
Explanation / Answer
.file 1 ""
.section .mdebug.abi32
.previous
.align 2
$LC0:
.ascii "The array content:"
.align 2
$LC1:
.ascii "Enter an integer: "
.align 2
$LC2:
.ascii "%d"
.align 2
$LC3:
.ascii "%d "
.text
.align 2
.globl readArray
.set nomips16
.set nomicromips
.ent readArray
.type readArray, @function
############################################################################
readArray:
.frame $sp,72,$31 # vars= 8, regs= 9/0, args= 16, gp= 8
.mask 0x80ff0000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-72
sw $17,40($sp)
move $17,$4
lw $4,%got($LC0)($28)
lw $25,%call16(puts)($28)
movz $31,$31,$0
.cprestore 16
sw $22,60($sp)
sw $21,56($sp)
sw $31,68($sp)
sw $23,64($sp)
sw $20,52($sp)
sw $19,48($sp)
sw $18,44($sp)
sw $16,36($sp)
move $21,$6
addiu $4,$4,%lo($LC0)
.reloc 1f,R_MIPS_JALR,puts
1: jalr $25
move $22,$5
lw $28,16($sp)
blez $21,$L1
nop
blez $22,$L1
move $16,$0
lw $20,%got($LC1)($28)
lw $19,%got($LC2)($28)
lw $18,%got($LC3)($28)
addiu $23,$sp,24
addiu $20,$20,%lo($LC1)
addiu $19,$19,%lo($LC2)
b $L4
addiu $18,$18,%lo($LC3)
$L8:
beq $22,$16,$L1
addiu $17,$17,4
$L4:
lw $25,%call16(puts)($28)
nop
.reloc 1f,R_MIPS_JALR,puts
1: jalr $25
move $4,$20
lw $28,16($sp)
move $5,$23
lw $25,%call16(scanf)($28)
nop
.reloc 1f,R_MIPS_JALR,scanf
1: jalr $25
move $4,$19
lw $28,16($sp)
lw $5,24($sp)
lw $25,%call16(printf)($28)
sw $5,0($17)
move $4,$18
.reloc 1f,R_MIPS_JALR,printf
1: jalr $25
addiu $16,$16,1
lw $28,16($sp)
bne $21,$16,$L8
nop
$L1:
lw $31,68($sp)
lw $23,64($sp)
lw $22,60($sp)
lw $21,56($sp)
lw $20,52($sp)
lw $19,48($sp)
lw $18,44($sp)
lw $17,40($sp)
lw $16,36($sp)
j $31
addiu $sp,$sp,72
.set macro
.set reorder
.end readArray
.size readArray, .-readArray
.section .rodata.str1.4
.align 2
$LC4:
.ascii "Specify how many numbers should be stored in the array ("
.ascii "at most 8):"
.align 2
$LC5:
.ascii "After removing even integers"
.align 2
$LC6:
.ascii "After removing odd integers"
.text
.align 2
.globl removeEvenOdd
.set nomips16
.set nomicromips
.ent removeEvenOdd
.type removeEvenOdd, @function
removeEvenOdd:
.frame $sp,72,$31 # vars= 8, regs= 10/0, args= 16, gp= 8
.mask 0xc0ff0000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-72
sw $17,36($sp)
move $17,$4
lw $4,%got($LC4)($28)
lw $25,%call16(puts)($28)
movz $31,$31,$0
sw $31,68($sp)
.cprestore 16
sw $6,80($sp)
sw $23,60($sp)
sw $21,52($sp)
sw $18,40($sp)
addiu $4,$4,%lo($LC4)
sw $fp,64($sp)
sw $22,56($sp)
sw $20,48($sp)
sw $19,44($sp)
sw $16,32($sp)
sw $0,28($sp)
lw $21,%got($LC2)($28)
.reloc 1f,R_MIPS_JALR,puts
1: jalr $25
move $18,$5
lw $28,16($sp)
addiu $5,$sp,28
lw $25,%call16(scanf)($28)
nop
.reloc 1f,R_MIPS_JALR,scanf
1: jalr $25
addiu $4,$21,%lo($LC2)
lw $28,16($sp)
lw $23,28($sp)
lw $4,%got($LC0)($28)
lw $25,%call16(puts)($28)
nop
.reloc 1f,R_MIPS_JALR,puts
1: jalr $25
addiu $4,$4,%lo($LC0)
lw $28,16($sp)
blez $23,$L10
nop
blez $18,$L15
move $19,$17
lw $20,%got($LC3)($28)
lw $22,%got($LC1)($28)
move $16,$0
addiu $fp,$sp,24
addiu $22,$22,%lo($LC1)
addiu $21,$21,%lo($LC2)
b $L13
addiu $20,$20,%lo($LC3)
$L37:
beq $18,$16,$L12
addiu $19,$19,4
$L13:
lw $25,%call16(puts)($28)
nop
.reloc 1f,R_MIPS_JALR,puts
1: jalr $25
move $4,$22
lw $28,16($sp)
move $5,$fp
lw $25,%call16(scanf)($28)
nop
.reloc 1f,R_MIPS_JALR,scanf
1: jalr $25
move $4,$21
lw $28,16($sp)
lw $5,24($sp)
lw $25,%call16(printf)($28)
sw $5,0($19)
move $4,$20
.reloc 1f,R_MIPS_JALR,printf
1: jalr $25
addiu $16,$16,1
lw $28,16($sp)
bne $23,$16,$L37
nop
$L12:
lw $5,28($sp)
nop
blez $5,$L15
nop
$L28:
lw $2,80($sp)
nop
beq $2,$0,$L17
move $4,$17
move $2,$0
$L20:
lw $3,0($4)
addiu $2,$2,1
andi $3,$3,0x1
slt $6,$2,$5
beq $3,$0,$L18
slt $7,$2,$18
sw $0,0($4)
$L18:
beq $6,$0,$L19
nop
bne $7,$0,$L20
addiu $4,$4,4
$L19:
lw $4,%got($LC6)($28)
lw $25,%call16(puts)($28)
nop
.reloc 1f,R_MIPS_JALR,puts
1: jalr $25
addiu $4,$4,%lo($LC6)
lw $28,16($sp)
$L24:
lw $2,28($sp)
nop
blez $2,$L9
nop
blez $18,$L9
move $16,$0
lw $20,%got($LC3)($28)
b $L27
addiu $20,$20,%lo($LC3)
$L38:
beq $18,$16,$L9
addiu $17,$17,4
$L27:
lw $25,%call16(printf)($28)
lw $5,0($17)
.reloc 1f,R_MIPS_JALR,printf
1: jalr $25
move $4,$20
lw $2,28($sp)
addiu $16,$16,1
slt $2,$16,$2
lw $28,16($sp)
bne $2,$0,$L38
nop
$L9:
lw $31,68($sp)
lw $fp,64($sp)
lw $23,60($sp)
lw $22,56($sp)
lw $21,52($sp)
lw $20,48($sp)
lw $19,44($sp)
lw $18,40($sp)
lw $17,36($sp)
lw $16,32($sp)
j $31
addiu $sp,$sp,72
$L17:
move $2,$0
li $8,1 # 0x1
$L23:
lw $3,0($4)
addiu $2,$2,1
andi $3,$3,0x1
slt $6,$2,$5
bne $3,$0,$L21
slt $7,$2,$18
sw $8,0($4)
$L21:
beq $6,$0,$L22
nop
bne $7,$0,$L23
addiu $4,$4,4
$L22:
lw $4,%got($LC5)($28)
lw $25,%call16(puts)($28)
nop
.reloc 1f,R_MIPS_JALR,puts
1: jalr $25
addiu $4,$4,%lo($LC5)
lw $28,16($sp)
b $L24
nop
$L10:
lw $5,28($sp)
nop
blez $5,$L15
nop
bgtz $18,$L28
nop
$L15:
lw $2,80($sp)
nop
beq $2,$0,$L22
nop
b $L19
nop
.set macro
.set reorder
.end removeEvenOdd
.size removeEvenOdd, .-removeEvenOdd
.section .rodata.str1.4
.align 2
$LC7:
.ascii "Enter zero to remove even numbers or non-zero to remove "
.ascii "odd numbers"
.section .text.startup,"ax",@progbits
.align 2
.globl main
.set nomips16
.set nomicromips
.ent main
.type main, @function
main:
.frame $sp,88,$31 # vars= 40, regs= 6/0, args= 16, gp= 8
.mask 0x801f0000,-4
.fmask 0x00000000,0
.set noreorder
.cpload $25
.set nomacro
addiu $sp,$sp,-88
sw $20,80($sp)
lw $20,%got($LC7)($28)
lw $25,%call16(puts)($28)
sw $31,84($sp)
movz $31,$31,$0
sw $19,76($sp)
sw $18,72($sp)
sw $17,68($sp)
sw $16,64($sp)
.cprestore 16
lw $19,%got($LC2)($28)
.reloc 1f,R_MIPS_JALR,puts
1: jalr $25
addiu $4,$20,%lo($LC7)
lw $28,16($sp)
addiu $17,$sp,56
lw $25,%call16(scanf)($28)
move $5,$17
.reloc 1f,R_MIPS_JALR,scanf
1: jalr $25
addiu $4,$19,%lo($LC2)
lw $28,16($sp)
lw $6,56($sp)
lw $18,%got(removeEvenOdd)($28)
addiu $16,$sp,24
move $4,$16
move $25,$18
.reloc 1f,R_MIPS_JALR,removeEvenOdd
1: jalr $25
li $5,8 # 0x8
lw $28,16($sp)
nop
lw $25,%call16(puts)($28)
nop
.reloc 1f,R_MIPS_JALR,puts
1: jalr $25
addiu $4,$20,%lo($LC7)
lw $28,16($sp)
move $5,$17
lw $25,%call16(scanf)($28)
nop
.reloc 1f,R_MIPS_JALR,scanf
1: jalr $25
addiu $4,$19,%lo($LC2)
lw $6,56($sp)
move $4,$16
move $25,$18
.reloc 1f,R_MIPS_JALR,removeEvenOdd
1: jalr $25
li $5,8 # 0x8
lw $31,84($sp)
lw $20,80($sp)
lw $19,76($sp)
lw $18,72($sp)
lw $17,68($sp)
lw $16,64($sp)
j $31
addiu $sp,$sp,88
.set macro
.set reorder
.end main
.size main, .-main
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.