Implement a MIPS assembly language program that defines main, readArray and sear
ID: 3745919 • Letter: I
Question
Implement a MIPS assembly language program that defines main, readArray and searchElement 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 searchElement procedure takes parameters of an array of integers, and its length, and asks a user how many integers to read in and calls the readArray procedure. It also asks a user to enter an integer to search. Then it should go through the array to see if each number is same as the integer to search. If an element in the array is the same as the integer to search, double its content, by multiplying it by 2, and set the variable found to 1 so that it can print out a message to say whether it was found or not. Then it prints out the updated content of the array.
The main procedure asks a user how many times to call the searchElement procedure, and it repeats it based on the entered number.
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.
The following shows how it looks like in a C program:
The following is a sample output (user input is in bold):
Enter an integer to specify how many times to repeat:
2
Specify how many numbers should be stored in the array (at most 9):
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
Enter an integer to search:
1
Result Array Content:
2
-12
53
-4
5
32
2
7
The entered element was found.
Explanation / Answer
.section .rodata
.LC0:
.prompt "Enter an integer: "
.LC1:
.prompt "%d"
.LC2:
.prompt "The array content:"
.LC3:
.prompt "%d "
.text
.globl readArray
.type readArray, @function
readArray:
.LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $40, %esp
movl 8(%ebp), %eax
movl %eax, -28(%ebp)
movl %gs:20, %eax
movl %eax, -12(%ebp)
xorl %eax, %eax
movl $0, -16(%ebp)
jmp .L2
.L4:
subl $12, %esp
pushl $.LC0
call puts
addl $16, %esp
subl $8, %esp
leal -20(%ebp), %eax
pushl %eax
pushl $.LC1
call __isoc99_scanf
addl $16, %esp
movl -16(%ebp), %eax
leal 0(,%eax,4), %edx
movl -28(%ebp), %eax
addl %eax, %edx
movl -20(%ebp), %eax
movl %eax, (%edx)
addl $1, -16(%ebp)
.L2:
movl -16(%ebp), %eax
cmpl 16(%ebp), %eax
jge .L3
movl -16(%ebp), %eax
cmpl 12(%ebp), %eax
jl .L4
.L3:
subl $12, %esp
pushl $.LC2
call puts
addl $16, %esp
movl $0, -16(%ebp)
jmp .L5
.L7:
movl -16(%ebp), %eax
leal 0(,%eax,4), %edx
movl -28(%ebp), %eax
addl %edx, %eax
movl (%eax), %eax
subl $8, %esp
pushl %eax
pushl $.LC3
call printf
addl $16, %esp
addl $1, -16(%ebp)
.L5:
movl -16(%ebp), %eax
cmpl 16(%ebp), %eax
jge .L10
movl -16(%ebp), %eax
cmpl 12(%ebp), %eax
jl .L7
nop
.L10:
nop
movl -12(%ebp), %eax
xorl %gs:20, %eax
je .L9
call __stack_chk_fail
.L9:
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.size readArray, .-readArray
.section .rodata
.align 4
.LC4:
.prompt "Specify how many numbers should be stored in the array (at most 9):"
.LC5:
.prompt "Enter an integer to search:"
.LC6:
.prompt "Result Array Content:"
.align 4
.LC7:
.prompt "The entered element was not found"
.LC8:
.prompt "The entered element was found"
.text
.globl searchElement
.type searchElement, @function
searchElement:
.LFB1:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
subl $56, %esp
movl 8(%ebp), %eax
movl %eax, -44(%ebp)
movl %gs:20, %eax
movl %eax, -12(%ebp)
xorl %eax, %eax
movl $0, -20(%ebp)
subl $12, %esp
pushl $.LC4
call puts
addl $16, %esp
subl $8, %esp
leal -28(%ebp), %eax
pushl %eax
pushl $.LC1
call __isoc99_scanf
addl $16, %esp
movl -28(%ebp), %eax
subl $4, %esp
pushl %eax
pushl 12(%ebp)
pushl -44(%ebp)
call readArray
addl $16, %esp
subl $12, %esp
pushl $.LC5
call puts
addl $16, %esp
subl $8, %esp
leal -24(%ebp), %eax
pushl %eax
pushl $.LC1
call __isoc99_scanf
addl $16, %esp
movl $0, -16(%ebp)
jmp .L12
.L15:
movl -20(%ebp), %eax
leal 0(,%eax,4), %edx
movl -44(%ebp), %eax
addl %edx, %eax
movl (%eax), %edx
movl -24(%ebp), %eax
cmpl %eax, %edx
jne .L13
movl $1, -16(%ebp)
movl -20(%ebp), %eax
leal 0(,%eax,4), %edx
movl -44(%ebp), %eax
addl %edx, %eax
movl -20(%ebp), %edx
leal 0(,%edx,4), %ecx
movl -44(%ebp), %edx
addl %ecx, %edx
movl (%edx), %edx
addl %edx, %edx
movl %edx, (%eax)
.L13:
addl $1, -20(%ebp)
.L12:
movl -28(%ebp), %eax
cmpl %eax, -20(%ebp)
jge .L14
movl -20(%ebp), %eax
cmpl 12(%ebp), %eax
jl .L15
.L14:
subl $12, %esp
pushl $.LC6
call puts
addl $16, %esp
movl $0, -20(%ebp)
jmp .L16
.L18:
movl -20(%ebp), %eax
leal 0(,%eax,4), %edx
movl -44(%ebp), %eax
addl %edx, %eax
movl (%eax), %eax
subl $8, %esp
pushl %eax
pushl $.LC3
call printf
addl $16, %esp
addl $1, -20(%ebp)
.L16:
movl -28(%ebp), %eax
cmpl %eax, -20(%ebp)
jge .L17
movl -20(%ebp), %eax
cmpl 12(%ebp), %eax
jl .L18
.L17:
cmpl $0, -16(%ebp)
jne .L19
subl $12, %esp
pushl $.LC7
call puts
addl $16, %esp
jmp .L22
.L19:
subl $12, %esp
pushl $.LC8
call puts
addl $16, %esp
.L22:
nop
movl -12(%ebp), %eax
xorl %gs:20, %eax
je .L21
call __stack_chk_fail
.L21:
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE1:
.size searchElement, .-searchElement
.section .rodata
.align 4
.LC9:
.prompt "Enter an integer to specify how many times to repeat:"
.text
.globl main
.type main, @function
main:
.LFB2:
.cfi_startproc
leal 4(%esp), %ecx
.cfi_def_cfa 1, 0
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
.cfi_escape 0x10,0x5,0x2,0x75,0
movl %esp, %ebp
pushl %ebx
pushl %ecx
.cfi_escape 0xf,0x3,0x75,0x78,0x6
.cfi_escape 0x10,0x3,0x2,0x75,0x7c
subl $32, %esp
movl %gs:20, %eax
movl %eax, -12(%ebp)
xorl %eax, %eax
movl %esp, %eax
movl %eax, %ebx
movl $9, -24(%ebp)
movl -24(%ebp), %eax
leal -1(%eax), %edx
movl %edx, -20(%ebp)
sall $2, %eax
leal 3(%eax), %edx
movl $16, %eax
subl $1, %eax
addl %edx, %eax
movl $16, %ecx
movl $0, %edx
divl %ecx
imull $16, %eax, %eax
subl %eax, %esp
movl %esp, %eax
addl $3, %eax
shrl $2, %eax
sall $2, %eax
movl %eax, -16(%ebp)
subl $12, %esp
pushl $.LC9
call puts
addl $16, %esp
subl $8, %esp
leal -32(%ebp), %eax
pushl %eax
pushl $.LC1
call __isoc99_scanf
addl $16, %esp
movl $0, -28(%ebp)
jmp .L24
.L25:
movl -16(%ebp), %eax
subl $8, %esp
pushl -24(%ebp)
pushl %eax
call searchElement
addl $16, %esp
addl $1, -28(%ebp)
.L24:
movl -32(%ebp), %eax
cmpl %eax, -28(%ebp)
jl .L25
nop
movl %ebx, %esp
movl -12(%ebp), %eax
xorl %gs:20, %eax
je .L27
call __stack_chk_fail
.L27:
leal -8(%ebp), %esp
popl %ecx
.cfi_restore 1
.cfi_def_cfa 1, 0
popl %ebx
.cfi_restore 3
popl %ebp
.cfi_restore 5
leal -4(%ecx), %esp
.cfi_def_cfa 4, 4
ret
.cfi_endproc
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.