flat assembler
Message board for the users of flat assembler.

Index > DOS > Very curious about something I read.

Author
Thread Post new topic Reply to topic
2



Joined: 26 Sep 2006
Posts: 92
2 08 Oct 2006, 07:20
I read somewhere about old processors not having a DIV instruction and using
shifts for multiplication and division.

It's easy to see that you could use shifts for mutliplying by ten,but what about
dividing by ten? I normally wouldn't bother but since we have ten as the
standard base that all non programmers already understand, I was thinking
about it and wondered what the quickest way of doing it was.

So,can you divide by ten with only shifts,addition,and subtraction?

_________________
There are 10 kinds of people in the world.
Those who know binary and those who haven't met me.
Post 08 Oct 2006, 07:20
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20363
Location: In your JS exploiting you and your system
revolution 08 Oct 2006, 08:39
Quote:
can you divide by ten with only shifts,addition,and subtraction?
Yes, this is how division is done by the CPU internally. Now days it is a little bit faster with double-bit estimation etc. but basically it is still a compare-subtract-shift algorithm.

For the specific case of division by ten we can do better than the div instruction by multiplying by an approximate reciprocal and then shifting down to get the quotient. There are already some topics on this board showing how it is done.
Post 08 Oct 2006, 08:39
View user's profile Send private message Visit poster's website Reply with quote
Goplat



Joined: 15 Sep 2006
Posts: 181
Goplat 08 Oct 2006, 17:36
On the 6502 processor that was used in the C64, Apple II, and various other computers there was not only no divide, but no multiply either. A common way of dividing was doing something like this:

Code:
        LDA     #0
        LDX     #8
divlp:  ASL     number
        ROL
        CMP     #10
        BCC     low
        SBC     #10
        INC     number
low:    DEX
        BNE     divlp
    


This would be about the x86 equivalent of it (though of course it would be pointless to actually do this)
Code:
        mov     al,0
        mov     cx,16
divlp:  shl     dx,1
        rcl     al,1
        cmp     al,10
        jb      low
        sub     al,10
        inc     dx
low:    loop    divlp
    
Post 08 Oct 2006, 17:36
View user's profile Send private message Reply with quote
2



Joined: 26 Sep 2006
Posts: 92
2 10 Oct 2006, 00:54
That's cool,but I'm a little lost. What does rcl do and why is dx left shifted?
Post 10 Oct 2006, 00:54
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.