How to print only five elements from an array in marie assembly language

I need to print only 5 elements of an array in marie

I tried to but it just looped

ORG 100

Main,    Load  X           // Load the address of the array
         Store Addr
         Load  5          // Set up a counter for 5 elements
         Store Count
Loop,    Load  Addr        // Load the address of the array
         Add   Count       // Add the current value of the counter to the address
         Subt  1          // Move to the next element (assuming each element is 1 word)
         Store Addr
         Load  Addr        // Load the address of the current element
                     // Print the element
         Subt  1          // Decrement the counter
         Store Count
         Skipcond  000     // If Count <= 0, exit the loop
         Jump  Loop
         Halt

Addr,    DEC   0
Count,   DEC   0
X,       DEC   0
Array,   DEC   10          // Example array with 10 elements
         DEC   20
         DEC   30
         DEC   40
         DEC   50
         DEC   60
         DEC   70
         DEC   80
         DEC   90
         DEC   100

Upvotes: 0

Views: 121

Answers (2)

trincot
trincot

Reputation: 350310

There are these issues in your code:

  • Not loading the address of X:

    Load  X           // Load the address of the array
    

    Here you don't actually do what the comment says. Instead you load the contents of X, i.e. the value stored at memory location X, which is 0 in your case. To do what you intended, you can define X to really store the address, using the ADR type specifier:

    X,       ADR   Array
    
  • Not loading the counter correctly:

    Load  5          // Set up a counter for 5 elements
    

    Here you load whatever is at address 5 into the accumulator.

    To load the value 5, first put that value in a memory location with your assembler code:

    Five, DEC 5
    

    And then do:

    Load Five
    

    For the same reason you shouldn't do Subt 1, but store 1 somewhere and refer to it with Subt One.

  • Not updating the counter correctly: at the time you execute Subt in order to decrease the counter, the accumulator does not have the counter value, so you will update the counter with a wrong value. You should first load the counter, right before performing this Subt instruction.

  • The loop condition is not correct: the comment says <=, but actually the skip happens when the accumulator is strictly less than zero: so you still continue with an iteration when the counter is 0. In that case you should exit the loop. You could fix this by checking the that the counter is greater than 0, and swap the two different actions (halt or loop).

  • Traversing in reverse: with Add Count and decreasing the count, you actually visit array elements in the reversed order, with the fifth coming first, and the first, last. You could increment your array pointer by 1 to get values in their normal order.

  • There is no instruction in your code that actually reads the value at the calculated address and prints it. To read it, you can use LoadI.

Here is the corrected code:

         Load  X           / Load the address of the array
         Store Addr
         Load  Five        / Set up a counter for 5 elements
         Store Count
Loop,    LoadI Addr        / Load the value at the current address
         Output
         Load  Addr        / Now load the address itself
         Add   One         / Move to the next element
         Store Addr        
         Load  Addr        / Load the address of the current element
         Load  Count
         Subt  One
         Store Count
         Skipcond  800     / If Count > 0, continue the loop
         Halt
         Jump  Loop

One,     DEC   1
Five,    DEC   5
Addr,    DEC   0
Count,   DEC   0
X,       ADR   Array       / Now X has the address of Array
Array,   DEC   10          / Example array with 10 elements
         DEC   20
         DEC   30
         DEC   40
         DEC   50
         DEC   60
         DEC   70
         DEC   80
         DEC   90
         DEC   100

You can assemble and run this code at MARIE.js.org.

Upvotes: 0

Armali
Armali

Reputation: 19375

MARIE is a strange architecture having no immediate addressing mode. This means e. g.

         Load  5          // Set up a counter for 5 elements

doesn't do what you thought and wrote, it rather loads the content of address 5. Instead of this and the following Store Count you could just initialize Count with the desired value:

Count,   DEC   5

Load the address of the array now is more complicated - in order to initialize a location with the address, we must know it. (Unfortunately the assembler doesn't accept something like DEC Array.) In your simple case, we could move the array near the start of the code so that we can count the location more easily.

Main,    Jump  Loop
Array,   DEC   10          // Example array with 10 elements
         DEC   20
         DEC   30
         DEC   40
         DEC   50
         DEC   60
         DEC   70
         DEC   80
         DEC   90
         DEC   100
Addr,    HEX   101         // Array is located 1 word after origin 100

Then the only things missing in your program are the instructions to load and output the element from the array and to load the to-be-decremented counter.

Upvotes: 0

Related Questions