flat assembler
Message board for the users of flat assembler.

Index > Main > unsigned divide, round up?

Author
Thread Post new topic Reply to topic
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.
Post 08 Feb 2011, 13:11
View user's profile Send private message Reply with quote
edfed



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




mov dx,ax
and dx,0fh
je @f
add ax,16
@@:
shr ax,4
Post 08 Feb 2011, 13:21
View user's profile Send private message Visit poster's website Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17287
Location: In your JS exploiting you and your system
revolution
Add 15 to your numerator.
Code:
mov eax,[myvalue]
add eax,15
shr eax,4
mov [myresult],eax    
Post 08 Feb 2011, 13:46
View user's profile Send private message Visit poster's website Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4238
Location: 2018
edfed
Smile
no jcc, cool!!!
Post 08 Feb 2011, 14:12
View user's profile Send private message Visit poster's website Reply with quote
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
adc ax, 0
and ax, 0x0FFF
;ax = output
Post 08 Feb 2011, 20:47
View user's profile Send private message Reply with quote
Hrstka



Joined: 05 May 2008
Posts: 17
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.
Post 14 Feb 2011, 20:12
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. Also on YouTube, Twitter.

Website powered by rwasa.