
February 22nd, 2008, 11:53 AM
|
|
Registered User
|
|
Join Date: Feb 2008
Posts: 2
Time spent in forums: 45 m 16 sec
Reputation Power: 0
|
|
|
Translate a C++ program to Pep/8 Assembly language
For this program, I have to translate the following C++ program to Peop/8 Assembly language. This program multiplies two integers using a recursive shift-and-add algorithm
Code:
#include <iostream>
using namespace std;
int times(int mpr, int mcand)
{
if (mpr==0)
{return 0;}
else if (mpr%2 ==1)
{
return mcand+times (mpr /2 , mcand*2);
}
else
{return times (mpr/2, mcand*2);
}
}
int main()
{
int n,m;
cout<<"enter n and m"<<endl;
cin>>n>>m;
cout<<"Product: "<<times (n,m)<<endl;
return 0;
}
Here is what I have
Code:
BR main
n: .EQUATE 2
m: .EQUATE 4
retVal: .EQUATE 6
;
;
main: DECI -6,s ; cin>>n;
DECI -4,s ; cin>>m;
SUBSP 6, i ; push params and retVal
CALL times ;
DECO retVal, s ; cout<<times
STOP
times: LDA 2, s ; load n
BRNE elseIf ;else
LDA retVal, s ; A:=0, if(mpr==0)
ADDA 0, i
STA retVal ,s ; store 0 to return value
RET0;
elseIf: LDA n, s ; load mpr
ASRA ; mpr/2
SUBA n,s ; substract to find mpr % 2
CPA 1, i ; if (mpr%2 == 1)
BRNE else ;otherwise, go to else
LDA n, s ;load mpr
ASRA ;mpr/2
STA -8,s ;store mpr/2
LDA m,s ;load mcand
ASLA ;mcand*2
STA -6,s ;store mcand* 2
SUBSP 8, i ;push params and retVal
LDA retVal, s ; load retVal to s
ADDA 4, s ;add mcand to it
STA retVal,s
CALL times ;times (mpr/2, mcand*2)
RET8;
else: LDA n, s ;load mpr
ASRA ;mpr/2
STA -8,s ;store mpr/2
LDA m,s ;load mcand
ASLA ;mcand*2
STA -6,s ;store mcand* 2
SUBSP 8, i ;push params and retVal
CALL times ;times (mpr/2, mcand*2)
RET8;
.END;
My output is always 0. I don't quite understand the concept of CALL and return. Any help would be truly appreciated.
|