Saturday, December 19, 2009

Consuming R3

Last time, I left with two outstanding problems. Did some reading and figured them out, plus one I didn't know about.
  • My STR command was storing words because it is supposed to store words. I needed to use the Byte version of the write command, STRB.
  • My branching using BMI wasn't working. The N flag is not defined for comparisons on unsigned numbers.
  • Also, CMP does not require an S to set flags - it is implied.
So, I got to work fixing those things.

First I ran through it with four blocks of code and branch commands taking me to the end. After each save of a byte, I shifted bits to the right 8 bits, consuming the value in R3 as it was being saved. This may have design fall-out later, since I need to store the size of the value in bytes when I save it.

The blocks were so similar, I just had to make it into a loop, even with my comment last time about not complicating things. Here's the code: (I used the NOPs to host breakpoints I used in stepping through the program.)

; Get the base from the Heap Manageer
LDR R2, =Heap_Mem ; temp just the Heap_Mem

ldr R3, =0xDDCCBBAA ; Set R3 to value

AND R4, R3, #0xFF ; Set R4 to R3 masked & 00000000000000000000000011111111 0xFF
STRB R4, [R2] ; Save R4 to location R2

ADD R2, R2, #1 ; increment R2
CMP R3, R4 ; compare R3 and R4
BEQ end_of_save ; if equal, jump to end_of_save

NOP

AND R4, R3, #0xFF ; Set R4 to R3 masked & 00000000000000000000000011111111 0xFF
STRB R4, [R2] ; Save R4 to location R2
ADD R2, R2, #1 ; increment R2
MOVS R3, R3, LSR #8 ; Consume lowest 8 bits of R3
CMP R3, R4 ; compare R3 and R4
BNE end_of_save ; if equal, jump to end_of_save

NOP

MOV R3, R3, LSR #8 ; Consume lowest 8 bits of R3
AND R4, R3, #0xFF ; Set R4 to R3 masked & 00000000000000000000000011111111 0xFF
STRB R4, [R2] ; Save R4 to location R2
ADD R2, R2, #1 ; increment R2
CMP R3, R4 ; compare R3 and R4
BEQ end_of_save ; if equal, jump to end_of_save


NOP

MOV R3, R3, LSR #8 ; Consume lowest 8 bits of R3
AND R4, R3, #0xFF ; Set R4 to R3 masked & 00000000000000000000000011111111 0xFF
STRB R4, [R2] ; Save R4 to location R2
ADD R2, R2, #1 ; increment R2
CMP R3, R4 ; compare R3 and R4
BEQ end_of_save ; if equal, jump to end_of_save

NOP

end_of_save


The only difference between the first time through the loop and the other three was that the first byte-write and the remaining ones was that I needed to shift R3 in those. I decided to implement it as a loop, regardless of what I'd said about keeping it simple.

This got a little easier when I realized I could use MOV R3, R3, LSR #8 to set the zero flag. Otherwise, I had to both shift R3 and compare R3 to R4. (Not sure if this is a clever feature of ARM Assembly or a giant hack that will fall apart.)

Here's some more code:

start_save AND R4, R3, #0xFF ; Set R4 to R3 masked & 00000000000000000000000011111111 0xFF
STRB R4, [R2] ; Save R4 to location R2
ADD R2, R2, #1 ; increment R2
MOVS R3, R3, LSR #8 ; Consume lowest 8 bits of R3

BNE start_save ; R3 is 0, jump to start_save

I tested it with values AA, BBAA, CCBBAA, DDCCBBAA, and 0. They all seemed to work fine.

So, it's time to make a run at making this a subroutine. I'm using the simple subroutine method (don't remember what to call it - the kind without a stack.)

The Keil tool warned me - while building the files - that BX was the preferred way to do it. Thanks, helpful Keil tool.

Here's the result:

; 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

; Get the base from the Heap Manager
LDR R2, =Heap_Mem ; temp just the Heap_Mem
ldr R3, =0x00 ; Set R3 to value
MOV R14, R15
B start_save


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

stop
BAL stop ; gives me a loop to be caught in


start_save AND R4, R3, #0xFF ; Set R4 to R3 masked & 00000000000000000000000011111111 0xFF
STRB R4, [R2] ; Save R4 to location R2
ADD R2, R2, #1 ; increment R2
MOVS R3, R3, LSR #8 ; Consume lowest 8 bits of R3

BNE start_save ; R3 is 0, jump to start_save

BX R14

END ; end.


This version also works with the values that I tested.

No comments:

Post a Comment