Algoprog
Algoprog

Reputation: 21

How does division work in MIXAL assembly?

I am trying to perform a simple integer division (9/2=?) but MIX builder throws Integer overflow error. Am I doing something wrong? Here is the code:

ORIG    1000
START   NOP
A   CON 0
B   CON 0
ENTA    2
STA A
ENTX    9
DIV A
STA A
HLT
END START

Upvotes: 0

Views: 216

Answers (2)

Rutger van Bergen
Rutger van Bergen

Reputation: 11

That is because rA is not only used to store the quotient, but also considered part of the number that is divided. As specified at the bottom of page 131 of volume 1 of TAOCP, under the DIV bullet: "The value of rA and rX, treated as a 10-byte number rAX with the sign of rA, is divided by the value V." It further states: "If V = 0 or if the quotient is more than five bytes in magnitude (this is equivalent to the condition that |rA| >= |V|), registers A and X are filled with undefined information and the overflow toggle is set on." In the case of your code, rA is set to 2, and then stored at the address where the divisor ("V") is taken from. This means that rA == V, triggering the condition just described.

The reason I remember this is that, due to the treatment of rA and rX as a 10-byte number in the case of DIV, this operator was quite a pain to implement in MixEmul. :)

Upvotes: 1

Algoprog
Algoprog

Reputation: 21

The solution was to add ENTA 0 (set register rA = 0) before DIV A. I really don't know the reason why you have to reset the register where the quotient is saved...

Upvotes: 0

Related Questions