Willsy Posted November 25, 2010 Share Posted November 25, 2010 Ok, here we go... +1 and +2 Which is greater? +2, obviously. Ok then: -1 and -2 Which is greater? There is no agreement here in the office! Some say -1 and some say -2. I'm leaning towards -1 being greater, but only when you imagine a conventional number line. If you don't care about 0, then you could say -2 is greater! To put this into a programming context... If I am counting backwards, say from -1 to -10 in a loop, which JMP instruction do I use? (I don't have access to my beloved Classic99 today, otherwise I would knock up some assembly to test it. But, essentially:) CLR R0 ; INITIAL VALUE LI R1,-1 ; INCREMENT VALUE LI R2,-50 ; END POINT AGAIN < DO SOME STUFF HERE > A R1,R0 ; GO BACKWARDS C R0,R2 ; COMPARE TO -50 JGT AGAIN ; LOOP IF NOT GREATER So, I want the code to exit when R0 (in this case) =-51 You are probably thinking "Why not just test for equality then you don't care about positive or negative numbers..." - true, but only true when the incrementor is 1 or -1. If it's 3 or -9 etc then you won't ever get equality, so you are reduced to looking for boundary crosses. This is connected with a nasty, evil bitch of a bug in +LOOP in TurboForth which I now have to correct, after major long-term procrastination. I have to fix it now, because I am just about to document the functionality! So, to summarise, where 'n' is positive, we can test for boundary crossing with JGT to exit a loop: LI R0,0 ; INITIAL VALUE LI R1,10 ; BOUNDARY LI R2,3 ; 'N' LOOP A R2,R0 ; ADD N TO VALUE C R0,R1 ; CROSSED THE BOUNDARY? JGT EXIT ; EXIT IF YES JMP LOOP ; OTHERWISE REPEAT EXIT .... So where N=3 as above, we would get 0 3 6 9 12 and out of the loop. But what about when we are negative? Do we use JGT or JLT? Quote Link to comment Share on other sites More sharing options...
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.