EASy68K  
It is currently Thu Sep 19, 2019 5:07 am

All times are UTC




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: BHI VS. BGT
PostPosted: Fri Dec 16, 2011 3:39 pm 
Offline

Joined: Fri Dec 16, 2011 12:32 am
Posts: 17
If I understand correctly, BHI is used when we are working with unsigned numbers and BGT is used when we are working with signed numbers. Which one should we use if we do CMP 4,6 that is 6-4=-2?BHI since both numbers are positive and unsigned or BGT since the result is negative? When choosing BHI or BGT do we just look at the range of numbers we will be working with or do we have to take into consideration the result also?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2011 5:20 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1102
The numbers being compared are the key.

If the any of the numbers being compared might contain a negative number then use signed compares. If the numbers being compared contain only unsigned numbers then use unsigned compares.

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2011 7:47 pm 
Offline

Joined: Fri Dec 16, 2011 12:32 am
Posts: 17
profkelly wrote:
The numbers being compared are the key.

If the any of the numbers being compared might contain a negative number then use signed compares. If the numbers being compared contain only unsigned numbers then use unsigned compares.


Oh I see, thanks!

I would appreciate it if someone could also explain why it is that BGT branches when NVZ* + N*V*Z* = 1.

Can someone explain BGT as someone explained BHI to me in this manner:
BHI is true if C*Z*=1:
Subtracting a larger number from a smaller number will result in a negative number, which will set the carry flag. Subtracting equal numbers will result in 0, which will set the zero flag.

A number that is higher is neither negative nor zero, so both flags are cleared.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 16, 2011 10:54 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1102
BGT Brances if N and V are equal and Z=0.

If the numbers are the same then Z=1.
For all other cases:
Comparing a larger number to a smaller number when both are the same sign will result in N=0, V=0.
Comparing a positive number to a negative number will result in N=1, V=1

Code:
; Compare 127 to 100
    move.b  #127,d0     ; larger positive
    sub.b   #100,d0     ; minus smaller positive, N=0, V=0, Z=0
 
; Compare 100 to 127   
    move.b  #100,d0     ; smaller positive
    sub.b   #127,d0     ; minus larger positive,  N=1, V=0, Z=0

; Compare -100 to -127
    move.b  #-100,d0    ; larger negative
    sub.b   #-127,d0    ; minus smaller negative, N=0, V=0, Z=0

; Compare -127 to -100
    move.b  #-127,d0    ; smaller negative
    sub.b   #-100,d0    ; minus larger negative,  N=1, V=0, Z=0

; Compare -100 to 100
    move.b  #-100,d0    ; smaller negative
    sub.b   #100,d0     ; minus larger positive,  N=0, V=1, Z=0
   
; Compare 100 to -100
    move.b  #100,d0     ; larger positive
    sub.b   #-100,d0    ; minus smaller negative, N=1, V=1, Z=0

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 19, 2011 7:34 am 
Offline

Joined: Fri Dec 16, 2011 12:32 am
Posts: 17
profkelly wrote:
BGT Brances if N and V are equal and Z=0.

If the numbers are the same then Z=1.
For all other cases:
Comparing a larger number to a smaller number when both are the same sign will result in N=0, V=0.
Comparing a positive number to a negative number will result in N=1, V=1

Code:
; Compare 127 to 100
    move.b  #127,d0     ; larger positive
    sub.b   #100,d0     ; minus smaller positive, N=0, V=0, Z=0
 
; Compare 100 to 127   
    move.b  #100,d0     ; smaller positive
    sub.b   #127,d0     ; minus larger positive,  N=1, V=0, Z=0

; Compare -100 to -127
    move.b  #-100,d0    ; larger negative
    sub.b   #-127,d0    ; minus smaller negative, N=0, V=0, Z=0

; Compare -127 to -100
    move.b  #-127,d0    ; smaller negative
    sub.b   #-100,d0    ; minus larger negative,  N=1, V=0, Z=0

; Compare -100 to 100
    move.b  #-100,d0    ; smaller negative
    sub.b   #100,d0     ; minus larger positive,  N=0, V=1, Z=0
   
; Compare 100 to -100
    move.b  #100,d0     ; larger positive
    sub.b   #-100,d0    ; minus smaller negative, N=1, V=1, Z=0


Thanks for answering my question. I don't understand two things.

Code:
; Compare -100 to 100
    move.b  #-100,d0    ; smaller negative
    sub.b   #100,d0     ; minus larger positive,  N=0, V=1, Z=0[/quote]


Why N=0 and V=1? Why isn't N=1? -100 - 100 is -200 which is a negative number.

Code:
; Compare 100 to -100
    move.b  #100,d0     ; larger positive
    sub.b   #-100,d0    ; minus smaller negative, N=1, V=1, Z=0


I don't understand why N=1 and V=1? 100-[-100]=200 which is positive.

I did run everything through the simulator and you are correct. But I am working everything out on my own on paper and these two things aren't clear to me.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 19, 2011 10:06 am 
Offline

Joined: Mon Dec 27, 2004 11:40 pm
Posts: 318
The range of a signed byte is -128, %10000000, to + 127, %01111111

Quote:
Why N=0 and V=1? Why isn't N=1? -100 - 100 is -200 which is a negative number.

-200 won't fit in a byte as a signed number and wraps round to +56

Quote:
I don't understand why N=1 and V=1? 100-[-100]=200 which is positive.

+200 won't fit in a byte as a signed number and wraps round to -56

Lee


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 19, 2011 1:50 pm 
Offline
User avatar

Joined: Thu Dec 16, 2004 6:42 pm
Posts: 1102
The "wrap around" to a number that is out of the range of signed numbers is indicated by V=1.

_________________
Prof. Kelly


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2011 9:36 am 
Offline

Joined: Fri Dec 16, 2011 12:32 am
Posts: 17
profkelly wrote:
The "wrap around" to a number that is out of the range of signed numbers is indicated by V=1.


I worked all of it out on paper, everything makes sense. Thanks a lot to both of you for clearing up this concept for me!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: Bing [Bot] and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group