General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Iron Speed
 
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:
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  
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: 180 Bobidybob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 1 h 30 m 14 sec
Reputation Power: 2
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
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!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five &quot;checkpoints&quot; for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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

Iron Speed




© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway