JA and JG Question PowerBASIC Peer Support Community (original) (raw)
Community
You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
-
- Join Date: Mar 2004
- Posts: 6251
JA and JG Question
18 Nov 2022, 02:04 AM
'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
*
Member
* Join Date: Oct 1999
* Posts: 5396
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.
## Comment
*
Member
* Join Date: Mar 2004
* Posts: 6251
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
*
Member
* Join Date: Mar 2000
* Posts: 11083
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
'
## Comment
*
Member
* Join Date: Mar 2000
* Posts: 11083
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)
## Comment
*
Member
* Join Date: Mar 2004
* Posts: 6251
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
*
Member
* Join Date: Oct 1999
* Posts: 5396
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.
## Comment
*
Member
* Join Date: Mar 2000
* Posts: 11083
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)
Comment
Copyright © 1998-2023 PowerBASIC Tools, LLC - All Rights Reserved.
Powered by vBulletin® Version 6.1.2
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.
All times are GMT-5. This page was generated at 03:04 AM.
Working...
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.