2. 4. The following problems deal with translating from C to ARM. Assume that th
ID: 3747470 • Letter: 2
Question
2.4. The following problems deal with translating from C to ARM. Assume that the variables f, g, h, i, and j are assigned to registers r0, r1, r2, r3, and r4, respectively. Assume that the base address of the arrays A and B are in registers r6 and r7,respectively.
a.
f = g + h + B[4];
b.
f = g – A[B[4]];
2.4.1 For the C statements above, what is the corresponding ARM assembly code?
2.4.2 For the C statements above, how many ARM assemblyinstructions are needed to perform the C statement? Umm
a.
f = g + h + B[4];
b.
f = g – A[B[4]];
Explanation / Answer
2.4.1 For condition "f = g + h + B[4]" the program in C will look like this:
int main() {
int f,g,h,i=5,j=5;
int A[i],B[j];
f=g+h+B[4];
return 0;
}
b) Code in ARM assembly language will look like this:
_dl_relocate_static_pie:
repz ret
nop WORD PTR cs:[rax+rax*1+0x0]
nop DWORD PTR [rax+0x0]
main:
push rbp
mov rbp,rsp
push r13
push r12
sub rsp,0x30
mov rax,rsp
mov rcx,rax
mov DWORD PTR [rbp-0x20],0x5
mov DWORD PTR [rbp-0x1c],0x5
mov eax,DWORD PTR [rbp-0x20]
movsxd rdx,eax
sub rdx,0x1
mov QWORD PTR [rbp-0x18],rdx
movsxd rdx,eax
mov r12,rdx
mov r13d,0x0
movsxd rdx,eax
mov r10,rdx
mov r11d,0x0
cdqe
lea rdx,[rax*4+0x0]
mov eax,0x10
sub rax,0x1
add rax,rdx
mov r11d,0x10
mov edx,0x0
div r11
imul rax,rax,0x10
sub rsp,rax
mov rax,rsp
add rax,0x3
shr rax,0x2
shl rax,0x2
mov QWORD PTR [rbp-0x28],rax
mov eax,DWORD PTR [rbp-0x1c]
movsxd rdx,eax
sub rdx,0x1
mov QWORD PTR [rbp-0x30],rdx
movsxd rdx,eax
mov r8,rdx
mov r9d,0x0
movsxd rdx,eax
mov rsi,rdx
mov edi,0x0
cdqe
lea rdx,[rax*4+0x0]
mov eax,0x10
sub rax,0x1
add rax,rdx
mov edi,0x10
mov edx,0x0
div rdi
imul rax,rax,0x10
sub rsp,rax
mov rax,rsp
add rax,0x3
shr rax,0x2
shl rax,0x2
mov QWORD PTR [rbp-0x38],rax
mov rax,QWORD PTR [rbp-0x38]
mov edx,DWORD PTR [rax+0x10]
mov rax,QWORD PTR [rbp-0x28]
movsxd rdx,edx
mov eax,DWORD PTR [rax+rdx*4]
mov edx,DWORD PTR [rbp-0x3c]
sub edx,eax
mov eax,edx
mov DWORD PTR [rbp-0x40],eax
mov eax,0x0
mov rsp,rcx
lea rsp,[rbp-0x10]
pop r12
pop r13
pop rbp
ret
nop WORD PTR cs:[rax+rax*1+0x0]
nop
2.4.2)
For the C statements above,the total ARM assembly instructions required will be 10 or more(if
dynamic array index is accessed during runtime)
Note :There are many tools online that can be used to translate one programming language to other,that can be helpful for you)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.