User Tools

Site Tools


converting_20a_20number_20to_20a_20string

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
This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
converting_20a_20number_20to_20a_20string.txt · Last modified: 2024/01/05 00:22 by 127.0.0.1