Write an ARM assembly language program which implements a loop to calculate the
ID: 3734733 • Letter: W
Question
Write an ARM assembly language program which implements a loop to calculate the integer division of two unsigned numbers stored in r2 and r1 (i.e., computes r2/r1), and store the result in r0. You can do this by counting successive subtractions of the divisor (r1) from the dividend (r2) until the difference is less than the divisor. Your code should not destroy the values of r1 or r2
Explanation / Answer
MOV R0,#0 ;clear R0 to accumulate result MOV R3,#1 ;set bit 0 in R3, which will ;shifted left then right .start CMP R2,R1 MOVLS R2,R2,LSL#1 MOVLS R3,R3,LSL#1 BLS start .next CMP R1,R2 ;carry set if R1>R2 (don't ask why) SUBCS R1,R1,R2 ;subtract R2 from R1 if this would ;give a positive answer ADDCS R0,R0,R3 ;and add the current bit in R3 to ;the accumulating answer in R0 MOVS R3,R3,LSR#1 ;Shift R3 right into carry flag MOVCC R2,R2,LSR#1 ;and if bit 0 of R3 was zero, also ;shift R2 right BCC next ;If carry not clear, R3 has shifted ;back to where it started, and we ;can end .divide_end MOV R25, R24 ;exit routine
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.