5
mov eax, 0
mov ebx, 0
mov edx, 0
mov ax, 31
mul cx
mov bx, 12
div bx
add bp, ax
mov eax, 0
mov ebx, 0
mov bp, bp
mov al, 7
div al

can anyone tell me whats wrong with the div al instruction in this block of code, so as I'm debugging every number of bp i calculated, when i divide by al it give me 1 as the remainder, why is this happen?

the remainder should be store back to ah register

thank in advance

edited code :

mov eax, 0
mov ebx, 0
mov edx, 0
mov ax, 31
mul cx
mov bx, 12
div bx
add bp, ax
mov eax, 0
mov ebx, 0
mov ax, bp
mov bl, 7
div bl
mov al, 0
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
bluebk
  • 169
  • 1
  • 6
  • 21
  • `mov reg, 0` would be better written as `xor reg, reg` – phuclv Oct 22 '13 at 02:31
  • 2
    How to without requiring `div`: http://stackoverflow.com/questions/8021772/assembly-language-how-to-do-modulo GCC 4.8 does not use div by default because it is slow: http://stackoverflow.com/questions/4361979/how-does-the-gcc-implementation-of-module-work-and-why-does-it-not-use-the – Ciro Santilli OurBigBook.com Oct 20 '15 at 20:30

2 Answers2

5

You can't use al as divisor, because the command div assumes ax to be the dividend.

This is an example for dividing bp by 7

mov ax,bp // ax is the dividend
mov bl,7  // prepare divisor
div bl    // divide ax by bl

This is 8 bit division, so yes the remainder will be stored in ah. The result is in al.

To clarify: If you write to al you partially overwrite ax!

|31..16|15-8|7-0|
        |AH.|AL.|
        |AX.....|
|EAX............|
typ1232
  • 5,535
  • 6
  • 35
  • 51
  • @bluebk where do you get integer overflow? you should not write anything to al if you want to divide bp by something, because you will overwrite ax (the dividend) – typ1232 May 06 '13 at 17:56
  • i got integer over flow at div bl instruction in the edited code – bluebk May 06 '13 at 17:57
  • @bluebk well then maybe this is because your result does not fit into `al`. If `bp` is anything greater than 0x700 you will get the integer overflow – typ1232 May 06 '13 at 18:06
  • my bp for example is 9E8, then should i use bx instead of bl? – bluebk May 06 '13 at 18:08
  • @bluebk you can't do a 8 bit division of 9b8 by 7. the result is greater than 0xff. – typ1232 May 06 '13 at 18:10
1

edited code:

mov eax, 0
mov ebx, 0
mov ax, 31
mul cx
mov bx, 12
div bx
add bp, ax
mov eax, 0
mov ebx, 0
mov edx, 0
mov ax, bp
mov bx, 7
div bx
mov esi, edx 
mov eax, 0
bluebk
  • 169
  • 1
  • 6
  • 21