In the book I am studying I am confronted with the following program when it tries to explain div and mul (only part of the program here that is relevant):

opWord DW 100
sourceByte DB 50
sourceWord DW 4000



mov ax,
div ; al <-- ax div 2 right!?

mov ax,
mov dx,
div ; ax <-- ax,dx div

So my questions;

The first part is right isn't it. The answer being 2.

How about the second part is it ax*dx/ i.o.w. 50*50/4000? Or am I misinterpreting the way it works here? Any help would be greatly appreciated.

// SuperCali
Posted on 2001-09-07 18:39:36 by SuperCali
The only thing your missing is that dx:ax is divided, and that means dx*(2^16) + ax . dx:ax means a 32-bit value, dx as the most significant word and ax is the least significant word. The answer is 819.

(Damn child f**ked up my key_oard!) I can't type the letter "b" - I got that by cut-n-paste! So, now everytime I want to type the letter "b", I have to hit Ctrl-v! Arrr! I'm pissed! :mad:
Posted on 2001-09-07 18:56:52 by bitRAKE
Thanks for the help. At first I didn't understand how you got 819 but then I relised I had typed the text wrong if one looks at the code it's opWord (which is 100 not 50) that is injected into ax and dx so the answer would be twice 819.

Just a little follow up. I am now learning 16bit assembler but seeing as I will move on to 32bit once finished, I have another question. Would the two 16bit registers ax and dx then make up the 32bit register eax? And if so this would be an easier way to write the above code in 32bit:

mov eax, 6553700

// SuperCali
Posted on 2001-09-07 19:24:15 by SuperCali
bbbbbb, ah the "b" works now! :) Just took the keyboard apart, but not before being covered in water! It was full of water, and still mostly worked, wow!

To make a long story short: there are many versions of the DIV/IDIV instructions, look on page 3-84 of Intel's Volume 2: Instruction Set Reference. The last DIV instruction that isn't above, divides EAX:EDX (a 64-bit number). DX:AX does not equal EAX. Each register is extended to 32-bits.
Posted on 2001-09-07 19:35:45 by bitRAKE
In the old days (pre 386) registers were 16 bits, AX BX etc. You could use these registers as 1 16 bit word or 2 8-bit bytes (AH and AL are the 2 halves of AX). In order to support 32-bit values for multiply and divide, the DX and AX registers are combined.

Enter the 386. All registers (except segment) are now extended to 32-bit, as in EAX. All the old instructions that deal with AX AH and AL still work the same. But a bunch of new instructions to deal with the "extended" 32-bit registers were introduced.

The secret to DIV is in the size of the divisor. If the divisor is 8 bits, the dividend is assumed to be the 16-bit number in AX. The quotient is returned in AL and the remainder in AH. If the divisor is 16 bits, the dividend is assumed to be the 32-bit number in DX:AX. The quotient is returned in AX and the remainder in DX. And starting with the 386, if the divisor is 32 bits, the dividend is assumed to be the 64-bit number in EDX:EAX. The quotient is returned in EAX and the remainder in EDX.

Posted on 2001-09-07 21:30:57 by S/390

bbbbbb, ah the "b" works now! :) Just took the keyboard apart, but not before being covered in water! It was full of water, and still mostly worked, wow!

Coulda been worse. I know someone who's cat peed in his keyboard. It ended up in a pool at his feet when he turned the keyboard over ! :p

Posted on 2001-09-07 21:57:39 by Kayaker

As bit rake is getting at,

"e" == two times ' '

so EAX == two times 'ax' (or 32 bits), which is DOUBLE the AX register. (Hence the term DOUBLE or DWORD to refer to all 32 bit varibables).

Every varariable you see in 16 bit has an "e" in 32 bit. They still behave the same, have same functions with other intructions, except in MUL/DIV it's usage migrates to a new level (yet simular in nature to how they worked in 16 bit apps). I have a discussion on it posted on my web page... ( click the WWW at the bottom)

My only warning is to truely learn 16 bit, you have to get all tied up with interupts, and Segment/offset addressing modes. These are both complicated in nature (and takes energy to learn), and are completely useless in 32 apps. (int is use a bit, but only at very advanced stuff). So i thought i would let you know now to take what you can from 16 bit, but if your struggling on these issues, drop em, mov on to 32 bit, and come back to them when you feel "advanced"... as 32 bit programming is far easier as a whole...

PS: back to the register stuff... in extended 32 bits:

The standard 4 "A","B","C","D", reg's in extended mode still suport 16 bit access:

EAX = 12345678 h


al == 78h
ax == 5678h
eax == 12345678h

(there is no upper-ax, to get this you shift it down 16 bits, and then acces it from 'ax')

shr eax, 16 ; BTW: you can do real #'s in 32, but not in 16 bit
ax == 1234h
al == 34h

The rest of the registers, si, bp, di, sp, etc. are now in extended mode only. (esi, ebp, edi, esp, etc...) which are 32 bit values.

This is a brief intro.. there is still alot more, but i thought i would "try" to give you a heads up, if i could...

Posted on 2001-09-08 00:49:59 by NaN
Thanks for all the help, this bard is a real life saver. Also NAN I appreciate the advice and it sounds like I will do as you suggest and not go to deep into interupts, and Segment/offset addressing modes unless of course I find them not to difficult. The only reason I am looking at 16-bit assembler is because the book I have happens to deal only with 16-bit TASM. Also I heard a few people recommend I start wiht c or c++ first but I considered if I wanted to program in assembler why complicate the matter by first learning a hig-level language. If I need to learn it later then so be it but I at least can see no problem with starting with assembler.

By the way on to related question. From what I have gathered by reading posts on this board and miscellaneous other texts is that there is a difference in programming assembler using MASM and Spasm etc. Is this difference considerable so as to warrant looking at only one and sticking wiht it, i.e. when I get to 32bit asembler should I for example pick one (like MASM) and stick to it? And if so which would be best to look at. My main goal being in the end to program mainly for windows 32bit but do some work in unix (bsd mainly) and SPARC assembler. Well Thnaks for all the help so far.

// SuperCali
Posted on 2001-09-08 06:03:34 by SuperCali