Translate this C code to assembly For the msp430 # include < msp430g2553.h > int
ID: 3671088 • Letter: T
Question
Translate this C code to assembly For the msp430
#include <msp430g2553.h>
int count;
int scount;
int z;
int main(void) {
WDTCTL = WDTPW | WDTHOLD; // Stop watchdogtimer
P1DIR = 0xFF;
P2DIR = 0x00;
count = 0;
scount = 0;
z = 0;
while (1)
{
if ((P2IN ) == 0x01)
{
z = 1;
}
if ((P2IN )== 0x02)
{
z = 0;
}
if ((P2IN )== 0x04)
{
z = 2;
}
if (P2IN == 0x09)
{
z = 1;
}
if (P2IN == 0x0A)
{
z = 0;
}
if (P2IN == 0x0C)
{
z = 2;
}
if (z == 1)
{
count++;
scount++;
if ((P2IN & BIT3) == BIT3)
{
P1OUT |= count;
_delay_cycles(1000000);
}
if ((P2IN & BIT3) != BIT3)
{
P1OUT |= scount;
_delay_cycles(10000);
}
P1OUT &= ~0xFF;
}
if (z == 0)
{
if ((P2IN & BIT3) == BIT3)
{
P1OUT |= count;
_delay_cycles(1000000);
}
if ((P2IN & BIT3) != BIT3)
{
P1OUT |= scount;
_delay_cycles(10000);
}
P1OUT &= ~0xFF;
}
if (z == 2)
{
count = 0;
scount = 0;
_delay_cycles(1000000);
P1OUT &= ~0xFF;
}
}
}
Explanation / Answer
Here is the respective assembly code for the given c code ..
_delay_cycles(int):
push {r7}
sub sp, sp, #12
add r7, sp, #0
str r0, [r7, #4]
add r7, r7, #12
mov sp, r7
pop {r7}
bx lr
main:
push {r7, lr}
sub sp, sp, #48
add r7, sp, #0
ldr r2, [r7, #28]
ldr r3, [r7, #24]
orr r3, r2, r3
str r3, [r7, #20]
mov r3, #255
str r3, [r7, #16]
mov r3, #0
str r3, [r7, #12]
mov r3, #0
str r3, [r7, #44]
mov r3, #0
str r3, [r7, #40]
mov r3, #0
str r3, [r7, #36]
b .L16
.L17:
nop
.L16:
ldr r3, [r7, #8]
cmp r3, #1
bne .L3
mov r3, #1
str r3, [r7, #36]
.L3:
ldr r3, [r7, #8]
cmp r3, #2
bne .L4
mov r3, #0
str r3, [r7, #36]
.L4:
ldr r3, [r7, #8]
cmp r3, #4
bne .L5
mov r3, #2
str r3, [r7, #36]
.L5:
ldr r3, [r7, #8]
cmp r3, #9
bne .L6
mov r3, #1
str r3, [r7, #36]
.L6:
ldr r3, [r7, #8]
cmp r3, #10
bne .L7
mov r3, #0
str r3, [r7, #36]
.L7:
ldr r3, [r7, #8]
cmp r3, #12
bne .L8
mov r3, #2
str r3, [r7, #36]
.L8:
ldr r3, [r7, #36]
cmp r3, #1
bne .L9
ldr r3, [r7, #44]
add r3, r3, #1
str r3, [r7, #44]
ldr r3, [r7, #40]
add r3, r3, #1
str r3, [r7, #40]
ldr r2, [r7, #8]
ldr r3, [r7, #4]
ands r2, r2, r3
ldr r3, [r7, #4]
cmp r2, r3
bne .L10
ldr r2, [r7, #32]
ldr r3, [r7, #44]
orr r3, r2, r3
str r3, [r7, #32]
movw r0, #16960
movt r0, 15
bl _delay_cycles(int)
.L10:
ldr r2, [r7, #8]
ldr r3, [r7, #4]
ands r2, r2, r3
ldr r3, [r7, #4]
cmp r2, r3
beq .L11
ldr r2, [r7, #32]
ldr r3, [r7, #40]
orr r3, r2, r3
str r3, [r7, #32]
movw r0, #10000
bl _delay_cycles(int)
.L11:
ldr r3, [r7, #32]
bic r3, r3, #255
str r3, [r7, #32]
.L9:
ldr r3, [r7, #36]
cmp r3, #0
bne .L12
ldr r2, [r7, #8]
ldr r3, [r7, #4]
ands r2, r2, r3
ldr r3, [r7, #4]
cmp r2, r3
bne .L13
ldr r2, [r7, #32]
ldr r3, [r7, #44]
orr r3, r2, r3
str r3, [r7, #32]
movw r0, #16960
movt r0, 15
bl _delay_cycles(int)
.L13:
ldr r2, [r7, #8]
ldr r3, [r7, #4]
ands r2, r2, r3
ldr r3, [r7, #4]
cmp r2, r3
beq .L14
ldr r2, [r7, #32]
ldr r3, [r7, #40]
orr r3, r2, r3
str r3, [r7, #32]
movw r0, #10000
bl _delay_cycles(int)
.L14:
ldr r3, [r7, #32]
bic r3, r3, #255
str r3, [r7, #32]
.L12:
ldr r3, [r7, #36]
cmp r3, #2
bne .L17
mov r3, #0
str r3, [r7, #44]
mov r3, #0
str r3, [r7, #40]
movw r0, #16960
movt r0, 15
bl _delay_cycles(int)
ldr r3, [r7, #32]
bic r3, r3, #255
str r3, [r7, #32]
b .L16
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.