Hi there.

I am writing a routine in PowerBASIC assembler that does some division, and I thought I'd take the opportunity to practise dividing in the ASM level.

Basically I have to divide a number (lSeconds) with 3600. I then have to retrieve the result, but not the remainder, into dblHours.




Local lHours As Long

! mov EAX, 3600
! mov ECX, lSeconds ; lSeconds eg 3661
! div ECX
! mov lHours, EAX ; lHours sould equal 1. There will be a remainder, but I don't need it.


When I reach the line ! div ECX and try to execute it, an exception is raised and the program halts. Can someone point out the error I've made here, admittedly I'm new to using the DIV syntax, so I'm not sure if I'm using the registers properly.

Many thanks.
Jas.
Posted on 2003-12-28 17:50:42 by MrClyfar
Try following.

xor edx,edx
mov eax, 3600
mov ecx, lSeconds
div ecx

When doing a division you must first clear edx.
Posted on 2003-12-28 18:47:53 by JimmyClif
xor edx,edx

mov eax, 3600
mov ecx, ISeconds
div ecx ; does: 3600/ISeconds
mov IHours, eax

; but you wanted to divide ISeconds by 3600, so this is the way to do it:
mov ecx, 3600
mov eax, ISeconds
div ecx ; does: ISeconds/3600
mov IHours, eax


(div reg32 ; edx:eax / reg32)
Posted on 2003-12-28 19:26:31 by scientica
Hi again.

Thanks for that, xor EDX, EDX did allow the div call to work. Though I'm not too sure how to get the answer! I've looked at EAX, but after div ECX call, EAX = 0. And EDX = 3600.

I tried using DL and DH to see what values where in there, but neither of them were correct.

As test if you use lSeconds = 7661



! xor EDX, EDX ; EDX = 0
! mov EAX, 3600
! mov ECX, lSeconds ; ECX = 7661
! div ECX ; 7661 / 3660 = 2.128055555
! mov lHours, EAX ; EAX = 0 EDX = 00000E10 = 3600


What I was expecting to find was the answer 2 in one of the registers. The remainder isn't important in this case. As I've mentioned above, finding the 2 has proved elusive, can someone tell me which register I should be looking at for the answer?

Thanks.
Jas.
Posted on 2003-12-29 06:47:24 by MrClyfar
Your method is wrong


mov eax, ISeconds
mov ecx, 3600
cdq
idiv ecx
;eax = answer
;edx = remainder


Btw did you look into scientica's reply?
Posted on 2003-12-29 07:16:44 by roticv
Hi again.

Cheers for that, it worked a treat. I had checked out the previous replies, but having only just woken up, I guess I missed the obvious clues in scientica's reply! Appologies for that - schoolboy error :)

Many thanks.
Jas.
Posted on 2003-12-29 07:31:00 by MrClyfar

Cheers for that, it worked a treat. I had checked out the previous replies, but having only just woken up, I guess I missed the obvious clues in scientica's reply! Appologies for that - schoolboy error :)

hehe :) No problem, it happes to all of us who haven't had a big cup of coffee yet.
Posted on 2003-12-29 18:30:44 by scientica