'===========================================================================
' Subject: STRING REPRESENTATION OF LONG #    Date: 01-28-97 (19:59)       
'  Author: Brian Mclaughlin                   Code: ASM                    
'  Origin: comp.lang.basic.misc             Packet: ASMCODE.ABC
'===========================================================================
; LSTR.ASM  - assemble with MASM 5.1 or compatible assembler.
;           - for use with QB4.x only.
;           - written by Brian McLaughlin. Released to public domain.
;
; The purpose of this FUNCTION is to accept a long integer and pass
; back a string representation of that long integer, so the number
; 1234567 becomes the string "1234567".
;
; Unlike STR$, this routine will not pull the QB4x floating-point
; code into your EXE file, saving you 9K. Also, unlike STR$, this
; FUNCTION does not add a leading space to positive long integers.
;
; Declare it:   DECLARE FUNCTION LStr$ (Integer&)

.MODEL MEDIUM, BASIC
.DATA
    Leng DW 11
    Addr DW ?
    String DB 11 DUP(0)

.CODE

LStr PROC FAR USES SI DI, LInteger:WORD
    Mov BX, LInteger
    Mov AX, [BX]    ; DX:AX contains number to convert
    Mov DX, [BX+2]  
    Xor BX, BX      ; BL will count places, BH hold sign
    Mov SI, Offset String
    Add SI, 10      ; point SI at the end of the string
    Or  DX, DX      ; find out if DX:AX is signed (negative)
    Js  Signed      ; if signed, jump to handle it
Resume:
    Mov DI, 10      ; we're dividing by 10
    Mov CX, AX      ; save low word in CX
    Mov AX, DX      ; put the high word down in AX
    Xor DX, DX      ; and zero out DX
    Div DI          ; now divide 00:highword by 10
    Xchg CX, AX     ; retrieve old low word, while saving quotient in CX
    Div DI          ; now divide remainder:lowword by 10
    Xchg CX, DX     ; put new high word in DX, remainder in CX
    Add CX, "0"     ; convert remainder to ASCII character
    Mov Byte Ptr [SI], CL   ; put low byte of remainder into String at SI
    Dec SI          ; point one byte prior
    Inc BL          ; keep a count of iterations
    Or  AX, AX      ; is there anything left?
    Jnz Resume      ; if so, loop back untilDX:AX is empty
    Or  DX, DX
    Jnz Resume
    Or  BH, BH      ; see if we need to add the "-" character
    Jz  NoSign      ; if no sign, skip ahead
    Inc BL          ; keep track of length of string
    Mov Byte Ptr [SI], BH   ; put sign character at head of string
    Xor BH, BH      ; clear it to make BX hold length only
    Dec SI          ; point one prior, to remain consistant
NoSign:
    Inc SI
    Mov Addr, SI
    Mov Leng, BX
    Mov AX, Offset Leng
    Ret 
Signed:
    Mov BH, "-"
    Neg DX         ; perform a 32-bit negation on DX:AX
    Neg AX
    Sbb DX, 0
    Jmp SHORT Resume
LStr ENDP
     END
