Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

array.ibcm Write another IBCM program that reads in an array and prints its elements forwards and backwards. Your program will first be given the array

array.ibcm

Write another IBCM program that reads in an array and prints its elements forwards and backwards. Your program will first be given the array size n as input. The next n lines of input will contain the contents of the array.

  • The array base address is hard-coded into memory, meaning it's a pre-set value that isn't obtained by user input. We will be inputting arrays of many different sizes into your program, so make sure you carefully choose where to store your array in memory.
  • Before your program halts, it should print out the array both forwards and backwards.

You MUST iterate through the array by creating the array load instruction, similarly as was done in lecture in the array-ssummation.ibcm program. You may NOT have a series of separate instructions to each load a separate value from the array - such a program will receive zero credit.

Sample array.ibcm Run

Below is a sample execution run to show you the output we are looking for. The output shown is a result of running array.ibcm in the ibcm simulator.

# input # First input is size of the array (3) # Next inputs are the array contents (1,2,3) 3 1 2 3 # output 0001 0002 0003 0003 0002 0001

Hints

Leave room for mistakes

Sprinkle some nop (opcode: B) instructions throughout your program that can be replaced later in case you realize that you need some extra variables or are missing some instructions.

Working with arrays

Arrays can be difficult to work with in IBCM given the extremely limited instruction set. We'll walk through the array summation example again from the slides to point out some helpful concepts.

How do you add variables in IBCM? Why, with the add instruction, of course. However, for arrays, the address you want to add from changes every time! How do we account for that?

We need to dynamically change the add instruction before executing it to make it point to the right address. If we can generate the appropriate instruction based off of the starting address of the array and the index we want (hint: look back to lab 4), we can then store that later on in our program to execute automatically!

Thus, if we wanted to sum an array, we would:

  1. Create a variable set to 5000 - notice how it's just an add instruction without an address. This is adit in the slides.
  2. Load that instruction into the accumulator
  3. Use actual add instructions to add the base array address a and the current array index i to the accumulator
    • The accumulator should now contain something like 5xyz, where xyz is the location of a[i] in your program.
    • The accumulator now contains a valid add instruction!
  4. Store that instruction in the line where step 6 would be running
  5. Load your accumulated sum
  6. Whatever was here previously was overwritten by step 4, and so now we execute the instruction 5xyz to add the value of a[i] to the sum. This is doit in the slides.
  7. Store the sum!
  8. Repeat steps 2-7 until the end of the array

While this deals specifically with the example in the slides, it will hopefully clarify the general approach of iterating through arrays and give you more insight into how to print them out.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Android 4 Application Development

Authors: Reto Meier

3rd Edition

1118223853, 9781118223857

Students also viewed these Programming questions