; Written by: Deepak Kumar. March 25, 2024 ; File: divSubroutine.asm ; Program to divide two numbers by repeated subtraction to get quotient and remainder ; Algorithm ; A = 22 ; B = 7 ; quotient = 0 ; while A >= B do ; A = A - B ; q = q + 1 ; remainder = A .ORIG x6000 A .FILL 22 B .FILL 7 .END ; ; The main program ; .ORIG x600A START LD R2, A LD R3, B JSR DIVMOD HALT ; DIVMOD ; R2 contains dividend (A), R3 contains divisor (B) ; Result is put in R0 (Quotient) and R1 (remainder) ; R4 is used to store -R3 ; R5 is used to store the result of subtraction (A - B) ; Registers: R0 and R1 will return results. ; DIVMOD ADD R1, R2, #0 ; R1 = R2 (A) NOT R4, R3 ; R4 = -R3: R4 = NOT(R3) and Adding 1 to R4 ADD R4, R4, #1 AND R0, R0, #0 ; R0 = 0 ; while R1 >= R3 : R5 = R1 - R3 : R5 = R1 + R4 LOOP ADD R5, R1, R4 BRn DONE ; do ADD R1, R1, R4 ; R1 = R1 - R3 ADD R0, R0, #1 ; R0 = R0 + 1 BR LOOP DONE RET .END