Jump to content

Ethiopian multiplication: Difference between revisions

added MMIX example
(added MMIX example)
Line 758:
show( (17 ethiopicmult 34) );
end</lang>
 
=={{header|MMIX}}==
 
In order to assemble and run this program you'll have to install MMIXware from [http://www-cs-faculty.stanford.edu/~knuth/mmix-news.html]. This provides you with a simple assembler, a simulator, example programs and full documentation.
 
<lang mmix>A IS 17
B IS 34
 
pliar IS $255 % designating main registers
pliand GREG
acc GREG
str IS pliar % reuse reg $255 for printing
 
LOC Data_Segment
GREG @
BUF OCTA #3030303030303030 % reserve a buffer that's big enough to hold
OCTA #3030303030303030 % a max (signed) 64 bit integer:
OCTA #3030300a00000000 % 2^63 - 1 = 9223372036854775807
% string is terminated with NL, 0
 
LOC #1000 % locate program at address
% Main is the entry point of the program
Main SET pliar,A % initialize registers for calculation
SET pliand,B
SET acc,0
1H BEV pliar,2F % if pliar is even skip incr. acc with pliand
ADD acc,acc,pliand %
2H SR pliar,pliar,1 % halve pliar
SL pliand,pliand,1 % and double pliand
PBNZ pliar,1B % repeat from 1H while pliar > 0
// result: acc = 17 x 34
// next: print result --> stdout
// $0 and $1 are temp registers
LDA str,BUF+19 % points after the end of the string
2H SUB str,str,1 % update buffer pointer
DIV acc,acc,10 % do a divide and mod
GET $0,rR % get digit from special pupose reg. rR
% containing the remainder of the division
INCL $0,'0' % convert to ascii
STBU $0,str % place digit in buffer
PBNZ acc,2B % next
% 'str' points to the start of the result
TRAP 0,Fputs,StdOut % output answer to stdout
TRAP 0,Halt,0 % exit</lang>
Assembling:
<pre>~/MIX/MMIX/Progs> mmixal ethiopianmult.mms</pre>
Running:
<pre>~/MIX/MMIX/Progs> mmix ethiopianmult
578</pre>
 
=={{header|Modula-3}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.