How to use the div instruction to find remainder in x86 assembly?

℡╲_俬逩灬. 提交于 2020-01-21 11:40:09

问题


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

回答1:


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............|



回答2:


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


来源:https://stackoverflow.com/questions/16403941/how-to-use-the-div-instruction-to-find-remainder-in-x86-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!