Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Maarten Brock
03/24/12 01:25
Read: 486 times
Tilburg
The Netherlands


 
#186851 - Both Carry and MSB
Responding to: ???'s previous message
No both the carry flag and the most significant bit must be examined after subtraction of signed integers. A better solution is to offset both operands into unsigned space before subtracting by 'adding' 0x8000. Here's what SDCC generates:

;------------------------------------------------------------
;Allocation info for local variables in function 'sub'
;------------------------------------------------------------
;y                         Allocated with name '_sub_PARM_2'
;x                         Allocated to registers r6 r7 
;------------------------------------------------------------
; sub.c:1: char sub (int x, int y)
; -----------------------------------------
;  function sub
; -----------------------------------------
_sub:
        mov     r6,dpl
        mov     r7,dph
;       sub.c:3: return x < y;
        clr     c
        mov     a,r6
        subb    a,_sub_PARM_2
        mov     a,r7
        xrl     a,#0x80
        mov     b,(_sub_PARM_2 + 1)
        xrl     b,#0x80
        subb    a,b
        clr     a
        rlc     a
        mov     dpl,a
        ret

 

And the zero flag only reflects the most sigificant byte. For equality checking you'd better use CJNE.

List of 5 messages in thread
TopicAuthorDate
16 bit vs 8bit signed compare      Slavko Kocjancic      03/23/12 16:05      
   8051 arithmetic 16 bit signed integer      KONSTANTINOS L. ANGELIS      03/23/12 17:15      
      The answer is MSB bit      Slavko Kocjancic      03/24/12 00:28      
         Both Carry and MSB      Maarten Brock      03/24/12 01:25      
   < Have tried "Tutorials" there?      Chico Magalh„es      03/24/12 01:27      

Back to Subject List