read decimal number

This commit is contained in:
pommicket 2021-11-11 19:04:19 -05:00
parent a8f48b329a
commit 8c7f2f4a58
2 changed files with 95 additions and 14 deletions

104
03/in02
View file

@ -1,3 +1,17 @@
im
--TE
BA
im
--nu
cl
jm
:-ex
::TE
'd
'1
'2
'6
\n
// open input file // open input file
im im
--IF --IF
@ -167,12 +181,77 @@ JA
im im
##3c. ##3c.
sy sy
// convert string representation of number starting at rbx and ending with a newline to number in rax
::nu
DB
im
##1.
+B
IA start by storing pointer to actual number (not including base) in rsi
BD
zA
lb
BA
im
##64. ascii 'd'
je
:-#d decimal
im
##78. ascii 'x'
je
:-#x hexadecimal
jm
:-bn unrecognized number base
// convert newline-terminated decimal representation in rsi to number in rax
::#d
zA
JA use rdi to store number
::dL decimal loop
BI
zA
lb
BA
im
##a.
je
:-d$ newline reached
im
##30.
jg
:-bn bad digit (<'0')
im
##39.
jl
:-bn bad digit (>'9')
im
##ffffffffffffffd0.
+B
CA put numerical value of digit in rcx
im
##a.
BA
AJ
+* multiply by 10
BC
+B add digit
JA
// increment rsi
BI
im
##1.
+B
IA
jm
:-dL keep looping
::d$
AJ
re return
// convert ASCII hex digit in rbx to number in rax. // convert ASCII hex digit in rbx to number in rax.
::hx ::hx
im im
##30. compare with ascii '0' ##30. compare with ascii '0'
jg jg
:-bd bad digit if < '0' :-bn bad if < '0'
im im
##39. ##39.
jl jl
@ -185,23 +264,23 @@ jm
im im
##61. ASCII 'a' ##61. ASCII 'a'
jg jg
:-bd bad digit (not 0-9, and less than 'a') :-bn bad digit (not 0-9, and less than 'a')
im im
##66. ASCII 'f' ##66. ASCII 'f'
jl jl
:-bd bad digit (not 0-9, and greater than 'f') :-bn bad digit (not 0-9, and greater than 'f')
im im
##ffffffffffffffa9. -87 (10 - 'a') ##ffffffffffffffa9. -87 (10 - 'a')
::hX ::hX
+B +B
re // return re // return
// bad digit // bad number
::bd ::bn
im im
##2. stderr ##2. stderr
JA JA
im im
--BD error message --BN error message
IA IA
im im
##a. length of error message ##a. length of error message
@ -234,16 +313,17 @@ jm
'0 '0
'3 '3
00 00
::BD bad digit error message ::BN bad number error message
'B 'B
'a 'a
'd 'd
20 20
'd 'n
'i 'u
'g 'm
'i 'b
't 'e
'r
\n \n
::LI line buffer ::LI line buffer
~~ ~~

View file

@ -2,10 +2,11 @@
;J=d0 ;J=d0
A=d60 A=d60
syscall syscall x3c
res d1000
:label :label
B+=J B+=J
B<<=9 B<<=d9
B-=J B-=J
J?<B:label J?<B:label