Search code examples
assemblyarmmicrocontrollermicroprocessors

How to calculate the size of the file with assembly instructions of Arm


After converting the below assembly code to binary my file size is 48 bytes. Since in arm each instruction is 4 bytes. I think the label entry is - 4 bytes, arr - 3 bytes, eoa - 1 byte, start till loop - 12 bytes, loop till stop - 16 bytes, stop - 4 bytes, thus the total is 40 bytes. I don't why it shows 48 bytes.

    .text
entry:  b start
arr:    .byte 10, 20, 30
eoa:
    .align
start:  ldr r0, =eoa
        ldr r1, =arr
        mov r3, #0
loop:   ldrb r2, [r1], #1
        add r3, r3, r2 
        cmp r0, r1
        bne loop
stop:   b stop

user@stretch:~/Desktop/Gnu_Toolchain/Sum_An_Array$ arm-none-eabi-nm -n sum_an_array.elf
         U _start
00000000 t entry
00000004 t arr
00000007 t eoa
00000008 t start
00000014 t loop
00000024 t stop
00010030 T __bss_end__
00010030 T _bss_end__
00010030 T __bss_start
00010030 T __bss_start__
00010030 T __data_start
00010030 T _edata
00010030 T _end
00010030 T __end__
00080000 T _stack

Solution

  •     .text
    entry:  b start
    arr:    .byte 10, 20, 30
    eoa:
        .align
    start:  ldr r0, =eoa
            ldr r1, =arr
            mov r3, #0
    loop:   ldrb r2, [r1], #1
            add r3, r3, r2
            cmp r0, r1
            bne loop
    stop:   b stop
    
    
    Disassembly of section .text:
    
    00000000 <entry>:
       0:   ea000000    b   8 <start>
    
    00000004 <arr>:
       4:   140a        .short  0x140a
       6:   1e              .byte   0x1e
    
    00000007 <eoa>:
        ...
    
    00000008 <start>:
       8:   e59f0018    ldr r0, [pc, #24]   ; 28 <stop+0x4>
       c:   e59f1018    ldr r1, [pc, #24]   ; 2c <stop+0x8>
      10:   e3a03000    mov r3, #0
    
    00000014 <loop>:
      14:   e4d12001    ldrb    r2, [r1], #1
      18:   e0833002    add r3, r3, r2
      1c:   e1500001    cmp r0, r1
      20:   1afffffb    bne 14 <loop>
    
    00000024 <stop>:
      24:   eafffffe    b   24 <stop>
      28:   00000007    .word   0x00000007
      2c:   00000004    .word   0x00000004
    

    with experience you can get a rough idea of the size, but just assemble and disassemble.

    fun:
        ldr r0,=0x12345678
        bx lr
    
    00000000 <fun>:
       0:   e51f0000    ldr r0, [pc, #-0]   ; 8 <fun+0x8>
       4:   e12fff1e    bx  lr
       8:   12345678    .word   0x12345678
    
     
    fun:
        ldr r0,=0x00001100
        bx lr
    
    00000000 <fun>:
       0:   e3a00c11    mov r0, #4352   ; 0x1100
       4:   e12fff1e    bx  lr
    

    Tools like gnu assembler that support the ldr rd,=label pseudo instruction, will choose the optimal instruction, others either don't support it or might not optimize. So you can't simply say that is going to be three words to implement those two assembly language instructions.

    You could have done it without the psuedo-instruction

        .text
    entry:  b start
    arr:    .byte 10, 20, 30
    eoa:
        .align
    start:  ldr r0, eoa_add
            ldr r1, arr_add
            mov r3, #0
    loop:   ldrb r2, [r1], #1
            add r3, r3, r2
            cmp r0, r1
            bne loop
    stop:   b stop
    eoa_add: .word eoa
    arr_add: .word arr
    
    
    
    Disassembly of section .text:
    
    00000000 <entry>:
       0:   ea000000    b   8 <start>
    
    00000004 <arr>:
       4:   140a        .short  0x140a
       6:   1e              .byte   0x1e
    
    00000007 <eoa>:
        ...
    
    00000008 <start>:
       8:   e59f0018    ldr r0, [pc, #24]   ; 28 <eoa_add>
       c:   e59f1018    ldr r1, [pc, #24]   ; 2c <arr_add>
      10:   e3a03000    mov r3, #0
    
    00000014 <loop>:
      14:   e4d12001    ldrb    r2, [r1], #1
      18:   e0833002    add r3, r3, r2
      1c:   e1500001    cmp r0, r1
      20:   1afffffb    bne 14 <loop>
    
    00000024 <stop>:
      24:   eafffffe    b   24 <stop>
    
    00000028 <eoa_add>:
      28:   00000007    .word   0x00000007
    
    0000002c <arr_add>:
      2c:   00000004    .word   0x00000004
    

    and have had a closer estimate.