flat assembler
Message board for the users of flat assembler.

Index > DOS > Multiplication of BCD

Author
Thread Post new topic Reply to topic
bipocich



Joined: 17 Nov 2013
Posts: 18
bipocich
Multiply doesn't work well.
Where is a bug?

Code:
format MZ
entry main: start
stack 100h

;-------------------------------------------------
;MAKRA
;-------------------------------------------------
macro wyswietl text
{
   mov dx , text
   mov ah, 09h
   int 21h
}

macro wstaw text
{
   mov [text],al

}

macro zerowanie text
{
local petla
  mov al, [spacja]
mov cx,9
mov si,0
petla:
     mov [text+si],al
     inc si
     loop petla

}



macro kreskowanie text
{
  mov al, [spacja]
mov cx,8
mov si,0
petla2:
     mov [text+si],al
     inc si
     loop petla2

}

macro kopiowanie text
{

mov cx,8
mov si,0
petla:
     mov al, [text+si]
     mov [pomocnicza+si],al
     inc si
     loop petla

}



macro konwersja tmp
{
local petla
mov cx,8
mov si,7
petla:
mov al, [tmp+si]
rcr al, 1
rcl [xyz],1
loop petla

}

macro pierwszy_znak tmp
{
local pobierz_znak, okej
pobierz_znak:
call pobierz

cmp al,'0'
je okej

cmp al,'1'
je okej
jne pobierz_znak

okej:
mov [tmp],al
wstaw tmp
wyswietl tmp
}



macro wyswietl_liczba text
{
   local plus, minus, koniec, ptla
   mov al,[text]

   cmp al,'0'
   je plus
   jne minus

   plus:
   sub al,5
   jmp koniec

   minus:
   sub al,4

   koniec:
   wstaw tmp
   wyswietl tmp

        mov cx,8
        mov si,1
        ptla:


        mov al,[text+si]
        wstaw tmp
        wyswietl tmp
        inc si
        loop ptla


}



macro wyswietl_liczba_w text
{
   local plus2, minus2, minus_koncowy, plus_koncowy, koniec, ptla, norma, wyzej, koniec1

   mov al,[liczba1]

   cmp al,'0'
   je plus2
   jne minus2


   minus2:
   mov al,[liczba2]

   cmp al,'0'
   je minus_koncowy
   jne plus_koncowy


   plus2:
   mov al,[liczba2]

   cmp al,'0'
   je plus_koncowy
   jne minus_koncowy



   plus_koncowy:
   mov al,'+'
   jmp koniec

   minus_koncowy:
   mov al,'-'

   koniec:
   wstaw tmp
   wyswietl tmp

        mov cx,8
        mov si,1
        ptla:


        mov al,[text+si]

        cmp al, '9'
        jb norma
        jnb wyzej

        wyzej:
        add al,0
        wstaw tmp
        wyswietl tmp
        jmp koniec1

        norma:
        wstaw tmp
        wyswietl tmp

        koniec1:
        inc si
        loop ptla


}

macro binarnie tmp
{
local ptla_wyswietl, tak, wysw
mov cx, 36
ptla_wyswietl:
rcl[tmp],1

jc tak
mov dl, '0'
jmp wysw


tak:
mov dl,'1'

wysw:
mov ah,02
int 21h

loop ptla_wyswietl

}
;-------------------------------------------------
;MAKRA
;-------------------------------------------------



segment main
start:




mov ax, dane
mov ds, ax
zerowanie liczba1
zerowanie liczba2
zerowanie liczba3

gBówna:
call czysc


call dana1
call mnozenie
call dana2
call rownasie
call wynik

call pytanie
call linia1
call opcje
call linia2
call wybor

ety1:
call pobierz

cmp al, 'Q'
je koniec
;jne ety1


cmp al, 'L'
je wprowadz_dana1

cmp al, 'P'
je wprowadz_dana2

cmp al, 'W'
je mno|enie

;cmp al, 'L'
;je wykonaj_operacje
jne ety1




;mov ah, 4ch
;int 21h

;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
wprowadz_dana1:

zerowanie liczba1
zerowanie liczba3


wyswietl text1

;pierwszy_znak liczba1

pobierz_znak1:

call pobierz

cmp al,'0'
je okej

cmp al,'1'
je okej
jne pobierz_znak1

okej:
mov [liczba1],al
wstaw tmp
wyswietl tmp



mov cx,8
mov si,1
spr1:

call pobierz



cmp al,'0'
jb spr1

cmp al,'9'
jbe dobrze

jne spr1



dobrze:

mov [liczba1+si],al
wstaw tmp
wyswietl tmp
inc si
loop spr1



call gBówna



;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
wprowadz_dana2:

zerowanie liczba2
zerowanie liczba3


wyswietl text2

;pierwszy_znak liczba2


pobierz_znak2:
call pobierz

cmp al,'0'
je okej1

cmp al,'1'
je okej1
jne pobierz_znak2

okej1:
mov [liczba2],al
wstaw tmp
wyswietl tmp


mov cx,8
mov si,1
spr3:

call pobierz

cmp al,'0'
jb spr3

cmp al,'9'
jbe dobrze1

jne spr3



dobrze1:

mov [liczba2+si],al
wstaw tmp
wyswietl tmp
inc si
loop spr3



call gBówna

;-------------------------------






pobierz:
mov ah, 07h
int 21h
ret

koniec:
mov ah, 4ch
int 21h

mnozenie:
mov bh,0
mov dl, 00
mov dh, 1
mov ah, 2
int 10h
wyswietl text4
ret

rownasie:
mov bh,0
mov dl, 00
mov dh, 3
mov ah, 2
int 10h
wyswietl text5
ret


dana1:
mov bh,0
mov dl, 00
mov dh, 00
mov ah, 2
int 10h
wyswietl text1



wyswietl_liczba liczba1

wyswietl przerwa
binarnie liczba1

;konwersja liczba1



ret

dana2:
mov bh,0
mov dl, 0
mov dh, 2
mov ah, 2
int 10h
wyswietl text2

wyswietl_liczba liczba2

wyswietl przerwa
konwersja liczba2



ret


wynik:
mov bh,0
mov dl, 0
mov dh, 4
mov ah, 2
int 10h
wyswietl text3

;konwersja liczba3

wyswietl_liczba_w liczba3


ret


wynik1:
mov bh,0
mov dl, 0
mov dh, 4
mov ah, 2
int 10h
wyswietl text3

konwersja liczba3

wyswietl xyz


ret

pytanie:
mov bh,0
mov dl, 0
mov dh, 6
mov ah, 2
int 10h
wyswietl text6
ret

opcje:
mov bh,0
mov dl, 0
mov dh, 8
mov ah, 2
int 10h
wyswietl text7
ret



czysc:
mov ah, 0
mov al, 3
int 10h
ret


linia1:
mov bh,0
mov dl, 0
mov dh, 7
mov ah, 2
int 10h
wyswietl text9
ret


linia2:
mov bh,0
mov dl, 0
mov dh, 9
mov ah, 2
int 10h
wyswietl text9
ret


wybor:
mov bh,0
mov dl, 0
mov dh, 10
mov ah, 2
int 10h
wyswietl text10
ret



mno|enie:


;mov bx, liczba1
mov al, [liczba1+8]
mov ah, [liczba1+7]
shr ah, 4
mov [liczba4+3],al

mov al, [liczba1+6]
mov ah, [liczba1+5]
shr ah, 4
mov [liczba4+2],al

mov al, [liczba1+4]
mov ah, [liczba1+3]
shr ah, 4
mov [liczba4+1],al

mov al, [liczba1+2]
mov ah, [liczba1+1]
shr ah, 4
mov [liczba4],al


mov [liczba3],0

mno|enie_gBówne:



     mov cx,8
     mov si,8
     clc

 petla_mno|enie:



        mov al,[liczba2+si] ; tab[i]
        sub al,48
        mov ah,[przen]
        cmp ah,0
        je ok12
        inc al
ok12:
        mov ah,[liczba3+si] ; dodaj z przeniesieniem
        sub ah,48
        add al,ah
        daa
        mov ah,0
        mov[przen],ah
        cmp al, 0fh
        jb ok11
        and al,0fh
        inc [przen]
ok11:

        cmp al, 0FFh
        jne  ety1x
        mov al,1
ety1x:
        add al,48
        mov [liczba3+si],al

        dec si
        loop petla_mno|enie

;sub [liczba4],1
;das


 petla_odejmowanie:
        ;mov dl,[liczba1+si] ; tab[i]
       ; sub [liczba4+3],1
        ;das
        ;sbb [liczba4+2],0
        ;das
        ;sbb [liczba4+1],0
        ;das
        ;sbb [liczba4],0
        ;das


        mov al,[liczba4+3] ; tab[i]
        sbb al,1 ; dodaj z przeniesieniem

        cmp al, 0FFh
        jne  ety1x1
        mov al,1
ety1x1:
        add al,30h
        mov [liczba4+3],al



 mov al,[liczba4+3] ; tab[i]
        sbb al,1 ; dodaj z przeniesieniem

        cmp al, 0FFh
        jne  ety1x1
        mov al,1
ety1x1:
        add al,30h
        mov [liczba4+3],al



cmp [liczba4],'0'
je koniec_mno|enia


loop mno|enie_gBówne

;mov al, cf
;cmp al,1

koniec_mno|enia:

   cmp [przen],1
   je gwiazdkowanie



   jmp gBówna
   ;jmp wynik1

gwiazdkowanie:
        ; mov [liczba3],'*'
         mov al,[gwiazdka]
         mov [liczba3+0],al
         mov [liczba3+1],al
         mov [liczba3+2],al
         mov [liczba3+3],al
         mov [liczba3+4],' '
         mov [liczba3+5],' '
         mov [liczba3+6],' '
         mov [liczba3+7],' '
         mov [liczba3+8],' '

     jmp gBówna

segment dane
liczba1 db '         $'
liczba2 db '         $'
liczba3 db '         $'
pomocnicza db '        $'
tekst db 'TESTOWY$'
text1 db 'Dana1 = $'
text2 db 'Dana2 = $'
text3 db 'Wynik = $'
text4 db '  *     $'
text5 db '  =     $'
text6 db 'Co chcesz zrobic?$'
text7 db ' <L> - wybor dane1; <P> - wybor dane2; <W> - wykonanie operacji; <Q> - wyjscie$'
text9 db '------------------------------------------------------------------------------$'
text10 db 'Wybor: $'
tmp db ' $'
tmp1 db 0
przerwa db '  $'
spacja db ' $'
gwiazdka db '*$'
xyz db 45h
przen db 0
liczba4 db 0,0,0,0    



"mnożenie_główne:" label starts multiplication code Wink

Info:
L - enter the first number
P - Enter the second number
W - Show result
Q - Exit
Post 21 Dec 2013, 01:51
View user's profile Send private message Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1180
Location: Unknown
HaHaAnonymous
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 19:00; edited 1 time in total
Post 21 Dec 2013, 03:08
View user's profile Send private message Reply with quote
dogman



Joined: 18 Jul 2013
Posts: 114
dogman
No! BCD representations of decimal values are EXACT. Binary is an approximation of decimal values. If you do money calculations with floating point expect to lose your ass in court at some point.
Post 21 Dec 2013, 18:01
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
dogman,

In integer domain, they're equivalent. Rational numbers are harder (1/5 can't go binary, 1/3 isn't expressible by BCD too).
Post 21 Dec 2013, 18:23
View user's profile Send private message Reply with quote
dogman



Joined: 18 Jul 2013
Posts: 114
dogman
That's why I said "BCD representations of decimal values are EXACT." Please read before disagreeing Wink
Post 22 Dec 2013, 16:11
View user's profile Send private message Reply with quote
bipocich



Joined: 17 Nov 2013
Posts: 18
bipocich
HaHaAnonymous, THX for your response Wink

Yes, multiplying BCD Very Happy
Quote:
Isn't better to convert it to binary, multiply and then convert it to BCD again?

Good idea, if you help me to implement it(code), I will be happy Very Happy

Spox, HaHaAnonymous - no problem Smile
Post 22 Dec 2013, 21:34
View user's profile Send private message Reply with quote
bipocich



Joined: 17 Nov 2013
Posts: 18
bipocich
dogman, THX for your response and comments Wink


Quote:
BCD representations of decimal values are EXACT

Yes, it's true.
Quote:
If you do money calculations with floating point expect to lose your ass in court at some point.

??:>
Post 22 Dec 2013, 21:40
View user's profile Send private message Reply with quote
bipocich



Joined: 17 Nov 2013
Posts: 18
bipocich
baldr, THX for your response and attention Wink
Post 22 Dec 2013, 21:49
View user's profile Send private message Reply with quote
bipocich



Joined: 17 Nov 2013
Posts: 18
bipocich
dogman, THX for your response and attention Wink

dogman, baldr and others,
Can anybody help me in this forum, rather than to argue about bits??XD

THX.
Post 22 Dec 2013, 21:53
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17279
Location: In your JS exploiting you and your system
revolution
You can use the FBLD and FBSTP instructions to do the transfers to/from the FPU and perform the computations there. You can get a full 64-bits of precision that way.

Otherwise if you need more precision or don't want to use the FPU then there are AAA, AAD, AAM and AAS instructions for low density BCD coding. Plus DAA and DAS for higher density storage.

IMO it is always easier to store numbers and values natively in binary and do computations natively. And then only for presentation purposes would you need to convert to some form of ASCII/BCD representation.
Post 22 Dec 2013, 22:05
View user's profile Send private message Visit poster's website Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
bipocich wrote:
dogman, baldr and others,
Can anybody help me in this forum, rather than to argue about bits?
Well, your source doesn't compile.

Show us some efforts; nobody wants to do your homework instead of you, but somebody will help if you'll behave appropriately (i.e. present compilable source, indicate exact location of the problem instead of "it doesn't work", confirm that you take some measures to solve that problem yourself).
Post 23 Dec 2013, 00:10
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.