Converting a number to a string

by Richard Russell, January 2015

You may wish to convert a number to a string in assembly language (equivalent to the STR$ function in BASIC). This can easily be done by calling an internal routine within the interpreter. Here is how to do it (requires BBC BASIC for Windows version 6):

      ; eax = print format control (equivalent to @%)
      ; bx|ecx|edx = number to print (80-bit variant)
      ; edi = address of destination buffer
      push eax
      mov  ebp,esp
      call @fn%(12)
      pop  eax
      mov  byte [edx+ecx],0

On completion a NUL-terminated string is stored in the supplied buffer.

To illustrate the use of this code here is a complete program which prints the value of PI using assembly language:

        DIM Buffer% 255, P% 100, L% -1
        [OPT 10
        .ppi
        mov  eax,&1414     ; G20 format
        mov  bx,&4000      ; Value of PI ...
        mov  ecx,&C90FDAA2 ; .. as an ...
        mov  edx,&2168C235 ; .. 80-bit float
        mov  edi,Buffer%   ; Destination buffer
 
        push eax
        mov  ebp,esp
        call @fn%(12)
        pop  eax
        mov  byte [edx+ecx],0
 
        mov  esi,Buffer%
        .vout
        lodsb
        call "oswrch"
        or   al,al
        jnz  vout
        ret
        ]
 
        CALL ppi
        PRINT