Announcement

Collapse
No announcement yet.

JA and JG Question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    JA and JG Question

    'Intel document and Google both say JA (jump if above) is for unsigned,
    'JG (jump if greater) is for signed.
    '
    '1 is above, AKA greater, than -1. But I get reverse of expected results.
    '(( two positives works as expected, for two negatives JA and JG both
    ' result in "wrong" (also unexpected)))
    '
    ' I missed, or misread, something somewhere.
    '
    Code:
    #compile exe
    #dim all
    #if %pb_cc32
      #console off
    #endif
    function pbmain () as long
      local hTWin as dword
      #register none
      txt.window ("JA vs JG", 100, 100, 30, 30) to hTWin
      ! mov ebx, -1&
      ! mov edi, 1&
      ! cmp ebx, edi
      ! ja Test_JA_Correct
        txt.print "Test JA wrong"
        ! jmp Test_JG
      Test_JA_Correct:
        txt.print "Test JA right"
      Test_JG:
      ! mov ebx, -1&
      ! mov edi, 1&
      ! cmp ebx, edi
      ! jg Test_JG_Correct
        txt.print "Test JG wrong"
        ! jmp Done
      Test_JG_Correct:
        txt.print "Test JG right"
      Done:
      txt.print
      txt.print "Done, any key to exit."
    
      txt.waitkey$
      txt.end
    end function '
    '
    'Thanks for useful comments.
    Dale

    #2
    Dale,

    If I understand your question correctly, JA "jump above" is for unsigned comparisons, JG "jump greater" is for signed comparison. It only matters when the number being tested is above 2 gig. Using JA is useful if you want a number range that is unsigned without hav ing to test negative numbers, -1 will be above the number range of say 1 to 1000 so you only have to test the top number in your range.
    hutch at movsd dot com
    The MASM Forum - SLL Modules and PB Libraries

    http://www.masm32.com/board/index.php?board=69.0

    Comment


      #3
      Thanks for the reply but . . .
      I have negatives to compare with each other as well as 1 to -1 and 0 to -1.

      ALL negative LONG numbers are larger than 2G when viewed as unsigned!
      (late add - inserted word LONG. 2G is incorrect for INTEGERs and QUADs)

      Does this mean JG does not take twos compliment into consideration while JA does??
      Last edited by Dale Yarker; 18 Nov 2022, 06:34 AM.
      Dale

      Comment


        #4
        Originally posted by Dale Yarker View Post
        Thanks for the reply but . . .
        I have negatives to compare with each other as well as 1 to -1 and 0 to -1.

        ALL negative numbers are larger than 2G when viewed as unsigned!

        Does this mean JG does not take twos compliment into consideration while JA does??
        If you have negatives to compare, why are you using an instruction (ja) that compare's UINTs (UNSIGNED integers i.e. DWORDS)

        Your code is not very clear, but it is working as documented.
        This should make it clearer what is happening:

        '
        Code:
        #COMPILE EXE
        #DIM ALL
        
        FUNCTION PBMAIN () AS LONG
          LOCAL hTWin AS DWORD
          #REGISTER NONE
          TXT.WINDOW ("JA vs JG", 100, 100, 30, 50) TO hTWin
          TXT.PRINT "Testja"
          ! mov ebx, -3&
          ! mov edi, 2&
          ! cmp ebx,edi
          ! ja Test_JA_Correct 'jump if DWORD ebx > DWORD edi
            TXT.PRINT "EBX not greater"
            ! jmp Test_JG
          Test_JA_Correct:
            TXT.PRINT "EBX greater (-3 unsigned is a BIG number!)"
            TXT.PRINT "TEstjg"
          Test_JG:
          ! mov ebx, -3&
          ! mov edi,  2&
          ! cmp ebx, edi
          ! jg Test_JG_Correct 'jump if LONG ebx > LONG edi
            TXT.PRINT "EBX not greater  -3 < 2"
            ! jmp Done
          Test_JG_Correct:
            TXT.PRINT "EBX Greater"
          Done:
          TXT.PRINT
          TXT.PRINT "Done, any key to exit."
        
          TXT.WAITKEY$
          TXT.END
        END FUNCTION
        '
        =========================
        https://camcopng.com
        =========================

        Comment


          #5
          Originally posted by Dale Yarker View Post
          Thanks for the reply but . . .
          I have negatives to compare with each other as well as 1 to -1 and 0 to -1.

          ALL negative numbers are larger than 2G when viewed as unsigned!

          Does this mean JG does not take twos compliment into consideration while JA does??
          Forget about "twos complement" and other fancy terms
          Just think of it as:
          When you put values into the two registers,
          JA treats them as two DWORDs (0 to 4,294,967,295) so any negative LONG that you stuff into the register will be greater than any positive LONG.
          ​JG treats them as LONGs (-2,147,483,648 to +2,147,483,647)

          =========================
          https://camcopng.com
          =========================

          Comment


            #6
            f you have negatives to compare, why are you using an instruction (ja) that compare's UINTs (UNSIGNED integers i.e. DWORDS)
            Only tried JA when JG didn't seem to be doing what I though it should.
            JG treats them as LONGs (-2,147,483,648 to +2,147,483,647)
            Yes it does. The reword of the txt.prints helped. I did indeed misread the Intel document description of CMP!

            Thanks,


            Dale

            Comment


              #7
              Dale,

              Something to note with the two conditional jumps, the data being compared is neither signed or unsigned, it is the conditional jump that determines if the data in the register or memory is signed or unsigned. You can load the register or memory with either a LONG or a DWORD in high level notation but the comparison is determined by the conditional jump, not the loaded high level data type.
              hutch at movsd dot com
              The MASM Forum - SLL Modules and PB Libraries

              http://www.masm32.com/board/index.php?board=69.0

              Comment


                #8
                Just to amplify slightly
                It's not a case of an arithmetic compare of signed or unsigned integers.

                Regardless of what has been put into the two registers, CMP does a binary subtraction of the contents of the second register from the contents of the first register and throws away the result. The subtraction may set various flags in the Flag Register based on what happens during the operation and the result. (Zero Flag,Carry Flag,Sign Flag and Overflow Flag aka ZF,CF,SF.OF)

                JA and JG will jump according to the status of some of these flags.
                JA jumps if ZF and CF are both zero.
                JG jumps if ZF is zero and SF=OF. (both 0 or both 1)

                =========================
                https://camcopng.com
                =========================

                Comment

                Working...
                X
                😀
                🥰
                🤢
                😎
                😡
                👍
                👎