Question

3) Write an assembly program that is algorithmically equivalent to the following C++ code. Treat the variable y as a short in


avr assembly please
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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..!!

Add a comment
Know the answer?
Add Answer to:
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...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT