0

I understand that this code could probably look a lot cleaner, and be more efficient, but I am using only the code that has been provided so far in our book. I am trying to create a 2D array, have the user input a salesman number(row number), quantity sold(value), and product id(column number). Once they are finished they enter a value of -1. It should show each salesman's total sales (row totals), and each products total sales (column totals). I can enter information for the first row just fine, but the program freezes when I try to enter information for Salesman 2, 3, or 4. Suggestions?? Here is the portion of the code that I believe has the problem.

    L1: mov     edx, OFFSET msg1
        call    WriteString
        call    ReadInt
        .WHILE(eax != quit)
            mov     ebx, OFFSET tableE
            dec     eax
            add     ebx, RowSizeE
            imul    ebx, eax
            call    Crlf
            mov     edx, OFFSET msg2
            call    WriteString
            call    ReadDec
            mov     ecx, eax
            call    Crlf
            mov     edx, OFFSET msg3
            call    WriteString
            call    ReadDec
            mov     esi, eax
            dec     esi
            mov     tableE[ebx + esi*TYPE tableE], ecx
            call    Crlf
            mov     eax, 0
            mov     ebx, 0
            mov     edx, 0
            mov     ecx, 0
            mov     esi, 0
            loop    L1
        .ENDW
  • As a courtesy, if you see your question comes out with broken formatting please fix it yourself (also use the preview while editing). Also, comment your code especially if you want others to help. Last but not least, provide details, don't just say that the program has an error. – Jester Apr 25 '16 at 23:06
  • This is the opposite of a minimal complete example that demonstrates your problem. This is your whole program. Keep taking stuff out until you have a program that's as small as possible (and doesn't do much), but still has the same problem. You'll probably find your bug along the way while taking stuff out, esp. if you're using a debugger to examine registers/memory and see if the problem is still present. – Peter Cordes Apr 25 '16 at 23:47
  • 1
    Thanks for shrinking the code, but there are still no comments explaining what you you want it to be doing at each step, or why. Anyway, zeroing `ecx` right before a [`loop` instruction](http://stackoverflow.com/questions/35742570/why-is-the-loop-instruction-slow-couldnt-intel-have-implemented-it-efficiently) is probably a bug. – Peter Cordes Apr 26 '16 at 00:58

1 Answers1

1

Two problems in your program.

  • You're trying to loop 4 times (because there are 4 salesmen) but nowhere in the program do you setup such counter in ECX. Worse, you even manage to zero the ECX register right before the loop instruction (as noted by @PeterCordes).

        mov     ecx, 4            ;NEW
    L1: mov     edx, OFFSET msg1
        call    WriteString
        call    ReadInt
        .WHILE(eax != quit)
           push    ecx            ;NEW
           mov     ebx, OFFSET tableE
           ...
           mov     tableE[ebx + esi*TYPE tableE], ecx
           call    Crlf
           xor     eax, eax       \
           xor     ebx, ebx        | Better way to zero these registers
           xor     edx, edx        |
           xor     esi, esi       /
           pop     ecx            ;NEW
           loop    L1
        .ENDW
    
  • You initialize the EBX register wrongly. This is the correct way:

    dec     eax                   ;Decrement salesman number [1,4] -> [0,3]
    imul    eax, RowSizeE
    mov     ebx, eax
    
Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • ew, push/pop of the loop counter inside the loop? Just use a different register for the loop counter, and don't use `loop`. It also seems unlikely that zeroing four integer regs every iteration is actually necessary. – Peter Cordes May 02 '16 at 04:43