Message board for the users of flat assembler.
> Main > unsigned divide, round up?
Hi, i need to divide 2 16bit numbers and round up to the next integer.
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||
|08 Feb 2011, 13:21||
Add 15 to your numerator.
mov eax,[myvalue] add eax,15 shr eax,4 mov [myresult],eax
|08 Feb 2011, 13:46||
no jcc, cool!!!
|08 Feb 2011, 14:12||
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
|08 Feb 2011, 20:47||
My favourite way for rounding up:
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
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,
mov cl, 4 shr ax, cl
|14 Feb 2011, 20:12||
< Last Thread | Next Thread >
Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.
Website powered by rwasa.