How do you write x86 assembly code for the above main procedure and Addarrays function?
Main()
{
int A[100];
int B[100];
// initialization etc.
length = 100;
Call Addarrays(A, B, length)
}
Addarrays(int[] X, int[] Y, count)
{
i = 0;
while (i < count) {
X[i] = X[i] + Y[i];
I++
}
}
X86-64 9.2 code:
main:
push rbp
mov rbp, rsp
sub rsp, 816
mov DWORD PTR [rbp-4], 100
mov edx, DWORD PTR [rbp-4]
lea rcx, [rbp-816]
lea rax, [rbp-416]
mov rsi, rcx
mov rdi, rax
mov eax, 0
call Addarrays
mov eax, 0
leave
ret
Addarrays:
push rbp
mov rbp, rsp
mov QWORD PTR [rbp-24], rdi
mov QWORD PTR [rbp-32], rsi
mov DWORD PTR [rbp-36], edx
mov DWORD PTR [rbp-4], 0
jmp .L4
.L5:
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rdx
mov ecx, DWORD PTR [rax]
mov eax, DWORD PTR [rbp-4]
cdqe
lea rdx, [0+rax*4]
mov rax, QWORD PTR [rbp-32]
add rax, rdx
mov edx, DWORD PTR [rax]
mov eax, DWORD PTR [rbp-4]
cdqe
lea rsi, [0+rax*4]
mov rax, QWORD PTR [rbp-24]
add rax, rsi
add edx, ecx
mov DWORD PTR [rax], edx
add DWORD PTR [rbp-4], 1
.L4:
mov eax, DWORD PTR [rbp-4]
cmp eax, DWORD PTR [rbp-36]
jl .L5
nop
pop rbp
ret
if you like the answer please provide a thumbs up
Get Answers For Free
Most questions answered within 1 hours.