C Program: Document every line 1. Think of a c function int ReturnGreatest int *
ID: 3598863 • Letter: C
Question
C Program: Document every line
1. Think of a c function int ReturnGreatest int *n1, int *n2, int *n3) which takes 3 address pointer and return the greatest number of all the three. Write a C code and then use the compiler to generate its corresponding assembly code for this function (assuming for a 32-bit machine). (15) Make sure your code has appropriate comments, so that it would be understandable 2. Have a look at the assembly code and explain what is being done here. Instead of explaining verbosely, you can write a c code to explain that (hints: this function is returning some value). Assignment method. pushl %ebp movl %esp,%ebp pushl %ebx movl 12(%ebp),%ecx movl 8(%ebp), %edx movl 4(%ebp),%esi movl (%ecx),%eax xorl %eax, %eax and/ %esi, %eax movl-41%ebp),%ebx movl %ebp,%esp popl %ebp reExplanation / Answer
1)
C Code:-
int ReturnGreatest(int *n1,int *n2,int *n3){
int great;
if(*(n1) > *(n2)){
great=*(n1);
}
else{
great=*(n2);
}
if(great < *(n3)){
great=*(n3);
}
return great;
}
Assembly COde:-
_ReturnGreatest:
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-24], rdi
mov QWORD PTR [rbp-32], rsi
mov QWORD PTR [rbp-40], rdx
mov rax, QWORD PTR [rbp-24]
mov edx, DWORD PTR [rax]
mov rax, QWORD PTR [rbp-32]
mov eax, DWORD PTR [rax]
cmp edx, eax
jle .L2
mov rax, QWORD PTR [rbp-24]
mov eax, DWORD PTR [rax]
mov DWORD PTR [rbp-4], eax
jmp .L3
.L2:
mov rax, QWORD PTR [rbp-32]
mov eax, DWORD PTR [rax]
mov DWORD PTR [rbp-4], eax
.L3:
mov rax, QWORD PTR [rbp-40]
mov eax, DWORD PTR [rax]
cmp DWORD PTR [rbp-4], eax
jge .L4
mov rax, QWORD PTR [rbp-40]
mov eax, DWORD PTR [rax]
mov DWORD PTR [rbp-4], eax
.L4:
mov eax, DWORD PTR [rbp-4]
pop rbp
ret
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.