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

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
03/24/12 07:25
Read: times


 
#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            01/01/70 00:00      
   8051 arithmetic 16 bit signed integer            01/01/70 00:00      
      The answer is MSB bit            01/01/70 00:00      
         Both Carry and MSB            01/01/70 00:00      
   < Have tried "Tutorials" there?            01/01/70 00:00      

Back to Subject List