954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ to ASM

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..

#include <iostream>
using namespace std;

int main()
{
       int n, tmpA, tmpB
       int CO[0] = { 0 };
cin >> n;
CO[0] = 1;

for( int = 2; i < (n + 3); i++)
{
     tmpB = CO[k] + CO[k + 1];
     CO[k] = tmpA;
     cout << " " << tmpA;
     tmpA = tmpB;
}
cout  << endl;
}
return 0;
}


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

TITLE Combinatorial Coefficents
INCLUDE Irvine32.inc

.data
CO DWORD 40 DUP(0)
mystr1 BYTE "Please enter a positive number from 1 to 40: ", 0

i DWORD ?
k DWORD ?
n DWORD ?
tmpA DWORD ?
tmpB DWORD ?
.code 
main PROC
mov edx, OFFSET mystr1
call WriteString
call CrLf

call ReadInt
mov n, eax
mov ebx, CO[0] 
mov ebx, 1

    
 
LL:  
    mov  edx, 2  ; i = 2
    add  n, 3  ; n + 3 
    cmp edx, n
    jae QX
          
    
    mov tmpA, 1 ; tmpA = 1
    
    
       
LX: 
     mov k, 0
                                    ;mov edx, i
    dec edx ; i - 1
    cmp k, edx
    jae QX
    
    mov esi, OFFSET CO
    shl esi, 2
    
    mov ebx, CO[esi]
    add ebx, CO[esi + 4]
    mov eax, tmpA
    mov eax, CO[esi]
                                     ;mov tmpA, eax
                                     ;mov eax, tmpA 
    call WriteDec
    call CrLf
    mov eax, ebx
                                     ; mov tmpA, eax
    inc k
    inc edx
    jmp LL
    


    
    
   
    



   
QX:

exit
main ENDP
END main
heavyc
Newbie Poster
15 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

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)

mov eax, tmpA
    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...

perniciosus
Junior Poster in Training
78 posts since Nov 2005
Reputation Points: 29
Solved Threads: 4
 
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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

heavyc
Newbie Poster
15 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You