|
Deja Vu #04
31 марта 1998 |
|
CODING - Print numbers in different number systems: print decimal and hexadecimal, binary, and Roman numbers, seal numbers in the radix set by the user.

SoundTrack: "FuCK iSn'T A FacT Yet" (A. FER)
__________________________________________
(C) Kolotov Sergei g.Shadrinsk, SerzhSoft
__________________________________________
PRINTING numbers in different number systems
__________________________________________
*
One of the major computer problems
is the derivation of numbers on screen (in print).
And the numbers may be in different number systems, of which
there are not so little.
The most familiar to us all, of course
but the same is the decimal
S system, ie system schisc S representation with base 10. De
S dozen here appears not
) S accident: a man with a deep S Coy antiquity believed in the
power of singing S fingers, and they both R S is known - ten.
But com S puter "usual" assume
a thief is not in the decimal system, but in
binary, because All information in It seems the electric charges
is the current (1) and no current (0). Since the same move with
the "computer" language in the normal man?
The following procedure prints
number specified in the register A (accumulator) in decimal
form. Print runs standard command RST # 10 in the current
output stream. The length of the procedure, 23 bytes. In its
work, it uses a 3-byte table tens of degrees, and when
performing in cycle counts and prints the number of hundreds
(first figure), tens of (Second) and units (third digit)
numbers.
See Listing 1.
Yet it is unlikely that did not
will need to print, say,
hexadecimal or binary systems
notation. Yes, even in the same decimal, but, for example,
numbers in the range of not 0 to 255 and from 0 to 65535.
Therefore advisable to always enough "to have on hand" library
of procedures designed Print numbers in various number systems,
in various ranges. What me and realized ...
See Listing 2.
The structure of this library includes procedures for the
printing of numbers in various number systems. Namely: decimal,
hex, binary, installed
user (eg, octal), and
as a particularly exotic, the system Roman numbers.
Procedures that make up the library,
have much in common and fully understand the one, you can more
quickly grasp in the principle of rest. All headlines
printing procedures start with P
(Print). Further there are three letters that define a system
in which printed number. Then, separated by an underscore,
followed by one of the symbols of B or W, Determining the range
of numbers printed: bytes (Byte) or Word (Word). In the first
case (B), before calling the procedure, the number should
in register A (accumulator), and
In the second case (W) - in the register pair
HL. Printing is done in the current thread
O (Team RST # 10).
Library description PDES LIB.
1. Print decimal.
1.) Procedure PDEC_W - print numbers
located in the register pair HL. Print Range: 00000 .. 65535.
Length of procedure 26 bytes. Uses a table of degrees of tens
DECTB_W.
2.) Procedure PDEC_B - print the number of A
(000 .. 255), 11 bytes. The work is PDEC_W.
3.) Procedure PDEC_3B - print numbers
given by three registers: A, H, L (for
seniority). Print range: 00000000 ...
16777215 (2 ^ 24-1). The length of the procedure, 40 bytes.
Causes PDEC_B and PDEC_W. This procedure
was put into this makes it possible for a library with
sometimes encountered problems in printing numbers greater than
65535. For example, without this there manage to derive the
number of cycles in an interrupt.
4.) Table DECTB_W - contains dozens of 5 degrees, defined as
a word (2 bytes) - Length of 10 bytes.
Total length of section: 10 +40 +11 +26 = 87 bytes
2. Print hexadecimal numbers.
1.) Pr-pa PHEX_LB - printing ml. nibble in A (0 .. F), 14
bytes.
2.) Pr-pa PHEX_HB - print art. nibble in A (0 .. F), 4
bytes. Swaps the nibbles in the A and causes PHEX_LB.
3.) Pr-pa PHEX_B - print the number of A
(00 .. FF), 3 bytes. Two times is
PHEX_HB.
4.) Pr-pa PHEX_W - print the number of HL
(0000 .. FFFF), 5 bytes. Two times is
PHEX_B.
Total length of section: 5 +3 +4 +14 = 26 bytes.
3. Print binary numbers.
1.) Pr-pa PBIN_B - print the number of A (8
discharge), 12 bytes.
2.) Pr-pa PBIN_W - print the number of HL
(16 bits), 5 bytes. Two times is
PBIN_B.
Total length of section: 5 +12 = 17 bytes.
4. Print numbers in radix set by the user.
Served as a prototype for the procedure, published in an
excellent reference book - the book of H. Rodionov and A.
Larchenko "ZX-Spectrum for users and programmers. "
1.) Pr-pa MKUSETB - filling the table
USE_TBL degrees with the base defined in
battery. The length of the pr-algebra 28 bytes.
2.) Pr-pa PUSE_W - print number to HL.
Uses the previously created table MKUSETB
degrees USE_TBL. 33 bytes.
3.) Table USE_TBL - Table of powers,
32 bytes.
Total length of section: 32 +33 +28 = 93 bytes.
5. Printing Roman numerals.
1.) Pr-pa PRIM_W - print the number of HL
(I.. MMMCMXCIX), 77 bytes. Uses a table RIM_TBL.
2.) Pr-pa PRIM_B - print the number of A
(I.. CCLV). Length of 3 bytes. Calls the procedure PRIM_W.
3.) Table RIM_TBL - table of the significance of the letters
in the writing of Roman numerals. Length: 7 * 3 = 21 bytes.
Total length of section: 21 +3 +77 = 101 bytes.
Full size libraries: 324 bytes.
Listing 1. The printing bytes ten
quadratic system.
PDEC_B; print decimal numbers in Accu
mulyatore (000 .. 255)
LD HL, DECTB_B; address table
Litsa degree
It scores
LD B, # 03; max. WHO
possible to
lichestvo
figures
LP_PDB1 LD C, "0" -1; initialization
tion counter
LP_PDB2 INC C; increase
counter
SUB (HL); subtract
current
the degree of de
tens
JR NC, LP_PDB2; repeat
while A> = 0
ADD A, (HL); A = A mod (HL)
; C = STR $ (A
div (HL))
PUSH AF; keep A
on the stack
LD A, C, current
figure
RST # 10; digital printing
algebra
POP AF; recovery
tion Accu
mulyatora
INC HL; transition to
seq. degree
tens of degree
ki
DJNZ LP_PDB1; spun
Loop
figures
RET; exit
Procedure
;
DECTB_B DB 100,10,1; Table
degrees
10-ki for
byte
--------------;
PHEX_HB; print high-order nibble in A
(0 .. F)
RRCA; change
RRCA; places
RRCA; Senior
RRCA; and younger
nibbles
;----------------------------------------;
PHEX_LB; Print younger nibbles of A
(0 .. F)
PUSH AF; saved A
on the stack
AND # 0F; discarded
extra
senior bi
you
CP # 0A; if A <10,
JR C, GO_PHL; then rewrite
belched
ADD A, "A" - "9" -1; correction
mounting plate: village
le "9" is
"A"
GO_PHL ADD A, "0", translated into
ASCII-code
RST # 10; print HEX
figures
("0" .. "F")
POP AF; reductive
Does A with
Stack
RET; exit
Procedure
;----------------------------------------;
; Print in BIN system:;
;----------------------------------------;
PBIN_W; print binary number to HL
(16-bit 0 / 1)
LD A, H; print
Senior
Byte number
CALL PBIN_B; challenge of
procedure BIN
buy print
that
LD A, L; print
Jr.
Byte number
;----------------------------------------;
PBIN_B; print binary number in A
(8 bits 0 / 1)
LD B, # 08; in a byte - 8
bits
LP_PBB RLCA; alternately
'Put forward'
bits in the CF
PUSH AF; keep A
on the stack
LD A, # 18, depending on
depending on the
Flag C:
RLA; A = "0" or
A = "1"
RST # 10; print obviously
anterior bi
that
POP AF; restore
vit A with
Stack
DJNZ LP_PBB; bitwise
cycle
RET; exit
Procedure
;----------------------------------------;
; Print in USER system:;;
;----------------------------------------;
PUSE_W; Print numbers in the set
MKUSETB system MF-I in HL
PUSH HL; cast
printable
number on
stack
HL_PUW LD HL, _NULL; address of the end
Table
degrees
LP_PUW1 DEC HL; load in
DE next
LD D, (HL); degree
number
DEC HL; from the table,
LD E, (HL); moving
top-down
EX (SP), HL; address table
Litsa <->
print-e
number
XOR A; zeroing A
and reset
flag C
LP_PUW2 INC A; compute
regular
SBC HL, DE; digit number
la and results
result
JR NC, LP_PUW2; put in
A (A = 1 + (HL
div DE))
ADD HL, DE; restored
Lebanon (+)
value
ADD A, "0" -1; translate A
in ASCII-code
CP "9" +1, if code
less than "9"
JR C, GO_PUW1; the transition
Print
ADD A, "A" - "9" -1; correction
(After the "9"
is "A")
GO_PUW1 RST # 10; print obviously
anterior digital
algebra of
EX (SP), HL; HL = address
table
number ->
stack
DEC DE; if the degree
stump
LD A, D; numbers are not
equal to unity
boundary
OR E; (most lane
first element
table)
JR NZ, LP_PUW1; then continue
isplay work
POP HL; remove from
no stack
right 0
RET; exit
Procedure
;----------------------------------------;
MKUSETB; Create a table of powers with base
vaniem in A
LD HL, USE_TBL; address cos
given by
Table
LD DE, # 0001; initialization
tion counters
ka degree
LP_MUT1 LD (HL), E; record those
kuschego
INC HL; values
degree
LD (HL), D; in the table
and the transition
INC HL; for its next
yuschey cell
PUSH HL; store
address on
stack
LD B, A; founded th degree
penalties - "in
counter
cycle
LD HL, # 0000 reset
result
LP_MUT2 ADD HL, DE; counting
it results
JR C, GO_MUT; if HL>
65535, then
interrupt
Account
DJNZ LP_MUT2; repeat
times
EX DE, HL; result -
in DE (New
degree)
GO_MUT POP HL; restored
Lebanon
Address Table
Litsa
JR NC, LP_MUT1; if not
interrupted,
then repeat
LD (HL_PUW +1), HL; address of the end
Table ->
in PUSE_W
RET; exit
Procedure
;----------------------------------------;
USE_TBL DS 32; table degrees are
Booths of schis
leniya
;----------------------------------------;
; Print in RIM system:;
;----------------------------------------;
PRIM_B; Print numbers in Roman records in A
(I.. CCLV)
LD L, A; copy
LD H, # 00; A in HL
;----------------------------------------;
PRIM_W; Print numbers in Roman entries
HL (I.. MMMCMXCIX)
PUSH HL; cast
printable
number on
stack
LD HL, RIM_TBL; address table
Litsa characters
DB # DD; work with
half
Register IX
LD L, # 07; LD XL, the number of
characters in p.
radix
LP_PRW1 LD E, (HL); read the
from the table
value
INC HL; next
sign Reams
Coy system
LD D, (HL); reckoning
and put
in DE
INC HL; then re
Giustra C
read the
LD C, (HL); ASCII-code
mark
INC HL; transition to
next
sign
LD (HL_PRW +1), HL; preserved
address the following
blowing
mark
EX (SP), HL; address -> to
stack, HL =
pechat.chislo
XOR A; zeroing
counter and
Reset CF
LP_PRW2 INC A; in the loop
produce
SBC HL, DE; division HL
on DE (you
read
JR NC, LP_PRW2; unavailable
overflow
line)
ADD HL, DE; restored
Lebanon (+)
value
DEC A; because A 1
more HL
div DE,
JR Z, GO_PRW1; then if A = 1
- Nothing
print
LD B, A; number
print
Character
LP_PRW3 LD A, C; character code
RST # 10; print
his
DJNZ LP_PRW3; how to
before - so
to and Pec
Tai
GO_PRW1 DB # DD; work with
half
Register IX
LD A, L; LD A, XL -
number of current
schego sign
DEC A; if
Last
sign (I), then
JR Z, GO_PRW4; dvuhbukven
Nogo soche
Tania no
EX (SP), HL; HL = address
sled.znaka,
number-> stack
RRA; if the number
current
mark parity
LIMITED,
JR C, GO_PRW2; a combination
state with the following
blowing values
com
INC HL; otherwise - ne
reprygnut
in one
INC HL; sign. In
result
get the hell
res
INC HL; sign for
get
double
GO_PRW2 LD A, C; znakosoche
Tania (IV,
CM, XL ...)
LD C, (HL); taken from
Table
value
for
INC HL; this sign
and received
LD B, (HL); difference
between values
cheniem
INC HL; ground
and these values
cheniem.
EX DE, HL; For example,
for foundations
Nogo mark X
AND A; additional
tive bu
children I, and their
SBC HL, BC; difference
corresponding
respectively:
EX DE, HL; 10-1 = 9, ie
IX
LD C, A; code bases
Nogo sign
LD A, (HL); additional code
Executive
mark
EX (SP), HL; HL = print
my number
SBC HL, DE; if it is <
additional
Nogo
JR C, GO_PRW3; value
is not printing
Tai
RST # 10; printing up
filler
Nogo sign
LD A, C; main
sign
RST # 10; print
JR GO_PRW4; result -
dvuhbukv.
combination
GO_PRW3 ADD HL, DE; reductive
Whether (+) y
number
GO_PRW4 EX (SP), HL; number -> to
stack
HL_PRW LD HL, _NULL; address the following
blowing
sign in
Table
DB # DD; work with
half
Register IX
DEC L; DEC XL -
reduce
counter
signs
JR NZ, LP_PRW1; twist cycle
while there is
More characters
POP HL; not removed
must start
zero with
Stack
RET; exit
Procedure
;----------------------------------------;
RIM_TBL; Table significance of the letters in napisa
SRI Roman numerals
DW 1000
DB "M"; M = 1000; CM = 900
DW 500
DB "D"; D = 500; CD = 400
DW 100
DB "C"; C = 100; XC = 90
DW 1950
DB "L"; L = 50; XL = 40
DW 1910
DB "X"; X = 10; IX = 9
DW 5
DB "V"; V = 5; IV = 4
DW 1
DB "I"; I = 1;-nop
;-------------------------------------- -; ; End of PDES LIB
2.0;; ;----------------------------------------;
Other articles:
Similar articles:
В этот день... 16 December