; subroutine Mr0
; 8-Bit magnitude / Sign Bit -> 2's Complement Conversion
;
; input: r0 = magnitude
; Bits 21H & 22H = sign bits of operands X and Y
; (set if negative)
;
; output: r0 = signed byte
;
; alters: acc
Mr0: jb 21H, Mr0b ;test X sign
jb 22H, Mr0a ;test Y sign
ret
Mr0b: jnb 22H, Mr0a
ret
Mr0a: mov a, r0 ;if r0 negative, get abs value
cpl a ;complement magnitude of X
inc a ;r0 = complement(r0)+1
mov r0, a ;save in 2's complement
ret ;done
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 16bit | 8051 Program - signed to unsigned conversion 32bit | 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