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
bluebk
  • 169
  • 1
  • 6
  • 20

2 Answers2

4

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,395
  • 6
  • 34
  • 50
  • @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
  • 20