C和汇编调用一例

  1. 现在实现下面功能:
  2. 实现字符串拷贝,当程序开始执行时,先有_start标号进入汇编文件,然后跳入main函数,将字符串拷贝函数的定义放在汇编文件中,在c中调用。
  3. 目录结构:
  4. image

strcpy.s

   1:  .global _start
   2:  .global mystrcpy
   3:   
   4:  _start:
   5:      B main
   6:  mystrcpy:
   7:      mov r2,#0
   8:      ldrb r2, [r0];
   9:      strb r2, [r1]
  10:      cmp r2, #0
  11:      beq rr
  12:      add r0, #1
  13:      add r1, #1
  14:      b mystrcpy
  15:  rr:
  16:     moveq pc,lr
  17:   

 

 

strcpy.c

   1:   
   2:  char *source = "pengdonglin137@163.com";
   3:   
   4:  char dest[20] ="";
   5:   
   6:  extern void mystrcpy(char *src, char *dst);
   7:   
   8:  int main(void){
   9:   
  10:      mystrcpy(source, dest);
  11:      while(1);
  12:      return 0;
  13:  }

Makefile

   1:  all:
   2:      arm-none-eabi-gcc -g -c strcpys.s -o mystrcpys.o        //
   3:      arm-none-eabi-gcc -g -c strcpyc.c -o mystrcpyc.o    
   4:      arm-none-eabi-ld -Ttext 0x20000 mystrcpys.o mystrcpyc.o -o mystrcpy.elf
   5:      arm-none-eabi-objdump -D mystrcpy.elf >p.dis   //反汇编
   6:  clean:
   7:      rm -rf *.o *.elf *.dis
   8:      

 

S5PC100.init

target remote 127.0.0.1:3333
monitor reset halt

 

下面是反汇编:

   1:   
   2:  mystrcpy.elf:     file format elf32-littlearm
   3:   
   4:   
   5:  Disassembly of section .text:
   6:   
   7:  00020000 <_start>:
   8:     20000:    ea000008     b    20028 <main>   //调到main函数,C语言中的函数名在汇编中就是一个标号
   9:   
  10:  00020004 <mystrcpy>:
  11:     20004:    e3a02000     mov    r2, #0
  12:     20008:    e5d02000     ldrb    r2, [r0]      
  13:     2000c:    e5c12000     strb    r2, [r1]
  14:     20010:    e3520000     cmp    r2, #0
  15:     20014:    0a000002     beq    20024 <rr>
  16:     20018:    e2800001     add    r0, r0, #1
  17:     2001c:    e2811001     add    r1, r1, #1
  18:     20020:    eafffff7     b    20004 <mystrcpy>     
  19:   
  20:  00020024 <rr>:
  21:     20024:    01a0f00e     moveq    pc, lr   //将lr给pc,实现子程序的返回,lr在下面已经分析出来,是0x20044
  22:   
  23:  00020028 <main>:
  24:     20028:    e92d4800     push    {fp, lr}     //main是非叶子函数,要保存lr
  25:     2002c:    e28db004     add    fp, sp, #4
  26:     20030:    e59f3010     ldr    r3, [pc, #16]    ; 20048 <main+0x20>  //执行该指令时,pc为0x20038,加16为0x20048,将0x20048单元内容给R3,即R3等于0x28068
  27:     20034:    e5933000     ldr    r3, [r3]   //将0x28068内容给r3,为0x20050
  28:     20038:    e1a00003     mov    r0, r3    //r0为0x20050
  29:     2003c:    e59f1008     ldr    r1, [pc, #8]    ; 2004c <main+0x24>   //pc为0x20044,加8,为0x2004c,r1为0x2806c
  30:     20040:    ebffffef     bl    20004 <mystrcpy> //跳到0x20004(即函数mystrcpy的地址,就是汇编中的标号),pc为0x20048,pc存到lr,lr同时自减4,为0x20044
  31:     20044:    eafffffe     b    20044 <main+0x1c>    
  32:     20048:    00028068     andeq    r8, r2, r8, rrx
  33:     2004c:    0002806c     andeq    r8, r2, ip, rrx
  34:   
  35:  Disassembly of section .rodata:
  36:   
  37:  00020050 <.rodata>:
  38:     20050:    676e6570             ; <UNDEFINED> instruction: 0x676e6570     //0x70 ‘p’  0x65 ‘e’ 0x6e ‘n’ 0x67 ‘g’
  39:     20054:    676e6f64     strbvs    r6, [lr, -r4, ror #30]!
  40:     20058:    316e696c     cmncc    lr, ip, ror #18
  41:     2005c:    31403733     cmpcc    r0, r3, lsr r7
  42:     20060:    632e3336     teqvs    lr, #-671088640    ; 0xd8000000
  43:     20064:    00006d6f     andeq    r6, r0, pc, ror #26
  44:   
  45:  Disassembly of section .data:
  46:   
  47:  00028068 <__data_start>:
  48:     28068:    00020050     andeq    r0, r2, r0, asr r0
  49:   
  50:  Disassembly of section .bss:
  51:   
  52:  0002806c <dest>:
  53:      ...
  54:   
  55:  Disassembly of section .comment:
  56:   
  57:  00000000 <.comment>:
  58:     0:    3a434347     bcc    10d0d24 <_stack+0x1050d24>
  59:     4:    4e472820     cdpmi    8, 4, cr2, cr7, cr0, {1}
  60:     8:    34202955     strtcc    r2, [r0], #-2389    ; 0x955
  61:     c:    322e362e     eorcc    r3, lr, #48234496    ; 0x2e00000
  62:      ...
  63:   
  64:  Disassembly of section .debug_aranges:
  65:   
  66:  00000000 <.debug_aranges>:
  67:     0:    0000001c     andeq    r0, r0, ip, lsl r0
  68:     4:    00000002     andeq    r0, r0, r2
  69:     8:    00040000     andeq    r0, r4, r0
  70:     c:    00000000     andeq    r0, r0, r0
  71:    10:    00020000     andeq    r0, r2, r0
  72:    14:    00000028     andeq    r0, r0, r8, lsr #32
  73:      ...
  74:    20:    0000001c     andeq    r0, r0, ip, lsl r0
  75:    24:    004d0002     subeq    r0, sp, r2
  76:    28:    00040000     andeq    r0, r4, r0
  77:    2c:    00000000     andeq    r0, r0, r0
  78:    30:    00020028     andeq    r0, r2, r8, lsr #32
  79:    34:    00000028     andeq    r0, r0, r8, lsr #32
  80:      ...
  81:   
  82:  Disassembly of section .debug_info:
  83:   
  84:  00000000 <.debug_info>:
  85:     0:    00000049     andeq    r0, r0, r9, asr #32
  86:     4:    00000002     andeq    r0, r0, r2
  87:     8:    01040000     mrseq    r0, (UNDEF: 4)
  88:     c:    00000000     andeq    r0, r0, r0
  89:    10:    00020000     andeq    r0, r2, r0
  90:    14:    00020028     andeq    r0, r2, r8, lsr #32
  91:    18:    63727473     cmnvs    r2, #1929379840    ; 0x73000000
  92:    1c:    2e737970     mrccs    9, 3, r7, cr3, cr0, {3}
  93:    20:    3a440073     bcc    11001f4 <_stack+0x10801f4>
  94:    24:    6c63655c     cfstr64vs    mvdx6, [r3], #-368    ; 0xfffffe90
  95:    28:    65737069     ldrbvs    r7, [r3, #-105]!    ; 0x69
  96:    2c:    6f72705f     svcvs    0x0072705f
  97:    30:    7463656a     strbtvc    r6, [r3], #-1386    ; 0x56a
  98:    34:    796d5c73     stmdbvc    sp!, {r0, r1, r4, r5, r6, sl, fp, ip, lr}^
  99:    38:    63727473     cmnvs    r2, #1929379840    ; 0x73000000
 100:    3c:    47007970     smlsdxmi    r0, r0, r9, r7
 101:    40:    4120554e     teqmi    r0, lr, asr #10
 102:    44:    2e322053     mrccs    0, 1, r2, cr2, cr3, {2}
 103:    48:    01003132     tsteq    r0, r2, lsr r1
 104:    4c:    0000a480     andeq    sl, r0, r0, lsl #9
 105:    50:    14000200     strne    r0, [r0], #-512    ; 0x200
 106:    54:    04000000     streq    r0, [r0]
 107:    58:    00000001     andeq    r0, r0, r1
 108:    5c:    00250100     eoreq    r0, r5, r0, lsl #2
 109:    60:    002f0000     eoreq    r0, pc, r0
 110:    64:    00280000     eoreq    r0, r8, r0
 111:    68:    00500002     subseq    r0, r0, r2
 112:    6c:    00400002     subeq    r0, r0, r2
 113:    70:    01020000     mrseq    r0, (UNDEF: 2)
 114:    74:    0000004c     andeq    r0, r0, ip, asr #32
 115:    78:    3e010801     cdpcc    8, 0, cr0, cr1, cr1, {0}
 116:    7c:    28000000     stmdacs    r0, {}    ; <UNPREDICTABLE>
 117:    80:    50000200     andpl    r0, r0, r0, lsl #4
 118:    84:    00000200     andeq    r0, r0, r0, lsl #4
 119:    88:    03000000     movweq    r0, #0
 120:    8c:    6e690504     cdpvs    5, 6, cr0, cr9, cr4, {0}
 121:    90:    0c040074     stceq    0, cr0, [r4], {116}    ; 0x74
 122:    94:    01000000     mrseq    r0, (UNDEF: 0)
 123:    98:    00005202     andeq    r5, r0, r2, lsl #4
 124:    9c:    05010100     streq    r0, [r1, #-256]    ; 0x100
 125:    a0:    00005804     andeq    r5, r0, r4, lsl #16
 126:    a4:    08010600     stmdaeq    r1, {r9, sl}
 127:    a8:    00000051     andeq    r0, r0, r1, asr r0
 128:    ac:    00005807     andeq    r5, r0, r7, lsl #16
 129:    b0:    00006f00     andeq    r6, r0, r0, lsl #30
 130:    b4:    006f0800     rsbeq    r0, pc, r0, lsl #16
 131:    b8:    00130000     andseq    r0, r3, r0
 132:    bc:    13070406     movwne    r0, #29702    ; 0x7406
 133:    c0:    04000000     streq    r0, [r0]
 134:    c4:    00000020     andeq    r0, r0, r0, lsr #32
 135:    c8:    005f0401     subseq    r0, pc, r1, lsl #8
 136:    cc:    01010000     mrseq    r0, (UNDEF: 1)
 137:    d0:    00000c09     andeq    r0, r0, r9, lsl #24
 138:    d4:    52020100     andpl    r0, r2, #0
 139:    d8:    01000000     mrseq    r0, (UNDEF: 0)
 140:    dc:    80680305     rsbhi    r0, r8, r5, lsl #6
 141:    e0:    20090002     andcs    r0, r9, r2
 142:    e4:    01000000     mrseq    r0, (UNDEF: 0)
 143:    e8:    00005f04     andeq    r5, r0, r4, lsl #30
 144:    ec:    03050100     movweq    r0, #20736    ; 0x5100
 145:    f0:    0002806c     andeq    r8, r2, ip, rrx
 146:      ...
 147:   
 148:  Disassembly of section .debug_abbrev:
 149:   
 150:  00000000 <.debug_abbrev>:
 151:     0:    10001101     andne    r1, r0, r1, lsl #2
 152:     4:    12011106     andne    r1, r1, #-2147483647    ; 0x80000001
 153:     8:    1b080301     blne    200c14 <_stack+0x180c14>
 154:     c:    13082508     movwne    r2, #34056    ; 0x8508
 155:    10:    00000005     andeq    r0, r0, r5
 156:    14:    25011101     strcs    r1, [r1, #-257]    ; 0x101
 157:    18:    030b130e     movweq    r1, #45838    ; 0xb30e
 158:    1c:    110e1b0e     tstne    lr, lr, lsl #22
 159:    20:    10011201     andne    r1, r1, r1, lsl #4
 160:    24:    02000006     andeq    r0, r0, #6
 161:    28:    0c3f002e     ldceq    0, cr0, [pc], #-184    ; ffffff78 <_stack+0xfff7ff78>
 162:    2c:    0b3a0e03     bleq    e83840 <_stack+0xe03840>
 163:    30:    0c270b3b     stceq    11, cr0, [r7], #-236    ; 0xffffff14
 164:    34:    01111349     tsteq    r1, r9, asr #6
 165:    38:    06400112             ; <UNDEFINED> instruction: 0x06400112
 166:    3c:    24030000     strcs    r0, [r3]
 167:    40:    3e0b0b00     vmlacc.f64    d0, d11, d0
 168:    44:    0008030b     andeq    r0, r8, fp, lsl #6
 169:    48:    00340400     eorseq    r0, r4, r0, lsl #8
 170:    4c:    0b3a0e03     bleq    e83860 <_stack+0xe03860>
 171:    50:    13490b3b     movtne    r0, #39739    ; 0x9b3b
 172:    54:    0c3c0c3f     ldceq    12, cr0, [ip], #-252    ; 0xffffff04
 173:    58:    0f050000     svceq    0x00050000
 174:    5c:    490b0b00     stmdbmi    fp, {r8, r9, fp}
 175:    60:    06000013             ; <UNDEFINED> instruction: 0x06000013
 176:    64:    0b0b0024     bleq    2c00fc <_stack+0x2400fc>
 177:    68:    0e030b3e     vmoveq.16    d3[0], r0
 178:    6c:    01070000     mrseq    r0, (UNDEF: 7)
 179:    70:    01134901     tsteq    r3, r1, lsl #18
 180:    74:    08000013     stmdaeq    r0, {r0, r1, r4}
 181:    78:    13490021     movtne    r0, #36897    ; 0x9021
 182:    7c:    00000b2f     andeq    r0, r0, pc, lsr #22
 183:    80:    03003409     movweq    r3, #1033    ; 0x409
 184:    84:    3b0b3a0e     blcc    2ce8c4 <_stack+0x24e8c4>
 185:    88:    3f13490b     svccc    0x0013490b
 186:    8c:    000a020c     andeq    r0, sl, ip, lsl #4
 187:      ...
 188:   
 189:  Disassembly of section .debug_line:
 190:   
 191:  00000000 <.debug_line>:
 192:     0:    0000003c     andeq    r0, r0, ip, lsr r0
 193:     4:    00200002     eoreq    r0, r0, r2
 194:     8:    01020000     mrseq    r0, (UNDEF: 2)
 195:     c:    000d0efb     strdeq    r0, [sp], -fp
 196:    10:    01010101     tsteq    r1, r1, lsl #2
 197:    14:    01000000     mrseq    r0, (UNDEF: 0)
 198:    18:    00010000     andeq    r0, r1, r0
 199:    1c:    63727473     cmnvs    r2, #1929379840    ; 0x73000000
 200:    20:    2e737970     mrccs    9, 3, r7, cr3, cr0, {3}
 201:    24:    00000073     andeq    r0, r0, r3, ror r0
 202:    28:    05000000     streq    r0, [r0]
 203:    2c:    02000002     andeq    r0, r0, #2
 204:    30:    2f301600     svccs    0x00301600
 205:    34:    2f2f2f2f     svccs    0x002f2f2f
 206:    38:    02302f2f     eorseq    r2, r0, #188    ; 0xbc
 207:    3c:    01010002     tsteq    r1, r2
 208:    40:    00000039     andeq    r0, r0, r9, lsr r0
 209:    44:    00200002     eoreq    r0, r0, r2
 210:    48:    01020000     mrseq    r0, (UNDEF: 2)
 211:    4c:    000d0efb     strdeq    r0, [sp], -fp
 212:    50:    01010101     tsteq    r1, r1, lsl #2
 213:    54:    01000000     mrseq    r0, (UNDEF: 0)
 214:    58:    00010000     andeq    r0, r1, r0
 215:    5c:    63727473     cmnvs    r2, #1929379840    ; 0x73000000
 216:    60:    2e637970     mcrcs    9, 3, r7, cr3, cr0, {3}
 217:    64:    00000063     andeq    r0, r0, r3, rrx
 218:    68:    05000000     streq    r0, [r0]
 219:    6c:    02002802     andeq    r2, r0, #131072    ; 0x20000
 220:    70:    004c1900     subeq    r1, ip, r0, lsl #18
 221:    74:    9f010402     svcls    0x00010402
 222:    78:    01000602     tsteq    r0, r2, lsl #12
 223:    7c:    Address 0x0000007c is out of bounds.
 224:   
 225:   
 226:  Disassembly of section .debug_frame:
 227:   
 228:  00000000 <.debug_frame>:
 229:     0:    0000000c     andeq    r0, r0, ip
 230:     4:    ffffffff             ; <UNDEFINED> instruction: 0xffffffff
 231:     8:    7c020001     stcvc    0, cr0, [r2], {1}
 232:     c:    000d0c0e     andeq    r0, sp, lr, lsl #24
 233:    10:    00000018     andeq    r0, r0, r8, lsl r0
 234:    14:    00000000     andeq    r0, r0, r0
 235:    18:    00020028     andeq    r0, r2, r8, lsr #32
 236:    1c:    00000028     andeq    r0, r0, r8, lsr #32
 237:    20:    8e080e42     cdphi    14, 0, cr0, cr8, cr2, {2}
 238:    24:    42028b01     andmi    r8, r2, #1024    ; 0x400
 239:    28:    00040b0c     andeq    r0, r4, ip, lsl #22
 240:   
 241:  Disassembly of section .debug_str:
 242:   
 243:  00000000 <.debug_str>:
 244:     0:    20554e47     subscs    r4, r5, r7, asr #28
 245:     4:    2e342043     cdpcs    0, 3, cr2, cr4, cr3, {2}
 246:     8:    00322e36     eorseq    r2, r2, r6, lsr lr
 247:     c:    72756f73     rsbsvc    r6, r5, #460    ; 0x1cc
 248:    10:    75006563     strvc    r6, [r0, #-1379]    ; 0x563
 249:    14:    6769736e     strbvs    r7, [r9, -lr, ror #6]!
 250:    18:    2064656e     rsbcs    r6, r4, lr, ror #10
 251:    1c:    00746e69     rsbseq    r6, r4, r9, ror #28
 252:    20:    74736564     ldrbtvc    r6, [r3], #-1380    ; 0x564
 253:    24:    72747300     rsbsvc    r7, r4, #0
 254:    28:    63797063     cmnvs    r9, #99    ; 0x63
 255:    2c:    4400632e     strmi    r6, [r0], #-814    ; 0x32e
 256:    30:    63655c3a     cmnvs    r5, #14848    ; 0x3a00
 257:    34:    7370696c     cmnvc    r0, #1769472    ; 0x1b0000
 258:    38:    72705f65     rsbsvc    r5, r0, #404    ; 0x194
 259:    3c:    63656a6f     cmnvs    r5, #454656    ; 0x6f000
 260:    40:    6d5c7374     ldclvs    3, cr7, [ip, #-464]    ; 0xfffffe30
 261:    44:    72747379     rsbsvc    r7, r4, #-469762047    ; 0xe4000001
 262:    48:    00797063     rsbseq    r7, r9, r3, rrx
 263:    4c:    6e69616d     powvsez    f6, f1, #5.0
 264:    50:    61686300     cmnvs    r8, r0, lsl #6
 265:    54:    Address 0x00000054 is out of bounds.
 266:   
 267:   
 268:  Disassembly of section .debug_loc:
 269:   
 270:  00000000 <.debug_loc>:
 271:     0:    00000000     andeq    r0, r0, r0
 272:     4:    00000004     andeq    r0, r0, r4
 273:     8:    007d0002     rsbseq    r0, sp, r2
 274:     c:    00000004     andeq    r0, r0, r4
 275:    10:    00000008     andeq    r0, r0, r8
 276:    14:    087d0002     ldmdaeq    sp!, {r1}^
 277:    18:    00000008     andeq    r0, r0, r8
 278:    1c:    00000028     andeq    r0, r0, r8, lsr #32
 279:    20:    047b0002     ldrbteq    r0, [fp], #-2
 280:      ...
 281:   
 282:  Disassembly of section .ARM.attributes:
 283:   
 284:  00000000 <_stack-0x80000>:
 285:     0:    00002d41     andeq    r2, r0, r1, asr #26
 286:     4:    61656100     cmnvs    r5, r0, lsl #2
 287:     8:    01006962     tsteq    r0, r2, ror #18
 288:     c:    00000023     andeq    r0, r0, r3, lsr #32
 289:    10:    4d524105     ldfmie    f4, [r2, #-20]    ; 0xffffffec
 290:    14:    4d445437     cfstrdmi    mvd5, [r4, #-220]    ; 0xffffff24
 291:    18:    02060049     andeq    r0, r6, #73    ; 0x49
 292:    1c:    01090108     tsteq    r9, r8, lsl #2
 293:    20:    01140412     tsteq    r4, r2, lsl r4
 294:    24:    03170115     tsteq    r7, #1073741829    ; 0x40000005
 295:    28:    011a0118     tsteq    sl, r8, lsl r1
 296:    2c:    Address 0x0000002c is out of bounds.
 297:   

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/pengdonglin137/p/3029259.html