Wednesday, December 23, 2009

Spaghetti is Tasty!

So, I wrote the compress function I mentioned previously.

The implementation works like this: It checks for cases where two entries have a pattern of [zero][not zero]. This means that there is an empty value before a non-empty value.

My whole implementation is klugey. First, I compare the first two values. Then I start up a loop that compares adjacent values going down the line.

After the first pass, control goes to a section that checks whether there are any viloations of the [zero][not zero] rule.

If so, control returns to the beginning of the routine to start over.

This does work. (After I fixed the memory violation that came from a typo.) Here's the code:
compress_man

LDR R2, =Heap_Manager ; Will use R2 as address of heap Man
MOV R5, R2
LDR R3, =0x3fc
ADD R5, R5, R3 ; Set to top of heap manager.
; First pass ; Not sure about edge case here.
LDR R3, [R2] ; Load the value at R2 into R3
CMP R3, #0 ; Compare R3 to 0.
BNE begin_c_loop ; First number is greater than 0. Jump ahead.
LDR R4, [R2, #4] ; Load the value at R2+4 into R4
CMP R4, #0 ; Compare R4 to 0.
BEQ begin_c_loop ; If R4 is 0, jump ahead.
STR R4, [R2] ; Store the value from R2+4 at R2
MOV R3, #0
STR R3, [R2, #4] ; Store 0 in R2+4

begin_c_loop ; loop for checking.
ADD R2, R2, #4 ; Increment R2
CMP R2, R5 ; compare R2 and R5
BEQ check_manager
; NOT EXITING OUT OF LOOP - START HERE.
LDR R3, [R2] ; Load the value at R2 into R3
CMP R3, #0 ; Compare R3 to 0.
BNE begin_c_loop ; First number is greater than 0. Go through loop again.
LDR R4, [R2, #4] ; Load the value at R2+4 into R4
CMP R4, #0 ; Compare R4 to 0.
BEQ begin_c_loop ; If R4 is 0, jump ahead.
STR R4, [R2] ; Store the value from R2+4 at R2
MOV R3, #0
STR R3, [R2, #4] ; Store 0 in R2+4
B begin_c_loop


check_manager
LDR R2, =Heap_Manager ; First compare can;t be wrong.
cm_loop
ADD R2, R2, #4
CMP R2, R5 ; compare R2 and R5
BEQ check_done
LDR R3, [R2] ; Load the value at R2 into R3
CMP R3, #0 ; Compare R3 to 0.
BNE cm_loop ; First number is greater than 0. Go through loop again.
LDR R4, [R2, #4] ; Load the value at R2+4 into R4
CMP R4, #0 ; Compare R4 to 0.
BEQ cm_loop ; If R4 is 0, jump ahead.

B compress_man



check_done
BX R14 ; Return
It's just not very pretty. Or efficient.

No comments:

Post a Comment