What is the assembly code for this case We are given three 3
What is the assembly code for this case:
We are given three 32-bit signed integers. We need to add the smallest number to the largest number and multiply that sum by 2017. We cannot use mul/imul instructions.
Solution
In the following, I’m attaching a C function which is further converted to assembly language code [based on x86-64 gcc 6.3]. By default C takes int as 32 bit signed integer thus it meets the requirement as well. It gets the result by repetitive addition rather using mul/imul.
Coding: C
int fnc(int no1, int no2, int no3)
{
int h, l, i;
if (no1>no2 && no1>no3) h = no1;
if (no2>no1 && no2>no3) h = no2;
if (no3>no1 && no3>no2) h = no3;
if (no1<=no2 && no1<=no3) l = no1;
if (no2<=no1 && no2<=no3) l = no2;
if (no3<=no1 && no3<=no2) l = no3;
h += l;
l = h;
for (i=0; i<2017; i++) h += l;
return h;
}
Coding: Assembly
fnc(int, int, int):
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-20], edi
mov DWORD PTR [rbp-24], esi
mov DWORD PTR [rbp-28], edx
mov eax, DWORD PTR [rbp-20]
cmp eax, DWORD PTR [rbp-24]
jle .L2
mov eax, DWORD PTR [rbp-20]
cmp eax, DWORD PTR [rbp-28]
jle .L2
mov eax, DWORD PTR [rbp-20]
mov DWORD PTR [rbp-4], eax
.L2:
mov eax, DWORD PTR [rbp-24]
cmp eax, DWORD PTR [rbp-20]
jle .L3
mov eax, DWORD PTR [rbp-24]
cmp eax, DWORD PTR [rbp-28]
jle .L3
mov eax, DWORD PTR [rbp-24]
mov DWORD PTR [rbp-4], eax
.L3:
mov eax, DWORD PTR [rbp-28]
cmp eax, DWORD PTR [rbp-20]
jle .L4
mov eax, DWORD PTR [rbp-28]
cmp eax, DWORD PTR [rbp-24]
jle .L4
mov eax, DWORD PTR [rbp-28]
mov DWORD PTR [rbp-4], eax
.L4:
mov eax, DWORD PTR [rbp-20]
cmp eax, DWORD PTR [rbp-24]
jg .L5
mov eax, DWORD PTR [rbp-20]
cmp eax, DWORD PTR [rbp-28]
jg .L5
mov eax, DWORD PTR [rbp-20]
mov DWORD PTR [rbp-8], eax
.L5:
mov eax, DWORD PTR [rbp-24]
cmp eax, DWORD PTR [rbp-20]
jg .L6
mov eax, DWORD PTR [rbp-24]
cmp eax, DWORD PTR [rbp-28]
jg .L6
mov eax, DWORD PTR [rbp-24]
mov DWORD PTR [rbp-8], eax
.L6:
mov eax, DWORD PTR [rbp-28]
cmp eax, DWORD PTR [rbp-20]
jg .L7
mov eax, DWORD PTR [rbp-28]
cmp eax, DWORD PTR [rbp-24]
jg .L7
mov eax, DWORD PTR [rbp-28]
mov DWORD PTR [rbp-8], eax
.L7:
mov eax, DWORD PTR [rbp-8]
add DWORD PTR [rbp-4], eax
mov eax, DWORD PTR [rbp-4]
mov DWORD PTR [rbp-8], eax
mov DWORD PTR [rbp-12], 0
.L9:
cmp DWORD PTR [rbp-12], 2016
jg .L8
mov eax, DWORD PTR [rbp-8]
add DWORD PTR [rbp-4], eax
add DWORD PTR [rbp-12], 1
jmp .L9
.L8:
mov eax, DWORD PTR [rbp-4]
pop rbp
ret



