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
200 views
in Technique[技术] by (71.8m points)

32 bit division in assembly language

I'm learning assembly language programming in my college course (80386 programming). Currently, I'm learning the ins and outs of 32 bit division using the EDX and EAX registers to store the remainder and quotient respectively. I want to know, why do I not get the correct output when I don't include

mov edx, 00000000h

in my program. The program either doesn't run or gives the wrong output.

Relevant source code:

mov edx, 00000000h
mov eax, 1234567Ah
div 11111111h ; now EDX=remainder, EAX=quotient

I will post the full code if needed.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

When using DIV with a 32-bit operand, it will divide the 64-bit value in EDX:EAX (high DWORD in EDX, low DWORD in EAX) by the operand and put the quotient in EAX and the remainder in EDX.

Therefore you should clear EDX prior to the division if the value you want to divide is 32 bits (fits in EAX alone). Failure to do so will result in incorrect results, or a division overflow if the quotient doesn't fit in EAX.


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

...