finish assignment 5
This commit is contained in:
@@ -4,16 +4,15 @@
|
|||||||
.globl multiply
|
.globl multiply
|
||||||
.text
|
.text
|
||||||
.thumb_func
|
.thumb_func
|
||||||
// R0 = a, R1 = b, R2 = result (m)
|
// R0 = a, R1 = b, R2 = result (m), R3 = odd/even mask/result
|
||||||
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
|
||||||
|
multiply2:
|
||||||
CMP.N R1, #1
|
CMP.N R1, #1
|
||||||
BNE.N check_even // goto check_even if b == 1
|
BNE.N check_even // goto check_even if b == 1
|
||||||
MOVS.N R2, R0 // move a to result
|
ADDS.N R2, R0 // move a to result
|
||||||
B.N exit // goto exit
|
B.N exit // goto exit
|
||||||
check_even:
|
check_even:
|
||||||
MOVS.N R3, #1 // set R3 to 1 as bit mask
|
MOVS.N R3, #1 // set R3 to 1 as bit mask
|
||||||
@@ -21,12 +20,12 @@ check_even:
|
|||||||
BNE.N odd // goto exit if b == 0
|
BNE.N odd // goto exit if b == 0
|
||||||
even:
|
even:
|
||||||
LSLS.N R0, R0, #1 // a = a << 1
|
LSLS.N R0, R0, #1 // a = a << 1
|
||||||
LSRS.N R1, R1, #1 // b = b << 1
|
LSRS.N R1, R1, #1 // b = b >> 1
|
||||||
B.N multiply2 // recurse
|
B.N multiply2 // recurse
|
||||||
odd:
|
odd:
|
||||||
ADDS.N R2, R0 // add a to result
|
ADDS.N R2, R0 // add a to result
|
||||||
LSLS.N R0, R0, #1 // a = a << 1
|
LSLS.N R0, R0, #1 // a = a << 1
|
||||||
LSRS.N R1, R1, #1 // b = b << 1
|
LSRS.N R1, R1, #1 // b = b >> 1
|
||||||
B.N multiply2 // recurse
|
B.N multiply2 // recurse
|
||||||
exit:
|
exit:
|
||||||
MOVS.N R0, R2 // move result to R0
|
MOVS.N R0, R2 // move result to R0
|
||||||
BIN
assignment_assembly/assignment5/result.png
Normal file
BIN
assignment_assembly/assignment5/result.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 39 KiB |
@@ -41,4 +41,16 @@ En het test resultaat:
|
|||||||
|
|
||||||
## A smarter tail recursive multiply algorithm
|
## A smarter tail recursive multiply algorithm
|
||||||
|
|
||||||
|
Voor deze opdracht heb ik niet exect de gegeven C code na gemaakt. Ik heb de twee functies als een geschreven. Hierbij is het argument `m` van `function multiply2` verplaast als laaste argument i.p.v. de eerste en de `if (b == 0)` heb ik verplaats naar `multiply`.
|
||||||
|
|
||||||
|
Het verplatsen van argument `m` is alleen een probleem waneer er andere code is die gebruik maakt dat deze functie behalve `multiply`.
|
||||||
|
|
||||||
|
Het verplasen van de `if (b == 0)`statement is een optimazatie. Deze statement kan nooit waar zijn na de eerste iteratie. `b` wordt steeds 1 plek naar recht geschoven tot dat de laaste bit die `1` is op de 1 positie staat. In dat geval eindigd de funcie direct.
|
||||||
|
|
||||||
|
De asambly code:
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Het test resultaat:
|
||||||
|
|
||||||
|

|
||||||
|
|||||||
Reference in New Issue
Block a user