2

I'm trying to write a subroutine for MARIE that will cube a number using repeated addition. I know I need to add the number by itself equal to its amount three times (so if its 4 I'll need to add 4 to itself 4 times, times 3). I don't have a real good idea on how to loop 3 more times. Also not sure on how to use JnS.

From examples I've seen they use JnS to store values. Here's what I have

/Subroutine for finding cube of number
/
Load Num        /Load the first number
Store Count     /Store this number to use for looping repeated addition
Loop,   Load Sum        /Load the sum for first number into AC
AddI Num        /Add the value in AC of first number
Store Sum       /Store the sum
Load Count      /Load Count again
Subt One        /Subtract one from our counter
Store Count     /Store this new number for our counter
Skipcond 800        /If Count > 0, skip next instruction
Jump Loop       /Continue loop if Count is greater than 0
DNH
  • 29
  • 5

1 Answers1

0

Store the growing addition in a variable Sum. Use the values at the variables address with add not AddI. It needs to be added the same number of times as itself to find the square, including the original number, hence minus one from the Num.

The Squared value then can be stored in a second variable Square, which needs to be summed the the number of times of the original number.

For example.
23 = 22 * 2 = (2+2)+(2+2)
33 = 32 * 3 = (3+3+3)+(3+3+3)+(3+3+3)

A dynamic way of doing it, is to ask for input. Calculate the number of times it needs to be added using two loops. Then implement the loops as shown above.

    Input
    Store Num
    Store Sum
    Subt One    /Reduce iterations for square.
    Store Count

SquareLoop, Load Count
    Skipcond 800
    Jump ResetCounter   /When count=0 end program.
    Subt One
    Store Count
    Load Sum
    Add Num
    Output  /Keeping track of the process.
    Store Sum

    Jump SquareLoop

ResetCounter, Load Num
    Subt One    /Reduce iterations for cube loop.
    Store Count    
    Load Sum
    Store Square    /The squared value to sum. 

CubeLoop,  Load Count
    Skipcond 800
    Jump End    /When count=0 end program.
    Subt One
    Store Count
    Load Sum
    Add Square
    Output  /Keeping track of the process.
    Store Sum

    Jump CubeLoop

Output
End, Halt

Num, Dec 0
Count, Dec 1
Square, Dec 0
Sum, Dec 0
One, Dec 1
  • 1
    Your algorithm has O(n^2) complexity. You could instead do `n^3 = n^2 * n` by looping n times, adding `n^2`, giving you `O(n)` total additions. (First loop calculates n^2, then the next loop adds *that* value `n` times instead of using `n^2` as a loop counter.) – Peter Cordes Nov 16 '18 at 16:45
  • 1
    asm formatting tip: indent the instructions one level deeper than the labels, so branch targets (tops of loops) stand out. MARIE is already hard enough to read / follow (because it takes so many instructions to get so little done on an accumulator architecture with no immediate operands) that this should help. I made an edit, hope you like. – Peter Cordes Nov 16 '18 at 16:48
  • @PeterCordes yes I was wondering about that - re reducing the number of loops. I will edit it tomorrow it's late now, Thanks for the formatting tips and edit. I'm brushing up on some old skills. I have another post that I'll need to improve the formatting on. But it's 4am here. so it can wait :) –  Nov 16 '18 at 17:02
  • 1
    Yup, assembly is fun, but I find Marie and some other toy architectures *too* simplified, to the point where it's really cumbersome to get anything done. Register machines (like ARM or x86) are much better for small simple algorithms because you can keep a few local variables in registers. LC-3 is a nicer toy architecture (https://en.wikipedia.org/wiki/LC-3), being a register machine that includes some bitwise booleans. An ISA without that feels ridiculous to anyone with asm / low-level experience with binary numbers. But it's still weird not having a right shift until the LC-3b extension. – Peter Cordes Nov 16 '18 at 17:07
  • @PeterCordes thanks for the feedback. I improved the code. I will finish answering the unanswered MARIE questions and then more onto ARM or x86. I really want to learn assembly. I've always preferred the lower level languages. –  Nov 17 '18 at 04:39
  • There's a lot of good stuff in the x86 tag wiki: https://stackoverflow.com/tags/x86/info. It's pretty sprawling, but some of the most valuable links in each section are near the top of their respective sections. The other tag wikis (like ARM and MIPS) aren't as extensive because I'm most interested in x86. (And nobody else put as much effort into curating useful links and putting them in tag wikis.) – Peter Cordes Nov 17 '18 at 05:07
  • @PeterCordes very nice tag wiki. I'm already using the links. Thanks. I feel like a baby beginner :) –  Nov 17 '18 at 05:13
  • 1
    Cheers, thanks for the feedback that it *is* actually useful for someone (re?)learning / brushing up on asm, and not just as my personal bookmarks page. :P The FAQ section could use some work, and/or better canonicals for several of the bullet points if you happen to come across any FAQ topics or canonicals that aren't already linked. – Peter Cordes Nov 17 '18 at 05:17
  • @PeterCordes will do. –  Nov 17 '18 at 05:18