Sunday, December 20, 2009

Following the Flesh

I went back over the code and found a couple of simple typos that messed things up. For instance, ADD R2, R2 instead of ADD R2, R2, R5.

After I wrung out the errors, it worked for two consecutive saves.

Then I tested it for 5 saves. Seems to be working.

So, what else needs to be done?
  • read_value: Takes an offset in R3 and reads the value from the heap.
  • delete_value: Takes an offset in R3 and deletes the entry from the manager. Heap is untouched.
  • compress: Removes any empty entries from the manager.
  • compact_heap: Reads each entry from the manager, reads corresponding value in heap, writes value in heap, starting at 0, and writes 0 to the rest of the heap.
After having gone through all this, today, though, I thing the rest will be straight forward, if not exactly cake-walky.

Wanna see the code? Sure you do.

; A little Heap Manager that will manage a 256 byte heap that can
; store 1, 2, 3, and 4 byte values.
; Name: HelloWorld.s
; modifying to play with different aspects.

AREA HelloWorld, CODE, READONLY ;
EXPORT mystart ; Export this code to the project
IMPORT Heap_Mem ; This is the base of the heap.
IMPORT Heap_Manager ; base for the Heap Manager

; This version of the program manually loads five values into memory then unloads them.
mystart
; This section sets R3 to a test value and gotos write_value
ldr R3, =0xDDCCBBAA ; Set R3 to value
MOV R14, R15
B write_value

ldr R3, =0x11 ; Set R3 to value
MOV R14, R15
B write_value

ldr R3, =0x345678 ; Set R3 to value
MOV R14, R15
B write_value

ldr R3, =0x1234 ; Set R3 to value
MOV R14, R15
B write_value

ldr R3, =0xabcdef12 ; Set R3 to value
MOV R14, R15
B write_value

LDR R2, =Heap_Mem ; testing.
LDR R3, [R2]
NOP

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

write_value LDR R4, =Heap_Manager ; Load beginning of Heap Manager
LDR R6, =0x400 ; Length of the Heap Manager 0x400 or 1024 bytes
LDR R5, =0xFFFFFFFC ; 2's comp rep of -4
; EDGE Condition: if the 255 spot is full, this stuff fails.
next ADD R6, R6, R5 ; Add -4 to R4 - moves offset back to the previous word
LDR R2, [R4, R6] ; get value at Heap Manager
CMP R6, #0 ; Check whehter we've hit the beginning of the Heap Manager
BEQ done ; If so, jump out of the loop
CMP R2, #0 ; Check whether the value is 0
BEQ next ; if 0, go back to the start of the loop.

done

; R2 is the value of the saved heap memory offset (including size)
MOV R5, R2, LSR#24 ; set R5 to the size of the element in the heap
AND R2, R2, #0xFF ; mask off the high bits (that record size)
ADD R2, R2, R5 ; add offset to size
; R2 is now the offset for the next write to the heap.

; TODO write code here to fail routine if R2 > 255 - means that out of space in heap.


ADD R4, R4, R6 ; add offset to Heap Manager location.
; R4 is the address that previous heap pointer resides at

CMP R2, #0 ; if R2 has a value of 0, this is the first write action and should write to 0 offset
BEQ first_write
ADD R4, R4, #4

first_write
; 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 R 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 R 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.
STR R5, [R4] ; Store (the offset in Heap Memory) at R4.

; Entry is now stored in Heap_Manager

LDR R5, =Heap_Mem ; 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
; to do: modify to return offset of Heap manager entry in R3 (subtract Heap_Manager from R4)
BX R14

END ; end.

(I know it's a little hard to read this in the HTML formatting.)

After I was done, it occurred to me that I should return the offset of the in Heap_Manager. It won't be hard to implement, but it will have to wait.

No comments:

Post a Comment