Is it possible to fit the conversion of a 16 bit signed integer to left-aligned ASCII without leading zeroes in just 65 bytes?
Robert Prins wrote:[code elided]
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
That looks like 17 instructions, most of them two-byte, 5 one-byte and a couple that are longer, so 32-35 bytes?
On Fri, 14 May 2021 21:53:32 +0200
Terje Mathisen <terje.mathisen@nospicedham.tmsw.no> 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:
That looks like 17 instructions, most of them two-byte, 5 one-byte and a couple that are longer, so 32-35 bytes?
1E=30 bytes
--
Bah, and indeed Humbug.
On Sat, 15 May 2021 11:21:43 +0100
"Kerr-Mudd, John" <admin@nospicedham.127.0.0.1> wrote:
On Fri, 14 May 2021 21:53:32 +0200
Terje Mathisen <terje.mathisen@nospicedham.tmsw.no> wrote:
Robert Prins wrote:[code elided]
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
I took the liberty of moving the "mov bx,10" out of the loop!
How embarrassing; needs bx setting if +ve!
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 B8FF7F mov ax,0x7FFF
7 00000003 BF[2F00] 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 7D06 jge notneg
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 notneg:
19 00000012 BB0A00 mov bx,10
20
21 next:
22 ; xor dx,dx
23 00000015 99 cwd ; ok as hibit has been removed
24 00000016 F7F3 div bx
25 00000018 52 push dx ; Remainder is the digit
26 00000019 41 inc cx
27 0000001A 85C0 test ax,ax ; Is it zero yet?
28 0000001C 75F7 jnz next
29
30 dump_digits:
31 0000001E 58 pop ax
32 0000001F 0430 add al,'0'
33 00000021 AA stosb
34 00000022 E2FA loop dump_digits
35
36 convlth equ $-putnum
37
38 00000024 B82409 mov ax,0x100*9+'$'
39 00000027 AA stosb
40 00000028 BA[2F00] mov dx,Ostr
41 0000002B CD21 int 0x21
42 0000002D C3 ret
43 0000002E 1E db convlth
44 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?
1E=30 bytes
Is it possible to fit the conversion of a 16 bit signed integer to left-aligned
ASCII without leading zeroes in just 65 bytes?
Is it possible to fit the conversion of a 16 bit signed integer to
left-aligned
ASCII without leading zeroes in just 65 bytes?
As no specifications have been posted, I do have some code that outputs a signed number using just 25 bytes.
The trick ? Filling the buffer backwards (putting the sign at the end).
Kerr-Mudd, John wrote:
On Sat, 15 May 2021 11:21:43 +0100
"Kerr-Mudd, John" <admin@nospicedham.127.0.0.1> wrote:
On Fri, 14 May 2021 21:53:32 +0200
Terje Mathisen <terje.mathisen@nospicedham.tmsw.no> wrote:
Robert Prins wrote:[code elided]
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
I did consider printing each digit directly to the console, it probably
would not have added much to the code size for an unsigned value, but
getting the sign logic correct was much easier when using the STOSB buffer.
xor cx,cx
test ax,ax
jge not_negative
xchg ax,bx
mov dl,'-' - '0'
call print_char
xchg ax,bx
neg ax
not_negative:
mov bx,10
next:
...
test ax,ax
jnz next
print_digits:
pop dx
call print_char
loop print_digits
ret
print_char:
add dl,'0'
mov ah,2
int 21h
ret
The code is replacement code for the itoa routine in the Borland TP3 compiler, so it has to give the same result.
On 2021-05-16 08:25, R.Wieser 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?
As no specifications have been posted, I do have some code that outputs a
signed number using just 25 bytes.
The trick ?   Filling the buffer backwards (putting the sign at the
end).
The code is replacement code for the itoa routine in the Borland TP3 compiler, so it has to give the same result.
On 2021-05-16 13:09, Terje Mathisen wrote:
Robert Prins wrote:
On 2021-05-16 08:25, R.Wieser 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?
As no specifications have been posted, I do have some code that outputs a >>> signed number using just 25 bytes.
The trick ? Filling the buffer backwards (putting the sign at the end).
The code is replacement code for the itoa routine in the Borland TP3 compiler,
so it has to give the same result.
Why didn't you state so?
I should have done earlier, rather than in that follow-up, apologies.
The key here is that this function returns a TP string, right?
No, see the code of the current routine I also posted.
For what it's worth, -32768 is problematic for all routines posted, it doesn't
change sign when negated! TP3 also doesn't accept it as valid integer, unless
coded as $8000.
Robert
--
Robert AH Prins
robert(a)prino(d)org
The hitchhiking grandfather - https://prino.neocities.org/indez.html
Some REXX code for use on z/OS - https://prino.neocities.org/zOS/zOS-Tools.html
Robert Prins wrote:
On 2021-05-16 08:25, R.Wieser 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?
As no specifications have been posted, I do have some code that outputs a >>> signed number using just 25 bytes.
The trick ?   Filling the buffer backwards (putting the sign at the end).
The code is replacement code for the itoa routine in the Borland TP3 compiler,
so it has to give the same result.
Why didn't you state so?
The key here is that this function returns a TP string, right?
This means that the space for it has to be allocated, but I don't remember anymore if that is done by the caller or callee in that environment?
No, see the code of the current routine I also posted.
For what it's worth, -32768 is problematic for all routines posted, it doesn't change sign when negated!
On Sun, 16 May 2021 20:11:15 +0000
Robert Prins <robert@nospicedham.prino.org> wrote:
On 2021-05-16 13:09, Terje Mathisen wrote:
Robert Prins wrote:
On 2021-05-16 08:25, R.Wieser 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?
As no specifications have been posted, I do have some code that outputs a >>>>> signed number using just 25 bytes.
The trick ?   Filling the buffer backwards (putting the sign at the end).
The code is replacement code for the itoa routine in the Borland TP3 compiler,
so it has to give the same result.
Why didn't you state so?
I should have done earlier, rather than in that follow-up, apologies.
The key here is that this function returns a TP string, right?
No, see the code of the current routine I also posted.
Not that I've seen on this NG.
Robert,
No, see the code of the current routine I also posted.
Missing : the current routine.
For what it's worth, -32768 is problematic for all routines posted, it doesn't change sign when negated!
:-) You forgot that after the sign change part you have an unsigned value. And in that mode 0x8000 is decimally represented as 32768. The initial code Terje posted, with its "xor dx,dx" (instead of the later, shorter "cwd"), does the job as expected.
As no specifications have been posted, I do have some code that outputs a signed number using just 25 bytes.
The trick ? Filling the buffer backwards (putting the sign at the end).
As no specifications have been posted, I do have some code that outputs a signed number using just 25 bytes.
The trick ? Filling the buffer backwards (putting the sign at the end).
Show the code !
As no specifications have been posted, I do have some code that outputs a
signed number using just 25 bytes.
The trick ? Filling the buffer backwards (putting the sign at the
end).
Show the code !
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 66:22:09 |
Calls: | 6,654 |
Files: | 12,200 |
Messages: | 5,331,881 |