I decided to assigning R2 the value of the base for the value to be read. I would assign R4 the value of the next value in the heap (the value to be read plus the offset) and then increment R2 until it equaled R4, stopping the read before reading R4.
This all worked, with one small problem: I had written values to memory from the least significant bit to the most significant bit. That way, I could write 0x00112233 as three bytes without having to do anything special to deal with 0x00112200. But reading from the lower end to the higher end, I would get something like 0x332211.
I thought about changing the way that the write routine worked to save the higher bits first, but didn't want to deal with the added complexity. Instead, I decided to decrement R4 down to R2.
This approach made the routine woolly, again, but it does seem to give the right results.
Here's some code:
read_value
; Takes heap manager offset in R3
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.
MOV R4, R2 ; Entry also into R4
AND R2, R2, #0xFF ; mask off just the offset part of entry.
LDR R5, =Heap_Mem ; 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.
BX R14 ; Return
Next time: I have three more routines in mind for this exercise:
- Delete: Set's the entry in Heap Manager to 0. Memory in the heap is untouched, but won't be accessed.
- Compress: Clears out empty spaces in the Heap Manager.
- Compact: Rewrites the contents of the heap to be contiguous.
No comments:
Post a Comment