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

Dani AI

Generated

Common reasons a working C++ loop breaks when rewritten in MASM/Irvine32 are straightforward and repeatable: incorrect array sizing or indexing, wrong byte-scaling when forming element addresses, reinitializing loop counters inside the loop, and losing temporary values (or clobbering registers) across calls. ’s point about the array declaration is the most critical first check — the data structure must actually exist and have the correct element size before any indexing happens.

A practical checklist that covers the gaps left by the replies:

  • Make the C++ version compile and run first, then translate each single operation (init, index, load, store, compare, jump) one at a time into assembly.
  • Treat indexes in bytes: element_address = base_address + (index * element_size). Do not shift the array base — scale the index, then add it to the base.
  • Initialize loop counters and limits once before entering the loop; don’t reset them inside the loop body. Keep increment/decrement consistent with the compare/jump. was right to flag mixed counting directions and repeated initialization.
  • Preserve temporaries and be explicit about where each variable lives (register, stack, or memory). If a library routine is called, consult its docs for which registers it may clobber; save/restore anything you need afterward.
  • Watch array bounds: MASM won’t protect you from out-of-range writes; a single bad store can silently corrupt control flow.

Debugging workflow suggestions: add short, verifiable checks (print an element after each update or dump the array slice), use a debugger to inspect memory addresses and register contents, and if unsure how a compiler translates a construct, compile the C++ to assembly and compare the generated sequence. Finally, note that reported the problem was resolved after a walkthrough with an instructor — the core lessons above are the ones that typically fix these translation errors.

Recommended Answers

All 3 Replies

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.