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, 11: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, 02:47 PM
Bobidybob's Avatar
Bobidybob Bobidybob is offline
Contributing Abuser
Click here for more information
 
Join Date: Apr 2007
Location: Starkville, MS
Posts: 318 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 Days 10 h 22 m 49 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 02:53 PM.

Reply With Quote
  #3  
Old May 23rd, 2009, 02: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!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

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




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
Stay green...Green IT