arm - Assembly Language: How to stick a list of array into a word? -
how combine array of 4 byte 1 32-bit. first item should go significant nibble of result. store result in 32-bit variable result.
input: [list] = 0xc, 0x2, 0x6, 0x9 (each item byte, use dcb define variable of type byte)
output: [result] = 0x0c020609
edit answer:
add r1, r0 mov r1, r1, lsl #8 add r0, r0, #8 add r1, r0 mov r1, r1, lsl #8 add r0, r0, #8 add r1, r0 mov r1, r1, lsl #8 add r0, r0, #8 add r1, r0
what you're describing same thing treating 4 contiguous bytes 32-bit integer stored in big-endian byte-order.
according gcc (on godbolt compiler explorer), best way byte-swap big-endian arm-native endian instruction arm provides explicitly purpose:
rev r0, r0
#include <stdint.h> #include <endian.h> #include <string.h> // type-punning unions alternative memcpy char array int union be_bytes { uint32_t be_word; char bytes[4]; }; uint32_t be_bytes_to_native( char *array ) { union be_bytes tmp; memcpy(tmp.bytes, array, 4); // memcpy since take char* arg instead of union be_bytes * arg. // *think* (union be_bytes*)array safe, i'm not 100% sure. // gnu c , many other compilers guarantee writing 1 union member , reading safe. iso c doesn't, technically isn't portable. return be32toh(tmp.be_word); // endian.h, uses compiler builtins, inline asm, or c shift , mask instructions. }
compiles to
be_bytes_to_native: ldr r0, [r0] @ unaligned rev r0, r0 bx lr
without rev instruction, @dwelch's answer on endianness conversion in arm suggests 4-instruction sequence byte-swapping 32-bit value in arm:
eor r3,r1,r1, ror #16 bic r3,r3,#0x00ff0000 mov r0,r1,ror #8 eor r0,r0,r3, lsr #8
note how combines use of barrel shifter instructions other mov. i'm still not sure add r0, r0, #8
(r0 += 8) in code supposed for.
Comments
Post a Comment