Works very simmilar to multiplcation, except for where the answer goes.
Recall
mov rax, 5mov r10, 5mul r10
The answer will be stored in rax and rdx (well, not really, cause its not large enough, but you get the idea)
Division is different because: rax holds the answer rdx holds the remainer
Thus, there is no difference between the / and % operators, they are both div and idiv
Furthermore, you must assure that while the second register is not needed for the operation, it must be cleared.
For unsigned division, you can set the second register to 0.
For signed division, use the following commands:
cbw ; converts a byte to a wordcwd ; converts a word to a dwordcdq ; converts a dword to a qwordcqo ; converts a qword into a dqword
Examples
Caution: The following examples are unchecked and may be incorrect. Verify important info.
byte = word/byte
mov dx, 0mov ax, word[x]movzx, byte[y]div cx moves frmo word/byte to word/wordresult is still a bytemov byte[answer], almov byte[remained], dl
mov al, byte[x]cbw (this will properly sign extend al into ah, only used when you dont know the value of ah? check this)idiv byte[y]mov byte[answer], almov byte[remainder], ah