Write an assembly program to find the largest element by searching an array. Use
ID: 3811606 • Letter: W
Question
Write an assembly program to find the largest element by searching an array. Use cmp instruction and the appropriate jump instruction (signed or unsigned) to translate the if and while statements. Use $ operator to calculate the size of the array, and inc instruction to incremenet the index of the array.
int ary[] = {1,5,-3,-4,0,6,11,-9,18}
int index = 0;
int max = ary[0];
int arraySize = sizeof array / sizeof max
while (index < arraySize)
{
if (ary[index < arraySize)
max = ary[index];
}
Explanation / Answer
Assembly language proram'
main:
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-48], 1
mov DWORD PTR [rbp-44], 5
mov DWORD PTR [rbp-40], -3
mov DWORD PTR [rbp-36], -4
mov DWORD PTR [rbp-32], 0
mov DWORD PTR [rbp-28], 6
mov DWORD PTR [rbp-24], 11
mov DWORD PTR [rbp-20], -9
mov DWORD PTR [rbp-16], 18
mov DWORD PTR [rbp-4], 0
mov eax, DWORD PTR [rbp-48]
mov DWORD PTR [rbp-8], eax
mov DWORD PTR [rbp-12], 9
.L4:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-12]
jge .L2
mov eax, DWORD PTR [rbp-4]
cdqe
mov eax, DWORD PTR [rbp-48+rax*4]
cmp eax, DWORD PTR [rbp-12]
jge .L3
mov eax, DWORD PTR [rbp-4]
cdqe
mov eax, DWORD PTR [rbp-48+rax*4]
mov DWORD PTR [rbp-8], eax
.L3:
add DWORD PTR [rbp-4], 1
jmp .L4
.L2:
mov eax, 0
pop rbp
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.