Friday, December 25, 2009

It's Alive

Well, my starter project is complete - not perfect, but it does what I specked it out to do. Yay, me.

Yes, here is the code:

; 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 HEAP, NOINIT, READWRITE, ALIGN=3

Heap_Size EQU 0x00000500 ; line changed

Heap_Mem SPACE Heap_Size

Temp EQU 0x100
Heap_Manager EQU (Heap_Mem + Temp)

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.

; This section sets R3 to a test value and gotos write_value
mystart 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

LDR R3, =0x1
MOV R14, R15
B read_value

LDR R3, =0x0
MOV R14, R15
B delete_value

LDR R3, =0x1
MOV R14, R15
B read_value

MOV R14, R15
B 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

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

read_value
; Takes heap manager offset in R3
; Returns 0xFFFFFFFF if you try to read from a non-entry.
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_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
error_out LDR R3, =0xFFFFFFFF
BX R14

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.

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]
BX R14 ; Return

compress_man

LDR R2, =Heap_Manager ; Will use R2 as address of heap Man
MOV R5, R2
LDR R3, =0x3fc
ADD R5, R5, R3 ; Set to top of heap manager.
; First pass ; Not sure about edge case here.
LDR R3, [R2] ; Load the value at R2 into R3
CMP R3, #0 ; Compare R3 to 0.
BNE begin_c_loop ; First number is greater than 0. Jump ahead.
LDR R4, [R2, #4] ; Load the value at R2+4 into R4
CMP R4, #0 ; Compare R4 to 0.
BEQ begin_c_loop ; If R4 is 0, jump ahead.
STR R4, [R2] ; Store the value from R2+4 at R2
MOV R3, #0
STR R3, [R2, #4] ; Store 0 in R2+4

begin_c_loop ; loop for checking.
ADD R2, R2, #4 ; Increment R2
CMP R2, R5 ; compare R2 and R5
BEQ check_manager

LDR R3, [R2] ; Load the value at R2 into R3
CMP R3, #0 ; Compare R3 to 0.
BNE begin_c_loop ; First number is greater than 0. Go through loop again.
LDR R4, [R2, #4] ; Load the value at R2+4 into R4
CMP R4, #0 ; Compare R4 to 0.
BEQ begin_c_loop ; If R4 is 0, jump ahead.
STR R4, [R2] ; Store the value from R2+4 at R2
MOV R3, #0
STR R3, [R2, #4] ; Store 0 in R2+4
B begin_c_loop


check_manager
LDR R2, =Heap_Manager ; First compare can;t be wrong.
cm_loop
ADD R2, R2, #4
CMP R2, R5 ; compare R2 and R5
BEQ check_done
LDR R3, [R2] ; Load the value at R2 into R3
CMP R3, #0 ; Compare R3 to 0.
BNE cm_loop ; First number is greater than 0. Go through loop again.
LDR R4, [R2, #4] ; Load the value at R2+4 into R4
CMP R4, #0 ; Compare R4 to 0.
BEQ cm_loop ; If R4 is 0, jump ahead.

B compress_man



check_done
BX R14 ; Return




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.

LDR R2, =Heap_Mem ; Heap memory base. Start writing at this address
LDR R7, =Heap_Mem

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

BX R14 ; Return

END ; end.


Next up:

I'm going to implement a little stack so I can do subroutine calls recursively.

Also, I need to go back over the ARM instruction set. I've used only a handful of stuff here and I should get to know the other commands. This page has been helpful so far: Instruction Set.

No comments:

Post a Comment