Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Program: Document every line: There are two seperate codes, in problem number

ID: 3600568 • Letter: C

Question

C Program: Document every line: There are two seperate codes, in problem number 2 explain what is going on with C code

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 re

Explanation / Answer

C Code to accomplish the Greatest of 3 numbers:

#include <stdio.h>

int greatest3Numbers(int *, int *, int *);

int main()
{
int n1,n2,n3;//3 variables to hold 3 numbers.
int greatest; // to store the greatest of 3 numbers.

printf(" Enter Number 1: ");
scanf(" %d", &n1);

printf(" Enter Number 2: ");
scanf(" %d", &n2);

printf(" Enter Number 3: ");
scanf(" %d", &n3);

//Above code - logic to read each number

greatest = greatest3Numbers(&n1, &n2, &n3); //pass the address of each number

if(n1 == n2 && n1 == n3)
printf(" All the numbers are Equal"); // check if all numbers are equal

else
printf(" Greatest(%d, %d, %d) = %d", n1, n2, n3, greatest); // print the greatest.


printf(" ");
return 0;
}

int greatest3Numbers(int *a, int *b, int *c) {

int result;

if((*a >= *b) && (*a >= *c))
result = *a;

else if (*b >= *c)
result = *b;

else
result = *c;

return result;
}

Assembly Code:

.LC0:

.string " Enter Number 1: "

.LC1:

.string " %d"

.LC2:

.string " Enter Number 2: "

.LC3:

.string " Enter Number 3: "

.LC4:

.string " All the numbers are Equal"

.LC5:

.string " Greatest(%d, %d, %d) = %d"

.LC6:

.string " "

main:

push rbp

mov rbp, rsp

sub rsp, 16

mov edi, OFFSET FLAT:.LC0

mov eax, 0

call printf

lea rax, [rbp-8]

mov rsi, rax

mov edi, OFFSET FLAT:.LC1

mov eax, 0

call scanf

mov edi, OFFSET FLAT:.LC2

mov eax, 0

call printf

lea rax, [rbp-12]

mov rsi, rax

mov edi, OFFSET FLAT:.LC1

mov eax, 0

call scanf

mov edi, OFFSET FLAT:.LC3

mov eax, 0

call printf

lea rax, [rbp-16]

mov rsi, rax

mov edi, OFFSET FLAT:.LC1

mov eax, 0

call scanf

lea rdx, [rbp-16]

lea rcx, [rbp-12]

lea rax, [rbp-8]

mov rsi, rcx

mov rdi, rax

call greatest3Numbers(int*, int*, int*)

mov DWORD PTR [rbp-4], eax

mov edx, DWORD PTR [rbp-8]

mov eax, DWORD PTR [rbp-12]

cmp edx, eax

jne .L2

mov edx, DWORD PTR [rbp-8]

mov eax, DWORD PTR [rbp-16]

cmp edx, eax

jne .L2

mov edi, OFFSET FLAT:.LC4

mov eax, 0

call printf

jmp .L3

.L2:

mov ecx, DWORD PTR [rbp-16]

mov edx, DWORD PTR [rbp-12]

mov eax, DWORD PTR [rbp-8]

mov esi, DWORD PTR [rbp-4]

mov r8d, esi

mov esi, eax

mov edi, OFFSET FLAT:.LC5

mov eax, 0

call printf

.L3:

mov edi, OFFSET FLAT:.LC6

call puts

mov eax, 0

leave

ret

greatest3Numbers(int*, int*, int*):

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

jl .L6

mov rax, QWORD PTR [rbp-24]

mov edx, DWORD PTR [rax]

mov rax, QWORD PTR [rbp-40]

mov eax, DWORD PTR [rax]

cmp edx, eax

jl .L6

mov rax, QWORD PTR [rbp-24]

mov eax, DWORD PTR [rax]

mov DWORD PTR [rbp-4], eax

jmp .L7

.L6:

mov rax, QWORD PTR [rbp-32]

mov edx, DWORD PTR [rax]

mov rax, QWORD PTR [rbp-40]

mov eax, DWORD PTR [rax]

cmp edx, eax

jl .L8

mov rax, QWORD PTR [rbp-32]

mov eax, DWORD PTR [rax]

mov DWORD PTR [rbp-4], eax

jmp .L7

.L8:

mov rax, QWORD PTR [rbp-40]

mov eax, DWORD PTR [rax]

mov DWORD PTR [rbp-4], eax

.L7:

mov eax, DWORD PTR [rbp-4]

pop rbp

ret