Design a subroutine called \"change_array\", which has the following function. T
ID: 3726809 • Letter: D
Question
Design a subroutine called "change_array", which has the following function. The subroutine will process an array of longword-sized values stored in memory, as follows. For each value in the array, if it is more than 100, then change it to 100; if it is less than 0, then change it to a positive value with the same magnitude if it is not more than 100 and not less than 0, then change it to 0. The input parameters of the subroutine are: starting address and length (i.e., the number of longword-sized values) of the array to be processed. The two input parameters are passed by using address register A4 and data register D4, respectively. In other words, if your main program will call the subroutine to process a specific array, the main program should first store to A4 the starting address of the specific array, and store to D4 the length of the specific array, and then call the subroutine. In the subroutine, by using A4 and D4, it can locate the array to be processed. All registers used by the subroutine must be preserved. Also design one segment of your main program to call the subroutine to process a specific array which has 50 longword-sized values stored at memory starting from address 0x00002000.Explanation / Answer
change_array(int*, int):
pushq %rbp
movq %rsp, %rbp
movq %rdi, -24(%rbp)
movl %esi, -28(%rbp)
movl $0, -4(%rbp)
movl $0, -4(%rbp)
.L6:
movl -4(%rbp), %eax
cmpl -28(%rbp), %eax
jge .L2
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
cmpl $100, %eax
jle .L3
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl $100, (%rax)
jmp .L4
.L3:
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
testl %eax, %eax
jns .L5
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl (%rax), %eax
movl -4(%rbp), %edx
movslq %edx, %rdx
leaq 0(,%rdx,4), %rcx
movq -24(%rbp), %rdx
addq %rdx, %rcx
cltd
xorl %edx, %eax
subl %edx, %eax
movl %eax, (%rcx)
jmp .L4
.L5:
movl -4(%rbp), %eax
cltq
leaq 0(,%rax,4), %rdx
movq -24(%rbp), %rax
addq %rdx, %rax
movl $0, (%rax)
.L4:
addl $1, -4(%rbp)
jmp .L6
.L2:
nop
popq %rbp
ret
main:
pushq %rbp
movq %rsp, %rbp
subq $48, %rsp
movl $1, -48(%rbp)
movl $-10, -44(%rbp)
movl $0, -40(%rbp)
movl $99, -36(%rbp)
movl $100, -32(%rbp)
movl $-55, -28(%rbp)
movl $105, -24(%rbp)
movl $109, -20(%rbp)
movl $49, -16(%rbp)
movl $150, -12(%rbp)
leaq -48(%rbp), %rax
movl $10, %esi
movq %rax, %rdi
call change_array(int*, int)
movl $0, %eax
leave
ret
# 1, -10, 0 , 99, 100, -55, 105, 109, 49, 150 array values and n = 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.