CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11

Thread: logical error

  1. #1
    Join Date
    Nov 2005
    Posts
    79

    logical error

    Hi I have this program that counts the values of a table. All values are initial at 1.
    I use the assembler masm 6.11

    The problem I have is that the program does not go through the loop 10 times after the first iteration, it exits. I don see what is wrong can someone see the problem?
    Code:
    ;
    ; register usage: BX= pointer to table.
    ;                 CX= counter
    ;                 AX= current partial sum
    ;
    ;
    ;
      TITLE ADDTABLE
      .MODEL SMALL        ;conventions- small model
    ;
    ; stack initialization
    ;
     .stack 100H          ; Allocate 256 bytes stack
    ;
    ;
    ;   Local data
    ;
      .DATA
    COUNTER    DW 10          ;length of table
    TABLE          DW 100 DUP(1)  ;Reserve storage for table
    RESULT        DW ?           ;sum of entries
    ;
    ;   Code Segment
    ;
    ;
      .CODE
    START:
           MOV  AX, @DATA      ;Load segment register
           MOV  DS, AX         ;Into DS register
    ;
    ;      Initialization area
    ;
           MOV  BX, OFFSET TABLE   ; initialize PTR to top of table
           MOV  CX, COUNTER        ; initialize counter to # of entries
           XOR  AX,AX                     ; clear current sum
    ;
    ; Loop to add table entries
    ;
    ADDLOP:
           ADD  AX,WORD PTR [BX]  ; ADD NEXT ENTRY
           INC  BX                ;Point to next entry by incrementing
           INC  BX
           LOOPNZ ADDLOP           ;If not, add next entry
    ;
    ;     DONE, store sum and Exit
    ;
           MOV  RESULT, AX         ;store SUM
           MOV  AX, 4C00H      ;InA and call DOS
    ;
           END START
    Thank you

  2. #2
    Join Date
    Feb 2009
    Posts
    2

    Re: logical error

    The problem is that you're using LOOPNZ.

  3. #3
    Join Date
    Apr 2003
    Posts
    1,755

    Re: logical error

    I don't see any problem in your code. The LOOPNZ should be ok.

    How are you debuging ung program? Are you using debug.exe? Maybe you are using "p" (proceed) instead of "t" (trace) that is why you cant see the loop in action.

    If you don't care about the value of zero flag and loop only until CX == 0, simply use LOOP.

    I'm not sure though.

  4. #4
    Join Date
    Feb 2009
    Posts
    2

    Re: logical error

    Why do you think LOOPNZ is OK? (it's not)

  5. #5
    Join Date
    Apr 2003
    Posts
    1,755

    Re: logical error

    Quote Originally Posted by Slicerwizard View Post
    Why do you think LOOPNZ is OK? (it's not)
    LOOPNZ means loop "WHILE CX <> 0 AND ZERO FLAG IS CLEAR". Within the loop (up to 10), the ZERO FLAG IS ALWAYS CLEAR so the NZ is always true. In effect it's just like a simple LOOP

    I'll ask you the same question. What makes you think it's wrong? If it's not LOOPNZ or LOOP then what should it be?
    Last edited by rxbagain; March 1st, 2009 at 05:57 AM.

  6. #6
    Join Date
    Mar 2004
    Location
    KL, Malaysia
    Posts
    63

    Re: logical error

    LOOPNZ will continue the loop as long as CX is not zero OR the zero condition is not set.

    So for your case, just use LOOP, which will drop to next instruction when CX equals 0.

  7. #7
    Join Date
    Apr 2003
    Posts
    1,755

    Re: logical error

    Quote Originally Posted by ckweius View Post
    LOOPNZ will continue the loop as long as CX is not zero OR the zero condition is not set.

    So for your case, just use LOOP, which will drop to next instruction when CX equals 0.
    Just to make it clear "LOOPNZ will continue the loop as long as CX is not zero AND the zero condition is not set" not "LOOPNZ will continue the loop as long as CX is not zero OR the zero condition is not set"

    If you say OR, it will continue the loop even if CX has already rolled over as long as "the zero condition is not set".

    All these LOOP, LOOPNZ and LOOPZ will stop whenever CX becomes 0. The only difference is that LOOPNZ and LOOPZ can terminate earlier depending on the ZERO FLAG. LOOPNZ will terminate earlier when "Zero flag is set". while the LOOPZ will terminate earlier when "Zero flag is clear".

    But ckweius is right, you dont need to check the value of Zero flag so you can just use a simple LOOP (although LOOPNZ should be no problem in your case)

    Edit: I attached the snapshot how I tested the code using debug and verified that LOOPNZ is working.
    Attached Images Attached Images
    Last edited by rxbagain; March 1st, 2009 at 11:41 AM.

  8. #8
    Join Date
    Mar 2004
    Location
    KL, Malaysia
    Posts
    63

    Re: logical error

    Quote Originally Posted by rxbagain View Post
    Just to make it clear "LOOPNZ will continue the loop as long as CX is not zero AND the zero condition is not set" not "LOOPNZ will continue the loop as long as CX is not zero OR the zero condition is not set"

    If you say OR, it will continue the loop even if CX has already rolled over as long as "the zero condition is not set".
    No, I am pretty sure that LOOPNZ will continue the loop as long as CX is not zero OR the zero condition is not set. Read this

  9. #9
    Join Date
    Apr 2003
    Posts
    1,755

    Re: logical error

    Read carefully the link you gave. It says there "continues to the next instruction (to exit the loop)" NOT "continue the loop". When we say "continue the loop", It should mean "to continue to iterate" not to end the iteration.

    LOOPNZ Loop While Not Zero; Intel 80x86; used to implement DO loops, WHILE loops, UNTIL loops, and similar constructs, decrements the ECX or CX (count) register and then tests to see if it is zero, if the ECX or CX register is zero or the Zero Flag is set (one) then the program continues to the next instruction (to exit the loop), otherwise the program makes a byte branch (to continue the loop); equivalent to LOOPNE; does not modify flags
    You must be trying to say the "otherwise" part so you should also change the "OR" to "AND" in your statement
    Code:
    LOOPNZ will continue the loop as long as CX is not zero AND the Zero Flag is not set.
    Try to debug the code and see how the LOOPNZ works.
    Last edited by rxbagain; March 1st, 2009 at 11:29 PM.

  10. #10
    Join Date
    Apr 2003
    Posts
    1,755

    Re: logical error

    Here's another debug screen I made. I used "p" (proceed) in this case. Take a look of the values of AX, BX and CX. Although you can't see the loop going back to "ADD AX, [BX]", you can tell by the values of the registers that 10 iterations was performed properly. Maybe this is how brad debugs his code and he is wondering why the loop did not jump to "ADD AX, [BX]".

    Unless brad has some other code within the loop (if he omitted some code) that could affect the zero flag when LOOPNZ is encountered, his code should work fine.
    Attached Images Attached Images

  11. #11
    Join Date
    Mar 2004
    Location
    KL, Malaysia
    Posts
    63

    Re: logical error

    I have done the code trace, rxbagain is correct.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured