Signed 32bit to ASCII?


Results 1 to 5 of 5

Threaded View

  1. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Hi Henrik,

    I didn't have anything already made, but using N-Bit_Math it only took an hour or so to write and test.

    The Main program just sets 4 different values and calls Signed32: to display them
    You can use the MOVE? macros to put your value in the Value32 variable.

    Code:
    PRECISION  CON 4 SYSTEM         ' 4 bytes = 32-bit
    INCLUDE "N-Bit_MATH.pbp"
    
    Value32  VAR BYTE[PRECISION]
    Divisor  VAR BYTE[PRECISION]
    Temp32   VAR BYTE[PRECISION]
    B32      VAR BYTE[PRECISION]
    W1       VAR WORD
    Sign     VAR BIT
    
    HSEROUT ["Ready-Set-Go",13,10]
    
    
    Main:
        @  MOVE?CP  -2147483648, _Value32
        GOSUB Signed32 : HSEROUT [13,10]
        
        @  MOVE?CP  2147483647, _Value32
        GOSUB Signed32 : HSEROUT [13,10]
    
        @  MOVE?CP  1234, _Value32
        GOSUB Signed32 : HSEROUT [13,10]
    
        @  MOVE?CP  -1234, _Value32
        GOSUB Signed32 : HSEROUT [13,10]
    STOP
    
    ;----[Send signed 32-bit value via USART]---------------------------------------------------
    Signed32:
        @  MATH_CLR _Temp32                           ; clear Temp32 in case we need ABS
        Sign = Value32.0(31)                          ; If the value is negative
        IF Sign THEN
            HSEROUT ["-"]                             ; display a negative sign
            @  MATH_SUB  _Temp32, _Value32, _Temp32   ; get the absolute value (0-value)
        ELSE
            @  MOVE?PP   _Value32, _Temp32
        ENDIF
    
        @  MOVE?CP  1000000, _Divisor
        @  MATH_DIV  _Temp32, _Divisor, _B32          ; divide by 1,000,000 gets top 4 digits
        @  MOVE?PW   _B32, _W1                        ; copy digits to PBP word var
    ;    @  MATH_MUL  _B32, _Divisor, _B32             ; multiply those digits by 1,000,000
    ;    @  MATH_SUB  _Temp32, _B32, _Temp32           ; subtract from original value
    
        @  MOVE?CP   1000, _Divisor                   ; divide remainder by 1,000
        @  MATH_DIV  REG_Z, _Divisor, _B32            ; 3 digits are in B32, 3 digits in REG_Z
    
        IF W1 > 0 THEN                                ; W1 still has top 4 digits
            HSEROUT [DEC W1]                          ; if top 4 are > 0, display them
            @  MOVE?PW  _B32, _W1                     ; display next 3 digits (with 0's)
            HSEROUT [DEC3 W1]
            @  MOVE?PW  REG_Z, _W1                    ; display lowest 3 digits (with 0's)
            HSEROUT [DEC3 W1]
        ELSE                                          ; else - top 4 digits are 0
            @  MOVE?PW  _B32, _W1                     ; check next 3 digits
            IF W1 > 0 THEN                            ; if > 0 then display (without 0's)
                HSEROUT [DEC W1]
                @  MOVE?PW  REG_Z, _W1                ; display lowest 3 digits (with 0's)
                HSEROUT [DEC3 W1]
            ELSE                                      ; else - Top 7 digits are 0
                @  MOVE?PW  REG_Z, _W1                ; display lowest 3 (without 0's)
                HSEROUT [DEC W1]
            ENDIF
        ENDIF
    RETURN
    This is the output from the test program.



    It could probably benefit from a little optimization, but it works.
    HTH,
    Last edited by Darrel Taylor; - 5th May 2010 at 22:47. Reason: Some optimization - red=changed, blue=removed
    DT

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts