General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 22nd, 2008, 10:53 AM
PC2008 PC2008 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 2 PC2008 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old February 22nd, 2008, 01:47 PM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 319 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 10 h 59 m 13 sec
Reputation Power: 3
Send a message via AIM to Bobidybob
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.

Reply With Quote
  #3  
Old May 23rd, 2009, 01:20 AM
huequanhuynh huequanhuynh is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 1 huequanhuynh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 12 m 14 sec
Reputation Power: 0
Talking

Quote:
Originally Posted by PC2008
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 n, s		; load n	
	BRNE elseIf;else		; A:=0, if(mpr==0)
	ADDA 0, i
	STA -2 ,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.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Translate a C++ program to Pep/8 Assembly language


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek