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.
Saturday, January 23, 2010
Wednesday, January 6, 2010
More Documentation
One place where I worked, there was a developer who said that the way to document your code was to let it sit for a week or two and then go over it with an eye towards explaining the stuff that you find hard after being away from it for a week.
More documentation tonight.
I did a summary section around definitions of the terms I used (Heap, Heap Manager, Value, Entry, and so forth.)
Then I went back over some of the line comments, trying to make them clearer.
At one point, I hit a region where I wasn't sure how the code was supposed to work.
Which is where I will pick up tomorrow.
I also got a book about the Gnu compiler that I'm looking over. More on that if I give it more head space.
More documentation tonight.
I did a summary section around definitions of the terms I used (Heap, Heap Manager, Value, Entry, and so forth.)
Then I went back over some of the line comments, trying to make them clearer.
At one point, I hit a region where I wasn't sure how the code was supposed to work.
Which is where I will pick up tomorrow.
I also got a book about the Gnu compiler that I'm looking over. More on that if I give it more head space.
Saturday, January 2, 2010
Some Things I Would Do if I were Really Writing This
This blog is about learning the pieces and parts of ARM Assembly language. If I were really writing a heap manager, I would do the following:
I'm assuming this design is for a heap manager that stores objects of various sizes. That's why I include the size of the value (in bytes) along with it's position in the heap. If this were a string pool, I might just include the position in the heap and use a delimiter character. I believe this is how actual string pools are organized.
If it were a string pool, it might be worth while to include, instead of size, other information about the string saved. For instance, if you match a string with an existing string (as Java does) in the pool, it could speed things up to know the first character or first ten characters. That way, when comparing a string to all the strings in the pool, you would only have to read a few close possible matches from the heap.
The design I chose only used 8 bits to address locations in the heap. The design has another 16 bits free for this, bringing us to a total of 16777216 possible addressable bytes. That's one fourth the size of the addressable memory in an ARM chip.
Likewise, I only implemented values of size 1-4. This design can accommodate values of 1 to 256 with little tinkering. Again, this would be good for objects of varying size, but bad for strings.
So, if I were writing a heap manager, I would start by figuring out what kind of values would be living on the heap. The size of the Heap Manager portion of memory could be customized.
I'd start with a test section of code. This should include more than just the vanilla cases I used in this version. It would include the edge cases - the situation where you fill the heap completely or fill the manager completely. The cases where you would end up reading or writing outside the heap area.
And I think I'd design it in C, at least as a first version. For managing memory, I don't think a higher level language - like C# or Java - would be a good idea. It's one of the few things that you need C or assembly for.
I'm assuming this design is for a heap manager that stores objects of various sizes. That's why I include the size of the value (in bytes) along with it's position in the heap. If this were a string pool, I might just include the position in the heap and use a delimiter character. I believe this is how actual string pools are organized.
If it were a string pool, it might be worth while to include, instead of size, other information about the string saved. For instance, if you match a string with an existing string (as Java does) in the pool, it could speed things up to know the first character or first ten characters. That way, when comparing a string to all the strings in the pool, you would only have to read a few close possible matches from the heap.
The design I chose only used 8 bits to address locations in the heap. The design has another 16 bits free for this, bringing us to a total of 16777216 possible addressable bytes. That's one fourth the size of the addressable memory in an ARM chip.
Likewise, I only implemented values of size 1-4. This design can accommodate values of 1 to 256 with little tinkering. Again, this would be good for objects of varying size, but bad for strings.
So, if I were writing a heap manager, I would start by figuring out what kind of values would be living on the heap. The size of the Heap Manager portion of memory could be customized.
I'd start with a test section of code. This should include more than just the vanilla cases I used in this version. It would include the edge cases - the situation where you fill the heap completely or fill the manager completely. The cases where you would end up reading or writing outside the heap area.
And I think I'd design it in C, at least as a first version. For managing memory, I don't think a higher level language - like C# or Java - would be a good idea. It's one of the few things that you need C or assembly for.
Three Possible Errors - and then Some
I'm back at my Heap Manager, today doing some error handling. There are three invalid situations that should be dealt with:
In all cases, I want it to fail before writing. In the case of writing to the heap, if it writes outside the valid area of the heap, it could corrupt the Heap Manager (which sits on top of the heap.)
As for number 1:
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.
Since an entry in the Heap Manager describes the location and size of the bytes to live on the heap, we can check whether this write will be OK by adding the size to the offset and comparing it with the managed heap size.
A fix for number 2 was already included in my code. I guess I saw that one coming. Here's the code:
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.
There are two ways out of the loop. Either you fall through with a 0 value entry or you hit the top and jump to a write error.
Finally, for number 3, it's also already done. I did not remember writing it this way, but here is the code:
CMP R2, #0 ; Check for 0 value, meaning read is not valid
BEQ error_out
Attempting a read on a 0 value entry jumps to an error.
MORE POSSIBLE ERRORS
I've been assuming that the calling code would know not to ask for a value outside the size of the Heap Manager. Not really safe. Here's some simple code to check the input for a possible read:
LDR R2, =Manager_Offset ; Set R2 to Manager_Offset
CMP R2, R3 ; Compare entry to be read with manger offset
BCC read_error ; If value is too large, jump to error.
Finally, deleting a Heap Manager value is simple but could break things badly. For instance, it will write over a value in memory with 0 -- any value above the specified Heap_Manager value. Also, it writes over a value of 0 with a value of 0 without error.
Here's the revised delete routine, which checks to see that the input is not greater than the size of the manager.
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 read with manger 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 ; Get address of heap_manager entry.
LDR R3, [R4] ; check the value at R4
CMP R3, #0 ; 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
- Write a value to the heap outside 0-255.
- Write an entry to a word in Heap Manager outside 0-255.
- Read a manager entry with a value of 0.
In all cases, I want it to fail before writing. In the case of writing to the heap, if it writes outside the valid area of the heap, it could corrupt the Heap Manager (which sits on top of the heap.)
As for number 1:
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.
Since an entry in the Heap Manager describes the location and size of the bytes to live on the heap, we can check whether this write will be OK by adding the size to the offset and comparing it with the managed heap size.
A fix for number 2 was already included in my code. I guess I saw that one coming. Here's the code:
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.
There are two ways out of the loop. Either you fall through with a 0 value entry or you hit the top and jump to a write error.
Finally, for number 3, it's also already done. I did not remember writing it this way, but here is the code:
CMP R2, #0 ; Check for 0 value, meaning read is not valid
BEQ error_out
Attempting a read on a 0 value entry jumps to an error.
MORE POSSIBLE ERRORS
I've been assuming that the calling code would know not to ask for a value outside the size of the Heap Manager. Not really safe. Here's some simple code to check the input for a possible read:
LDR R2, =Manager_Offset ; Set R2 to Manager_Offset
CMP R2, R3 ; Compare entry to be read with manger offset
BCC read_error ; If value is too large, jump to error.
Finally, deleting a Heap Manager value is simple but could break things badly. For instance, it will write over a value in memory with 0 -- any value above the specified Heap_Manager value. Also, it writes over a value of 0 with a value of 0 without error.
Here's the revised delete routine, which checks to see that the input is not greater than the size of the manager.
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 read with manger 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 ; Get address of heap_manager entry.
LDR R3, [R4] ; check the value at R4
CMP R3, #0 ; 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
Friday, January 1, 2010
Another Pass
I spent several hours on the tasks that I set for myself yesterday. By far the biggest item was the structural change described in the third bullet.
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:
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.
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.
Subscribe to:
Posts (Atom)