This one took a lot longer than I expected, but I think the design assumed in my earlier version was faulty. I assumed you could find the highest entry in the heap manager, add the value size to the heap offset, and get the next free place in the heap. This only works until you've deleted and written over an entry. After that, the highest entry points to someplace down in the heap.
I fixed this by doing two passes through the heap manager. The first pass finds the highest used portion of the heap. The second pass finds an empty value in the heap manager to use.
In addition to the stuff already described, I did the following:
- Change names of routines to start with "routine_"
- Change routines to begin and end with push and pop actions.
- Removed the compress routine.
- Modify routine_write_value to return the offset of the entry in R3.
- Edit pass to tidy up the tab space.
Here's the code:
; Revised heap manager.
;
;
;
AREA HEAP, NOINIT, READWRITE, ALIGN=3
Heap_Size EQU 0x500 ; Size of heap. 0x500, from my earlier project
Stack_Size EQU 0x400 ; Size of stack. 0x400
Manager_Offset EQU 0x100 ; Size of ordinary heap. Manager lives on top of it.
Heap_Memory SPACE Heap_Size ; Allocate memroy for the heap.
Stack_Memory SPACE Stack_Size ; Allocate memory for the stack.
Heap_Manager EQU (Heap_Memory + Manager_Offset) ; Set Heap_Manager base to top of heap.
AREA HelloWorld, CODE, READONLY ;
EXPORT Vectors ; Export this code to the project
; Exception handlers.
Vectors B Reset_Handler
Undef_Handler B Undef_Handler
B SWI_Handler
SWI_Handler ; Consider using this for error condition
Reset_Handler
LDR sp, =Stack_Memory ; Set stack memory. This points to the base of the stack.
ADD sp, sp, #Stack_Size ; Because this is a decrementing stack, we need to point to the top of the stack.
B mystart
mystart ; Testing code.
LDR R3, =0xDDCCBBAA ; Set R3 to value
MOV R14, R15
B routine_write_value
LDR R3, =0x11 ; Set R3 to value
MOV R14, R15
B routine_write_value
LDR R3, =0x345678 ; Set R3 to value
MOV R14, R15
B routine_write_value
LDR R3, =0x1234 ; Set R3 to value
MOV R14, R15
B routine_write_value
LDR R3, =0xabcdef12 ; Set R3 to value
MOV R14, R15
B routine_write_value
LDR R3, =0x1
MOV R14, R15
B routine_read_value
LDR R3, =0x0
MOV R14, R15
B routine_delete_value
LDR R3, =0x1
MOV R14, R15
B routine_read_value
MOV R14, R15
B routine_compact_heap
stop
BAL stop ; gives me a loop to be caught in
; This routine looks in R3 for a value to save. It saves it in the first free heap space
; It saves an entry in the heap manager
; R3 input
; R2, R4, R5, R6, R7
routine_write_value
STMFD sp!, {R2, R4-R7, R14}
MOV R2, #0
LDR R4, =Heap_Manager ; Load beginning of Heap Manager
MOV R5, R4
ADD R5, R5, #0x400 ; Add length of the Heap Manager 0x400 or 1024 bytes
LDR R6, [R4] ; Get value at 0 offset.
CMP R6, #0
BEQ next
MOV R7, R6, LSR#24 ; Get the size of this entry.
AND R6, R6, #0xFF ; Mask value of offset.
ADD R6, R6, R7 ; Get value of offset plus size.
CMP R2, R6 ; compare R2 with candidate.
BCS next ; If C is set, jump to next.
MOV R2, R6 ; Replace R2 with higher value.
; EDGE Condition: if the 255 spot is full, this stuff fails.
next ADD R4, R4, #4 ; Add 4 to R4 - moves address to next possible entry.
CMP R4, R5
BEQ done ; Once we hit the top of manager, jump to done.
LDR R6, [R4] ; Get value at R4.
CMP R6, #0 ; check whether the entry is 0
BEQ next ; If so, go to next.
MOV R7, R6, LSR#24 ; Get the size of this entry.
AND R6, R6, #0xFF ; Mask value of offset.
ADD R6, R6, R7 ; Get value of offset plus size.
CMP R2, R6 ; compare R2 with candidate.
BCS next ; If C is set, jump to next.
MOV R2, R6 ; Replace R2 with higher value.
B next
done ; At this point, R2 is the lowest available heap offset.
; TODO add checking code to verify that the the offset is valid.
; Testing size of R3.
MOV R5, R3, LSR#8 ; Load R3 into R5, shifted R 8 bits Value will be consumed.
CMP R5, #0 ; Compare with 0. If R3 is less than 8 bits, now R5 will be 0
BNE case_2 ; If larger than 0, jump ahead
LDR R5, =0x1000000 ; If 0, only 1 byte. Set size to 001.
B done_cases ; Jump to end.
case_2 MOV R5, R5, LSR#8 ; Shift R5 8 bits.
CMP R5, #0 ; See if there are still any bits.
BNE case_3 ; If more bits, jump ahead.
LDR R5, =0x2000000 ; If 0, only 1 byte. Set size to 010.
B done_cases ; Jump to end.
case_3 MOV R5, R5, LSR#8 ; Shift R5 8 bits.
CMP R5, #0 ; See if there are still any bits.
BNE case_4 ; If more bits, jump ahead.
LDR R5, =0x3000000 ; If 0, only 1 byte. Set size to 011.
B done_cases ; Jump to end.
case_4 LDR R5, =0x4000000 ; Default case. Must be 4 bytes. Set size to 100.
done_cases
ADD R5, R5, R2 ; Add size to offset. R5 is now value to save.
LDR R4, =Heap_Manager
LDR R7, [R4] ; Check 0 case.
CMP R7, #0
BEQ offset_zero
MOV R6, R4
ADD R6, R6, #0x400 ; R6 is now ceiling of heap manager.
find_zero_entry ADD R4, R4, #4 ; Increment R4 by one word.
CMP R4, R6 ; Compare R4 with ceiling.
BEQ write_error ; Reached end of heap manager without finding 0.
LDR R7, [R4] ; Load R7 with entry.
CMP R7, #0 ; Check whether entry is 0.
BNE find_zero_entry
offset_zero
STR R5, [R4] ; Store (the offset in Heap Memory) at R4.
; Entry is now stored in Heap_Manager
LDR R5, =Heap_Memory ; Add offset to Heap_Mem base.
ADD R2, R2, R5 ; Add offset to Heap_Mem base.
; R2 is now the address in the heap where the value in R3 will be saved.
save_byte AND R5, R3, #0xFF ; Set R5 to R3 masked & 00000000000000000000000011111111 0xFF
STRB R5, [R2] ; Save R5 to location R2.
ADD R2, R2, #1 ; Increment R2.
MOVS R3, R3, LSR#8 ; Consume lowest 8 bits of R3.
BNE save_byte ; R3 is 0, jump to start_save
LDR R3, =Heap_Manager ; return offset of entry in manager.
SUB R3, R4, R3
MOV R3, R3, LSR#2 ; Divide by 4. Offset should refer to words, not bytes.
LDMFD sp!, {R2, R4-R7, R14}
BX R14
write_error LDR R3, =0xFFFFFFFF
LDMFD sp!, {R2, R4-R7, R14}
BX R14
routine_read_value
; Takes heap manager offset in R3
; Returns 0xFFFFFFFF if you try to read from a non-entry.
; R3 input
; R2, R4, R5, R6
STMFD sp!, {R2, R4-R6, R14}
LDR R4, =Heap_Manager ; Set R4 to Heap_Manager base.
MOV R3, R3, LSL#2 ; Multiply R3 by 4: Heap manager uses words, not bytes
ADD R4, R4, R3 ; Get address of heap_manager entry.
LDR R2, [R4] ; Load entry into R2.
CMP R2, #0 ; Check for 0 value, meaning read is not valid
BEQ error_out
MOV R4, R2 ; Entry also into R4
AND R2, R2, #0xFF ; mask off just the offset part of entry.
LDR R5, =Heap_Memory ; Set R5 to Heap_Mem base
ADD R2, R2, R5 ; R2 is now first address of value in heap memory.
MOV R4, R4, LSR#24 ; Shift bits over so that only the size bits remain.
ADD R4, R2, R4 ; R4 is now the first address after the value to be read.
MOV R3, #0 ; Set R3 to 0
LDR R6, =0xFFFFFFFF
MOV R5, #0
read_loop MOV R3, R3, LSL#8 ; Shift R3 8 bits left.
ADD R4, R4, R6 ; Decrements R4 to read next byte
LDRB R5, [R4] ; Read byte
ADD R3, R3, R5 ; Add byte to R3
CMP R2, R4 ; compare R2 and R4.
BNE read_loop ; If not same, repeat loop.
LDMFD sp!, {R2, R4-R6, R14}
BX R14 ; Return
error_out LDR R3, =0xFFFFFFFF
LDMFD sp!, {R2, R4-R6, R14}
BX R14
routine_delete_value
; Takes offset of value to be deleted in R3.
; Returns 1 in R3 if there is a problem.
; Returns 0 in R3 for success.
; R3 input
; R4
STMFD sp!, {R4, R14}
LDR R4, =Heap_Manager ; Set R4 to Heap_Manager base.
MOV R3, R3, LSL#2 ; Multiply R3 by 4: Heap manager uses words, not bytes
ADD R4, R4, R3 ; Get address of heap_manager entry.
MOV R3, #0 ; Set heap manager entry to 0
STR R3, [R4]
LDMFD sp!, {R4, R14}
BX R14 ; Return
routine_compact_heap
; Read each saved byte in the heap and write them from the beginning,
; writing over deleted values and updating the Heap Manager as we go.
; R2, R3, R4, R5, R6, R7, R8, R9
STMFD sp!, {R2-R9, R14}
LDR R2, =Heap_Memory ; Heap memory base. Start writing at this address
LDR R7, =Heap_Memory
LDR R3, =Heap_Manager ; Heap manager base.
MOV R4, #0x0 ; Set offset to 0.
ADD R5, R3, #0x400 ; Top of Heap Manager
heap_compact_loop
LDR R6, [R3, R4] ; Load entry into R6
CMP R6, #0 ; Check whther entry has content
BEQ empty_entry
MOV R8, R6, LSR#24
AND R6, R6, #0xFF ; Mask off lower 8 bits
; Save the new offset (R2 - R7) and old size (R8) to the space specified in (R3 + R4)
SUB R9, R2, R7
ADD R9, R9, R8, LSL#24
STR R9, [R3, R4]
; Read and write.
ADD R6, R6, R7 ; get location of first byte to read.
ADD R8, R8, R6 ; get location of next byte beyond the value.
compact_loop LDRB R9, [R6] ; read first byte.
STRB R9, [R2] ; store first byte.
ADD R2, R2, #1 ; increment R2.
CMP R2, R3 ; check against end of heap
BEQ com_done
ADD R6, R6, #1 ; increment R6
cmp R8, R6 ; check whether we are at the end of the write
BNE compact_loop ; If not, go back to the loop for more writing
empty_entry
ADD R4, R4, #4 ; Increment R4 (Heap Manager loop)
ADD R6, R3, R4 ; Set R6 to next Heap Manager location
cmp R5, R6 ; Check whether the heap manager current location is at the top of the heap
BNE heap_compact_loop
com_done
LDMFD sp!, {R2-R9, R14}
BX R14 ; Return
END ; end.
No comments:
Post a Comment