C++ Multiplication in Assembly

Please support our Assembly advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2008
Posts: 31
Reputation: Spagett912 is an unknown quantity at this point 
Solved Threads: 0
Spagett912's Avatar
Spagett912 Spagett912 is offline Offline
Light Poster

C++ Multiplication in Assembly

 
0
  #1
May 8th, 2008
I'm trying to translate the following code from C++ into Asembly language but am having trouble. I know that I have to make a cmp and one or more jmp. If any1 has sugguestions or tips thats great. Here's the C++ coding:

  1. int x = 1 * 2;
  2. int y = 3 * 4;
  3.  
  4. cout << "x = " << x << endl;
  5. cout << "y = " << y << endl;
  6.  
  7. if (x == y)
  8. cout << "x and y are the same." << endl;
  9. else
  10. cout << "x and y are not the same." << endl;
  11.  
  12.  
  13. return 0;
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: C++ Multiplication in Assembly

 
0
  #2
May 9th, 2008
Is there anything wrong with the output of
g++ -S prog.cpp
Use it as a guide for your own attempt.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 11
Reputation: kermitaner is an unknown quantity at this point 
Solved Threads: 1
kermitaner kermitaner is offline Offline
Newbie Poster

Re: C++ Multiplication in Assembly

 
0
  #3
May 9th, 2008
  1. include /masm32/include/masm32rt.inc
  2. ;compile with masm32 console link & compile
  3. .data
  4. x dd 1*2
  5. y dd 3*4
  6. .code
  7. start:
  8. print "x="
  9. print str$(x),13,10
  10. print "y="
  11. print str$(y),13,10
  12. invoke cmpmem,addr x,addr y,sizeof x
  13. .if (eax!=0)
  14. print "equal",13,10
  15. .else
  16. print "not equal",13,10
  17. .endif
  18. exit
  19. end start

probably not what u expected ;-) but using a HL Macro Assembler, the difference to other languages ( c) is not so big as one would assume ....
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 31
Reputation: Spagett912 is an unknown quantity at this point 
Solved Threads: 0
Spagett912's Avatar
Spagett912 Spagett912 is offline Offline
Light Poster

Re: C++ Multiplication in Assembly

 
0
  #4
May 9th, 2008
Ummm, yeah that isn't really what I expected lol since I'm using the Kip Irvine files. Btw sorry I think I forgot to mention we can't use .IF statements lol.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: C++ Multiplication in Assembly

 
0
  #5
May 9th, 2008
> I'm using the Kip Irvine files.
And now you're only telling us this?

> I forgot to mention we can't use .IF statements lol.
More restrictions on the solution, is there anything else you should be telling us?

Post YOUR attempt.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 31
Reputation: Spagett912 is an unknown quantity at this point 
Solved Threads: 0
Spagett912's Avatar
Spagett912 Spagett912 is offline Offline
Light Poster

Re: C++ Multiplication in Assembly

 
0
  #6
May 10th, 2008
Wow i'm really sorry about all that. I was looking at a different program that I wasn't allowed to use .IF statements. So thanks for the help I'll check out what u gave me now.
Last edited by Spagett912; May 10th, 2008 at 3:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 31
Reputation: Spagett912 is an unknown quantity at this point 
Solved Threads: 0
Spagett912's Avatar
Spagett912 Spagett912 is offline Offline
Light Poster

Re: C++ Multiplication in Assembly

 
0
  #7
May 10th, 2008
Ok now the only problems i'm getting is that cmpmem is an undefined symbol and print is undefined as well. Finally i have this error unmatched block nesting : main
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 11
Reputation: kermitaner is an unknown quantity at this point 
Solved Threads: 1
kermitaner kermitaner is offline Offline
Newbie Poster

Re: C++ Multiplication in Assembly

 
0
  #8
May 10th, 2008
my code is for the original masm32 package, u can find some download links here
http://www.masm32.com/masmdl.htm
make sure to install in top level of ur drive, otherwise the standard include paths won't work.

there are two files: build.bat and buildc.bat in the /bin directory, the later is the correct one for console apps.

the print macro is a very useful macro, located in include file: /macros/macros.asm, see description in
help file: /help/hlhelp.hlp - Console Mode Macros - print

the cmpmem is a routine located in /include/masm32.inc and the corresponding library is
/lib/masm32.lib ,
see description in /help/masmlib.hlp - Memory Functions - cmpmem
( both functions/macros are available through the
include /masm32/include/masm32rt.inc
line only )


it might be a good idea to check some step by step assembly tutorials first, before getting too deep into some programming tasks to early.
though i don't think that assembler is much more difficult than other programming languages, it sure is different and the learning curve in the beginning takes more time than with other languages.
its better to spend more time with the basics in the beginning, than wasting too much time with error fixing when u take the second step before the first...
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 31
Reputation: Spagett912 is an unknown quantity at this point 
Solved Threads: 0
Spagett912's Avatar
Spagett912 Spagett912 is offline Offline
Light Poster

Re: C++ Multiplication in Assembly

 
0
  #9
May 10th, 2008
Is there some other way to output information other than using the "Print" command?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 11
Reputation: kermitaner is an unknown quantity at this point 
Solved Threads: 1
kermitaner kermitaner is offline Offline
Newbie Poster

Re: C++ Multiplication in Assembly

 
0
  #10
May 11th, 2008
u can try:
invoke crt_printf,addr msg
or with the irvine libs:
;lea edx,msg
;call Writestring
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2159 | Replies: 10
Thread Tools Search this Thread



Tag cloud for Assembly
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC