Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

x86 - 8086 assembly on DOSBox: Bug with idiv instruction?

I was helping a friend of mine debug his program, and we narrowed it down to an issue which occurs even here:

.MODEL small
.STACK 16
.CODE
start:
    mov ax, 044c0h
    mov bl, 85
    idiv bl
exit:
    mov ax, 4c00h
    int 21h

end start

After assembling it with tasm 4.1, and running it on DOSBox 0.74, it goes into an infinite loop. When inspecting it with turbo debugger one can see it happens after the idiv instruction, which for some reason modifies the cs and ip registers, and after two seemingly random instructions restores them to point to the idiv line, executing it again ad infinitum.

Does anyone have any explanation for this?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This question is a variation on other division related failures. The x86 tag wiki has some additional links:


The apparently random code your debugger seems to jump to is the Arithmetic Exception handler (also the same one as Divide by Zero). What is happening is that your code is experiencing a Division Overflow. You are doing a 16-bit/8-bit IDIV. From the documentation:

Signed divide AX by r/m8, with result stored in: AL ← Quotient, AH ← Remainder.

enter image description here

You will notice that for division with an 8-bit divisor (in your case BL) the range for the quotient is -128 to +127. 044c0h IDIV 85 is 207 (decimal). 207 doesn't fit in a signed 8-bit register so you get division overflow and the cause of your unexpected problem.

To resolve this you can move up to a 16-bit divisor. So you can place your divisor in BX (16-bit register). That would be mov bx, 85. Unfortunately it isn't so simple. When using a 16-bit divisor the processor assumes the dividend is 32-bits with high 16-bits in DX and lower 16-bits in AX.

Signed divide DX:AX by r/m16, with result stored in AX ← Quotient, DX ← Remainder.

To resolve this you have to sign extend the 16-bit value in AX. This is simple as you only need to use the CWD instruction after placing the value in AX. From the instruction set reference

DX:AX ← sign-extend of AX.

Effectively if the Most Significant Bit (MSB) of AX is 0 DX will become 0. If the MSB is 1 then DX would become 0ffffh (all bits set to one). The sign bit of a number is the MSB.

With all this in mind your division code could be adjusted to take a 16-bit divisor:

mov ax, 044c0h
cwd                ; Sign extend AX into DX (DX:AX = 32-bit dividend)
mov bx, 85         ; Divisor is 85
idiv bx            ; Signed divide of DX:AX by BX

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...