944,123 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3668
  • C++ RSS
Dec 7th, 2005
0

C++ to ASM

Expand Post »
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..
C++ Syntax (Toggle Plain Text)
  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??

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
heavyc is offline Offline
15 posts
since Sep 2005
Dec 7th, 2005
0

Re: C++ to ASM

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)
C++ Syntax (Toggle Plain Text)
  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...
Reputation Points: 29
Solved Threads: 4
Junior Poster in Training
perniciosus is offline Offline
78 posts
since Nov 2005
Dec 7th, 2005
0

Re: C++ to ASM

C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Dec 8th, 2005
0

Re: C++ to ASM

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
heavyc is offline Offline
15 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Easy Polymorphism Q Part 2 :)
Next Thread in C++ Forum Timeline: I guess it's a question about a loop





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC