I thought I better post my final code for this project. I've already done a couple of blogs summing up my project.
; Revised heap manager.
;
; This program manages a small memory heap.
;
; Terms:
;
; Value - A value of 1 to 4 bytes to be saved in managed memory. (This could be increased with a moderately difficult revision.)
; Heap - The managed memory where values are stored, in this version 256 bytes, each of which can be addressed.
; Value Offset - The offset from the beginning of the Heap in bytes.
; Heap Manager - A region of memory with entries that keep track of values saved in the heap. Current size: 256 words (1024 bytes).
; NOTE: The size allows each of the bytes in memory to host a one-byte value.
; Entry - The location and size of a value saved in memory.
; bits 0 - 7: Value offset
; bits 8 - 23: Reserved
; bits 24-26: Value size (1-4 bytes)
; bits 27-31: Reserved
; Entry Offset - The offset from the beginning of the Heap Manager.
;
; The Heap Manager maintains a list of entries, each of which corresponds to the beginning and size of the value saved in the Heap.
; An entry of 0 indicates a free spot in the Heap Manager, since it would otherwise signify an entry of 0 size at Value Offset 0.
; The following routines are included:
; routine_write_value: Write a value to the heap, record the location and size to the Heap Manager, and return the Entry Offset.
; routine_read_value: Take the Entry Offset and return the value read from the heap.
; routine_delete_value: Delete the specified entry, leaving the value unreadable.
; routine_compact_heap: Remove values from the heap that are no longer included in the Heap Manager.
;
AREA HEAP, NOINIT, READWRITE, ALIGN=3
Heap_Size EQU 0x500 ; Size of heap. 0x500.
Stack_Size EQU 0x400 ; Size of stack. 0x400.
Manager_Offset EQU 0x100 ; Size of ordinary heap. Manager lives on top of it.
; TODO: Define heap size as 4 times Manager_Offset
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 ; TODO: 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 ; Jump to the start of execution.
mystart ; Testing code. This section stores, reads, and deletes values and can be modified to test the program.
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 ; This is the end of execution for the program.
; This routine looks in R3 for a value to save. It saves the value in the first free heap space.
; It saves an entry in the Heap Manager, which includes the size and location of the value.
; R3 input
; R2, R4, R5, R6, R7 used as registers
routine_write_value
STMFD sp!, {R2, R4-R7, R14}
MOV R2, #0 ; Clear R2 for later comparisons.
LDR R4, =Heap_Manager ; Load beginning of Heap Manager.
MOV R5, R4 ; Load the beginning of Heap Manager into R5.
ADD R5, R5, #0x400 ; Add length of the Heap Manager 0x400 or 1024 bytes to R5.
; R5 is now used as a ceiling to test against so program does not access memory beyond the Heap Manager
; Look for the highest value included in the Heap Manager. This is where a new value will be written.
LDR R6, [R4] ; Get value at 0 offset.
CMP R6, #0 ; If R6 is 0, skip to next (beginning main compare loop)
BEQ next
MOV R7, R6, LSR#24 ; Get the size of the entry at 0 offset. We know it has a size because we compared it with 0 already.
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.
next ADD R4, R4, #4 ; Add 4 to R4 - moves address to next possible entry.
CMP R4, R5 ; Compare this value with the top of the Heap Manager.
BEQ done ; Once we hit the top of Heap 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. The program has to know the size of the value to store that in the entry.
MOV R5, R3, LSR#8 ; Load R3 into R5, shifted RIGHT 8 bits. Because the value is consumed, we have to load it into a second register.
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 RIGHT.
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 RIGHT.
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
; This code checks whether the value would be written over the bound of the Heap.
MOV R6, R5, LSR#24 ; Load the size we just calculated as into R6.
ADD R6, R5 ; Add the offset to the size. If this is equal to or greater than Manager_Offset, we are out of bounds.
LDR R7, =Manager_Offset ;
CMP R7, R6
BCC write_error ; If R6 greater than or equal to R7, jump to write error.
ADD R5, R5, R2 ; Add size to offset. R5 is now value to save.
; This code checks whether the value would be written over the bound of the heap.
LDR R4, =Heap_Manager
LDR R7, [R4] ; Check whether the first entry has a value of 0.
CMP R7, #0 ; If so, jump to storing the entry.
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 ; Continue loop.
offset_zero
STR R5, [R4] ; Store the Entry in Heap Manager 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.
; NOTE: This section should be reviewed.
; 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 save_byte.
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 Entry Offset in R3.
; Returns 0xFFFFFFFF if you try to read from an empty entry or if you try to read from outside the Heap Manager.
; R3 input
; R2, R4, R5, R6
STMFD sp!, {R2, R4-R6, R14}
LDR R2, =Manager_Offset ; Set R2 to Manager_Offset.
CMP R2, R3 ; Compare entry to be read with size of Heap. Relies on the fact that there can be one entry for each byte in the heap.
BCC read_error ; If value is too large, jump to error.
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: base plus offset.
LDR R2, [R4] ; Load entry into R2.
CMP R2, #0 ; Check for 0 value, meaning read is not valid.
BEQ read_error
MOV R4, R2 ; Copy entry also into R4. Breaking value into Value Offset and size of Value.
AND R2, R2, #0xFF ; Mask off just the offset part of entry. R2 is now the Value Offset.
LDR R5, =Heap_Memory ; Set R5 to Heap_Memory base.
ADD R2, R2, R5 ; Add offset to base. 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. R4 is now the size of the Value.
ADD R4, R2, R4 ; Add address of value to size. R4 is now the first address after the value to be read.
MOV R3, #0 ; Set R3 to 0.
LDR R6, =0xFFFFFFFF ; NOTE: I wanted to play with 2's complement subtraction.
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. This is the last byte written.
ADD R3, R3, R5 ; Add byte to R3
CMP R2, R4 ; Compare R2 and R4. R2 is the base.
BNE read_loop ; If not same, repeat loop.
LDMFD sp!, {R2, R4-R6, R14}
BX R14 ; Return
read_error LDR R3, =0xFFFFFFFF
LDMFD sp!, {R2, R4-R6, R14}
BX R14
routine_delete_value
; Takes offset of value to be deleted in R3.
; Returns 0xFFFFFFFF in R3 if there is a problem.
; Returns 0 in R3 for success.
; R3 input
; R2, R4
STMFD sp!, {R4, R14}
LDR R2, =Manager_Offset ; Set R2 to Manager_Offset - ONLY WORKS BECAUSE HEAP MANAGER = 4 TIMES MANAGED HEAP
CMP R2, R3 ; Compare Entry to be deleted with Manager Offset.
BCC delete_error ; If value is too large, jump to error.
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 ; Add Value Offset to base. R4 is now the address of the Heap Manager entry.
LDR R3, [R4] ; Get thr value at R4.
CMP R3, #0 ; Check the value at R4 to 0. If value is 0, this is not a valid entry.
; If value is 0, jump to delete error.
BEQ delete_error
MOV R3, #0 ; Set heap manager entry to 0.
STR R3, [R4]
LDMFD sp!, {R4, R14}
BX R14 ; Return
delete_error LDR R3, =0xFFFFFFFF
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 ; Size 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 ; If Entry is empty, there is no need to rewrite it.
MOV R8, R6, LSR#24 ; Save the size of the Value to R8.
AND R6, R6, #0xFF ; Mask off lower 8 bits. R6 is now the Value Offset.
; Save the new offset (R2 - R7) and old size (R8) to the space specified in (R3 + R4)
SUB R9, R2, R7 ; This updates the Entry to reflect where the Value will be saved.
ADD R9, R9, R8, LSL#24 ; Rollback is not possible at this point.
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. TODO: Verify this logic.
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). Done in words, not bytes, so +4, not +1.
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment