flat assembler
Message board for the users of flat assembler.

Author
2

Joined: 26 Sep 2006
Posts: 92
2
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.
08 Oct 2006, 07:20
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 18071
revolution
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.
08 Oct 2006, 08:39
Goplat

Joined: 15 Sep 2006
Posts: 181
Goplat
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
```
08 Oct 2006, 17:36
2

Joined: 26 Sep 2006
Posts: 92
2
That's cool,but I'm a little lost. What does rcl do and why is dx left shifted?
10 Oct 2006, 00:54
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

 Jump to: Select a forum Official----------------AssemblyPeripheria General----------------MainTutorials and ExamplesDOSWindowsLinuxUnixMenuetOS Specific----------------MacroinstructionsOS ConstructionIDE DevelopmentProjects and IdeasNon-x86 architecturesHigh Level LanguagesProgramming Language DesignCompiler Internals Other----------------FeedbackHeapTest Area

Forum Rules:
 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot vote in polls in this forumYou cannot attach files in this forumYou can download files in this forum