C++ to ASM

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2005
Posts: 15
Reputation: heavyc is an unknown quantity at this point 
Solved Threads: 0
heavyc heavyc is offline Offline
Newbie Poster

C++ to ASM

 
0
  #1
Dec 7th, 2005
I have a problem i have a fully operational C++ code that works but when i convert it to ASM (assembly) code it doesnt work I dont know where I am going wrong but here are both codes..
  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int n, tmpA, tmpB
  8. int CO[0] = { 0 };
  9. cin >> n;
  10. CO[0] = 1;
  11.  
  12. for( int = 2; i < (n + 3); i++)
  13. {
  14. tmpB = CO[k] + CO[k + 1];
  15. CO[k] = tmpA;
  16. cout << " " << tmpA;
  17. tmpA = tmpB;
  18. }
  19. cout << endl;
  20. }
  21. return 0;
  22. }

And here is the ASM code I hope this is the right place where people know how to program in ASM??

  1. TITLE Combinatorial Coefficents
  2. INCLUDE Irvine32.inc
  3.  
  4. .data
  5. CO DWORD 40 DUP(0)
  6. mystr1 BYTE "Please enter a positive number from 1 to 40: ", 0
  7.  
  8. i DWORD ?
  9. k DWORD ?
  10. n DWORD ?
  11. tmpA DWORD ?
  12. tmpB DWORD ?
  13. .code
  14. main PROC
  15. mov edx, OFFSET mystr1
  16. call WriteString
  17. call CrLf
  18.  
  19. call ReadInt
  20. mov n, eax
  21. mov ebx, CO[0]
  22. mov ebx, 1
  23.  
  24.  
  25.  
  26. LL:
  27. mov edx, 2 ; i = 2
  28. add n, 3 ; n + 3
  29. cmp edx, n
  30. jae QX
  31.  
  32.  
  33. mov tmpA, 1 ; tmpA = 1
  34.  
  35.  
  36.  
  37. LX:
  38. mov k, 0
  39. ;mov edx, i
  40. dec edx ; i - 1
  41. cmp k, edx
  42. jae QX
  43.  
  44. mov esi, OFFSET CO
  45. shl esi, 2
  46.  
  47. mov ebx, CO[esi]
  48. add ebx, CO[esi + 4]
  49. mov eax, tmpA
  50. mov eax, CO[esi]
  51. ;mov tmpA, eax
  52. ;mov eax, tmpA
  53. call WriteDec
  54. call CrLf
  55. mov eax, ebx
  56. ; mov tmpA, eax
  57. inc k
  58. inc edx
  59. jmp LL
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71. QX:
  72.  
  73. exit
  74. main ENDP
  75. END main
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 78
Reputation: perniciosus is an unknown quantity at this point 
Solved Threads: 4
perniciosus's Avatar
perniciosus perniciosus is offline Offline
Junior Poster in Training

Re: C++ to ASM

 
0
  #2
Dec 7th, 2005
IANAAP (I Am Not An Assember Programmer) but, some thoughts:
Feels like there is as many ways to write assembler as there are assemblers thought... What is the problem ?

Argh, whats up with the k variable in the c++ example (not declared) ? and counting goes in different directions (down in asm, up in c++)
What you do seem to be doing is to set edx to 2 and count down to zero, that cant be right... (Edit seems you are counting both edx and k up at the end, first edx - 1 + 1 each round, and k = 0 + 1 each round... I got a head ache right now so now I'll stop reading this)
Even worse you loop through the loop initation all the time (LL)...
(this looks odd)
  1. mov eax, tmpA
  2. mov eax, CO[esi]
And where did the swap/save between tmpA and tmpB go ?

And I have no idea about the included functions, do they preserv all registers, what arguments do they take, and so on...
/pern.*/i

Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. Albert Einstein
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,578
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1486
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: C++ to ASM

 
0
  #3
Dec 7th, 2005
  1. int CO[0] = { 0 };
The above is not a valid array -- its an array of 0 elements. that means "CO[0] = 1;" will produce undefined behavior because CO[0] does not exist.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 15
Reputation: heavyc is an unknown quantity at this point 
Solved Threads: 0
heavyc heavyc is offline Offline
Newbie Poster

Re: C++ to ASM

 
0
  #4
Dec 8th, 2005
thanks for taking an intrest in the code, but i went to my teacher and i actually figured it out again thank you for taking an intrest
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC