Here I am converting C++ code to assembly code,see below.
CPP_Code:-
#include<bits/stdc++.h>
int main()
{
short int y;
for(int x=2;x<=20;x=x+2)
{
if(x<10)
{
y=2*(x-1)+3;
}
else
{
y=x+5;
}
}
}
Assembly Code:-
main:
str fp, [sp, #-4]!
add fp, sp, #0
sub sp, sp, #12
mov r3, #2
str r3, [fp, #-8]
.L5:
ldr r3, [fp, #-8]
cmp r3, #20
bgt .L2
ldr r3, [fp, #-8]
cmp r3, #9
bgt .L3
ldr r3, [fp, #-8]
sub r3, r3, #1
lsl r3, r3, #16
lsr r3, r3, #16
lsl r3, r3, #1
lsl r3, r3, #16
lsr r3, r3, #16
add r3, r3, #3
lsl r3, r3, #16
lsr r3, r3, #16
strh r3, [fp, #-10] @ movhi
b .L4
.L3:
ldr r3, [fp, #-8]
lsl r3, r3, #16
lsr r3, r3, #16
add r3, r3, #5
lsl r3, r3, #16
lsr r3, r3, #16
strh r3, [fp, #-10] @ movhi
.L4:
ldr r3, [fp, #-8]
add r3, r3, #2
str r3, [fp, #-8]
b .L5
.L2:
mov r3, #0
mov r0, r3
add sp, fp, #0
ldr fp, [sp], #4
bx lr
__static_initialization_and_destruction_0(int, int):
push {fp, lr}
add fp, sp, #4
sub sp, sp, #8
str r0, [fp, #-8]
str r1, [fp, #-12]
ldr r3, [fp, #-8]
cmp r3, #1
bne .L9
ldr r3, [fp, #-12]
ldr r2, .L10
cmp r3, r2
bne .L9
ldr r0, .L10+4
bl std::ios_base::Init::Init() [complete object constructor]
ldr r2, .L10+8
ldr r1, .L10+12
ldr r0, .L10+4
bl __aeabi_atexit
.L9:
nop
sub sp, fp, #4
pop {fp, lr}
bx lr
.L10:
.word 65535
.word _ZStL8__ioinit
.word __dso_handle
.word _ZNSt8ios_base4InitD1Ev
_GLOBAL__sub_I_main:
push {fp, lr}
add fp, sp, #4
ldr r1, .L13
mov r0, #1
bl __static_initialization_and_destruction_0(int, int)
sub sp, fp, #4
pop {fp, lr}
bx lr
.L13:
.word 65535
Brief Explanation:-
I am converting CPP code to Assembly code,where as the y consider as the short int(16 bits).Once you can check it.
Thank you..!!
avr assembly please 3) Write an assembly program that is algorithmically equivalent to the following C++ code. Treat the variable y as a short int (16 bits) 1 int y; 2 for (int x = 2; x <= 20; x...
2) Write an assembly program that is algorithmically equivalent to the following C+t code Consider the variables to be 8-bit unsigned integers; you may initialize them to any values 0-255 if you like for testing purposes 13 int speed, lower, a, b; 14 15 if (speed < 98) { 16 17 if (speed <80) t 18 lower++; 19 20 21 else f 23 24 25 26 else t 27 28 2) Write an assembly program that is algorithmically equivalent to...
20) What is the output of the following segment of C code: int avg(int n, int* a); int main () { int array[4]={1,0,6,9}; printf("%d", avg(4, array)+ 1); system("pause"); return 0; } int avg(int n, int* a) { int i, sum=0; for (i=0;i<n;i++) { sum+=a[i]; } return sum/n; } a) 16 b) 5 c) 4 d) 8 21) What is the output of the following segment of C code: int x = 2; int y = 3;...
QUESTION 1 What will be displayed as a result of executing the following code? int x = 5, y = 20; x += 32; y /= 4; cout <<"x = " << x <<"y = " << y; A. x = 32, y = 4 B. x = 9, y = 52 C. x = 37, y = 5 D. x = 160, y = 80 8 points QUESTION 2 What will be the displayed when the following code...
Write an assembly function equivalent to the following C function. If X[k] > K[k+1], it swaps X[k] and X[k+1] and returns 1; otherwise, it returns 0. You may use either conditional branch or conditional execution or their combination. int swap(unsigned short X[], int k) if (X[k] > X[k+1]) { short tmp = X[k]; X[k+1] = X[k]; X[k] = tmp; return 1; else { return 0;
7. Write the following code segment in MARIE's assembly language (If-Else): If x < Y Then X = Y - X; Y = Y + 1; Else X = Y; Y = 0; Endif;
4. Consider the following C code and write its equivalent MIPS code [10 Marks] int func1 (int X[, int k) [ int j, y = 0; for (j = 0; j < k; j++) { 1) if (func2 (X [j]) y+ti { - return Y;
Problem #2 (20pts) Please write the MIPS assembly code corresponding to the following C code segment. Assume that f is assigned to register $s2 int Example int i, int j, int k) int f; f i+j-k-1: return fi
Translate the following piece of code into MIPS assembly. int x, y; int A=100, B=15, C=19, D=21; main(){ x = A + B; y = C + D; if (x > y) swap(&x,&y); } int swap (int *arg1,int *arg2) { int temp; temp = *arg1; *arg1 = *arg2; *arg2 = temp; }
1. [2 points] Write a MIPS assembly language program of the following C function and the code to call the function: int leaf_example (int g, h, i, j) { int f; f = (g + h) - (i + j); return f; مهه Arguments g, h, i, and j are passed to the function in registers $a0, $al, Şa2, and $a3, respectively while f in $50 (hence, need to save $50 on stack), and the result is to be stored...
Consider the AVR statements in r20, 0x03 ori r20, 20 out 0x02, r20 Complete the following C statement so that it is equivalent to the code above. 420; PINA PORTA DDRA PINB PORTB DDRB PINC PORTC DDRC 16 1B 20 24 2 3 Consider the AVR statements in r20, 0x03 ori r20, 20 out 0x02, r20 Complete the following C statement so that it is equivalent to the code above. 420; PINA PORTA DDRA PINB PORTB DDRB PINC PORTC DDRC...