Skip to content

Commit

Permalink
knapsack example solution
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Nov 6, 2024
1 parent 4c130b9 commit 8594e00
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions exercises/practice/knapsack/.meta/example.s
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,47 @@

/* extern uint32_t maximum_value(uint32_t maximum_weight, const item_t *items, size_t item_count); */
maximum_value:
mov x0, xzr
ret
lsl x2, x2, #3 /* number of bytes occupied by items */
add x2, x1, x2 /* end of items */
lsl x0, x0, #2
add x3, x0, #16
and x3, x3, #-16 /* number of bytes occupied by table, with (maximum_weight+1) uint32_t values, rounded up to a multiple of 16 */
mov x4, sp
sub sp, sp, x3
mov x5, sp

.clear:
stp xzr, xzr, [x4, #-16]! /* fill table with 0 */
cmp x4, x5
bne .clear

.next_item:
cmp x1, x2
beq .report

ldr w9, [x1], #4 /* load item weight, post-increment */
ldr w10, [x1], #4 /* load item value, post-increment */
lsl x9, x9, 2
mov x7, x0
subs x11, x7, x9
blt .next_item

.loop:
ldr w12, [sp, x11]
add w12, w12, w10 /* table value plus item value */
ldr w13, [sp, x7]
cmp w13, w12
bhs .skip /* unsigned >= */

str w12, [sp, x7] /* update table value */

.skip:
cbz x11, .next_item
sub x7, x7, #4
sub x11, x11, #4
b .loop

.report:
ldr w0, [sp, x0]
add sp, sp, x3
ret

0 comments on commit 8594e00

Please sign in to comment.