W1.2: assignment 5: first try, not working
This commit is contained in:
@@ -4,15 +4,30 @@
|
|||||||
.globl multiply
|
.globl multiply
|
||||||
.text
|
.text
|
||||||
.thumb_func
|
.thumb_func
|
||||||
|
// R0 = a, R1 = b, R2 = result (m)
|
||||||
multiply:
|
multiply:
|
||||||
MOVS.N R2, #0 // set result to 0
|
MOVS.N R2, #0 // set result to 0
|
||||||
|
multiply2:
|
||||||
CMP.N R1, #0
|
CMP.N R1, #0
|
||||||
BEQ.N exit // goto exit if b == 0
|
BEQ.N exit // goto exit if b == 0
|
||||||
loop:
|
|
||||||
ADDS.N R2, R0, R2 // add a to result
|
CMP.N R1, #1
|
||||||
SUBS.N R1, #1 // substract 1 from b
|
BNE.N check_even // goto check_even if b == 1
|
||||||
BEQ.N exit // goto exit if b == 0
|
MOVS.N R2, R0 // move a to result
|
||||||
B.N loop // loop otherwise
|
B.N exit // goto exit
|
||||||
|
check_even:
|
||||||
|
MOVS.N R3, #1 // set R3 to 1 as bit mask
|
||||||
|
ANDS.N R3, R1 // and R3 (bit mask) with b
|
||||||
|
BNE.N odd // goto exit if b == 0
|
||||||
|
even:
|
||||||
|
LSLS.N R0, R0, #1 // a = a << 1
|
||||||
|
LSRS.N R1, R1, #1 // b = b << 1
|
||||||
|
B.N multiply2 // recurse
|
||||||
|
odd:
|
||||||
|
ADDS.N R2, R0 // add a to result
|
||||||
|
LSLS.N R0, R0, #1 // a = a << 1
|
||||||
|
LSRS.N R1, R1, #1 // b = b << 1
|
||||||
|
B.N multiply2 // recurse
|
||||||
exit:
|
exit:
|
||||||
MOVS.N R0, R2 // move result to R0
|
MOVS.N R0, R2 // move result to R0
|
||||||
BX.N LR
|
BX.N LR
|
||||||
|
|||||||
Reference in New Issue
Block a user