没有div的ASM 8086分区 [英] ASM 8086 division without div
问题描述
我需要在asm 8086中编写类似b = a/6的程序,但没有DIV指令.我知道如何使用SAR,但是只有2,4,8,16 ...
I need to write in asm 8086 a program like b=a/6 but without the DIV instruction. I know how to do it with SAR but only 2,4,8,16...
mov ax,a
sar ax,1 ;//div a by 2
mov b,ax
我的问题是如何将其除以6?
my question is how can I do it to div by 6?
推荐答案
给出另一个答案的方法很简单强制循环,对于a
的较大值可能需要一段时间.此版本使用较大的块(类似于长除法问题),专门编码以将有符号数除以6:
The approach given an another answer is simple brute force loop, and can take a while for large values of a
. This is a version that uses larger chunks (working it like a long division problem) specifically coded to divide a signed number by 6:
; signed divide by 6
mov ax,a
mov cx,1000h ; initial count of how many divisors into ax to check for
mov bx,6000h ; value of "divisor * cx"
xor dx,dx ; result
top:
cmp ax,bx
jl skip
; we can fit "cx" copies of the divisor into ax, so tally them
add dx,cx
sub ax,bx
; optionally can have a "jz done" here to break out of the loop
skip:
shr bx,1
shr cx,1
jnz top
; copy result into ax
mov ax,dx
如果需要除6以外的值,则需要调整初始cx
和bx
值. cx
是设置位14的除数的2的幂次幂(因为位15是符号位;对于无符号除法,您希望设置位15). bx
是2的幂.如果a
的初始值有限制,则可以调整cx
和bx
的初始值,但必须小心,因为如果您输入错误,将会得到错误的答案.使它们过小.
If you need to divide something other than 6, the initial cx
and bx
values need to be adjusted. cx
is the power-of-two multiple of the divisor that leaves bit 14 set (since bit 15 is the sign bit; for an unsigned divide you'd want to have bit 15 set instead). bx
is that power of 2. If there are limits on the initial value for a
you can adjust the initial cx
and bx
values, but have to be careful because you'll get an incorrect answer if you make them too small.
这篇关于没有div的ASM 8086分区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!