home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.asm.x86      Ahh, the lost art of x86 assembly      4,675 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 4,338 of 4,675   
   Kerr-Mudd, John to Terje Mathisen   
   Re: ITOA in 65 bytes   
   15 May 21 11:21:43   
   
   From: admin@nospicedham.127.0.0.1   
      
   On Fri, 14 May 2021 21:53:32 +0200   
   Terje Mathisen  wrote:   
      
   > Robert Prins wrote:   
   > > Is it possible to fit the conversion of a 16 bit signed integer to   
   > > left-aligned ASCII without leading zeroes in just 65 bytes?   
   >   
   > Naively I would say yes: I assume you don't care about speed here?   
   >   
   > ;; AX has the value to be converted to ascii   
   [code elided]   
   I took the liberty of moving the "mov bx,10" out of the loop!   
   NASM .lst file:   
      
      
        1                                  org 0x100   
        2                                  cpu 8086   
        3   
        4                                  ;; AX has the value to be converted to   
   ascii   
        5                                  ;; Store the string to the buffer   
   pointed to by DI   
        6 00000000 B80180                         mov ax,0x8001   
        7 00000003 BF[3000]                       mov di,Ostr   
        8   
        9                                  putnum:   
       10 00000006 31C9                       xor cx,cx	; Count how many digits   
   we find   
       11 00000008 85C0                       test ax,ax	; Positive?   
       12 0000000A 7D09                        jge next   
       13   
       14                                  ;; Negative input value, so print a   
   '-' sign   
       15 0000000C C6052D                     mov byte [di],'-'   
       16 0000000F F7D8                       neg ax   
       17 00000011 47                         inc di   
       18   
       19 00000012 BB0A00                     mov bx,10   
       20   
       21                                  next:   
       22 00000015 31D2                       xor dx,dx   
       23 00000017 F7F3                       div bx   
       24 00000019 52                         push dx	; Remainder is the digit   
       25 0000001A 41                         inc cx   
       26 0000001B 85C0                       test ax,ax	; Is it zero yet?   
       27 0000001D 75F6                        jnz next   
       28   
       29                                  dump_digits:   
       30 0000001F 58                         pop ax   
       31 00000020 0430                       add al,'0'   
       32 00000022 AA                         stosb   
       33 00000023 E2FA                        loop dump_digits   
       34   
       35                                  convlth equ $-putnum   
       36   
       37 00000025 B82409                      mov ax,0x100*9+'$'   
       38 00000028 AA                          stosb   
       39 00000029 BA[3000]                    mov dx,Ostr   
       40 0000002C CD21                        int 0x21   
       41 0000002E C3                          ret   
       42 0000002F 1F                      db convlth   
       43                                     Ostr equ $   
      
      
      
      
   > That looks like 17 instructions, most of them two-byte, 5 one-byte and a   
   > couple that are longer, so 32-35 bytes?   
      
   1F=31 bytes   
      
      
   --   
   Bah, and indeed Humbug.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca