Reputation: 69
Basic question:
I wrote the following block of code:
DATASEG
Result db ?
Modulo db ?
var3 db -9
var4 db 2
CODESEG
start:
mov ah, 0
mov al, [var3]
mov bl, [var4]
div bl
mov [Result], al
mov [Modulo], ah
I get wrong result for -9/2. The result I get is "7B", seems that it treats "F7" as 247.
How can I get this done correctly while still defining var3 as a databyte (db)?
Or, is there any other way?
Thanks for answers
Upvotes: 2
Views: 151
Reputation: 39166
You must use idiv
to perform a signed division.
div
will not correctly process the negative number in var3
mov al, [var3]
cbw
idiv byte ptr [var4]
From your comments I see that you don't like the cbw
instruction and that you don't want to use the shortest code for the division instruction. You can always code it like:
mov al, [var3]
mov ah, FFh ;Only if you 'know' var3 has a negative value!
mov bl, [var4]
idiv bl
mov [Result], al
mov [Modulo], ah
Alternatively and still avoiding cbw
:
movsx ax, byte ptr [var3]
mov bl, [var4]
idiv bl
mov [Result], al
mov [Modulo], ah
Upvotes: 3