Question

Write the LEGv8 assembly program for following c program. Assume array1, array2 and array3 are stored...

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;
}
}

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Convert each of the below C code snippet to LEGv8 assembly code. Assume variable a, b,...
Convert each of the below C code snippet to LEGv8 assembly code. Assume variable a, b, and c is stored in registers X19, X20, and X21 respectively. Base address of d is stored in register X22. Comment your assembly code. (24 Points) a. if (a > b) d[a] = b + 8; else d[a] = b - 16; b. for (i=0;i<a; i++) a = d[i] + c; c. i = 0; while ( d[i] == b) if(( a - i...
Convert each of the below C code snippet to LEGv8 assembly code. Assume the variables a,...
Convert each of the below C code snippet to LEGv8 assembly code. Assume the variables a, b, and c are stored in registers X19, X20, and X21 respectively. Base address of d is stored in register X22. Comment your assembly code. a. if (a > b) d[a] = b + 8; else d[a] = b - 16; b. for (i=0;i 0) i++; a = d[i] + c; c. i = 0; while (d[i] == b) if ((a - i) >...
Implement the following C code in LEGv8 assembly. Hint: Remember that the stack pointer must remain...
Implement the following C code in LEGv8 assembly. Hint: Remember that the stack pointer must remain aligned on a multiple of 16. int fib (int n) { if (n==0) return 0; else if (n == 1) return 1; else return fib(n−1) + fib(n−2); } please "LEGv8" ! thanx :-)
Create a memory diagram for the following C program for the second time the program reaches...
Create a memory diagram for the following C program for the second time the program reaches point one. int foo(const char *x, int y); int main(void) { int a, b; a=foo("abcde", 'p'); b=foo("purple", 't'); return 0; } int foo(const char *x, int y) { int c = -1, i = 0; while (x[i] != '\0') { if (x[i] == y) i++; } //point 1 return c; }
Q3) Write a function that takes two arrays and their size as inputs, and calculates their...
Q3) Write a function that takes two arrays and their size as inputs, and calculates their inner product (note that both arrays must have the same size so only one argument is needed to specify their size). The inner product of two arrays A and B with N elements is a scalar value c defined as follows: N−1 c=A·B= A(i)B(i)=A(0)B(0)+A(1)B(1)+···+A(N−1)B(N−1), i=0 where A(i) and B(i) are the ith elements of arrays A and B, respectively. For example, theinnerproductofA=(1,2)andB=(3,3)isc1 =9;andtheinnerproductofC= (2,5,4,−2,1)...
Write the C55x assembly code for each of the following C snippet code shown below. Assume...
Write the C55x assembly code for each of the following C snippet code shown below. Assume the 8-bit values a, b, c, and d are stored in locations 0x600, 0x601, 0x602, and 0x603 respectively in the memory. Store the result x in location 0x604 and y in location 0x60C. Do not use software to generate the assembly code and comment your code. x = (a - b)3 - (c*d); for (j=0;j<=200;j++)   y = y + (a * b);
Write a program in Assembly language that calculates the maximum and minimum of 10 numbers stored...
Write a program in Assembly language that calculates the maximum and minimum of 10 numbers stored in memory and writes them to the memory locations. #using loop num: dw 56, 45, 36, 67, 76, 22, 89, 12, 29, 83 min: dw 0 max: dw 0 I wrote a codem but I got an error when I executed it [ORG 0x0100] jmp start num: dw 56, 45, 36, 67, 76, 22, 89, 12, 29, 83 min: dw 0 max: dw 0...
a. Write MIPS assembly code for the following machine code. Assume that the segment is placed...
a. Write MIPS assembly code for the following machine code. Assume that the segment is placed starting at location 8000. Create labels for branch and jump instructions and indicate the 0001 0001 0010 1000 0000 0000 0000 0011 1000 1110 0001 0001 0000 0000 0000 1000 0010 0010 0010 1000 0000 0000 0000 0110 0000 1000 0000 0000 0000 0001 0000 0010 b. Translate the following C function into MIPS assembly code. int f1(int ar1[], int ar2[], int n) {...
Translate the following C function into MIPS assembly code. Note that function f1 is defined somewhere...
Translate the following C function into MIPS assembly code. Note that function f1 is defined somewhere else. int f2(int x, int y) { int i, z = 0; for (i = x; i <= y; i++) z = z + f1(i, 5); return z; }
Write an assembly program to compute the following expressions Create a list named ‘z’ of size...
Write an assembly program to compute the following expressions Create a list named ‘z’ of size 3 using DUP operator. Leave the list ‘z’ uninitialized. You can denote the items in the list as [z0, z1, z2]. z0 =x+13 z1 = y-x z2= r+z1-13 Where x, y, r are 16-bit integer memory variables. x = 7, y = 20, r = 4 Use the debugger to verify your answer. Please answer using this format for code: .386 .model flat, stdcall...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT