2

I am trying to divide two single byte numbers, and then trying to get the quotient and remainder afterwards (placing them in single byte variables).

Here's my code so far:

;divide 8-bit number by the number 10
mov ax, [numb2]
mov cl, 10
div cl

;get quotient and remainder 
mov byte[quotient], al
mov byte[remainder], ah

The quotient is stored in the al, and the remainder is stored in the ah, right?

After running it, I get a "Floating point exception (core dumped)" from the console.

What's wrong with my code?


edit: the quotient, remainder, and numb2 variables are 8-bits


using Ubuntu x86 -- NASM

Kevin Lloyd Bernal
  • 355
  • 3
  • 8
  • 24

3 Answers3

2
;divide 8-bit number by the number 10
mov ax, [numb2]
mov cl, 10
xor ah,ah ;; add this line  this function allows to clear ah
div cl

;get quotient and remainder 
mov byte[quotient], al
mov byte[remainder], ah
Mahesh Uligade
  • 597
  • 8
  • 17
1

You can't move an 8-bit value into a 16-bit register with "mov". The CPU will pull in 16-bits starting at the memory offset 'numb2'. Whatever it's pulling in is too large to fit in al after the div. You should use:

mov al,byte ptr [numb2]  ;tasm/masm

OR

mov al,byte [numb2]      ;nasm

xor ah,ah
mov cl,10
div cl

Per comments: use "byte ptr" or "byte" depending on your assembler. But it's always good practice to specify the object's size. Otherwise, the assembler has to infer the object's size based upon the registers used.

0

I solved that problem by using the extended version of the registers (EAX,EBX,ECX,EDX). The remainder is stored in EDX and the quotient in EAX