In this assignment you are asked to translate C code to ARM64/ARMv8 assembly code, or trace assembly
Question:
In this assignment you are asked to translate C code to ARM64/ARMv8 assembly code, or trace assembly code. C code variables should be mapped to allocated stack frame space or ARMv8 registers in assembly. You must add comment to each line of assembly code. You can test your code with Dr. Shuqun Zhang's AMR64 Online Simulator.
Q1. Translate the following C-code snippets into ARM64/ARMv8 assembly-code snippets.You must add comment to each line of assembly code to explain what it does.
1) Arithmetic operations
int a =2, b = -3, c =10, d =13;
int u, v, w, x, y, z;
u = a * b;
v = d / a;
w = c ^ d;
x = c & d;
y = b <<3;
z = c >>2;
2) Branch and If statement
int a, b;
a =20;
if (a ==3)
b = a -1;
else
b = a +1;
3) Simple while-loop: sum of all odd numbers that are less than 50
int sum =0, n = 50;
int i = 1;
while ( i < n ) {
sum += i;
i +=2;
}
Q2. Read the following ARM64/ARMv8 assembly-code snippets to see what it does.To understand the functionality of the code snippet, please add comment to each line.
1) Branch
sub sp, sp,#16
mov w0,34
str w0, [sp,8]
mov w0,17
str w0, [sp,4]
mov w0,48
str w0, [sp]
ldr w0, [sp,8]
str w0, [sp,12]
ldr w1, [sp,12]
ldr w0, [sp,4]
cmp w1, w0
ble .L2
ldr w0, [sp,4]
str w0, [sp,12]
.L2:
ldr w1, [sp,12]
ldr w0, [sp]
cmp w1, w0
ble .L3
ldr w0, [sp]
str w0, [sp,12]
.L3:
mov w0,0
add sp, sp,16
2) Loop and branch
sub sp, sp,#32
str wzr, [sp,28]
mov x0,#0x1718
movk x0,0x1516, lsl16
movk x0,0x1314, lsl32
movk x0,0x1112, lsl48
str x0, [sp]
mov x0, sp
str x0, [sp,16]
str wzr, [sp,28]
b .L2
.L3:
ldr x0, [sp,16]
ldrb w0, [x0]
strb w0, [sp,15]
ldrb w0, [sp,15]
lsl w0, w0,#2
strb w0, [sp,15]
ldr x0, [sp,16]
ldrb w1, [sp,15]
strb w1, [x0]
ldr x0, [sp,16]
add x0, x0,1
str x0, [sp,16]
ldr w0, [sp,28]
add w0, w0,1
str w0, [sp,28]
.L2:
ldr w0, [sp,28]
cmp w0,7
ble .L3
mov w0,0
add sp, sp,32
Statistics For Business And Economics
ISBN: 9780132745659
8th Edition
Authors: Paul Newbold, William Carlson, Betty Thorne