Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following program using loops, branching and arithmetic instructions i

ID: 3872286 • Letter: W

Question

Write the following program using loops, branching and arithmetic instructions in MIPS:

A custodial staff person at the College does a peculiar thing at night. There is a corridor with a number of lights controlled by toggle switches. Initially all lights are off. Pressing the switch on a light will turn it on. Pressing it again will turn it off. This staff member walks down a corridor with n lights n times. On the ith trip the person only toggles the switches whose position number is divisible by i. When coming back to the initial position, the person does not press any switches. Write a MIPS assembly language program that determines the state (on or off) of the last light in the corridor after the staff member has made n trips up and down the corridor doing this routine.

Your program should prompt the user for the number of lights in the corridor, read the number and determine and print the value of the last light. You should print an error message if the user does not enter a positive integer. You are not required to have a loop for multiple runs, but I appreciate it when you do. If you use such a loop, you should terminate the loop with the user enters 0 for the number of lights.

Possible sample output:

Please enter a positive integer.

-3

This is not a positive integer. Please try again.

Please enter a positive integer.

3

The last bulb is off.

Please enter a positive integer.

0

Have a nice day.

Explanation / Answer

This program can be saved as lightest.s

MIPS CODE

AREA asm_function, CODE, READ_ONLY

    

; Exporting asm function

    EXPORT speedchecker

    

    ALIGN

speedchecker

    ; setting pointer to base address

    LDR     X1,=0x2009c02      ; pointer for port1 base

    

loop

    LDR     X3,=0x00000000      ; setting LED 1 as off (all bits are 'zero')

    STR     X3,[X1,#0x14]       ; setting direct outputs (base+0x14)

    LDR     X3,=0x00040000      

; setting LED bit on for ‘1’ (bit 18 i.e P1.18 = 'one')

    STR     X3,[X1,#0x14]       ; setting direct outputs (base+0x14)

           

    BRANCH       loop                ; branching loop (infinite loop!)

    

    ALIGN

    END

Function call (mainfunction.c)

#include "mbed.h"

extern "C" void speedchecker();

Digital_myled(LED);

int main() {

    

    speedchecker();  // executing assembler routine

    while(1)

{

    }

}

#include "mbed.h"

extern "C" void speedchecker();

Digital_myled(LED);

int main() {

    

    speedchecker();  // executing assembler routine

    while(1)

{

    }

}