Assembly level programming to find the factorial - microprocessors

MVI B 07h
LXI H 0007h
LXI D 0007h
DCR B
LOOP1: DCR B
MOV C B
INR B
LOOP: DAD D
DCR C
JNZ LOOP
MOV E L
MOV D H
DCR B
JNZ LOOP1
HLT
I couldn't find out the problem in my code. Can you please help me out? It's giving me partially wrong answer. The two LSB bits are correct but not the MSBs.

I'm not sure why you're doing the extra decrement (followed by the increment) at the LOOP1 label to the B register but when B is one it causes C to become 0, which then wraps around to FFh and performs the multiply loop another 255 times.
Instead why don't you take out the DCR B / INR B and before the multiply loop just set the H register to 0. The full program would look like this:
MVI B, 07h
LXI H, 0007h
LXI D, 0007h
DCR B
LOOP1:
MOV C, B
LXI H, 0
LOOP:
DAD D
DCR C
JNZ LOOP
MOV E, L
MOV D, H
DCR B
JNZ LOOP1
HLT

Related

How can I calculate all of the possible combinations of two 1x6 vectors in MATLAB?

I have two 1x6 vectors that I am eventually trying to just sum up, but I need to get all of the possible combinations of these vectors before doing so. The vectors will look like so:
V1=[a b c d e f];
V2=[A B C D E F];
What I need is to find all possible combinations of variables that will remain a 1x6 vector. I have been messing around for a while now and I think I have found a way by using various matrices but it seems terribly inefficient. An example of what I am looking for is as follows.
M=[a b c d e f;
A b c d e f;
A B c d e f;
A B C d e f;
A B C D e f;
A B C D E f;
A B C D E F;
. . .]
And so on and so forth until all combinations are found. Unfortunately I am not a MATLAB whiz hence the reason I'm reaching out. I'm sure there has to be a much simpler way than what I have been trying. I hope that my question was relatively clear. Any help is much appreciated! Thanks!
I used cellfun to create the indexes:
V1=['abcdef'];
V2=['ABCDEF'];
VV = [V1;V2];
l = length(V1);
pows = 0:l-1;
x = num2cell(2.^pows);
L = x{end};
rows = cellfun(#(x) reshape([ones(x,L/x);2*ones(x,L/x)],[2*L 1]),x,'Uniformoutput',0);
rows = cell2mat(rows);
cols = repmat(1:l,[2*L 1]);
idxs = sub2ind(size(VV),rows,cols);
M = VV(idxs);
and you get:
M =
abcdef
Abcdef
aBcdef
ABcdef
abCdef
AbCdef
aBCdef
ABCdef
abcDef
AbcDef
...

Maple: specify variable over which to maximize

This is a very simple question, but found surprisingly very little about it online...
I want to find the minimizer of a function in maple, I am not sure how to indicate which is the variable of interest? Let us take a very simple case, I want the symbolic minimizer of a quadratic expression in x, with parameters a, b and c.
Without specifying something, it does minimize over all variables, a, b, c and x.
f4 := a+b*x+c*x^2
minimize(f4, location)
I tried to specify the variable in the function, did not work either:
f5 :=(x) ->a+b*x+c*x^2
minimize(f5, location)
How should I do this? And, how would I do if I wanted over two variables, x and y?
fxy := a+b*x+c*x^2 + d*y^2 +e*y
f4 := a+b*x+c*x^2:
extrema(f4, {}, x);
/ 2\
|4 a c - b |
< ---------- >
| 4 c |
\ /
fxy := a+b*x+c*x^2 + d*y^2 +e*y:
extrema(fxy, {}, {x,y});
/ 2 2\
|4 a c d - b d - c e |
< --------------------- >
| 4 c d |
\ /
The nature of the extrema will depend upon the values of the parameters. For your first example above (quadratic in x) it will depend on the signum of c.
The command extrema accepts an optional fourth argument, such as an unassigned name (or an uneval-quoted name) to which is assigns the candidate solution points (as a side-effect of its calculation). Eg,
restart;
f4 := a+b*x+c*x^2:
extrema(f4, {}, x, 'cand');
2
4 a c - b
{----------}
4 c
cand;
b
{{x = - ---}}
2 c
fxy := a+b*x+c*x^2 + d*y^2 +e*y:
extrema(fxy, {}, {x,y}, 'cand');
2 2
4 a c d - b d - c e
{---------------------}
4 c d
cand;
b e
{{x = - ---, y = - ---}}
2 c 2 d
Alternatively, you may set up the partial derivatives and solve them manually. Note that for these two examples there is just a one result (for each) returned by solve.
restart:
f4 := a+b*x+c*x^2:
solve({diff(f4,x)},{x});
b
{x = - ---}
2 c
normal(eval(f4,%));
2
4 a c - b
----------
4 c
fxy := a+b*x+c*x^2 + d*y^2 +e*y:
solve({diff(fxy,x),diff(fxy,y)},{x,y});
b e
{x = - ---, y = - ---}
2 c 2 d
normal(eval(fxy,%));
2 2
4 a c d - b d - c e
---------------------
4 c d
The code for the extrema command can be viewed, by issuing the command showstat(extrema). You can see how it accounts for the case of solve returning multiple results.

Sum of two n bit numbers (n<=255) in 8085 microprocessor

LDA 2000h ; Store the no of byte that each number consists of in memory location 2000h.
MOV C A
MOV B A
LXI D 5000h
LXI H 2001h
STC
CMC
LOOP1: MOV A M
SHLD 7000h
LOOP: INX H
DCR C
JNZ LOOP
ADC M
STAX D
LHLD 7000h
LDA 2000h
MOV C A
INX D
INX H
DCR B
JNZ LOOP1
HLT
This is my code for the above mentioned problem. What would be more efficient algorithm to do so?

Which type of leftrotation is used by the MD5 hash algorithm?

In this piece of pseudocode from the MD5 Wikipedia website, fully available here, there is a pseudofunction leftrotate().
for each 512-bit chunk of message
break chunk into sixteen 32-bit words M[j], 0 ≤ j ≤ 15
//Initialize hash value for this chunk:
var int A := a0
var int B := b0
var int C := c0
var int D := d0
//Main loop:
for i from 0 to 63
if 0 ≤ i ≤ 15 then
F := (B and C) or ((not B) and D)
g := i
else if 16 ≤ i ≤ 31
F := (D and B) or ((not D) and C)
g := (5×i + 1) mod 16
else if 32 ≤ i ≤ 47
F := B xor C xor D
g := (3×i + 5) mod 16
else if 48 ≤ i ≤ 63
F := C xor (B or (not D))
g := (7×i) mod 16
dTemp := D
D := C
C := B
B := B + leftrotate((A + F + K[i] + M[g]), s[i])
A := dTemp
end for
//Add this chunk's hash to result so far:
a0 := a0 + A
b0 := b0 + B
c0 := c0 + C
d0 := d0 + D
end for
var char digest[16] := a0 append b0 append c0 append d0 //(Output is in little-endian)
//leftrotate function definition
leftrotate (x, c)
return (x << c) binary or (x >> (32-c));
However, is the leftrotate() function a logical rotate or a circular rotate? As when I looked up the function on the bitwise operations wikipedia I saw different leftrotations. Which one does the MD5 hashfunction use?
The rotation is defined on the first Wikipedia as:
leftrotate (x, c)
return (x << c) binary or (x >> (32-c));
On RFC 1321 the function is formulated differently, like so:
a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s)
Where s is the shift, but still I don't know what kind of leftrotate it is.
Found the answer while googling some other questions here
A bit rotation is also known as a circular shift rotation, extra information can be found here:
- Circular shift
- Bitwise operations
A circular shift shifts the numbers to left by an x amount, appending the extra ones back at the end.

NUL-byte between every other character in output

I'm using Ruby to read and then print a file to stdout, redirecting the output to a file in Windows PowerShell.
However, when I inspect the files, I get this for the input:
PS D:> head -n 1 .\inputfile
<text id="http://observer.guardian.co.uk/osm/story/0,,1009777,00.html"> <s> Hooligans NNS hooligan
, , , unbridled JJ unbridled passion NN passion
- : - and CC and no DT no executive JJ executiv
e boxes NNS box . SENT . </s>
... yet this for the output:
PS D:> head -n 1 .\outputfile
ÿ_< t e x t i d = " h t t p : / / o b s e r v e r . g u a r d i a n . c o . u k / o s m / s t o r y / 0 , , 1 0 0 9 7 7 7 , 0
0 . h t m l " > < s > H o o l i g a n s N N S h o o l i g a n , ,
, u n b r i d l e d J J u n b r i d l e d p a s s i o n N N p a s s i o n
- : - a n d C C a n d n o D T n o e x e c u t i v e J J
e x e c u t i v e b o x e s N N S b o x . S E N T . < / s >
How can this happen?
Edit: since my problem didn't have anything to do with Ruby, I've removed the Ruby-code, and included my usage of the Windows shell.
In PowerShell > is effectively the same as | Out-File and Out-File defaults to Unicode encoding. Try this instead of using >:
... | Out-File outputfile -encoding ASCII