Write the LEGv8 assembly program for following c program. Assume
array1, array2 and array3 are
stored in memory with base addresses at 1000, 2000 and 3000
respectively.
int absoluteDifference(int x, int y) {
int r;
if (x > y)
r = x - y;
else
r = y - x;
return r;
}
int main() {
for (int i=0; i < 10; i++) {
int a = array1[i];
int b = array2[i];
int c = absoluteDifference(a, b);
array3[i] = c;
}
}
The has modified to get the result
#include <iostream>
using namespace std;
int absoluteDifference(int x, int y) {
int r;
if (x > y)
r = x - y;
else
r = y - x;
return r;
}
int main() {
int array1=1000,array2=2000,array3=3000;{
array3 = absoluteDifference(array1, array2);
cout<<"The absolute difference is: "<< array3;
}
}
The conversion to the assembly language
absoluteDifference(int, int):
push rbp
mov rbp, rsp
mov DWORD PTR [rbp-20], edi
mov DWORD PTR [rbp-24], esi
mov eax, DWORD PTR [rbp-20]
cmp eax, DWORD PTR [rbp-24]
jle .L2
mov eax, DWORD PTR [rbp-20]
sub eax, DWORD PTR [rbp-24]
mov DWORD PTR [rbp-4], eax
jmp .L3
.L2:
mov eax, DWORD PTR [rbp-24]
sub eax, DWORD PTR [rbp-20]
mov DWORD PTR [rbp-4], eax
.L3:
mov eax, DWORD PTR [rbp-4]
pop rbp
ret
.LC0:
.string "The absolute difference is: "
main:
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], 1000
mov DWORD PTR [rbp-8], 2000
mov DWORD PTR [rbp-12], 3000
mov edx, DWORD PTR [rbp-8]
mov eax, DWORD PTR [rbp-4]
mov esi, edx
mov edi, eax
call absoluteDifference(int, int)
mov DWORD PTR [rbp-12], eax
mov esi, OFFSET FLAT:.LC0
mov edi, OFFSET FLAT:_ZSt4cout
call std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
mov rdx, rax
mov eax, DWORD PTR [rbp-12]
mov esi, eax
mov rdi, rdx
call std::basic_ostream<char, std::char_traits<char> >::operator<<(int)
mov eax, 0
leave
ret
__static_initialization_and_destruction_0(int, int):
push rbp
mov rbp, rsp
sub rsp, 16
mov DWORD PTR [rbp-4], edi
mov DWORD PTR [rbp-8], esi
cmp DWORD PTR [rbp-4], 1
jne .L9
cmp DWORD PTR [rbp-8], 65535
jne .L9
mov edi, OFFSET FLAT:_ZStL8__ioinit
call std::ios_base::Init::Init() [complete object constructor]
mov edx, OFFSET FLAT:__dso_handle
mov esi, OFFSET FLAT:_ZStL8__ioinit
mov edi, OFFSET FLAT:_ZNSt8ios_base4InitD1Ev
call __cxa_atexit
.L9:
nop
leave
ret
_GLOBAL__sub_I_absoluteDifference(int, int):
push rbp
mov rbp, rsp
mov esi, 65535
mov edi, 1
call __static_initialization_and_destruction_0(int, int)
pop rbp
ret
The ouput
Get Answers For Free
Most questions answered within 1 hours.