Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
759 views
in Technique[技术] by (71.8m points)

strange STRB in arm assembly

I'm quite new to programming and I was wondering what STRB exactly doing in this decompiled code, as i understand LSRS is a shifting to the right, ADDS is incrementing the r3 by 8 each loop because BNE is checking here for R3 equals 0x20, if not then continue looping :

loc_4EE
LSRS.W          R1, R5, R3
LSRS.W          R2, R6, R3
ADDS            R3, #8
STRB            R1, [R7,#0x40+var_40]
STRB            R2, [R7,#0x40+var_3C]
ADDS            R7, #1
CMP             R3, #0x20 ; ' '
BNE             loc_4EE

and here is the whole code with variable initialization (put it here, guess it can be important)

    CODE16
    EXPORT SEC_calc_erc
    SEC_calc_erc
    s= -0x68
    var_64= -0x64
    var_60= -0x60
    var_5C= -0x5C
    var_58= -0x58
    var_54= -0x54
    var_50= -0x50
    var_40= -0x40
    var_3C= -0x3C
    var_30= -0x30
    anonymous_3= -0x2C
    anonymous_4= -0x28
    anonymous_5= -0x24
    var_20= -0x20
    anonymous_0= -0x1C
    anonymous_1= -0x18
    anonymous_2= -0x14
    var_10= -0x10
    var_F= -0xF
    var_E= -0xE
    var_D= -0xD
    var_8= -8
    var_4= -4
    ; __unwind {
    LDR             R3, =(_GLOBAL_OFFSET_TABLE_ - 0x494)
    PUSH            {R4-R7,LR}
    MOV             R6, R0
    LDR             R0, =(unk_63C - 0x87B8)
    ADR             R4, 0x494
    ADDS            R3, R3, R4 ; _GLOBAL_OFFSET_TABLE_
    MOV             R5, R1
    MOV             R4, R2
    SUB             SP, SP, #0x6C
    ADDS            R7, R3, R0 ; unk_63C
    ADD.W           LR, SP, #0x6C+var_20
    LDMIA.W         R7, {R0-R3}
    ADD.W           R12, R7, #0x10
    ADD             R7, SP, #0x2C
    STMIA.W         LR, {R0-R3}
    ADD.W           LR, SP, #0x6C+var_30
    LDMIA.W         R12, {R0-R3}
    STMIA.W         LR, {R0-R3}
    MOVS            R1, #0  ; c
    MOVS            R2, #0x18 ; n
    ADD             R0, SP, #0x6C+s ; s
    BLX             memset
    MOVS            R1, #0  ; c
    MOVS            R2, #0x10 ; n
    MOV             R0, R7  ; s
    BLX             memset
    MOVS            R1, #0  ; c
    MOVS            R2, #0x10 ; n
    ADD             R0, SP, #0x6C+var_50 ; s
    BLX             memset
    MOVS            R1, #0  ; c
    MOVS            R2, #8  ; n
    ADD             R0, SP, #0x6C+var_10 ; s
    BLX             memset
    ADD             R0, SP, #0x6C+var_8 ; s
    MOVS            R1, #0  ; c
    MOVS            R2, #4  ; n
    BLX             memset
    MOVS            R3, #0
    loc_4EE
LSRS.W          R1, R5, R3
LSRS.W          R2, R6, R3
ADDS            R3, #8
STRB            R1, [R7,#0x40+var_40]
STRB            R2, [R7,#0x40+var_3C]
ADDS            R7, #1
CMP             R3, #0x20 ; ' '
BNE             loc_4EE
question from:https://stackoverflow.com/questions/65943308/strange-strb-in-arm-assembly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm quite new to programming

To programming in general or programming in assembly language?

The fragment below:

  MOVS            R3, #0
loc_4EE
  LSRS.W          R1, R5, R3
  LSRS.W          R2, R6, R3
  ADDS            R3, #8
  STRB            R1, [R7,#0x40+var_40]
  STRB            R2, [R7,#0x40+var_3C]
  ADDS            R7, #1
  CMP             R3, #0x20 ; ' '
  BNE             loc_4EE

can be expressed in (pseudo)-C as something like:

unsigned char *r7;    // initialised somewhere to point to some address on stack
unsigned int r5, r6;  // also initialised before this fragment to hold some values
int r3 = 0; 
do
{
   *(r7)   = (unsigned char)(r5>>r3);
   *(r7+4) = (unsigned char)(r6>>r3);
   r3 += 8; 
   r7 += 1;
} while (r3 != 32)

What is this done for - not a clue as we don't have the context of the task.
You can see this as a conversion from big-endian to little-endian, for example:

if r5 == 0xAABBCCDD, r6 == 0x11223344 the 8 bytes starting from that "address on stack" in r7 will be populated with:
0xDD 0xCC 0xBB 0xAA 0x44 0x33 0x22 0x11


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...