Monday, May 30, 2016

8051 Program - convert dptr to signed string

; Description:
; Convert 16 Bit Binary Value In DPTR To A Signed Decimal Number String.
;
; Entry Requirements:
; DPTR Has Value To Print As A Signed Decimal Number In External Memory
;
; On Exit:
; DPTR Points To Start Of Printable String
;
; Affected:
; PSW.Z, PSW.P, Acc
;
; Stack:
; 11 Bytes, Not Including Space Used By Called Routines
;
; Comments:
; Buffer Is In External Memory, And Needs 7 Bytes (Sign, Plus 5 Digits
; Plus ETX Terminator). Buffer Is Defined At The End Of The File.
; The Return String Is ETX Terminated. Since Result Is Returned In
; Buffer, It Will Need To Be Copied Or Saved If Additional Calls Are
; Made, Since The Buffer Will Be Overwritten.
;
X_BUFFER equ 9000H;
ETX equ 3 ; ASCII ETX Character
UTIL_BINTODEC:
push psw ; Save PSW
push 0 ; Save R0
push 1 ; Save R1
push 2 ; Save R2
push 3 ; Save R3
push 4 ; Save R4
push 5 ; Save R5
push 6 ; Save R6
push 7 ; Save R7
;
mov r6,dph ; Store High In R6
mov r7,dpl ; Store Low In R7
mov dptr,#X_BUFFER ; Point To Buffer
;
mov a,r6 ; Get High Of Value
jnb acc.7,loop1 ; If <= 32767, Skip
clr c ; Clear For SUBB
clr a ; Clear For SUBB
subb a,r7 ; Subtract Low
mov r7,a ; Move Back
clr a ; Clear For SUBB
subb a,r6 ; Subtract High
mov r6,a ; Move Back
mov a,#'-' ; Number Is Negative
movx @dptr,a ; Store Sign
inc dptr ; Next Location
;
; DPTR Contains Value From 0..32767.
;
loop1:mov r0,#high 10000 ; 10^5
mov r1,#low 10000 ; 10^5
clr f0 ; Haven't Stored Non-0 Value
;
; R0/R1 Now Has Factor
;
loop2:mov r2,6 ; Get Value To R2
mov r3,7 ; Get Value To R3
mov r4,0 ; Get Factor
mov r5,1 ; Get Factor
call UTIL_UDIV ; Divide Value By Factor
mov r6,4 ; Store Remainder High
mov r7,5 ; Store Remainder Low
;
; Quotient In R4/R5 Should Be In 0..9 Range. If 0, Don't Store Unless We've
; Stored A Previous Non-Zero Value
;
mov a,r3 ; Get Low Of Quotient
jnz loop3 ; If Not 0, Store It
jnb f0,loop4 ; If No Non-0 Before, Don't Store
loop3:setb f0 ; Non-0 Value
call UTIL_BINTOASC ; Make Printable
movx @dptr,a ; Store Character
inc dptr ; Next Location
;
; Now Divide Factor By 10 For Next Time
;
loop4:mov r2,0 ; Get Factor
mov r3,1 ; Get Factor
mov r4,#high 10 ; 10
mov r5,#low 10 ; 10
call UTIL_UDIV ; Factor = Factor / 10
mov r0,2 ; Update Factor High
mov r1,3 ; Update Factor Low
;
mov a,r0 ; Get Factor High
orl a,r1 ; OR In Factor Low
jnz loop2 ; If Not 0, Repeat
;
; Store The End Of String Marker And Print
;
jb f0,loop5 ; If We've Stored A Number, Skip
mov a,#'0' ; Put At Least 1 Zero
movx @dptr,a ; Store It
inc dptr ; Next Location
loop5:mov a,#ETX ; End Of String Marker
movx @dptr,a ; Store It
;
; Now Return Pointer To Buffer
;
mov dptr,#X_BUFFER ; Point To Print Buffer
;
; Recover Registers And Exit
;
pop 7 ; Recover R7
pop 6 ; Recover R6
pop 5 ; Recover R5
pop 4 ; Recover R4
pop 3 ; Recover R3
pop 2 ; Recover R2
pop 1 ; Recover R1
pop 0 ; Recover R0
pop psw ; Recover PSW
ret ; Return To Caller


Source: Assorted Utilities, John C. Wren 11/23/96



Related topics:
8051 Program - convert 8bit binary to bcd   |   8051 Program - convert 12bit binary to bcd   |   8051 Program - convert dptr to unsigned string   |   8051 Program - memory subroutines   |   8051 Program - math subroutines   |   8051 Program - conversion subroutines

List of topics: 8051

No comments:

Post a Comment