2

我正在尝试为 MARIE 编写一个子程序,它将使用重复加法来立方一个数字。我知道我需要将数字本身添加等于其数量的三倍(因此,如果它是 4,我需要将 4 添加到自身 4 次,乘以 3)。我对如何再循环 3 次没有真正的好主意。也不确定如何使用 JnS。

从我看到的示例中,他们使用 JnS 来存储值。这就是我所拥有的

/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
4

1 回答 1

0

将不断增长的加法存储在变量 Sum 中。将变量地址处的值与 add 而不是 AddI 一起使用。它需要添加与自身相同的次数才能找到平方,包括原始数字,因此从 Num 中减去一。

然后平方值可以存储在第二个变量Square中,该变量需要与原始数字的次数相加。

例如。
2 3 = 2 2 * 2 = (2+2)+(2+2)
3 3 = 3 2 * 3 = (3+3+3)+(3+3+3)+(3+3+3)

一种动态的方法是要求输入。计算需要使用两个循环添加的次数。然后实现如上所示的循环。

    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
于 2018-11-16T16:38:02.607 回答