Difference between revisions of "Assembly Language Division"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with " INP R0,2 INP R1,2 MOV R2,#0 MOV R3,#1 LOOP: SUB R0,R0,R1 ADD R2,R2,R3 CMP R0,#0 BGT LOOP BEQ END SUB R2,R2,R3...")
 
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
      INP R0,2
+
1      MOV R0, #20
      INP R1,2
+
2     MOV R1, #3
      MOV R2,#0
+
3      MOV R2, #0
      MOV R3,#1
+
  4 LOOP:SUB R0,R0,R1
  LOOP: SUB R0,R0,R1
+
5      ADD R2,R2,#1
      ADD R2,R2,R3
+
6      CMP R0,#0
      CMP R0,#0
+
7      BGT LOOP
      BGT LOOP
+
8      BEQ END
      BEQ END
+
9      SUB R2,R2,#1
      SUB R2,R2,R3
+
10    ADD R3,R0,R1
  END: OUT R2,4
+
  11 END:HALT
      HALT
+
 
 +
Line 1 & 2 will get the two numbers to divide.
 +
 
 +
Line 3 will clear register 2 and make sure its set to zero.
 +
 
 +
Line 4 is the loop label and it will subtract the second number from the first.
 +
 
 +
Line 5 will increment register 2 to count the number of subtractions we make.
 +
 
 +
Line 6 compares register 0 with zero.
 +
 
 +
Line 7 if the compare is greater than we still have subtractions to make.
 +
 
 +
Line 8 if the compare was equal, then we have completed and the number divides exactly.
 +
 
 +
Line 9 is reached if the compare was less than zero, so the last subtraction was one too far, so minus 1 from register 2.
 +
 
 +
Line 10 is to workout the remainder.
 +
 
 +
Line 11 is the end label and will halt.
 +
 
 +
R2 will contain the result of the integer division.
 +
 
 +
R3 will contain the remainder.

Latest revision as of 12:16, 21 January 2020

1      MOV R0, #20
2      MOV R1, #3
3      MOV R2, #0
4 LOOP:SUB R0,R0,R1
5      ADD R2,R2,#1
6      CMP R0,#0
7      BGT LOOP
8      BEQ END
9      SUB R2,R2,#1
10     ADD R3,R0,R1
11 END:HALT

Line 1 & 2 will get the two numbers to divide.

Line 3 will clear register 2 and make sure its set to zero.

Line 4 is the loop label and it will subtract the second number from the first.

Line 5 will increment register 2 to count the number of subtractions we make.

Line 6 compares register 0 with zero.

Line 7 if the compare is greater than we still have subtractions to make.

Line 8 if the compare was equal, then we have completed and the number divides exactly.

Line 9 is reached if the compare was less than zero, so the last subtraction was one too far, so minus 1 from register 2.

Line 10 is to workout the remainder.

Line 11 is the end label and will halt.

R2 will contain the result of the integer division.

R3 will contain the remainder.