|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
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. |
|
#2
|
||||
|
||||
|
now, I have NO idea how pep/8 works, but if it's similar to other assembly languages, when you do a call, it puts the next instruction after the call on the stack. so when you do a return, it will return to that instruction. for example:
Code:
blah: code 1 code 2 call perfect code 3 code 4 goto end perfect: code 5 code 6 ret code 7 code 8 end: code 9 in the above code, the order of code executed would be 1,2,5,6,3,4,9. at least thats how i remember assembly working. I was never really fond of assembly languages... If anyone more familiar with assembly wants to correct me, feel free ![]() but if i'm right and pep/8 is similar to the assembly i've worked with, then that should be how the return works. It doesnt return a value, but returns you back to where you had called the subroutine from.
__________________
Last edited by Bobidybob : February 22nd, 2008 at 01:53 PM. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > General Programming Help > Translate a C++ program to Pep/8 Assembly language |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|