Question

I have a question on assembly x86 architecture.

INCLUDE Irvine32.inc
.data
day BYTE 0
month BYTE 0
year BYTE 0

a BYTE 0
y BYTE 0
m BYTE 0
count BYTE 0

prompt1 BYTE "enter month: ",0
prompt2 BYTE "enter day: ",0
prompt3 BYTE "enter an year: ",0
prompt4 BYTE " the day of the week is ",0
.code
main PROC

mov edx, OFFSET prompt1
call writeString
mov eax, OFFSET month
mov ecx, 19
call readInt
call crlf

mov edx, OFFSET prompt2
call writeString
mov eax, OFFSET day
mov ecx, 19
call readInt
call crlf

mov edx, OFFSET prompt3
call writeString
mov eax, OFFSET year
mov ecx, 19
call readInt
call crlf

mov al, 14
sub al, month
mov bl, 12
div bl
mov a, al
mov al, 0

mov ah, year
sub ah, a
mov y, ah
mov ah, 0

mov bh, 12
mul a
add bh, a
mov ch, 2
sub bh, ch
mov m, bh
mov bh, 0
mov ch, 0

mov al, 31
mul m
mov ah, 12
div ah

mov bl, y
mov bh, day
add bh, y

mov cl, y
mov ch, 4
div ch
add bh, cl

mov dl, y
mov dh, 4
div dh
add bh, cl
add bh, al
mov al, 0

mov al, 7 // mod 7
div al



    exit
main ENDP

END main

So I am supposed to do these arithmetic equations:

a = (14 - month) / 12
y = year - a
m = month + (12 * a) - 2
d = (day + y + y/4 - y/100 + y/400 + (31 * m / 12)) % 7

For the last equation of mod 7, I got an error, "integer overflow", this is due to using bl I think.

If I do something like this

mov ax, "thing in parenthesis" 
mov cl, 7
div cl      

then I would get al is quotient, ah is remainder.

But how would I do mod without having to use ax register?

Was it helpful?

Solution

When dividing by a byte value you're dividing AX and storing the quotient in AL. Therefore you need to make sure that the quotient will fit in 8 bits, or you get the error that you describe in your question.

One way of accomplishing this is to clear AH before the division. I.e.

MOV AH,0

or

XOR AH,AH

For a signed division (IDIV) you'd use CBW to sign-extend AL into AX.

If your question was how to fix your issue without having to touch AH, then the answer is "you can't". There's no point to it anyway since AH still will be modified by the DIV instruction (to hold the remainder).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top