; subroutine Cr0r1
; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion
;
; input: r1, r0 = signed word
;
; output: r1, r0 = magnitude
; Bit 21H = sign (21H is set if negative number)
;
; alters: acc, C
Cr0r1: mov a, r1 ; high byte into accumulator
jb acc.7, c0a ; negative if bit 7 is 1
clr 21H ; clear sign bit if 'positive'
ret ; done
c0a: setb 21H ; set sign flag
mov a, r0 ; number is negative
cpl a ; complement
add a, #1 ; and add +1
mov r0, a
mov a, r1 ; get next byte
cpl a ; complement
addc a, #0
mov r1, a
ret
; subroutine Cr2r3
; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion
;
; input: r3, r2 = signed word
;
; output: r3, r2 = magnitude
; Bit 22H = sign (22H is set if negative number)
;
; alters: acc, C
Cr2r3: mov a, r3 ; read high into accumulator
jb acc.7, c1a ; negative if bit 7 is 1
clr 22H ; clear sign bit if 'positive'
ret ; done
c1a: setb 22H ; set sign flag
mov a, r2 ; number is negative
cpl a ; complement
add a, #1 ; and add +1
mov r2, a
mov a, r3 ; get next byte
cpl a ; complement
addc a, #0
mov r3, a
ret
; subroutine Cr4r5
; 16-Bit 2's Complement -> magnitude / Sign Bit Conversion
;
; input: r5, r4 = signed word
;
; output: r5, r4 = magnitude
; Bit 22H = sign (22H is set if negative number)
;
; alters: acc, C
Cr4r5: mov a, r5 ; read high into accumulator
jb acc.7, c3a ; negative if bit 7 is 1
clr 22H ; clear sign bit if 'positive'
ret ; done
c3a: setb 22H ; set sign flag
mov a, r4 ; number is negative
cpl a ; complement
add a, #1 ; and add +1
mov r4, a
mov a, r5 ; get next byte
cpl a ; complement
addc a, #0
mov r5, a
ret
Source: Maths Subroutines for the 8051 microcontroller, W.G.Marshall 2002
Related topics:
8051 Program - signed to unsigned conversion 8bit | 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