flat assembler
Message board for the users of flat assembler.

 Index > Main > unsigned divide, round up?
Author
bitshifter

Joined: 04 Dec 2007
Posts: 764
Location: Massachusetts, USA
bitshifter
Hi, i need to divide 2 16bit numbers and round up to the next integer.

For example:
16 / 16 = 1
17 / 16 = 2

Now i explain the purpose:
I am writing a real-mode paragraph allocator.
The client code may ask for byte-size memory block.
The minimum allocation block is one paragraph.
My allocator should return a segment ptr to paragraph (offset = 0)
(Kinda like a _seg* in C language)

So it needs to be 8086 instructions.
I would like not to use DIV if possible (for speed reasons)
But if it turns out to be a better way then so be it...
I thought of using SHR/ADC but SHR only set CF with last bit shifted out.
Maybe you have a simple solution?

Thanks for your time.

_________________
Coding a 3D game engine with fasm is like trying to eat an elephant,
you just have to keep focused and take it one 'byte' at a time.
08 Feb 2011, 13:11
edfed

Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
div 16
cmp dx,0
je @f
inc ax
@@:

mov dx,ax
and dx,0fh
je @f
@@:
shr ax,4
08 Feb 2011, 13:21
revolution
When all else fails, read the source

Joined: 24 Aug 2004
Posts: 17716
Location: In your JS exploiting you and your system
revolution
Code:
```mov eax,[myvalue]
shr eax,4
mov [myresult],eax    ```
08 Feb 2011, 13:46
edfed

Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed

no jcc, cool!!!
08 Feb 2011, 14:12
b1528932

Joined: 21 May 2010
Posts: 287
b1528932
So it needs to be 8086 code, so no shr using imm8 there. and no eAX

considering that you divide only by 16:

;ax = input
test ax, 0x000F
mov cl, 4
ror ax, cl
and ax, 0x0FFF
;ax = output
08 Feb 2011, 20:47
Hrstka

Joined: 05 May 2008
Posts: 19
Location: Czech republic
Hrstka
My favourite way for rounding up:
Code:
```neg ax
and ax, -16    ; round up to next paragraph
neg ax    ```

I'm not sure if the neg instruction exists on 8086, but neg ax can be replaced with
Code:
```xor ax, -1    ; = not ax
inc ax    ```

EDIT: I overlooked the div part, in this case use revolution's way. If shr ax, imm8 is not allowed,
Code:
```mov cl, 4
shr ax, cl    ```
should work.
14 Feb 2011, 20:12
 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