Problem from Axor #2
ZXNet echo conference «code.zx»
From Vlad Sotnikov → To All 15 September 2003
Hello, All!
Here's another problem:
There is a number HL, there is a certain part of this number DE, calculate the percentage
in A of this part.
We are waiting for your options for this procedure.
Vega/ex-Style Group.
FIDO: 2:5030/1512 ZXNET: 500:812/19 E-mail: vega56@mail.ru
From Kirill Frolov → To Vlad Sotnikov 15 September 2003
Press RESET immediately, Vlad Sotnikov!
On Sun, 14 Sep 03 23:14:17 +0400, Vlad Sotnikov wrote:
VS> Here's another problem:
VS> There is a number HL, there is a certain part of this number DE, calculate
VS> percentage in A of this part. We are waiting for your options for this procedure.
In other words, A = 100 * DE / HL. But the question arises about accuracy
result and the acceptable range of numbers. Let's say
that A is an integer, and HL and DE are unsigned 16-bit integers.
Then the solution will have the following form (the percentage is rounded to
nearest integer):
push hl
ld hl, 200
exx
ldhl,0
push hl
ld e, l
ld d, h
exx
call lmul
pop de
call ldiv
ld a, l
srl a
In this case, two library functions ldiv and
lmul description of which and the text in assembly language is given below:
lmul
=====
(DE
From Kirill Frolov → To Kirill Frolov 15 September 2003
Press RESET immediately, Kirill Frolov!
On Mon, 15 Sep 2003 18:09:36 +0000 (UTC), Kirill Frolov wrote:
KF> Then the solution will have the following form (the percentage is rounded to
KF> nearest integer):
KF> push hl
KF> ld hl, 200
KF> exx
KF> ld hl, 0
KF> push hl
^^^^^^^^^^^^^^^^^
This instruction should be deleted...
KF> ld e, l
KF> ld d, h
KF> exx
KF> call lmul
KF> pop de
KF> call ldiv
KF> ld a, l
KF> srl a
From Kirill Frolov → To Kirill Frolov 15 September 2003
Press RESET immediately, Kirill Frolov!
On Mon, 15 Sep 2003 18:22:37 +0000 (UTC), Kirill Frolov wrote:
KF>> Then the solution will have the following form (the percentage is rounded to
KF>> nearest integer):
KF>> push hl
KF>> ld hl, 200
KF>> exx
KF>> ld hl, 0
KF>> push hl
KF> ^^^^^^^^^^^^^^^^^
KF> this instruction should be deleted...
KF>> ld e, l
KF>> ld d, h
KF>> exx
KF>> call lmul
KF>> pop de
KF>> call ldiv
KF>> ld a, l
KF>> srl a
Required condition -- DE <= HL, if I understood the condition correctly
tasks.
Actually, you can use the mul function to multiply 16-bit
numbers, then the solution will look like this:
push hl
ld bc, 200
call mul
push de
exx
pop hl
ld de, 0
exx
pop de
call ldiv
xor a
ld a, l
rra
mul
====
(DE,HL) = DE * BC
The function multiplies unsigned 16-bit numbers given inregisters DE and BC, and places the result in registers DE and HL. B
The DE register stores the most significant bits of the result. Function
runs in approximately 800 processor cycles. See also
description of the amul function.
EXPORT global mul ; long (DE,HL) = DE*BC ~730t.
; unsigned multiplication function for 16-bit numbers
; (C) Kirill Frolov
; idea by Vasil Ivanov
; dehl=de*bc ~730t.
psect text
mul:
ldhl,0
ld a, c
ld c, l ; 18
add a, a
jr nc, $+4
add hl, de
adc a, c ; 29
; REPEAT 7
add hl, hl
rla
jr nc, $+4
add hl, de
adc a, c ; 40*7 309
add hl, hl
rla
jr nc, $+4
add hl, de
adc a,c
add hl, hl
rla
jr nc, $+4
add hl, de
adc a, c
add hl, hl
rla
jr nc, $+4
add hl, de
adc a,c
add hl, hl
rla
jr nc, $+4
add hl, de
adc a, c
add hl, hl
rla
jr nc, $+4
add hl, de
adc a,c
add hl, hl
rla
jr nc, $+4
add hl, de
adc a,c
; ENDR
ld c, a push hl ; 0chl
ld hl, 0
ld a, b
ld b, l ; 33
add a, a
jr nc, $+4
add hl, de
add a, b ; 29
; REPEAT 7
add hl, hl
rla
jr nc, $+4
add hl, de
add a, b ; 40*7 309
add hl, hl
rla
jr nc, $+4
add hl, de
add a, b
add hl, hl
rla
jr nc, $+4
add hl, de
add a, b
add hl, hl
rla
jr nc, $+4
add hl, de
add a, b
add hl, hl
rla
jr nc, $+4
add hl, de
add a, b
add hl, hl
rla
jr nc, $+4
add hl, de
add a, b
add hl, hl
rla
jr nc, $+4
add hl, de
add a, b
; ENDR
; ahl0 + 0cde = hlde
pop de
ld b, l
ld l, h
ld h, a
ld a, d
add a, b
ld d, a
ld b, 0
adc hl, bc
ex de, hl ; 60
ret ; 729t.