Hey, our instructor gave us some ARM code and while I get the gist of it, I\'m n
ID: 3551994 • Letter: H
Question
Hey, our instructor gave us some ARM code and while I get the gist of it, I'm not sure how it exactly works line by line. If someone can provide me detailed comments on it, I would appreciate it.
The following code snippet comes from a class called LED.s. LED.s is called from a C program, where the function prototype is defined as void LED(char * string1, char * string2, char * string3, int * switch).
LED is called later on in the program with LED(string1, string2, string3, &switch). If switch = 1, an LED will turn on.
This isn't all the ARM code I have, it's just the part I don't understand. If anyone can help me understand what exactly with the stack and registers, I'd be grateful. I'm going to write some more ARM code based off this, so I'd like to understand it completely before I go on.
strlength1:
MOV r8, #0
loop1:
LDRB r9, [r0, r8]
CMP r9, #32
IT NE
ADDNE r8, r8, #1
BNE loop1
MOV r12, r8
BX lr
strlength2:
MOV r8, #0
loop2:
LDRB r9, [r1, r8]
CMP r9, #32
IT NE
ADDNE r8, r8, #1
BNE loop1
MOV r12, r8
BX lr
strlength3:
MOV r8, #0
loop3:
LDRB r9, [r2, r8]
CMP r9, #32
IT NE
ADDNE r8, r8, #1
BNE loop1
MOV r12, r8
BX lr
LED:
push {r4-r11, ip, lr} /*pushes r4-r11, r12, r14 */
BL strlength1 /*get length of string1*/
MOV r4, r12 /*puts length of string1 in r4*/
BL strlength2 /*gets string2 length*/
MOV r5, r12 /*puts string2 length in r5*/
MOV r6, #0 /*initialize r6*/
MOV r7, #0 /*initialize r7*/
CMP r4, r5 /*check if equal*/
BLT nextcheck1
check12:
LDRB r8, [r0, r6]
LDRB r9, [r1, r7]
CMP r8, r9
BEQ increment1
ADD r6, r6, #1
LDRB r8, [r0, r6]
CMP r8, #32
BEQ nextcheck1
MOV r7, #0
STR r7, [r3]
B check12
increment1:
ADD r6, r6, #1
ADD r7, r7, #1
MOV r10, #1
STR r10, [r3]
LDRB r9, [r1, r7]
CMP r9, #32
BEQ end
B check12
nextcheck1:
MOV r6, #0
MOV r7, #0
check21:
LDRB r8, [r1, r6]
LDRB r9, [r0, r7]
CMP r8, r9
BEQ increment2
ADD r6, r6, #1
LDRB r8, [r1, r6]
CMP r8, #32
BEQ next1
MOV r7, #0
STR r7, [r3]
B check21
increment2:
ADD r6, r6, #1
ADD r7, r7, #1
MOV r10, #1
STR r10, [r3]
LDRB r9, [r0, r7]
CMP r9, #32
BEQ end
B check21
end:
pop {r4-r11,ip,lr}
BX LR
Explanation / Answer
its quiet clear
first you defined string length loop functions with r0,r1,r2
LDRB --load register(unsigned)
for strlength1 this is how LDRB works, 0 is moved to r8, r8 is loaded to r0 and then loaded to r9 for temporary use
then it is compared with 32
now about IT
The instructions (including branches) in the IT block, except the BKPT instruction, must specify the condition.
You do not need to write IT instructions in your code, because the assembler generates them for you automatically according to the conditions specified on the following instructions. However, if you do write IT instructions, the assembler validates the conditions specified in the IT instructions against the conditions specified in the following instructions.
Writing the IT instru
then branch exchange
similarly strlength 2 and 3
here there is no program defining stack
about turnLEDon
push
On ARM, the stack pointer normally points to the last occupied address on the stack. For example, when setting up the initial stack pointer, you normally initialize with with the address one past the end of the stack.
PUSH is just a synonym for STMDB using sp as the base register. The DB indicates the 'decrement-before' addressing mode.
then r12 from strlength1 is moved to r4 and r12 from strlength2 is moved to r5 and compared
if less than branch nextcheck1 is executed
similarly other loops
after compare the flag is set and branch instructions are followed.
i donno why strlength3 is therections ensures that you consider the placing of conditional instructions, and the choice of conditions, in the design of your code.
When assembling to ARM code, the assembler performs the same checks, but does not generate any IT instructions.
With the exception of CMP, CMN, and TST, the 16-bit instructions that normally affect the condition code flags, do not affect them when used inside an IT block.
A BKPT instruction in an IT block is always executed, so it does not need a condition in the {cond} part of its syntax. The IT block continues from the next instruction.
when compared is used a flag is set, depending on that if NE then 1 is added to r8 and stored in r8 and loop1 will be executed again.
for other flags r8 is moved to r12