MARIE simulator: multiplication of two numbers using addition - marie

multiplication of two numbers 6 and 3 by, repeatedly addition of 3 six times,using a loop that will add 3 six times and store the result into accumulator.

INPUT
STORE x
INPUT
STORE y
loop, LOAD x
ADD multiply
STORE multiply
LOAD y
SUBT one
STORE y
SKIPCOND 400
JUMP loop
LOAD multiply
OUTPUT
HALT
x, Dec 0
y, Dec 0
one, Dec 1
multiply, Dec 0

Related

Wondering what exactly is wrong with this MARIE assembly code? (Calculator)

ORG 100 /Start Here
INPUT
Store X /Store 1st input value in X
INPUT
Store Y / Store 2nd input value in Y
choice, INPUT
Store C /Store choice in C
if, Load C
Subt A
Skipcond 400
Jump elseif1
JnS ADDITION /Call subroutine
Jump end
elseif1, Load C
Subt S
Skipcond 400
Jump elseif2
JnS SUBTRACT /Call subroutine
Jump end
elseif2, Load C
Subt M
Skipcond 400
Jump elseif3
JnS MULTIPLY /Call subroutine
Jump end
elseif3, Load C
Subt D
Skipcond 400
Jump elseif4
JnS DIVISION /Call subroutine
Jump end
elseif4, Load C
Subt Q
Skipcond 400
Jump choice /re-enter choice
Halt /stop the program
end, Output /Display the value
Halt /Stop
X, DEC 0
Y, DEC 0
C, DEC 0
/add two numbers
ADDITION Load X
Add Y
JumpI ADDITION / Indirect jump to return.
/subtract two numbers
SUBTRACT Load X
Subt Y
JumpI SUBTRACT / Indirect jump to return.
/multiply two numbers using continuous add loop
MULTIPLY loop, Load num
Add X
Store num
Load Y
Subt one
Store Y
Skipcond 400
Jump loop
Load num
JumpI MULTIPLY / Indirect jump to return.
/divide two numbers and return the quotient
DIVIDE If, Load X
Subt Y
Skipcond 800
Jump EndIf
Store X
Load quo
Add one
Store quo
Jump If
EndIf, Load quo
JumpI DIVIDE / Indirect jump to return.
/constants values
one, DEC 1
num, DEC 0
rem, DEC 0
quo, DEC 0
A, DEC 97 /a
M, DEC 109 /m
S, DEC 115 /s
D, DEC 100 /d
Q, DEC 113 /q
This is a calculator that is supposed to add, subtract, multiply, and divide. Once the user is done with an operand it is supposed to directly go to the next one as well, but it is giving me errors and I can't seem to fix them. Everything looks fine to me but it keeps saying "Operand undefined." I'm sure this is an obvious error to others but I can't seem to fix it. Was hoping someone can help me with this! Thanks!

Marie JS to the power of

I need to write a code for raising x to the power of y in Marie.js. That's what I have for now, however it spits out an error SyntaxError:L41 - Unknown label Loop.
Input
Subt One
Store Count
Input
Store x
Store y
Jns Exp
Load Wyn
Output
End, Halt
Exp, Hex 0
Loop2, Load Count
Skipcond 800
JumpI Exp
JnS Multi
Load Wyn
Store x
Load Count
Subt One
Store Count
Jump Loop2
Multi, Hex 0
Load Zero
Store Wyn
Loop, Load x
Skipcond 800
JumpI Multi
Load Wyn
Add y
Store Wyn
Load x
Subt One
Store x
Jump Loop
x, Dec 2
y, Dec 3
Zero, Dec 0
One, Dec 1
Wyn, Dec 0
Count, Dec 0
I understand the necessity for 2 loops, which execute y-1 times, however Im completely clueless what I'm doing wrong.
You have to define a label before you can Jump to it; you have not defined the label Loop. You have defined a label named Loop2.

Matlab - filter matrix by having specific values

I have a matrix A and a vector b. I don't know their sizes, the size varies because it is the output of another function. What I want to do is filter A by a column (let's say jth column) which has at least one value that is in b.
How do I do this without measuring the size of b and concatenating every filtered result. Right now, the code is like this (assume j is a given value)
bsize=size(b,1);
for i=1:bsize
if i==1
a=A(A(:,j)==b(i),:);
else
a=[a; A(A(:,j)==b(i),:)];
end
end
I want to code a faster solution.
I am adding a numerical example just to make it clear. Let's say
A=[2 4
7 14
11 13
15 14]
and b=[4 14]
What I'm trying to do is filter to obtain the A matrix whose values are 4 and 14 in the second column, the elements of b to obtain the following output.
A=[2 4
7 14
15 14]
In my data A has more than 12000 rows and b has more than 100 elements. It doesn't always have to be the second column, sometimes the column index changes but that's not the problem now.
Use the ismember function to create a logical index based on column j=2 of A and vector b, and use that index into the rows of A:
output = A(ismember(A(:,j), b), :);

Matlab - find a missing time from a matrix and insert the missing time with a value

I have a series of times and returns in various matrices lets call them a b c. They are all x by 2 with column 1 being times in seconds and column 2 returns. While all the returns are over a set series of time intervals like 15s 30s 45s etc the problem is not all the matrices have all the time buckets so while a might be a 30 by 2 , b might only be a 28 by 2. Because it is missing say time 45 seconds and a return. I want to go through each matrix and where I am missing a time bucket I want to insert the bucket with a zero return - I am happy to create a control 30 by 1 matrix with all the times that need to be cross referenced
You can use ismember to locate these missing positions, so if a is the control vector and b is the missing data vector ind=find(ismember(a,b)==0); will give you the indices of a that are missing in b.
For example:
a=1:10;
b=[1:2 4:5 7:10];
ind=find(ismember(a,b)==0);
ind =
3 6
In order to to add zeros in the right places for b just
for n=1:numel(ind)
b=[b(1:ind(n)-1) , 0 , b(ind(n):end)];
end
b =
1 2 0 4 5 0 7 8 9 10

calculating x2 from poisson distributed data

So I have a table of values
v=0 1 2 3 4 5 6 7 8 9
#times obs.: 5 19 23 21 14 12 3 2 1 0
I am supposed to calculate chi squared assuming the data fits a poisson dist. with mean u=3.
I have to group values >=6 all in one bin.
I am unsure of how to plot the poisson dist., and most of all how to control what goes into what bin, if that makes sense.
I have plotted a histogram using histc before..but it was with random numbers that I normalized. The amount in each bin was set for me.
I am super new...sorry if this question sucks.
You use bar to plot a bar graph in matlab.
So this is what you do:
v=0:9;
f=[5 19 23 21 14 12 3 2 1 0];
fc=f(find(v<6)); % copy elements where v<=6 into a new array
fc(end+1)=sum(f(v=>6)); % append the sum of elements where v=>6 to that array
figure
bar(v(v<=6), fc);
That should do the trick...
Now you didn't actually ask about the chi squared calculation. I would urge you not to put values of v>6 all into one bin for that calculation, as it will give you a really bad result.
There is another technique: if you use the hist function, you can choose the bins - and Matlab will automatically put things that exceed the limits into the last bin. So if your observations were in the array Obs, you can do what was asked with:
h = hist(Obs, 0:6);
figure
bar(0:6, h)
The advantage is that you have the array h available (frequencies) for other calculations.
If you do instead
hist(Obs, 0:6)
Matlab will plot the graph for you in a single statement (but you don't have the values...)