; subroutine Cr0
; 8-Bit 2's Complement -> magnitude / Sign Bit Conversion
;
; input: r0 = signed byte
;
; output: r0 = magnitude
; Bit 21H = sign (21H is set if r0 is a negative number)
;
; alters: acc
Cr0: mov a, r0 ; read X into accumulator
jb acc.7, Cr0a ; X is negative if bit 7 is 1
clr 21H ; clear sign bit if 'positive'
ret ; done
Cr0a: cpl a ; X negative, find abs value
inc a ; XA = complement(XT)+1
mov r0, a ; save magnitude
setb 21H ; set sign bit if 'negative'
ret
; subroutine Cr1
; 8-Bit 2's Complement -> magnitude / Sign Bit Conversion
;
; input: r1 = signed byte
;
; output: r1 = magnitude
; Bit 22H = sign (22H is set if r1 is a negative number)
;
; alters: acc
Cr1: mov a, r1 ; read X into accumulator
jb acc.7, Cr1a ; X is negative if bit 7 is 1
clr 22H ; clear sign bit if 'positive'
ret ; done
Cr1a: cpl a ; X negative, find abs value
inc a ; XA = complement(XT)+1
mov r1, a ; save magnitude
setb 22H ; set sign bit if 'negative'
ret
Source: Maths Subroutines for the 8051 microcontroller, W.G.Marshall 2002
Related topics:
8051 Program - signed to unsigned conversion 16bit | 8051 Program - signed to unsigned conversion 32bit | 8051 Program - unsigned to signed conversion 8bit | 8051 Program - unsigned to signed conversion 16bit | 8051 Program - unsigned to signed conversion 32bit | 8051 Program - signed 8bit to 16bit conversion | 8051 Program - memory subroutines | 8051 Program - math subroutines | 8051 Program - conversion subroutines
List of topics: 8051
No comments:
Post a Comment